[Blog Machine vision] maximum contour and projection

Source: Internet
Author: User

maximum contour and projection    The most recent use of the maximum contour and projection operations. In retrospect, these two algorithms are indeed very common basic algorithms. summarized here and extracted. Maximum profile: The premise is that the image is processed by grayscale and threshold, and the results of canny can be processed directly, and in some cases it needs to be converted in color domain beforehand. The final result, should be a contour, of course, can be used to deal with a certain method to get the external rectangle. Looking for the biggest silhouette
VECTOR<CV::P oint> findbigestcontour (Mat src)
{
intIMAX = 0; Number representing the maximum contour
intImaxcontour =-1; Represents the size of the maximum contour
STD::VECTOR<STD::VECTOR<CV::P oint>>contours;
Findcontours (src,contours,cv_retr_list,cv_chain_approx_simple);
for(intI=0;i<contours.size (); i++)
{
intItmp = Contourarea (Contours[i]);//The contour size is used here
if(Imaxcontour < Itmp)
{
IMAX = i;
Imaxcontour = itmp;
}
}
returnContours[imax];
}
call method to get the external matrixRect boundrect = Boundingrect (Mat (Findbigestcontour (canny))); projection processing :      the premise and the maximum contour are very similar here. The main concern of projection is to obtain the ROI in the original image by projecting the image, or to obtain the quantitative results of how many down waveforms are in the waveform.
vector<int> Vectorv; Transverse circulation
vector<int> Vectorh; Longitudinal cycle
vector<int> vupper;
vector<int> Vdown;
vector<int> hupper;
vector<int> hdower;

Do the transverse loop
for(inti=0;i<ostu.cols;i++)
{
Mat data = Ostu.col (i);
intItmp = Countnonzero (data);
Vectorv.push_back (ITMP);
}
The upper waveform is vupper and the lower waveform is Vdown
for(intI=1;i<vectorv.size (); i++)
{
if(Vectorv[i-1] = = 0 && vectorv[i]>0)
{
Vupper.push_back (i);
}
if(vectorv[i-1]>0 && vectorv[i] = = 0)
{
Vdown.push_back (i);
}
}
To do a longitudinal cycle, this is often handled by a picture of the result of a transverse loop
for(intj=0;j<ostu.rows;j++)
{
Mat data = Roitmp.row (j);
intItmp = Countnonzero (data);
Vectorh.push_back (ITMP);
}
for(intJ=0;j<vectorh.size () -1;j++)
{
if(vectorh[j]>0 && vectorh[j+1] = = 0)
{
Hdower.push_back (j);
}
if(Vectorh[j] = = 0 && vectorh[j+1]>0)
{
Hupper.push_back (j);
}
}

Due to the processing of the upper waveform and the lower waveform may be problematic, need to do some processing
One of the treatments here is to put forward the short blank areas
for(intJ=0;j{
To draw an area between the blanks
intiwidth = hupper[j+1]-hdower[j];
if(Iwidth > 10)
{
Iresult = Iresult +1;
}
}


From for notes (Wiz)

[Blog Machine vision] maximum contour and projection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.