Ask Your Question
0

Scanning white pixel and make rectangle for border contour

asked 2017-06-11 10:43:50 -0500

choro gravatar image

updated 2017-06-11 14:30:31 -0500

LBerger gravatar image

Excuse me, i wanna scan white pixel in this image and make border around whole contour that has white color . But I dont know how to code it. This is the result. :( I'm so confuse .

C:\fakepath\1.JPG

C:\fakepath\2.JPG

Mat grayi;
//To store the thresholded image
Mat ret;
//convert the image to grayscale
cvtColor(intpro,grayi,CV_BGR2GRAY);
imshow("Gray Image",grayi);
//threshold the image
threshold(grayi,ret,0,255,CV_THRESH_OTSU);
imshow("Thres",ret);

Mat grayi;
//To store the thresholded image
Mat ret;
//convert the image to grayscale
cvtColor(intpro,grayi,CV_BGR2GRAY);
imshow("Gray Image",grayi);
//threshold the image
threshold(grayi,ret,0,255,CV_THRESH_OTSU);
imshow("Thres",ret);Mat horizontal(ret.cols,2,CV_32S);//horizontal histogram
horizontal = Scalar::all(255);
Mat vertical(ret.rows,1,CV_32S);//vertical histogram    
vertical = Scalar::all(255);

for(int i=0;i<ret.cols;i++)
{
    int hor= horizontal.at<int>(i,0)=countNonZero(ret(Rect(i,0,1,ret.rows)));           
cout<< hor;
rectangle(ret, Rect(0, i, hor, 1), Scalar(255,255,255), 2);
}
for(int i=0;i<ret.rows;i++)
{
    int ver = vertical.at<int>(i,0)= countNonZero(ret(Rect(0,i,ret.cols,1)));
    cout<< ver;
    //= countNonZero(ret(Rect(0,i,ret.cols,1)));
    rectangle(ret, Rect(0, i, ver, 1), Scalar(255,255,255), 2);
}
imshow("cc",ret);

} please anyone help me :(. Thank you

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2017-06-11 20:59:55 -0500

hoang anh tuan gravatar image

Try using the function findContours or boundingRect

edit flag offensive delete link more
0

answered 2017-06-11 14:21:17 -0500

Tetragramm gravatar image

Try using the function boundingRect.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-06-11 10:43:50 -0500

Seen: 204 times

Last updated: Jun 11 '17