Liu
Liu

Reputation: 29

Using ImageProcessor in ImageJ

I am new to java and imageJ. I already loaded one image and get an ImageProcessor which I called imgproc. And I found the boundary/box in the image which surround the features. Outside just background. I also found the pixels Matrix of this area. Now I am trying to only process this area in the image. And to do that with a previous existing code(method), my input parameter should be an ImageProcessor. So, my initial thought is to used the duplicate() method to make a copy of imgproc. And use the resize method to shrink it down to the size of the box I found before. But this did not work, as I tested with a show image method I have to display it. What I got is just a shrunk black picture. This initial thought is coded here:

ImageProcessor Whiteimproc=imgproc.duplicate();
ImageProcessor BWhiteimproc=Whiteimproc.resize(BWhiteMatrix.length,BWhiteMatrix[0].length);
BWhiteimproc.setIntArray(BWhiteMatrix);
//the next three lines are going to show the image
Image ImagetoShow=BWhiteimproc.createImage();
Img ShowImg= new Img();
ShowImg.imgFrame(ImagetoShow,"BWhite");`

Then I tried to use ImagePlus and create an new ImageProcessor. And it worked. As shown below:

ImagePlus imgWhite=IJ.createImage("white","jpg",BWhiteMatrix.length,BWhiteMatrix[0].length,1);
ImageProcessor BWhiteimproc=imgWhite.getProcessor();
BWhiteimproc.setIntArray(BWhiteMatrix);
//the next three lines are going to show the image
Image ImagetoShow=BWhiteimproc.createImage();
Img ShowImg= new Img();
ShowImg.imgFrame(ImagetoShow,"BWhite");

Would anyone help me with why is that? And I do know why I could not use ImageProcessor to define an new object of ImageProcessor Class.

Thanks

Upvotes: 0

Views: 4014

Answers (1)

Josef Borkovec
Josef Borkovec

Reputation: 1079

I am not sure but the first approach might not work because the type of the ImageProcessor is different from the one in the second approach. Try checking the runtime type of the ImageProcessors with BWhiteimproc.getClass().getName().

ImageProcessor#setIntArray(int[][]) does different things for different types of images. For 32-bit images it calls Float.intBitsToFloat(int) so if the int value is 100, the saved float value will be +0e100 (last 8 bits of a float is exponent) which is zero. For 8-bit and 16-bit images it casts the int to the less precise type (byte and short).

And I do know why I could not use ImageProcessor to define an new object of ImageProcessor Class.

ImageProcessor is an abstract class. You cannot create instances of abstract classes. Use one of the subclases instead:

  • ByteProcessor for 8-bit grayscale images
  • ShortProcessor for 16-bit grayscale images
  • FloatProcessor for 32-bit floating point grayscale images
  • ColorProcessor for RGB images

Upvotes: 1

Related Questions