Skip to content
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

Wrong colors when using Java2DFrameUtils to convert BufferedImage to Mat #1323

Closed
waicool20 opened this issue Oct 29, 2019 · 3 comments
Closed

Comments

@waicool20
Copy link

I'm trying to use Java2DFrameUtils class to convert my BufferedImage to Mat but it seems the R and B channel get mixed up in certain cases and I'm not able to assert what factor comes to play since some images show up just fine while others don't. I've checked the image properties through gimp and don't seem to see any problems between the cases.

I wrote this in kotlin hope you don't mind, the images here are random ones I found laying around as well.

import org.bytedeco.javacv.Java2DFrameUtils
import org.bytedeco.opencv.global.opencv_highgui
import java.io.File
import javax.imageio.ImageIO

fun main() {
    val image = ImageIO.read(File("Vim Cheat Sheet.png"))
    val imageMat = Java2DFrameUtils.toMat(image)

    opencv_highgui.imshow("Test", imageMat)
    opencv_highgui.waitKey()
}

Here's the original image:
Vim Cheat Sheet

And this is what shows up:
image


Another example:
calculator
image


This one works fine somehow
World Of Warships Iowa Class BB Missouri

image


And of course
bytedeco
image

@saudet
Copy link
Member

saudet commented Oct 29, 2019

You need to invert the R and B channels. To do that easily, we can set the flipChannels argument of Java2DFrameConverter.getBufferedImage() and/or Java2DFrameConverter.getFrame() to true.

@saudet
Copy link
Member

saudet commented Oct 29, 2019

Duplicate of #1053

@saudet saudet marked this as a duplicate of #1053 Oct 29, 2019
@saudet saudet closed this as completed Oct 29, 2019
@saudet
Copy link
Member

saudet commented Oct 29, 2019

As to why it sometimes happens and sometimes doesn't, that's just how ImageIO and OpenCV work. You can refer to issues #181 and #1075 for additional information about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants