I wat to open two transparent images inside opencv in C++

asked 2018-08-19 13:30:25 -0600

masoud gravatar image

I have 2 transparent images which I want them to be opened in one window in opencv. The code below opens two images but with out alpha channel because it's using (CV_8UC3) and those images which are transparent we should use (CV_8UC4) to show the transparency but when I change the code to (CV_8UC4) the program gives me an error.

I'll make (CV_8UC4) and error line bold;

the original code is (CV_8UC3)

microsoft visual studio error dialog shows this error : Unhandled exception at 0x00007FFA48633FB8 in openCV.exe: Microsoft C++ exception: cv::Exception at memory location 0x0000007563FDEA00. and the console application window shows this one : OpenCV Error: Assertion failed (channels() == CV_MAT_CN(dtype)) in cv::Mat::copyTo, file C:\build\master_winpack-build-win64-vc14\opencv\modules\core\src\copy.cpp, line 272

Is this code suitable for what I want to do?

This code opens multiple images but i've use it for two.

code is:

    void ShowManyImages(string title, int nArgs, ...) {
    int size;
    int i;
    int m, n;
    int x, y;

    // w - Maximum number of images in a row
    // h - Maximum number of images in a column
    int w, h;

    // scale - How much we have to resize the image
    float scale;
    int max;

    // If the number of arguments is lesser than 0 or greater than 12
    // return without displaying
    if (nArgs <= 0) {
        printf("Number of arguments too small....\n");
        return;
    }
    else if (nArgs > 14) {
        printf("Number of arguments too large, can only handle maximally 12 images at a time ...\n");
        return;
    }
    // Determine the size of the image,
    // and the number of rows/cols
    // from number of arguments
    else if (nArgs == 1) {
        w = h = 1;
        size = 300;
    }
    else if (nArgs == 2) {
        w = 2; h = 1;
        size = 1000;
    }
    else if (nArgs == 3 || nArgs == 4) {
        w = 2; h = 2;
        size = 300;
    }
    else if (nArgs == 5 || nArgs == 6) {
        w = 3; h = 2;
        size = 200;
    }
    else if (nArgs == 7 || nArgs == 8) {
        w = 4; h = 2;
        size = 200;
    }
    else {
        w = 4; h = 3;
        size = 150;
    }

    // Create a new 3 channel image

Mat DispImage = Mat::zeros(Size(100 + sizew, 60 + sizeh), CV_8UC4);

    // Used to get the arguments passed
    va_list args;
    va_start(args, nArgs);

    // Loop for nArgs number of arguments
    for (i = 0, m = 20, n = 20; i < nArgs; i++, m += (0)) {
        // Get the Pointer to the IplImage
        Mat img = va_arg(args, Mat);

        // Check whether it is NULL or not
        // If it is NULL, release the image, and return
        if (img.empty()) {
            printf("Invalid arguments");
            return;
        }

        // Find the width and height of the image
        x = img.cols;
        y = img.rows;

        // Find whether height or width is greater in order to resize the image
        max = (x > y) ? x : y;

        // Find the scaling factor to resize the image
        scale = (float)((float)max / size);

        // Used to Align the images
        if (i % w == 0 && m != 20) {
            m = 20;
            n += 20 + size;
        }

        // Set the image ROI to display the current image
        // Resize the input image and copy the it to the Single Big Image
        Rect ROI(m, n ...
(more)
edit retag flag offensive close merge delete

Comments

1

opencv is a computer-vision library, and has no builtin support for transparency in images.

berak gravatar imageberak ( 2018-08-20 00:46:37 -0600 )edit
1

I think berak is right

jsxyhelu gravatar imagejsxyhelu ( 2018-08-23 07:35:46 -0600 )edit

so what about CV_8UC4 or IMREAD_UNCHANGED which is for transparency

masoud gravatar imagemasoud ( 2018-08-26 01:38:52 -0600 )edit

@masoud, that will indeed load the images correctly. but you'll have to solve compositing and drawing next. (again no support for alpha transparency here)

imho, you're simply on the wrong bus.

berak gravatar imageberak ( 2018-08-26 01:45:30 -0600 )edit