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)ImagePlusint見ると、単一の ではなく s の配列が返されることがわかりますint

(x,y) のピクセル値を 4 要素配列として返します。グレースケール値は最初の要素で返されます。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 に答える