当我试图在OpenCV IOS中为变量赋予属性时,出现了奇怪的崩溃

Weird crash when I try to attribute a variable in OpenCV IOS

本文关键字:属性 崩溃 OpenCV IOS 变量      更新时间:2023-10-16

有些事情我不明白。我正在为我的IOS设备使用OpenCV IOS。

我有一个带有私有变量cv::Rect的C++类。这个变量位于我的.h文件中。在.cpp文件中,我有一个创建cv::Rect的方法。然后,我想将这个新创建的cv::Rect归因于我的类变量,但它崩溃了,我不明白为什么。

.h文件

class detection {

public:
    detection();
    cv::Mat processImage(cv::Mat frame);
    cv::Mat detectFace(cv::Mat frame);
public:
    cv::Rect getRectangleDetection();
    void setRectangleDetection(cv::Rect& rect);

private:
    cv::Rect _rectangeDetect;
};

.cpp文件

    cv::Mat detection::processImage(cv::Mat frame){
    Mat originalColorImage;
    frame.copyTo(originalColorImage);
    int cx = frame.cols/2;
    int cy = frame.rows/2;
    int width = 1000;
    int height = 1000;
    NSLog(@"[INFO] Rectangle creation");
    cv::Rect rect1(cx-width/2,  cy-height/2, width,height);
    cv::Rect test2;
    //test2 = rect1;//It works !
    setRectangleDetection(rect1); // or _rectangeDetect = rect1 --> both don't work
    cv::rectangle( originalColorImage,rect1,
                  cv::Scalar(255, 0, 0),4);

    return originalColorImage;
}

我看了一眼事故报告,发现:

exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000000
Triggered by Thread:  0
Filtered syslog:
None found
Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   detect          0x000000010007f580 cv::Rect_<int>::operator=(cv::Rect_<int> const&) (types.hpp:1689)
1   detect          0x000000010007f504 OpenCV::setRectangleDetection(cv::Rect_<int>) (OpenCV.mm:172) //This is the setter but If I'm not using the setter the error will come from _rectangleDetect = rect1.

我还尝试初始化变量cv::Rect,但行为相同。

你知道发生了什么吗?真的,我试图弄清楚为什么,但没有成功。我以前用过很多OpenCv,这是第一次发生这样的事情。

谢谢!

好的,我发现了问题。这是一个愚蠢的决定。事实上,在我的另一个类上,当我使用检测类时,我忘记了初始化对象。

Detection _detect = new Detection()

由于我没有从下面的行中得到任何错误,而且行为似乎很好(即使进行了一些调试(,所以我没有想到这一点。

_detect->processImage(frame);

无论如何,谢谢你们。