자바 에서 ImageIO.write(bufferedImage 가 그림 을 출력 할 때 그림 의 변경 문제
6274 단어 자바
package test;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class test {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedImage bufferedImage = ImageIO.read(new File("D:/adt-bundle-windows-x86_64-20140321/workspace/test/img/test6.jpg"));
BufferedImage bufferedImage2=grayimage( bufferedImage);
BufferedImage bufferedImage3=erzh( bufferedImage);// bufferedImage2
ImageIO.write(bufferedImage, "jpg", new File("D:/adt-bundle-windows-x86_64-20140321/workspace/test/img/yuantu.jpg"));
ImageIO.write(bufferedImage2, "jpg", new File("D:/adt-bundle-windows-x86_64-20140321/workspace/test/img/huidutu.jpg"));
ImageIO.write(bufferedImage3, "jpg", new File("D:/adt-bundle-windows-x86_64-20140321/workspace/test/img/ezh.jpg"));
}
public static BufferedImage grayimage(BufferedImage bufferedImage){
int h = bufferedImage.getHeight();
int w = bufferedImage.getWidth();
//BufferedImage bufferedImage2 = new BufferedImage(w, h, BufferedImage. TYPE_3BYTE_BGR );
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
int argb = bufferedImage.getRGB(x, y);
int r = (argb >> 16) & 0xFF;
int g = (argb >> 8) & 0xFF;
int b = (argb >> 0) & 0xFF;
int grayPixel=(int)(r+g+b)/3;
// rgb
int rgb = (grayPixel*256 + grayPixel)*256+grayPixel;
if(rgb>8388608) //rgb 8388608, 256*256*256
{
rgb = rgb - 16777216;
}
bufferedImage.setRGB(x, y, rgb);
//bufferedImage2.setRGB(x, y, rgb);
}
}
//return bufferedImage2;
return bufferedImage;
}
public static int[][] huidutujuzhen(BufferedImage bufferedImage)
{
//
bufferedImage=grayimage(bufferedImage);
int h = bufferedImage.getHeight();
int w = bufferedImage.getWidth();
//
int[][]gray=new int[h][w];
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
int a = bufferedImage.getRGB(x, y);
gray[y][x]=a;
}
}
return gray;
}
public static BufferedImage erzh(BufferedImage bufferedImage){
int h = bufferedImage.getHeight();
int w = bufferedImage.getWidth();
int[][] gray = huidutujuzhen( bufferedImage);
// BufferedImage bufferedImage2 = new BufferedImage(w, h, BufferedImage. TYPE_3BYTE_BGR );
//
int threshold = ostu(gray, w, h);
System.out.print(threshold);
// BufferedImage binaryBufferedImage = ImageIO.read(new File("D:/adt-bundle-windows-x86-20130219/workspace/wenzi/src/huiduhua.jpg"));
// BufferedImage binaryBufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);// TYPE_BYTE_GRAY、、TYPE_3BYTE_BGR
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
int gry=gray[y][x]&0xff;
if (gry > threshold)
{
gray[y][x]=0xFFFFFF;
}
else
{
gray[y][x]=0x000000;
}
bufferedImage.setRGB(x, y, gray[y][x] );
}
}
return bufferedImage;
}
//OTSU T 。
public static int ostu(int[][] gray, int w, int h) {
int[] histData =new int[256];// new int[w * h];
// Calculate histogram
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int red = 0xFF & gray[y][x];
histData[red]++;
}
}
// Total number of pixels
int total = w * h;
float sum = 0; // ,,,,,, histdata 。
for (int t = 0; t < 256; t++)
sum += t * histData[t];
float sumB = 0;
int wB = 0;
int wF = 0;
float varMax = 0;
int threshold = 0;
for (int t = 0; t < 256; t++) {
wB += histData[t]; // Weight Background//histData[t] t ,wB
if (wB == 0)
continue;
wF = total - wB; // Weight Foreground wF
if (wF == 0)
break;
sumB += (float) (t * histData[t]);// t
float mB = sumB / wB; // Mean Background t
float mF = (sum - sumB) / wF; // Mean Foreground sum
// Calculate Between Class Variance
float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);
// Check if new maximum found
if (varBetween > varMax) {
varMax = varBetween;
threshold = t;
}
}
return threshold;
}
}
bufferedImage , , ( , ) ImageIO.write BufferedImage bufferedImage2=grayimage( bufferedImage); bufferedImage , 2 !
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.