1

我使用 imageJ 库来读取 .tiff 图像文件。但是当我试图读取变量 c 中 image1 的像素时,我收到一条错误消息:“不兼容的类型:需要 int,找到 int []。我对 java 很陌生,所以有人可以告诉我如何解决这个问题。否则代码可以与其他图像格式一起正常工作。

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;

public class GetPixelCoordinates {

//int y, x, tofind, col;
/**
 * @param args the command line arguments
 * @throws IOException  
 */
    public static void main(String args[]) throws IOException {
        try {
            //read image file

            ImagePlus img = new ImagePlus("E:\\abc.tiff");

            //write file
            FileWriter fstream = new FileWriter("E:\\log.txt");
            BufferedWriter out = new BufferedWriter(fstream);

            //find cyan pixels
            for (int y = 0; y < img.getHeight(); y++) {
                for (int x = 0; x < image.getWidth(); x++) {

                    int c = img.getPixel(x,y);
                    Color color = new Color(c);


                     if (color.getRed() < 30 && color.getGreen() >= 225 && color.getBlue() >= 225) {
                         out.write("CyanPixel found at=" + x + "," + y);
                         out.newLine();

                     }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
4

1 回答 1

1

如果您查看in的文档getPixel(int,int)ImagePlus,您会发现它返回一个ints 数组而不是一个 s数组int

以 4 元素数组的形式返回 (x,y) 处的像素值。灰度值在第一个元素中重新调整。RGB 值在前 3 个元素中返回。对于索引彩色图像,RGB 值在前 3 个三个元素中返回,索引 (0-255) 在最后一个元素中返回。

看起来好像您正在处理 RGB 图像,因此您应该能够执行以下操作:

int [] colorArray = image1.getPixel(x,y);

int redValue = colorArray[0];
int greenValue = colorArray[1];
int blueValue = colorArray[2];
于 2012-02-07T08:15:29.803 回答