getBufferedImage() in ImagePlus returns wrong type

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

getBufferedImage() in ImagePlus returns wrong type

Harry Oswald
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    // COLOR_RGB as per ImageJ docs
after conversion, type is gray8
bufferedimg type is=13      // TYPE_BYTE_INDEXED as per jdk doc

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.
Reply | Threaded
Open this post in threaded view
|

Re: getBufferedImage() in ImagePlus returns wrong type

Albert Cardona-2
Harry,

All you want:

ImagePlus imp = IJ.openImage("F:\\myimgs\\Picture 001.jpg");

imp.setProcessor(imp.getTitle(),
                 imp.getProcessor().convertToByte(false));

IJ.log("image type: " + imp.getType());

BufferedImage bi = imp.getProcessor().getBufferedImage();

IJ.log("bi type: " + bi.getType());


Albert




Harry Oswald wrote:

>  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    //     COLOR_RGB as per ImageJ docs
>  after conversion, type is gray8
>  bufferedimg type is=13      // TYPE_BYTE_INDEXED as per jdk doc
>
>  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.


--
Albert Cardona
http://albert.rierol.net
Reply | Threaded
Open this post in threaded view
|

Re: getBufferedImage() in ImagePlus returns wrong type

Wayne Rasband
In reply to this post by Harry Oswald
This bug is fixed in the v1.43h daily build.

-wayne


________________________________________
From: Harry Oswald
Sent: Friday, October 09, 2009 6:24 AM
To: List IMAGEJ

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    //   COLOR_RGB as per ImageJ docs
after conversion, type is gray8
bufferedimg type is=13      // TYPE_BYTE_INDEXED as per jdk doc

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.