Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Display Color Variations - CvShowImage vs. Java Graphics2D

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!