I would like to draw the canny result on the original image

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;
    }
    }
}

related: c++ - I would like to draw the canny result on the original image - Stack Overflow

We have no idea where your line 229 is.
You make no attempt to check if the QImage to Mat conversion succeeds.

I have been using this conversion for a long time. it has always worked. but here it is in bitwise_or I think that in this case the conversion fails. can add that if I load the image directly mat and use imshow to display the image then everything works correctly

“his” line 229 is an assertion in OpenCV source, so it’s not his line. he doesn’t say where in his code the error happens.

Please can you see how many channels you have before you do a bitwise_or as i think you might have a spare alpha channel?

std::cout << edques.channels() << std::endl;
std::cout << image.channels() << std::endl;

thanks for the advice i checked it out. i got 3 feeds in edqes and image. I looked at the values ​​of the variables in the debugger. after converting to mat, the image changes its size. but maybe I misunderstood something. attach the debugger screenshot

01c1559d0f1

oh I’m sorry. i tested this on the first code.
but when checked on this:

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 got 3 channels for edqes and 4 channels for image. how i can fix it?

@Clinton Thank you so much! i solved this problem with
cvtColor(image, image, COLOR_BGRA2BGR);

Great stuff.
Was just going to reply but you got it.
I would check why you have alpha channel in the conversion (QImage2Mat)
have a great day

@Clinton hank you very much again!