c语言图像轮廓,如何在OpenCV,C中提取图像边界的轮廓

我尝试在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(y + x*src.rows, z) = src.at(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(y + x*src.rows,0);

new_image.at(y,x)[0] = centers.at(cluster_idx, 0);

new_image.at(y,x)[1] = centers.at(cluster_idx, 1);

new_image.at(y,x)[2] = centers.at(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 > contours;

vector 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 pnts;

findNonZero(detected_edges, pnts);

fout<

cout << "Total number of points: " << pnts.size() << endl;

for(size_t i = 0; i < pnts.size(); ++i){

cout << pnts[i] << endl;

fout<

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 >::iterator it = contours.begin(); it!=contours.end(); )

{

if (it->size()

it=contours.erase(it);

else

++it;

}

vector > 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 (b, a).val[0];

g1 = src.at (b, a).val[1];

r1 = src.at (b, a).val[2];

// Vec3b pixel = src.at (i, j);

printf("%d,%d,%d\t",b1,g1,r1);

int b2=b1;

int g2=g1;

int r2=r1;

fout << b2<

}

//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<

fout<

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

{

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 );

如何获得边界的轮廓以及如何获得关闭对象的轮廓

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值