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();
}
}
}
If you look at the documentation for getPixel(int,int)
in ImagePlus
you'll see that it returns an array of int
s 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];