简体   繁体   中英

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

I have a function that detects motion between two frames and stores a cropped image of only the moving Object in the variable cv::Mat result_cropped . Now I want to add a function that checks the result_cropped for black pixels. I wrote the code for that easely but I'm completly stuck on trying to implement it in my class.

For some reason my blackDetection(Mat & cropped) can't access the cropped image which results in the program crashing. Heres my simplified code:

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;
}

Again, the problem is that I can't manage to access result_cropped in blackDetection(Mat & result_cropped).

\\\\edit: my complete code for this class http://pastebin.com/3i0WdLG0 . Please someone help me. This problem doesn't make any sense for me..

You don't have a cv::waitKey() in blackDetection() , so you will crash before you get to the cvWaitKey() in run() . You are jumping to conclusions that result_cropped is "empty".

You have not allocated croppedBlack anywhere, so you will crash on croppedBlack.at<Vec3b>(y,x)[c] = .

Add this at the start of blackDetection() (eg):

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

To make it faster see How to scan images ... with OpenCV : The efficient way

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);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM