Overrule the OpenCV exception

asked 2015-05-09 08:16:47 -0500

215 gravatar image

updated 2015-05-09 20:04:37 -0500

Hi I am trying to overrule a openCV exception which occurs when the Mask which is applied on a image, lies outside of the dimension image... But i cannot make it do something else than catch that exeption when it happens... How do i make it overrule this problem?

This is the exception I want overruled:

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/matrix.cpp, line 323 terminate called after throwing an instance of 'cv::Exception' what(): /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/matrix.cpp:323: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat

code:

Point2f ImageConverter::Mean(vector<Point2f> points)
{
        Mat mean_;
        reduce(points,mean_,CV_REDUCE_AVG,1);
        Point2f mean(mean_.at<float>(0,0),mean_.at<float>(0,1));
        //cout << "Mean: " << mean_ << endl;
        return mean;

}

bool ImageConverter::std_dev(vector<Point2f> points)
{
        double Std_dev;
        Point2f mean = Mean(points);
        for(int i = 0; i < points.size(); i++)
        {      
                Std_dev += pow(euclideanDistance(points[i]-mean),2);

        }
        Std_dev = Std_dev/points.size();
        Std_dev = sqrt(Std_dev);
        cout << "STD_DEV: " << Std_dev << endl;
        if(Std_dev <= face.width)
        {
                cout << "Still reasonable fine " << endl;
                return true; //Threshold has to be added to know when it should re init the goodFeaturesToTrack Function.
        }
        else
        {
                cout <<"you are fucked - too much drifting " << endl;  
                return false;
        }
}
double ImageConverter::std_dev_determine(vector<Point2f> points)
{
        double Std_dev;
        Point2f mean = Mean(points);
        for(int i = 0; i < points.size(); i++)
        {      
                Std_dev += pow(euclideanDistance(points[i]-mean),2);

        }
        Std_dev = Std_dev/points.size();
        Std_dev = sqrt(Std_dev);
        return Std_dev;
}

        else
    {
                double start,duration;
                start  = ros::Time::now().toSec();
                if(posSendMsgCounter > posisit)
                {
                        posSendMsgCounter = 0;
                        cout << "posSendMsgCounter reset" << endl;  

                }
                posSendMsgCounter++;
        image = cv_ptr->image;
        flip(image, image, 1);
        Mat mask(image.size(),CV_8UC1,Scalar(0));
        mask(face).setTo(Scalar(255));
                flip(mask,mask,1);
                cvtColor(image,gray,COLOR_BGR2GRAY);
        equalizeHist(gray,gray);
        Rect bounds(0,0,image.rows,image.cols);
                face = face & bounds;
                if(!(1 <= face.x && face.x + face.width <= image.cols-1 && 1 <= face.y && face.y + face.height <= image.rows-1)) // Try to react when it comes outside => it has to leave the mask where it last was last found within the image. 
                {

                                 cout << "I am out" << endl;

                                 backup_center = Mean(goodFeatures);
                 Rect NewBOx;
                 NewBOx.x  = backup_center.x-faceWidth/2;
                 NewBOx.y  = backup_center.y-faceHeight/2;
                 NewBOx.width = face.width;
                 NewBOx.height = face.height;
                 face = NewBOx;
                 circle(image,backup_center,10,Scalar(255,0,255),-1,12,0);

                }

                cout << "face.x: " << face.x << " should be bigger than 0 "<<endl;
                cout << "face.x + face.width: " << face.x + face.width << " <= " << image.cols << endl;
                cout << "face.y: " << face.y << " should be bigger than 0 "<<endl;
                cout << "face.y + face.height: " << face.y + face.height << " <= " << image.rows << endl;
                imshow("Mask", mask);
        circle(cv_ptr->image, center_of_frame, 1, CV_RGB(0,255,255),8,8,0);
        static uint64_t c ...
(more)
edit retag flag offensive close merge delete

Comments

'overrule' would mean what exactly ?

berak gravatar imageberak ( 2015-05-09 08:38:55 -0500 )edit

So it doesn't end the program when it happens, but does something else.. I want the mask to be at the last location where it could be in the image.

215 gravatar image215 ( 2015-05-09 09:12:20 -0500 )edit

isn't that exactly, what try/catch is for ?

btw, you could try to avoid the whole situation, by cropping the rect used for the roi to the image bounds:

Rect bounds(0,0, img.rows, img.cols);

Rect r = ... // used for the roi
r = r & bounds; // limit to valid space
Mat roi = img(r);
berak gravatar imageberak ( 2015-05-09 09:23:03 -0500 )edit

when would you apply it.. ?

215 gravatar image215 ( 2015-05-09 10:21:41 -0500 )edit

oh, i assumed, you were constructing the Mask from a Rect, maybe i just misread you.

berak gravatar imageberak ( 2015-05-09 10:30:44 -0500 )edit

the mask is pre made but it moves, by keep making changing the parameter r.x and r.y

215 gravatar image215 ( 2015-05-09 10:47:50 -0500 )edit

ok, unfortunate. so you'll either have to do manual bouds-checking before, or do something in try/catch.

berak gravatar imageberak ( 2015-05-09 10:53:17 -0500 )edit

How would you do manual bound check and bypass the try/catch ?

215 gravatar image215 ( 2015-05-09 11:20:52 -0500 )edit

what ? do the same check as in the exception,.

0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows

berak gravatar imageberak ( 2015-05-09 11:58:11 -0500 )edit

Well.. thats what my if says, but the program goes to exception rather than be stuck at my if statement.. I understand if hard to make any sense of it without code.. i will add the code soon. beware of wall of code..

215 gravatar image215 ( 2015-05-09 12:02:53 -0500 )edit