cv::Mat to QImage conversion

后端 未结 3 1010
说谎
说谎 2021-02-10 08:01

I\'ve found very similiar topic: how to convert an opencv cv::Mat to qimage , but it does not solve my problem.

I have function converting cv::Mat to QImage



        
3条回答
  •  后悔当初
    2021-02-10 08:59

    Your solution to the problem is not efficient, in particular it is less efficient then the code I posted on the other question you link to.

    Your problem is that you have to convert from grayscale to color, or RGBA. As soon as you need this conversation, naturally a copy of the data is needed.

    My solution does the conversion between grayscale and color, as well as between cv::Mat and QImage at the same time. That's why it is the most efficient you can get.

    In your solution, you first try to convert and then want to build QImage around OpenCV data directly to spare a second copy. But, the data you point to is temporary. As soon as you leave the function, the cv::Mat free's its associated memory and that's why it is not valid anymore also within the QImage. You could manually increase the reference counter of the cv::Mat beforehand, but that opens the door for a memory leak afterwards.

    In the end, you attempt a dirty solution to a problem better solved in a clean fashion.

提交回复
热议问题