我尝试在OpenCV中使用findContours()
提取图像的轮廓我可以获得轮廓但是没有获得图像的边界轮廓我该如何才能得到它
这是我的代码段:
void main( )
{
src = imread( "F:/academic/pro4/t/twst.png" );
Mat samples(src.rows * src.cols, 3, CV_32F);
for( int y = 0; y < src.rows; y++ )
for( int x = 0; x < src.cols; x++ )
for( int z = 0; z < 3; z++)
samples.at<float>(y + x*src.rows, z) = src.at<Vec3b>(y,x)[z];
int clusterCount = 7;
Mat labels;
int attempts = 10;
Mat centers;
kmeans(samples, clusterCount, labels, TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 10000, 0.0001), attempts, KMEANS_PP_CENTERS , centers );
Mat new_image( src.size(), src.type() );
for( int y = 0; y < src.rows; y++ )
for( int x = 0; x < src.cols; x++ )
{
int cluster_idx = labels.at<int>(y + x*src.rows,0);
new_image.at<Vec3b>(y,x)[0] = centers.at<float>(cluster_idx, 0);
new_image.at<Vec3b>(y,x)[1] = centers.at<float>(cluster_idx, 1);
new_image.at<Vec3b>(y,x)[2] = centers.at<float>(cluster_idx, 2);
}
cv::erode(new_image,new_image,cv::Mat());
cv::dilate(new_image,new_image,cv::Mat());
imshow( "clustered image", new_image );
char filename[80];
sprintf(filename,"F:/academic/pro4/t/seg.png");
imwrite(filename, new_image);
cvtColor( new_image, src_grays, CV_BGR2GRAY );
createTrackbar( " Canny thresh:", "Source", &threshs, max_threshs, thresh_callbacks );
thresh_callbacks( 0, 0 );
waitKey( 0 );
}
void thresh_callbacks(int, void* )
{
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
cv::erode(src_grays,src_grays,cv::Mat());
cv::dilate(src_grays,src_grays,cv::Mat());
blur( src_grays, src_grays, Size(3,3) );
src_grays.convertTo(drawss, CV_8U);
imshow("AfterCluster", drawss);
Canny( src_grays, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
vector<Point> pnts;
findNonZero(detected_edges, pnts);
fout<<pnts.size();
cout << "Total number of points: " << pnts.size() << endl;
for(size_t i = 0; i < pnts.size(); ++i){
cout << pnts[i] << endl;
fout<<"pnts"<<pnts[i];}
dst = Scalar::all(0);
src_grays.copyTo( dst, detected_edges);
imshow( window_name, dst );
/// Find contours
Mat extended(detected_edges.size()+Size(2,2), detected_edges.type());
Mat markers = extended(Rect(1, 1, detected_edges.cols, detected_edges.rows));
findContours( detected_edges, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
for (vector<vector<Point> >::iterator it = contours.begin(); it!=contours.end(); )
{
if (it->size()<contour_length_threshold)
it=contours.erase(it);
else
++it;
}
vector<vector<Point> > contours_poly( contours.size() );
for(int i= 0; i < contours.size(); i++)
{approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
if ( hierarchy[i][3] != -1 ) {
// random colour
Scalar colour( (rand()&255), (rand()&255), (rand()&255) );
}
for(int j= 0; j < contours[i].size();j++) // run until j < contours[i].size();
{
int a= contours[i][j].x ;
int b =contours[i][j].y ;
if(j==1){
b1 = src.at<Vec3b> (b, a).val[0];
g1 = src.at<Vec3b> (b, a).val[1];
r1 = src.at<Vec3b> (b, a).val[2];
// Vec3b pixel = src.at<Vec3b> (i, j);
printf("%d,%d,%d\t",b1,g1,r1);
int b2=b1;
int g2=g1;
int r2=r1;
fout << b2<<','<<g2<<","<<r2<<'\t';
}
//printf("Point(%d, %d) %t", a, b);
std::cout << contours[i][j] << std::endl;
fout << contours[i][j];
}
//cout << " Area: " << contourArea(contours[i]) << endl;
//fout << " Area: " << contourArea(contours[i]) << endl;
fout<<endl;
fout<<endl;
printf ("%d \n", i);
printf ("%d \n", contours[i].size());
}
int a=contours.size();
Mat drawing_i = Mat::zeros( detected_edges.size(), CV_8UC3 );
for( int i = 0; i<contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing_i, contours, i, color, 2, 8, hierarchy, 0, Point() );
}
namedWindow( "Contours_i", CV_WINDOW_AUTOSIZE );
imshow( "Contours_i", drawing_i );
} 如何获得边界的轮廓以及如何获得关闭对象的轮廓