Reading RGB jp2 image results in Red and Blue channels reversed

asked 2016-07-09 10:35:08 -0600

jimfcarroll gravatar image

I'm using OpenCV 3.1.0 I'm calling it from Java.

When I read a jp2 file that was created using ImageIO I get an image with the red and blue channels reversed. When I open it using ImageIO ( https://github.com/jai-imageio/jai-im... ), it's fine. Is the opencv codec that loads the jp2 simply assuming the pixels are packed BGR rather than checking the relevant metadata?

I can fix this on a case by case basis by rewriting all of the images in memory before I do anything with them but this seems like a bug and I'd need to do it on an ad-hoc basis.

Thanks Jim

edit retag flag offensive close merge delete

Comments

The default color order in memory of OpenCV is BGR: http://docs.opencv.org/3.1.0/db/da5/tutorial_how_to_scan_images.html.

You can test it by loading jpeg, png, ... images.

Eduardo gravatar imageEduardo ( 2016-07-09 14:52:03 -0600 )edit

I think if you set the flag IMREAD_ANYCOLOR it will check the metadata.

Tetragramm gravatar imageTetragramm ( 2016-07-10 13:16:30 -0600 )edit

Thanks for the suggestions. I was pretty sure using the flag would work but unfortunately it didn't. If I write the image with OpenCV it works but if I had previously written it out it doesn't read it in correctly. I suppose it's possible the image is actually wrong but it reads in fine with ImageIO.

jimfcarroll gravatar imagejimfcarroll ( 2016-07-11 06:47:25 -0600 )edit

Well, if you do need to switch it, the best optimized way is to use cvtColor with the COLOR_RGB2BGR flag. I don't know how to check if it's switched except by manually.

Tetragramm gravatar imageTetragramm ( 2016-07-11 07:21:17 -0600 )edit