gpt4 book ai didi

c - 如何在 Opencv 和 C 中找到图像轮廓的角坐标

转载 作者:太空宇宙 更新时间:2023-11-03 21:14:23 25 4
gpt4 key购买 nike

我必须找到图像的角点,以便我可以将其裁剪成矩形。我已经找到了轮廓并在其上使用了 approxpoly() 函数。现在如何找到轮廓的角坐标?这是我的 C 代码->

#include <cv.h>
#include <highgui.h>

int main(int argc, char** argv)
{
IplImage *img,*gray;

if((img = cvLoadImage("save.jpg", 1)) == NULL)
{
printf("A Img open error\n");
}

gray=cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 1 );
cvCvtColor(img,gray,CV_BGR2GRAY);

IplImage* out_median = cvCreateImage(cvGetSize(gray),IPL_DEPTH_8U,1);
cvSmooth( gray,out_median,CV_MEDIAN,3);

IplImage* out_threshold = cvCreateImage( cvGetSize(out_median), out_median->depth, 1);
cvThreshold(out_median,out_threshold,1,255,CV_THRESH_BINARY);

CvMemStorage* storage = cvCreateMemStorage();
CvSeq* first_contour = NULL;

cvFindContours(out_threshold,storage,&first_contour,sizeof(CvContour),CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);

CvSeq* approx_polygon = NULL;
approx_polygon=cvApproxPoly(first_contour,sizeof(CvContour),storage,CV_POLY_APPROX_DP,0.01*cvArcLength(first_contour,CV_WHOLE_SEQ, 1),0);

//cvDrawContours(out_threshold,approx_polygon,cvScalarAll(255),cvScalarAll(255),100);
//cvShowImage("Contours", out_threshold );
//cvSaveImage("save_approxpoly_contour.jpg",out_threshold);


cvWaitKey(0);
return 0;

这是我应用 Approxpoly() 后的轮廓图像 enter image description here

最佳答案

我推荐使用 cvBlob :

获取 Blob :

cvThreshold(&src, &dst, 10, 255, CV_THRESH_BINARY);
IplImage *labelImg = cvCreateImage(cvGetSize(&dst),IPL_DEPTH_LABEL,1);
CvBlobs blobs;
unsigned int result = cvLabel(&dst, labelImg, blobs);

如果你有多个项目,找出限制

int borders[4];
borders[0] = 99999; //left
borders[1] = 99999; //top
borders[2] = 0; //right
borders[3] = 0; //bottom

for (CvBlobs::const_iterator it=blobs.begin(); it!=blobs.end(); ++it)
{
if((*it).second->maxx) < borders[0]){
borders[0] = (*it).second->maxx;
}
if(((*it).second->miny) < borders[1]){
borders[1] = (*it).second->miny;
}
if((*it).second->minx) > borders[2]){
borders[2] = (*it).second->minx;
}
if(((*it).second->maxy) > borders[3]){
borders[3] = (*it).second->maxy;
}
}

关于c - 如何在 Opencv 和 C 中找到图像轮廓的角坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10821335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com