OpenCV png c++ assertion failed
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...
had type == 24 (at the end)
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...)
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).
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
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.