Question

Using

Mat image;

I used

inRange(image,Scalar(170,100,0),Scalar(255,255,70),image);

and i detect object in blue, but I can't draw rectangle around the object.

Should I use mask? or something?

inRange(image,Scalar(170,100,0),Scalar(255,255,70),image);
GaussianBlur(image,image,Size(9,9),1.5);

for(int i = 2; i <image.cols-2;i++)
 for(int j = 2; j <image.rows-2;j++){
  if( image.at<Vec3b>(i-1,j-1)[0] > 200 &&
    image.at<Vec3b>(i-1,j)[0] > 200 &&
    image.at<Vec3b>(i-1,j+1)[0] > 200 &&
    image.at<Vec3b>(i,j-1)[0] > 200 &&
    image.at<Vec3b>(i,j)[0] > 200 &&
    image.at<Vec3b>(i,j+1)[0] > 200 &&
    image.at<Vec3b>(i+1,j-1)[0] > 200 &&
    image.at<Vec3b>(i+1,j)[0] > 200 &&
    image.at<Vec3b>(i+1,j+1)[0] > 200 
)
{

    if(min_x > i)
        min_x = i;
    if(min_y >j)
        min_y = j;
    if(max_x < i)
        max_x =i;
    if(max_y < j)
        max_y = j;

}
 }
 if(!(max_x==0 && max_y==0 && min_x==image.rows && min_y == image.cols))
 {
    rectangle(image,Point(min_x,min_y),Point(max_x,max_y),CV_RGB(255,0,0),2);
 }

  imshow("working", image);
  if(waitKey(100) >= 0) break;
}

}

This isn't working and a run time error. I don't know why.. help me!

Was it helpful?

Solution

Some tips:

  • Your image might be CV_8U3C, but inRange probably converts it to CV_8U, so better use for output a new Mat instance.

  • Use cv::findContours to detect your area.

  • Study meanshift used for tracking by opencv which might help you.

OTHER TIPS

You cannot use RGB image for the inrange method. You should transform your image to HSV color space, and use hue range of blue then, which is 95-135. There are so many "blue" possibilities at RGB space.

inRange(image,Scalar(95,0,0),Scalar(135,255,255),image);

The result will be a binary image, just find the contour and draw bounding rectangle around it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top