Ask Your Question
0

OpenCV png c++ assertion failed

asked 2013-09-06 04:50:15 -0500

srakowski gravatar image

updated 2013-09-06 06:51:06 -0500

Hi! My Code is:

Mat temp = imread(f_name, 1);


    cout << temp.channels() << "chanels in image" << endl ;
            getch()

;
        img = CImg<unsigned char>(temp.cols, temp.rows, 1, 4);
        width = img.width();
        high = img.height();
        static int ii = 0;
            cout << "Image readed nr " << ii << endl;
        // Rewrite from opencv to CImg
        for(int l = 0 ; l < width ; l++){
            for(int ll = 0 ; ll < high ; ll++){
                img (l,ll, 0 , 0) = temp.at<cv::Vec4b>(ll,l)[0]; 
                img (l,ll, 0 , 1) = temp.at<cv::Vec4b>(ll,l)[1]; 
                img (l,ll, 0 , 2) = temp.at<cv::Vec4b>(ll,l)[2];
                img (l,ll, 0 , 3) = temp.at<cv::Vec4b>(ll,l)[3]; // aplha chanell
            }
        }

It compile well, but in runtime there is assertion failed with mat.hpp in line 537. If i commented loops everything works without result ok.

Please Do Yiou have some ideas, please ?

Best Regards, Szymsztein

Edit:

Current code works, but rewriting from cimg to opencv not... :

CImg<unsigned char> finalimg2(x,a);
    ////////////////////////////////////////
    ////////////////////////////
    finalimg.append(finalimg2,'y',0);

    Mat tmp = Mat(finalimg.width(), finalimg.height(), DataType<cv::Vec4w>::type);
    // Rewrite from CImg to opencvi kno w
        for(int l = 0 ; l < finalimg.width() ; l++){
            for(int ll = 0 ; ll < finalimg.height() ; ll++){
                if( sizeof(finalimg(l,ll,0,0)) != 8)
                    cout << "(" << l << ", " << ll << ") size = " << sizeof(finalimg(l,ll,0,0)) << " ";
                tmp.at<cv::Vec4w>(ll,l)[0] = finalimg(l,ll, 0 , 0);
                tmp.at<cv::Vec4w>(ll,l)[1] = finalimg(l,ll, 0 , 1); 
                tmp.at<cv::Vec4w>(ll,l)[2] = finalimg(l,ll, 0 , 2); 
                tmp.at<cv::Vec4w>(ll,l)[3] = finalimg(l,ll, 0 , 3); 
            }
        }

        char resultName[15];
        //sprintf(resultName, "result%d.png", p);
        imwrite("result.png", tmp);

When i use Ve4w, these images which had type == 26, works well and are on output message, when i use vec4b, on output images there are only these images awhich had type ==26...

edit retag flag offensive close merge delete

Comments

had type == 24 (at the end)

srakowski gravatar imagesrakowski ( 2013-09-06 06:52:35 -0500 )edit

btw. i tried rewrite part with CImg to openCV and there only one method from CImg cause trouble for me: finalimg.append(finalimg2,'y',0); Because there are not the same in opencv (i tried += , but there are not the same size...)

srakowski gravatar imagesrakowski ( 2013-09-06 06:54:39 -0500 )edit

Ye, of course. The problem is that some of your images just aren't uchar. You need to check for the right type. Im not familiar with CImg. So I dont exactly know if you can check if they are uchar (1 Byte) or ushort (2 Bytes).

Moster gravatar imageMoster ( 2013-09-06 07:08:03 -0500 )edit

hmmm. i have no ide , because i am creating CImg<unsigned char> and it works but as i said with vec4b it write good some images , with vec4w write good with type -- 26

srakowski gravatar imagesrakowski ( 2013-09-06 07:21:21 -0500 )edit

Can you create an CImg<unsigned short>? You should do that in case Mat is of type 26. Then you need to check what type your CImg is while converting back to Mat.

Moster gravatar imageMoster ( 2013-09-06 07:44:47 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-09-06 05:12:03 -0500

berak gravatar image

updated 2013-09-06 05:15:22 -0500

imread(f_name, 1);

will (force)load a 3 channel img. so you can't access that by

temp.at<cv::Vec4b>(y,x); // that would need 4 channels, right ?

try :

imread(f_name, -1);

instead ( i.e don't force it into 3 channels )

and, ofc, make sure, that the image you're reading actually has 4 channels. unless it prints

4 chanels in image

on your console, your code will crash.

edit flag offensive delete link more

Comments

imread(f_name, -1); Still give me assertion...

srakowski gravatar imagesrakowski ( 2013-09-06 05:18:43 -0500 )edit

so, what did it print ?

check with another prog, if that image actually has 32bits / 4 channels. i bet it's not so.

berak gravatar imageberak ( 2013-09-06 05:25:52 -0500 )edit

btw, what assertion failed there ? what error do you get ?

berak gravatar imageberak ( 2013-09-06 05:27:12 -0500 )edit
srakowski gravatar imagesrakowski ( 2013-09-06 05:32:24 -0500 )edit

can you show what temp.type() outputs?

Moster gravatar imageMoster ( 2013-09-06 05:51:10 -0500 )edit

http://postimg.org/image/bemf4a1e5/

I see that temp.type() for index 0 is 24, and it is good processing For second image = index 1, temp.type() = 26 ???

srakowski gravatar imagesrakowski ( 2013-09-06 05:55:45 -0500 )edit

24 == CV_8UC4. that's what you want.

berak gravatar imageberak ( 2013-09-06 06:04:17 -0500 )edit
1

Ye, but 26 == CV_16UC4 which is most likely causing his issues.

Moster gravatar imageMoster ( 2013-09-06 06:11:07 -0500 )edit

so there is bad type, but how to cast this, please ?

srakowski gravatar imagesrakowski ( 2013-09-06 06:11:34 -0500 )edit
1

The second one you could access with cv::Vec4w which stand for 4 channel unsigned short.

Moster gravatar imageMoster ( 2013-09-06 06:15:25 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-09-06 04:50:15 -0500

Seen: 1,147 times

Last updated: Sep 06 '13