Ask Your Question
0

Display Color Variations - CvShowImage vs. Java Graphics2D

asked 2013-09-20 16:51:11 -0600

Exspecto gravatar image

Hello,

I've written c++ code that uses OpenCv to display the image from a usb-connected video camera. The code was converted into a Windows .dll to be used by a Java Applet.

Now, after instructing the video camera to change the background and foreground colors, the colors displayed on the screen by CvNameWindow/CvShowImage are not the same as in a Java Graphics2D object. For example:

OPENCV CVShowImage            JAVA Graphics2D
FG / BG                       FG / BG
=============================================
Black on White                Black on White
Black on Amber                Black on Lime Green
Black on Green                Black on Cyan
Blue on White                 Black on Red
Yellow on Blue                Cyan on Red
White on Black                White on Black
Amber on Black                Cyan on Black
Green on Black                Lime Green on Black
etc...

The c++ code that retrieves the image from the camera and converts it into a Java byte array is as follows:

JNIEXPORT jbyteArray JNICALL Java_maple_MapleJNI_maple_1capture_1image
(JNIEnv *env, jclass){

    // Capture image from video camera
    UCHAR result = maple_capture_image(IMAGE_TYPE_RGB, (UCHAR *) rgb_img->imageData);

    jsize len = 0;
    jbyteArray imgArray = NULL;

    if (result == STATUS_NO_ERROR){

        // Convert image data to a jbyteArray
        len = rgb_img->imageSize;
        imgArray = env->NewByteArray(len);

        if (imgArray != NULL){
            env->SetByteArrayRegion(imgArray, 0, len, (jbyte*) rgb_img->imageData);
        }
    }

    if (result != STATUS_NO_ERROR){
        maple_disconnect();         
    }

    return imgArray;

}

Here is the Java code that takes the byte array and displays it in a Graphic2D object:

image = MapleJNI.maple_capture_image(); // image is of type byte[]

if (image != null){

    BufferedImage buffImg = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_3BYTE_BGR);
    buffImg.getRaster().setDataElements(0, 0, imageWidth, imageHeight, image);
    graph = buffImg.createGraphics();
    repaint();
}

Would anyone know why the Java Grphics2D doesn't display the same colors as with CvNamedWindow/CvShowWindow? Do I need to perform additional processing on the image on the Java-side?

Thanks in advance for your help!

edit retag flag offensive close merge delete

Comments

1

sounds like rgb (java) vs bgr (opencv)

berak gravatar imageberak ( 2013-09-21 02:06:01 -0600 )edit

Hello Berak,

I was able to solve the problem by adding a call to the cvCvtColor(img, img, CV_BGR2RGB) however the image now flickers more often. Is there a way in OpenCV to capture the initial image as RGB so that I would not have to perform the conversion?

Thank you in advance

Exspecto gravatar imageExspecto ( 2013-09-23 11:40:57 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-09-23 11:24:33 -0600

Exspecto gravatar image

updated 2013-09-23 11:38:26 -0600

Problem was resolve by converting the image from BGR (OpenCV) to RGB (Java). The following was added to the C++ code:

cvCvtColor(_rgb_img, _rgb_img, CV_BGR2RGB);

Cheers!

edit flag offensive delete link more

Comments

Accept your own answer as solution when possible, so that the topic shows solved :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-23 11:39:58 -0600 )edit

Question Tools

Stats

Asked: 2013-09-20 16:51:11 -0600

Seen: 651 times

Last updated: Sep 23 '13