-
Notifications
You must be signed in to change notification settings - Fork 260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ConvertBufferedImage does not always return gray images for SingleBands #28
Comments
Having trouble understand. Can you clarify/give an example? |
Please check code below. It produces following output: I8: 3 Either I misunderstand your concept or there is something wrong. I expect 1 for all types.
ImageBase singleBand = GeneralizedImageOps.createSingleBand(ImageDataType.I8, 10, 10);
BufferedImage image = ConvertBufferedImage.convertTo(singleBand, null, false);
System.err.println("I8: " + image.getSampleModel().getNumBands());
singleBand = GeneralizedImageOps.createSingleBand(ImageDataType.I16, 10, 10);
image = ConvertBufferedImage.convertTo(singleBand, null, false);
System.err.println("I16: " + image.getSampleModel().getNumBands());
singleBand = GeneralizedImageOps.createSingleBand(ImageDataType.F32, 10, 10);
image = ConvertBufferedImage.convertTo(singleBand, null, false);
System.err.println("F32: " + image.getSampleModel().getNumBands());
|
Ok I understand. You've run into a case that was missed. I'm not sure what to do with F32 images. There really isn't an equivalent BufferedImage. Maybe TYPE_INT_RGB so that positive and negative numberse can be colorized? TYPE_BYTE_GRAY since it's just a single band? Throw an exception since there is no reasonable default? |
How about this: IMHO TYPE_INT_RGB is not better at all because it also has "only" 8 bit per color. So it is waste of memory then. |
what do you do with 16bit BufferedImages? It's a bit of a weird format. Most of the time I convert a float into a BufferedImage because I want to visualize it, so having an RGB format makes more sense |
I'm now leaning towards an exception since it's probably application specific what you want. |
Hi Peter, I have written a little test to understand the issue. As far as I can see the USHORT_GRAY is really the best option because it has 16 bit for the gray scale and really this information. INT_RGB reserves 8 bit per band so you have only a 8 bit gray scale there but three 9 times bigger data which makes it inefficient to handle/paint also. TYPE_3BYTE_BGR, type: 5, bands: 3, bits: 24 private void loadImages() throws IOException { BufferedImage read = ImageIO.read(new File("gradient.png")); printImageInfo("TYPE_3BYTE_BGR", read); ImageIO.write(read, "png", new File("typeCustom.png")); final BufferedImage gray = pushToFormat(read, BufferedImage.TYPE_BYTE_GRAY); printImageInfo("TYPE_BYTE_GRAY", gray); ImageIO.write(gray, "png", new File("typeByteGray.png")); final BufferedImage rgb = pushToFormat(read, BufferedImage.TYPE_INT_RGB); printImageInfo("TYPE_INT_RGB", rgb); ImageIO.write(rgb, "png", new File("typeIntRgb.png")); final BufferedImage ushort = pushToFormat(read, BufferedImage.TYPE_USHORT_GRAY); printImageInfo("TYPE_USHORT_GRAY", ushort); ImageIO.write(ushort, "png", new File("typeShortGray.png")); } private BufferedImage pushToFormat(BufferedImage read, int type) { final BufferedImage li = new BufferedImage(read.getWidth(), read.getHeight(), type); li.getGraphics().drawImage(read, 0,0, null); return li; } private void printImageInfo(String name, BufferedImage read) { System.err.println(name + ", type: " + read.getType() + ", bands: " + read.getSampleModel().getNumBands() + ", bits: " + read.getColorModel().getPixelSize()); System.err.println("first pixel value: " + read.getData().getPixel(0,0, (double[])null)[0]); System.err.println("last pixel value: " + read.getData().getPixel(read.getWidth() - 1, 0, (double[])null)[0]); } |
But it doesn't handle negative numbers. I tend to use RGB for floats when On Fri, Jun 26, 2015 at 2:47 AM, thhart notifications@github.com wrote:
"Now, now my good man, this is no time for making enemies." — Voltaire |
Well that broke a lot of stuff. For maintaining some backwards compatibility TYPE_BYTE_GRAY seems to be the best default.
If you need anything special just provide your own BufferedImage instead of null. That's what everything in VisualizeImageData does, which is where I put all the application specific convert for visualization. |
In ConvertBufferedImages.checkInputs there a gray scale image is only returned when the src image type is ImageInt16. But it should return a gray scale not by type but by src band width.
The text was updated successfully, but these errors were encountered: