gpt4 book ai didi

java - 使用 imageJ 库扫描 .tiff 图像的像素

转载 作者:太空宇宙 更新时间:2023-11-04 08:22:58 30 4
gpt4 key购买 nike

我正在使用 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();
}
}
}

最佳答案

如果你看documentation for getPixel(int,int) in ImagePlus您会看到它返回一个 int 数组,而不是单个 int:

Returns the pixel value at (x,y) as a 4 element array. Grayscale values are retuned in the first element. RGB values are returned in the first 3 elements. For indexed color images, the RGB values are returned in the first 3 three elements and the index (0-255) is returned in the last.

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

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

int redValue = colorArray[0];
int greenValue = colorArray[1];
int blueValue = colorArray[2];

关于java - 使用 imageJ 库扫描 .tiff 图像的像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9171531/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com