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

发布于 2025-01-03 12:16:52 字数 1472 浏览 2 评论 0原文

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

im using the imageJ library to read a .tiff image file. But when im trying to read the pixels of image1 in variable c, i get an error saying "incompatible types: required int, found int[].
im quiet new to java, so can somebody tell me how to get around this problem. The code is otherwise working fine with other image formats.

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();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記憶穿過時間隧道 2025-01-10 12:16:52

如果您查看 文档 <在 ImagePlus 中调用 code>getPixel(int,int) 您会看到它返回一个 int 数组,而不是单个 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];

If you look at the documentation for getPixel(int,int) in ImagePlus you'll see that it returns an array of ints rather than a single 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.

It looks as if you're dealing with an RGB image, so you should be able to do the following instead:

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

int redValue = colorArray[0];
int greenValue = colorArray[1];
int blueValue = colorArray[2];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文