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 put a # on the line of (CV_8UC4);
I'll put a * on the line of error;
//the code turned to 3 parts because of * and #.
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:
What I have tried:
void ShowManyImages(string title, int nArgs, ...) {
int size;
int i;
int m, n;
int x, y;
int w, h;
float scale;
int max;
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;
}
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;
}
# Mat DispImage = Mat::zeros(Size(100 + size*w, 60 + size*h), CV_8UC4);
va_list args;
va_start(args, nArgs);
for (i = 0, m = 20, n = 20; i < nArgs; i++, m += (0)) {
Mat img = va_arg(args, Mat);
if (img.empty()) {
printf("Invalid arguments");
return;
}
x = img.cols;
y = img.rows;
max = (x > y) ? x : y;
scale = (float)((float)max / size);
if (i % w == 0 && m != 20) {
m = 20;
n += 20 + size;
}
Rect ROI(m, n, (int)(x / scale), (int)(y / scale));
Mat temp; resize(img, temp, Size(ROI.width, ROI.height));
* temp.copyTo(DispImage(ROI));
}
namedWindow(title, 1);
imshow(title, DispImage);
waitKey();
va_end(args);
}
int main(int argc, char** argv)
{
Mat img1 = imread("c:\\1.png");
Mat img2 = imread("c:\\2.png");
ShowManyImages("Image", 2, img1, img2);
return 0;
}