Qt signal slot cv::Mat unable to read memory access violation

前端 未结 1 1562
灰色年华
灰色年华 2021-01-16 18:34

I have a Microsoft visual studio application that is grabbing frames from cameras and I am trying to display those frames in a Qt application. I am doing some processing wit

相关标签:
1条回答
  • 2021-01-16 19:08

    What I see is happenning:

    1. You get an image from queue. As per OpenCV docs:

      Mat& cv::Mat::operator= ( const Mat & m )

    Assigned, right-hand-side matrix. Matrix assignment is an O(1) operation. This means that no data is copied but the data is shared and the reference counter, if any, is incremented. Before assigning new data, the old data is de-referenced via Mat::release .

    1. Then you pass it as cv::Mat image (by value) when emitting signal. The copy constructor again doesn't copy any data:

    Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it. The reference counter, if any, is incremented. So, when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m . If you want to have an independent copy of the sub-array, use Mat::clone() .

    1. Your data pointers are queued on UI thread

    2. You get/try-get new frame triggering release from p.1

    3. Your queued slot is executed and crashes...

    Suggestion: I haven't worked much with it, but it seems something like cv::Mat::clone to make a deep copy is what you need, to prevent release of memory before it would be used by UI thread.

    Or possibly it would be enough to define image right when you pop it from queue:

    cv::Mat image = camera_q->pop();
    
    0 讨论(0)
提交回复
热议问题