I would like to draw the canny result on the original image. but i have an error. I think it’s a matter of converting from QImage to Mat and vice versa because this code works:
Mat image;
image = imread("C:\\mel.bmp",1);
Mat gray, edqes, out;
cvtColor(image, gray, COLOR_BGR2GRAY);
Canny(gray, edqes, 50, 150, 3);
cvtColor(edqes, edqes, COLOR_GRAY2BGR);
bitwise_or(edqes,image,out);
cvNamedWindow("original",CV_WINDOW_NORMAL);
cvNamedWindow("binary",CV_WINDOW_NORMAL);
cvNamedWindow("canny",CV_WINDOW_NORMAL);
cvNamedWindow("out",CV_WINDOW_NORMAL);
imshow("original",image);
imshow("binary", gray);
imshow("canny", edqes);
imshow("out", out);
cvWaitKey(0);
cvDestroyAllWindows();
when i use this code:
QImage temp = nPixDst->pixmap().toImage();
cv::Mat image = QImage2Mat( temp ) ;
Mat gray, edqes, out;
cvtColor(image, gray, COLOR_BGR2GRAY);
Canny(gray, edqes, 50, 150, 3);
cvtColor(edqes, edqes, COLOR_GRAY2BGR);
bitwise_or(edqes,image,out);
nPixDst->setPixmap( QPixmap::fromImage( Mat2QImage( out )) );
i have error:
Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array') in cv::binary_op, file E:\soft\build\opencv\opencv-src\modules\core\src\arithm.cpp, line 229
how can i fix this?
PS. here is the code to convert:
QImage MainWindow::Mat2QImage(cv::Mat const& src)
{
switch(src.type())
{
case CV_8UC4:
{
cv::Mat view(src);
QImage view2(view.data, view.cols, view.rows, view.step[0], QImage::Format_ARGB32);
return view2.copy();
break;
}
case CV_8UC3:
{
cv::Mat mat;
cvtColor(src, mat, cv::COLOR_BGR2BGRA); //COLOR_BGR2RGB doesn't behave so use RGBA
QImage view(mat.data, mat.cols, mat.rows, mat.step[0], QImage::Format_ARGB32);
return view.copy();
break;
}
case CV_8UC1:
{
cv::Mat mat;
cvtColor(src, mat, cv::COLOR_GRAY2BGRA);
QImage view(mat.data, mat.cols, mat.rows, mat.step[0], QImage::Format_ARGB32);
return view.copy();
break;
}
default:
{
//throw invalid_argument("Image format not supported");
return QImage();
break;
}
}
}
cv::Mat MainWindow::QImage2Mat(QImage const& src)
{
switch(src.format()) {
case QImage::Format_Invalid:
{
cv::Mat empty ;
return empty ;
break;
}
case QImage::Format_RGB32:
{
cv::Mat view(src.height(),src.width(),CV_8UC4,(void *)src.constBits(),src.bytesPerLine()) ;
return view ;
break;
}
case QImage::Format_RGB888:
{
cv::Mat out ;
cv::Mat view(src.height(),src.width(),CV_8UC3,(void *)src.constBits(),src.bytesPerLine());
cv::cvtColor(view, out, cv::COLOR_RGB2BGR);
return out ;
break;
}
default:
{
QImage conv = src.convertToFormat(QImage::Format_ARGB32);
cv::Mat view(conv.height(),conv.width(),CV_8UC4,(void *)conv.constBits(),conv.bytesPerLine());
return view ;
break;
}
}
}