我有下面的代码,它编译得很好。我已经进行了一些调试,它能够通过 getDimensions() 方法识别图像文件、查看尺寸以及切片的数量。但是,当我尝试使用 getPixel(x,y) 方法时。它不断给我一个全 0 数组。我的图像不是全黑的。
我不确定发生了什么。但我可以使用一些帮助。
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.imageio.ImageIO;
import ij.ImagePlus;
import java.util.ArrayList;
public class Test{
public static void main(String args[])throws IOException{
try{
ArrayList<Integer> totalWhitePixels = new ArrayList<Integer>();
ArrayList<Integer> totalYellowPixels = new ArrayList<Integer>();
ArrayList<Integer> totalBluePixels = new ArrayList<Integer>();
//read image file
ImagePlus img = new ImagePlus("C:\\Image74.tif");
for(int n=1; n<=img.getNSlices(); n++){
img.setSlice(n);
int whitePixelCount = 0;
int yellowPixelCount = 0;
int bluePixelCount = 0;
int testPixelCount = 0;
//write file
FileWriter fstream = new FileWriter("C:\\log.txt");
BufferedWriter out = new BufferedWriter(fstream);
//find pixel colors
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
int[] c = img.getPixel(x,y);
//testPixel
if (c[0]==0 && c[1]==0 && c[2]==0 && c[3]==0){
testPixelCount++;
}
//whitePixel
if (c[0]> 240 && c[1]> 240 && c[2]> 240) {
whitePixelCount++;
}
//yellowPixel
if (c[0]> 240 && c[1]> 240 && c[2]> 0){
yellowPixelCount++;
}
//bluePixel
if (c[0]> 0 && c[1]> 0 && c[2]> 240){
bluePixelCount++;
}
}
}
//System.out.println(whitePixelCount + "." + yellowPixelCount + "." + bluePixelCount);
//System.out.println(testPixelCount);
totalWhitePixels.add(whitePixelCount);
totalYellowPixels.add(yellowPixelCount);
totalBluePixels.add(bluePixelCount);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}