ImageJ从imagestack切片返回rgb格式的所有像素

时间:2013-07-20 05:04:02

标签: java pixel rgb imagej

我有下面的代码,它编译得很好。我做了一些调试,它能够通过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();
    }   
}
}   

1 个答案:

答案 0 :(得分:0)

按照斐济IJ.openImage(java.lang.String path)的建议,尝试使用tips for developers代替new ImagePlus("C:\\Image74.tif")。您可以找到更多开发者资源here,并且可以随时询问imagej mailing list