problem with imageconversion in ImageJ api

H

harryos

hi
I tried to convert an image to 8bit grayscale using imageJ api.
The code snippet is as follows

...
import ij.IJ;
import ij.ImagePlus;
import ij.process.ImageConverter;
import java.awt.image.BufferedImage;
.....

public static void toGrayscale(){
ImagePlus imp=new ImagePlus("F:\\myimgs\\Picture 001.jpg");
if (imp.getType() == ImagePlus.GRAY8){
System.out.println("gray8");
}else{
System.out.println("orig image color mode="+imp.getType());
ImageConverter ic = new ImageConverter(imp);
ic.convertToGray8();
IJ.saveAs(imp, "png", "F:\\myimgs\\newimage.png");
BufferedImage bufferedImage=imp.getBufferedImage();
if (imp.getType() == ImagePlus.GRAY8){
System.out.println("after conversion, type is gray8");
System.out.println("bufferedImage type is="+
+bufferedImage.getType());
}else{
System.out.println("still imp is="+imp.getType());
System.out.println("bufferedimg type
is="+bufferedImage.getType());

}

}

however ,when I run this using a truecolor image,I get the following
output.

orig image color mode=4 // TYPE_INT_BGR
after conversion, type is gray8
bufferedimg type is=13 // TYPE_BYTE_INDEXED

I am confused by the value 13.Shouldn't it be 10 viz TYPE_BYTE_GRAY ?
can someone tell me why this is happening?
thanks a lot
harry.
 
A

Andrew Thompson

hi
I tried to convert an image to 8bit grayscale using imageJ api.
The code snippet is as follows

You will probably have better luck talking to
other people who use ImageJ. They can be found
at..
thanks a lot

No wucken forry's.
 
J

John B. Matthews

harryos said:
orig image color mode=4 // TYPE_INT_BGR
after conversion, type is gray8
bufferedimg type is=13 // TYPE_BYTE_INDEXED

I am confused by the value 13.Shouldn't it be 10 viz TYPE_BYTE_GRAY ?
can someone tell me why this is happening? thanks a lot

Type BufferedImage.TYPE_BYTE_GRAY is non-indexed, while type
BufferedImage.TYPE_BYTE_INDEXED can have an IndexColorModel:

<http://java.sun.com/javase/6/docs/api/java/awt/image/BufferedImage.html
#TYPE_BYTE_INDEXED>

You can covert to BufferedImage.TYPE_BYTE_GRAY explicitly if that's what
you need:

<http://groups.google.com/group/comp.lang.java.programmer/
msg/d4c436ab64d4ced4>
 
K

Knute Johnson

harryos said:
hi
I tried to convert an image to 8bit grayscale using imageJ api.
The code snippet is as follows

..
import ij.IJ;
import ij.ImagePlus;
import ij.process.ImageConverter;
import java.awt.image.BufferedImage;
....

public static void toGrayscale(){
ImagePlus imp=new ImagePlus("F:\\myimgs\\Picture 001.jpg");
if (imp.getType() == ImagePlus.GRAY8){
System.out.println("gray8");
}else{
System.out.println("orig image color mode="+imp.getType());
ImageConverter ic = new ImageConverter(imp);
ic.convertToGray8();
IJ.saveAs(imp, "png", "F:\\myimgs\\newimage.png");
BufferedImage bufferedImage=imp.getBufferedImage();
if (imp.getType() == ImagePlus.GRAY8){
System.out.println("after conversion, type is gray8");
System.out.println("bufferedImage type is="+
+bufferedImage.getType());
}else{
System.out.println("still imp is="+imp.getType());
System.out.println("bufferedimg type
is="+bufferedImage.getType());

}

}

however ,when I run this using a truecolor image,I get the following
output.

orig image color mode=4 // TYPE_INT_BGR
after conversion, type is gray8
bufferedimg type is=13 // TYPE_BYTE_INDEXED

I am confused by the value 13.Shouldn't it be 10 viz TYPE_BYTE_GRAY ?
can someone tell me why this is happening?
thanks a lot
harry.

I don't know ImageJ but it is easy to do with plain old Java.

public static BufferedImage convertToGray(BufferedImage image) {
BufferedImage gray = new BufferedImage(image.getWidth(),
image.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
ColorConvertOp op = new ColorConvertOp(
image.getColorModel().getColorSpace(),
gray.getColorModel().getColorSpace(),null);
op.filter(image,gray);
return gray;
}
 
H

harryos

thanks K.J...The BufferedImage created using ColorConvertOp is of the
correct type .
When I queried the ImageJ group,I found out that the problem was
because of a bug in ImageJ..It seems to be corrected in the daily
build 1.43h of imageJ

thanks for your help.
harry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top