本文目录一览:
- java中BufferedImage图像有哪几种类型?
- java数字图像处理常用算法
- java中图像与数组转换
- Java图像去噪怎么实现?
- Java支持的图像文件格式?
- 如何用java转换图像格式为jpg
java中BufferedImage图像有哪几种类型?
Java的BufferedImage类是Image类的子类。属于一种类型,它是用来处理和操作的图像数据。 构造函数构造一个新BufferedImage一个具有指定ColorModel和Raster。 它计算的任意矩形区域BufferedImage并将其复制到指定的WritableRaster。 构造函数构造一个BufferedImage预定义图像类型之一,TYPE_BYTE_BINARY或TYPE_BYTE_INDEXED。 它返回默认RGB颜色模型(TYPE_INT_ARGB)和默认sRGB色彩空间中的整数像素。 Java是一种高级编程语言被广泛使用在现代世界。它可以支持和处理的数字图像处理有效地使用各种功能。
java数字图像处理常用算法
前些时候做毕业设计 用java做的数字图像处理方面的东西 这方面的资料ms比较少 发点东西上来大家共享一下 主要就是些算法 有自己写的 有人家的 还有改人家的 有的算法写的不好 大家不要见笑 一 读取bmp图片数据
// 获取待检测图像 数据保存在数组 nData[] nB[] nG[] nR[]中
public void getBMPImage(String source) throws Exception {
clearNData(); //清除数据保存区
FileInputStream fs = null;
try {
fs = new FileInputStream(source);
int bfLen = ;
byte bf[] = new byte[bfLen];
fs read(bf bfLen); // 读取 字节BMP文件头
int biLen = ;
byte bi[] = new byte[biLen];
fs read(bi biLen); // 读取 字节BMP信息头
// 源图宽度
nWidth = (((int) bi[ ] xff) ) | (((int) bi[ ] xff) ) | (((int) bi[ ] xff) ) | (int) bi[ ] xff;
// 源图高度
nHeight = (((int) bi[ ] xff) ) | (((int) bi[ ] xff) ) | (((int) bi[ ] xff) ) | (int) bi[ ] xff;
// 位数
nBitCount = (((int) bi[ ] xff) ) | (int) bi[ ] xff;
// 源图大小
int nSizeImage = (((int) bi[ ] xff) ) | (((int) bi[ ] xff) ) | (((int) bi[ ] xff) ) | (int) bi[ ] xff;
// 对 位BMP进行解析
if (nBitCount == ){
int nPad = (nSizeImage / nHeight) nWidth * ;
nData = new int[nHeight * nWidth];
nB=new int[nHeight * nWidth];
nR=new int[nHeight * nWidth];
nG=new int[nHeight * nWidth];
byte bRGB[] = new byte[(nWidth + nPad) * * nHeight];
fs read(bRGB (nWidth + nPad) * * nHeight);
int nIndex = ;
for (int j = ; j nHeight; j++){
for (int i = ; i nWidth; i++) {
nData[nWidth * (nHeight j ) + i] = ( xff) | (((int) bRGB[nIndex + ] xff) ) | (((int) bRGB[nIndex + ] xff) ) | (int) bRGB[nIndex] xff;
nB[nWidth * (nHeight j ) + i]=(int) bRGB[nIndex] xff;
nG[nWidth * (nHeight j ) + i]=(int) bRGB[nIndex+ ] xff;
nR[nWidth * (nHeight j ) + i]=(int) bRGB[nIndex+ ] xff;
nIndex += ;
}
nIndex += nPad;
}
}
} catch (Exception e) {
e printStackTrace();
throw new Exception(e);
} finally {
if (fs != null) {
fs close();
}
}
}
二 由r g b 获取灰度数组
public int[] getBrightnessData(int rData[] int gData[] int bData[]){
int brightnessData[]=new int[rData length];
if(rData length!=gData length || rData length!=bData length || bData length!=gData length){
return brightnessData;
} else {
for(int i= ;ibData length;i++){
double temp= *rData[i]+ *gData[i]+ *bData[i];
brightnessData[i]=(int)(temp)+((temp (int)(temp)) ? : );
}
return brightnessData;
}
}
三 直方图均衡化
public int [] equilibrateGray(int[] PixelsGray int width int height) {
int gray;
int length=PixelsGray length;
int FrequenceGray[]=new int[length];
int SumGray[]=new int[ ];
int ImageDestination[]=new int[length];
for(int i = ; i length ;i++) {
gray=PixelsGray[i];
FrequenceGray[gray]++;
}
// 灰度均衡化
SumGray[ ]=FrequenceGray[ ];
for(int i= ;i ;i++){
SumGray[i]=SumGray[i ]+FrequenceGray[i];
}
for(int i= ;i ;i++) {
SumGray[i]=(int)(SumGray[i]* /length);
}
for(int i= ;iheight;i++) {
for(int j= ;jwidth;j++) {
int k=i*width+j;
ImageDestination[k]= xFF | ((SumGray[PixelsGray[k]] ) | (SumGray[PixelsGray[k]] ) | SumGray[PixelsGray[k]]);
}
}
return ImageDestination;
}
四 laplace 阶滤波 增强边缘 图像锐化
public int[] laplace DFileter(int []data int width int height){
int filterData[]=new int[data length];
int min= ;
int max= ;
for(int i= ;iheight;i++){
for(int j= ;jwidth;j++){
if(i== || i==height || j== || j==width )
filterData[i*width+j]=data[i*width+j];
else
filterData[i*width+j]= *data[i*width+j] data[i*width+j ] data[i*width+j+ ]
data[(i )*width+j] data[(i )*width+j ] data[(i )*width+j+ ]
data[(i+ )*width+j] data[(i+ )*width+j ] data[(i+ )*width+j+ ];
if(filterData[i*width+j]min)
min=filterData[i*width+j];
if(filterData[i*width+j]max)
max=filterData[i*width+j];
}
}
for(int i= ;iwidth*height;i++){
filterData[i]=(filterData[i] min)* /(max min);
}
return filterData;
}
五 laplace 阶增强滤波 增强边缘 增强系数delt
public int[] laplaceHigh DFileter(int []data int width int height double delt){
int filterData[]=new int[data length];
int min= ;
int max= ;
for(int i= ;iheight;i++){
for(int j= ;jwidth;j++){
if(i== || i==height || j== || j==width )
filterData[i*width+j]=(int)(( +delt)*data[i*width+j]);
else
filterData[i*width+j]=(int)(( +delt)*data[i*width+j] data[i*width+j ]) data[i*width+j+ ]
data[(i )*width+j] data[(i )*width+j ] data[(i )*width+j+ ]
data[(i+ )*width+j] data[(i+ )*width+j ] data[(i+ )*width+j+ ];
if(filterData[i*width+j]min)
min=filterData[i*width+j];
if(filterData[i*width+j]max)
max=filterData[i*width+j];
}
}
for(int i= ;iwidth*height;i++){
filterData[i]=(filterData[i] min)* /(max min);
}
return filterData;
}
六 局部阈值处理 值化
// 局部阈值处理 值化 niblack s method
/*原理
T(x y)=m(x y) + k*s(x y)
取一个宽度为w的矩形框 (x y)为这个框的中心
统计框内数据 T(x y)为阈值 m(x y)为均值 s(x y)为均方差 k为参数(推荐 )计算出t再对(x y)进行切割 /
这个算法的优点是 速度快 效果好
缺点是 niblack s method会产生一定的噪声
*/
public int[] localThresholdProcess(int []data int width int height int w int h double coefficients double gate){
int[] processData=new int[data length];
for(int i= ;idata length;i++){
processData[i]= ;
}
if(data length!=width*height)
return processData;
int wNum=width/w;
int hNum=height/h;
int delt[]=new int[w*h];
for(int j= ;jhNum;j++){
for(int i= ;iwNum;i++){
for(int n= ;nh;n++)
for(int k= ;kw;k++){
delt[n*w+k]=data[(j*h+n)*width+i*w+k];
}
delt=thresholdProcess(delt w h coefficients gate);
for(int n= ;nh;n++)
for(int k= ;kw;k++){
processData[(j*h+n)*width+i*w+k]=delt[n*w+k];
}
}
}
return processData;
}
七 全局阈值处理 值化
public int[] thresholdProcess(int []data int width int height double coefficients double gate){
int [] processData=new int[data length];
if(data length!=width*height)
return processData;
else{
double sum= ;
double average= ;
double variance= ;
double threshold;
if( gate!= ){
threshold=gate;
}else{
for(int i= ;iwidth*height;i++){
sum+=data[i];
}
average=sum/(width*height);
for(int i= ;iwidth*height;i++){
variance+=(data[i] average)*(data[i] average);
}
variance=Math sqrt(variance);
threshold=average coefficients*variance;
}
for(int i= ;iwidth*height;i++){
if(data[i]threshold)
processData[i]= ;
else
processData[i]= ;
}
return processData;
}
}
八 垂直边缘检测 sobel算子
public int[] verticleEdgeCheck(int []data int width int height int sobelCoefficients) throws Exception{
int filterData[]=new int[data length];
int min= ;
int max= ;
if(data length!=width*height)
return filterData;
try{
for(int i= ;iheight;i++){
for(int j= ;jwidth;j++){
if(i== || i== || i==height || i==height ||j== || j== || j==width || j==width ){
filterData[i*width+j]=data[i*width+j];
} else{
double average;
//中心的九个像素点
average=data[i*width+j] sobelCoefficients*data[i*width+j ]+sobelCoefficients*data[i*width+j+ ]
data[(i )*width+j ]+data[(i )*width+j+ ]
data[(i+ )*width+j ]+data[(i+ )*width+j+ ];
filterData[i*width+j]=(int)(average);
}
if(filterData[i*width+j]min)
min=filterData[i*width+j];
if(filterData[i*width+j]max)
max=filterData[i*width+j];
}
}
for(int i= ;iwidth*height;i++){
filterData[i]=(filterData[i] min)* /(max min);
}
} catch (Exception e) {
e printStackTrace();
throw new Exception(e);
}
return filterData;
}
九 图像平滑 * 掩模处理(平均处理) 降低噪声
lishixinzhi/Article/program/Java/hx/201311/26286
java中图像与数组转换
按照你的要求编写的Java程序如下:( 要注意的地方见语句后面的注释)
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageWithArray {
public static void main(String[] args) {
// 读取图片到BufferedImage
BufferedImage bf = readImage("c:\\tmp\\6\\female.png");//这里写你要读取的绝对路径+文件名
// 将图片转换为二维数组
int[][] rgbArray1 = convertImageToArray(bf);
// 输出图片到指定文件
writeImageFromArray("c:\\tmp\\2.png", "png", rgbArray1);//这里写你要输出的绝对路径+文件名
System.out.println("图片输出完毕!");
}
public static BufferedImage readImage(String imageFile){
File file = new File(imageFile);
BufferedImage bf = null;
try {
bf = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
return bf;
}
public static int[][] convertImageToArray(BufferedImage bf) {
// 获取图片宽度和高度
int width = bf.getWidth();
int height = bf.getHeight();
// 将图片sRGB数据写入一维数组
int[] data = new int[width*height];
bf.getRGB(0, 0, width, height, data, 0, width);
// 将一维数组转换为为二维数组
int[][] rgbArray = new int[height][width];
for(int i = 0; i < height; i++)
for(int j = 0; j < width; j++)
rgbArray[i][j] = data[i*width + j];
return rgbArray;
}
public static void writeImageFromArray(String imageFile, String type, int[][] rgbArray){
// 获取数组宽度和高度
int width = rgbArray[0].length;
int height = rgbArray.length;
// 将二维数组转换为一维数组
int[] data = new int[width*height];
for(int i = 0; i < height; i++)
for(int j = 0; j < width; j++)
data[i*width + j] = rgbArray[i][j];
// 将数据写入BufferedImage
BufferedImage bf = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
bf.setRGB(0, 0, width, height, data, 0, width);
// 输出图片
try {
File file= new File(imageFile);
ImageIO.write((RenderedImage)bf, type, file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
图片输出完毕!
原图: 输出图:
Java图像去噪怎么实现?
流程不外乎是
- 读取图像文件;
- 扫描噪点;
- 去除噪点;
- 保存图像文件。 Java2D操作好像使用BufferedImage读取图像文件最方便,有一阵没弄这了,忘了。应该可以读取JPG,PNG,GIF图像。 识别噪点应该有专门的算法,我没研究过,百度一下应该能找到专门算法,然后写段代码就可以。我个人以为是独立一个像素与周围一定范围内的像素差异过大,就认为是噪点。可以有亮度,色相上的差别。BufferedImage可以读取每个像素的RGB,从而能识别色相的差别;还有个矩阵,用来由RGB计算亮度的,也就可以计算亮度差别了,这个网上都能找到。 输出也使用BufferedImage就可以。 关键是每个像素都要和周围像素比较,还要计算亮度,最少是三重循环了,如何提高效率是个大问题了。这个代码写好了也算一个高手了。
Java支持的图像文件格式?
java1.0支持显示gif和jpeg格式的图像文件,这些文件的扩展名为.gif、.jpg或.jpeg。在java.awt.image包、java.awt包和java.applet包中都有对图像的支持
如何用java转换图像格式为jpg
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
public class ImageFormat {
public static void main(String[] args) {
File file = new File("c:\\test.jpg");
changFormat(file, "png", new File("c:\\test.png"));// 转为png
changFormat(file, "bmp", new File("c:\\test.bmp"));// 转为bmp
//changFormat(file, "jpeg", new File("c:\\test.jpg"));// 转为jpg
changFormat(file, "gif", new File("c:\\test.gif"));// 转为gif
}
//第一个参数 原图的File对象 第二个参数 目标格式 第三个参数 输出图像的File对象
public static void changFormat(File srcFile, String format, File formatFile) {
try {
BufferedImage srcImg = ImageIO.read(srcFile);// 读取原图
ImageIO.write(srcImg, format, formatFile);// 用指定格式输出到指定文件
} catch (IOException e) {
e.printStackTrace();
}
}
}