Qimage to cv::Mat convertion strange behaviour

后端 未结 6 2223
离开以前
离开以前 2020-12-19 16:18

I am trying to create an application where I am trying to integrate opencv and qt.

I managed successfully to convert a cv::Mat to QImage by using the code below:

6条回答
  •  抹茶落季
    2020-12-19 17:16

    Just find out the "correct" solution of copying(not reference) the QImage to cv::Mat

    The answer of Martin Beckett is almost correct

    for (int i=0;i

    I don't see the full codes, but I guess you may want to use it like this way

    cv::Mat mat(image.height(), image.width(), CV_8UC3);
    for (int i=0;i

    But this code exist a problem, the memory allocated by cv::Mat may not have the same "bytesperline" as the QImage

    The solution I found is take the reference of the QImage first, then clone it

    return cv::Mat(img.height(), img.width(), format, img.bits(), img.bytesPerLine()).clone();    
    

    The solution of Martin Beckett suggested could generate correct result in most of the times, I didn't notice there are a bug until I hit it.

    Whatever, I hope this is a "correct" solution. If you find any bug(s), please let everyone know so we could have a change to improve the codes.

提交回复
热议问题