为什么我无法访问函数中的对象?

Why can't I access the Object in my function?

本文关键字:对象 函数 访问 为什么      更新时间:2023-10-16

我有一个函数,检测两帧之间的运动,并在变量cv::Mat result_庄稼中存储仅移动对象的裁剪图像。现在我想添加一个函数来检查result_庄稼是否有黑色像素。我写的代码很容易,但我完全卡住了,试图实现它在我的类。

由于某些原因,我的blackDetection(Mat &裁剪)无法访问裁剪后的图像,这会导致程序崩溃。下面是我的简化代码:

void ActualRec::run(){
    while (isActive){
    //...code to check for motion
    //if there was motion a cropped image will be stored in result_cropped      
        number_of_changes = detectMotion(motion, result, result_cropped, region,  max_deviation, color);
        if(number_of_changes>=there_is_motion) {
            if(number_of_sequence>0){
            // there was motion detected, store cropped image - this works 
            saveImg(pathnameThresh, result_cropped);
                if (blackDetection(result_cropped)==true){
                    //the cropped image has black pixels
                }
                else {
                   //the cropped image has no black pixels
            }
            number_of_sequence++;
        }
        else
        {
            // no motion was detected
        }
    }
}
bool ActualRec::blackDetection(Mat & result_cropped){
//...check for black pixels, program crashes since result_cropped is empty
//if i add imshow("test",result_cropped) I keep getting an empty window
   if (blackPixelCounter>0){
       return true;
   }
else return false;
}

再次,问题是我无法在blackDetection(Mat &result_cropped)。

\edit:该类的完整代码http://pastebin.com/3i0WdLG0。谁来帮帮我吧。这个问题对我来说没有任何意义。

blackDetection()中没有cv::waitKey(),因此在到达run()中的cvWaitKey()之前您将崩溃。你急于得出result_cropped是"空"的结论。

您没有在任何地方分配croppedBlack,因此您将在croppedBlack.at<Vec3b>(y,x)[c] =上崩溃。

blackDetection()(例如)开头添加:

croppedBlack.create(result_cropped.size(), result_cropped.type());

要使它更快,请参阅如何扫描图像…使用OpenCV:有效的方法

bool ActualRec::blackDetection(Mat& result_cropped)
{
    croppedBlack.create(result_cropped.size(), result_cropped.type());
    int blackCounter = 0;
    for(int y = 0; y < result_cropped.rows; ++y)
    {
        Vec3b* croppedBlack_row = croppedBlack.ptr<Vec3b>(y);
        Vec3b* result_cropped_row = result_cropped.ptr<Vec3b>(y);
        for(int x = 0; x < result_cropped.cols; ++x)
        {
            for(int c = 0; c < 3; ++c)
            {
               croppedBlack_row[x][c] = 
                    saturate_cast<uchar>(alpha * result_cropped_row[x][c] + beta);
            }
        }
    }
}