边缘提取后对所得边缘进行拟合。

qq_38089780 2017-07-25 08:41:10


如图为我所得到的边缘图像,提取边缘后获得了一些比较离散的点还有线,但是大致轮廓是有的。我想请教一下如何将该轮廓拟合成一条闭合的曲线? 请问有什么大体的方向么?

另外我已经将边缘坐标提取出来,根据坐标能拟合么?

因为是刚接触,很多方面都不太懂。如果有懂得大神请指点个方向,我自己查询相关资料就行。谢谢啦
...全文
1165 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-07-26
  • 打赏
  • 举报
回复
仅供参考:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
using namespace std;
using namespace cv;
Mat img,smallImg,gray,bw;
vector<Vec4i> hierarchy;
vector<vector<Point> > contours;
int threshval=128;
Rect r;
Rect maxrect,brect;
int idx,n;
const static Scalar colors[15]={
    CV_RGB(  0,  0,128),
    CV_RGB(  0,128,  0),
    CV_RGB(  0,128,128),
    CV_RGB(128,  0,  0),
    CV_RGB(128,  0,128),
    CV_RGB(128,128,  0),
    CV_RGB(128,128,128),
    CV_RGB(160,160,160),
    CV_RGB(  0,  0,255),
    CV_RGB(  0,255,  0),
    CV_RGB(  0,255,255),
    CV_RGB(255,  0,  0),
    CV_RGB(255,  0,255),
    CV_RGB(255,255,  0),
    CV_RGB(255,255,255),
};
Scalar color;
void gamma_correct(Mat& img, Mat& dst, double gamma) {
	Mat	temp;
	CvMat tmp;

	img.convertTo(temp,	CV_32FC1, 1.0/255.0, 0.0);
	tmp=temp;
	cvPow(&tmp,	&tmp, gamma);
	temp.convertTo(dst , CV_8UC1 , 255.0	, 0.0);
}
int main() {
    cvNamedWindow("display",1);
    img=imread("image.jpg",1);
    r.x=img.cols/10;
    r.y=img.rows/3;
    r.width=img.cols*8/10;
    r.height=img.rows*2/3;
    smallImg=img(r);
    cvtColor(smallImg,gray,CV_BGR2GRAY);
//  medianBlur(gray,gray,5);
    equalizeHist(gray,gray);
    gamma_correct(gray,gray,4.0);
    imshow("display",gray);
    waitKey(0);

	bw=(gray>threshval);
    imshow("display",bw);
    waitKey(0);

	Mat	Structure0=getStructuringElement(MORPH_RECT,Size(3,3));
    erode(bw,bw,Structure0,Point(-1,-1));
	Mat	Structure1=getStructuringElement(MORPH_RECT,Size(6,6));
    dilate(bw,bw,Structure1, Point(-1,-1));
    imshow("display",bw);
    waitKey(0);

    findContours(bw,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
    if (!contours.empty()&&!hierarchy.empty()) {
        idx=0;
        n=0;
        vector<Point> approx;
        for (;idx>=0;idx=hierarchy[idx][0]) {
            color=colors[idx%15];
//          drawContours(smallImg,contours,idx,color,1,8,hierarchy);
            approxPolyDP(Mat(contours[idx]), approx, arcLength(Mat(contours[idx]), true)*0.005, true);//0.005为将毛边拉直的系数
            const Point* p = &approx[0];
            int m=(int)approx.size();
            polylines(smallImg, &p, &m, 1, true, color);
            circle(smallImg,Point(p[0].x,p[0].y),2,color);
            circle(smallImg,Point(p[1].x,p[1].y),1,color);

            n++;
            if (1==n) {
                maxrect=boundingRect(Mat(contours[idx]));
            } else {
                brect=boundingRect(Mat(contours[idx]));
                CvRect mr(maxrect),br(brect);
                maxrect=cvMaxRect(&mr,&br);
            }
        }
        circle(smallImg,Point(maxrect.x+maxrect.width/2,maxrect.y+maxrect.height/2),2,CV_RGB(255,0,0));
    }
    imshow("display",smallImg);
    waitKey(0);
    cvDestroyWindow("display");
    return 0;
}
赵4老师 2017-07-26
  • 打赏
  • 举报
回复
Dilate 使用任意结构元素膨胀图像 void cvDilate( const CvArr* src, CvArr* dst, IplConvKernel* element=NULL, int iterations=1 ); src 输入图像. dst 输出图像. element 用于膨胀的结构元素。若为 NULL, 则使用 3×3 长方形的结构元素 iterations 膨胀的次数 函数 cvDilate 对输入图像使用指定的结构元进行膨胀,该结构决定每个具有最小值象素点的邻域形状: dst=dilate(src,element): dst(x,y)=max((x',y') in element))src(x+x',y+y') 函数支持(in-place)模式。膨胀可以重复进行 (iterations) 次. 对彩色图像,每个彩色通道单独处理。
赵4老师 2017-07-26
  • 打赏
  • 举报
回复
ApproxPoly 用指定精度逼近多边形曲线 CvSeq* cvApproxPoly( const void* src_seq, int header_size, CvMemStorage* storage, int method, double parameter, int parameter2=0 ); src_seq 点集数组序列 header_size 逼近曲线的头尺寸 storage 逼近轮廓的容器。如果为 NULL, 则使用输入的序列 method 逼近方法。目前仅支持 CV_POLY_APPROX_DP , 对应 Douglas-Peucker 算法. parameter 方法相关参数。对 CV_POLY_APPROX_DP 它是指定的逼近精度 parameter2 如果 src_seq 是序列,它表示要么逼近单个序列,要么在 src_seq 的同一个或低级层次上逼近所有序列 (参考 cvFindContours 中对轮廓继承结构的描述). 如果 src_seq 是点集的数组 (CvMat*) , 参数指定曲线是闭合 (parameter2!=0) 还是非闭合 (parameter2=0). 函数 cvApproxPoly 逼近一个或多个曲线,并返回逼近结果。对多个曲线的逼近,生成的树将与输入的具有同样的结构。(1:1 的对应关系).
qq_38089780 2017-07-26
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
仅供参考:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
using namespace std;
using namespace cv;
Mat img,smallImg,gray,bw;
vector<Vec4i> hierarchy;
vector<vector<Point> > contours;
int threshval=128;
Rect r;
Rect maxrect,brect;
int idx,n;
const static Scalar colors[15]={
    CV_RGB(  0,  0,128),
    CV_RGB(  0,128,  0),
    CV_RGB(  0,128,128),
    CV_RGB(128,  0,  0),
    CV_RGB(128,  0,128),
    CV_RGB(128,128,  0),
    CV_RGB(128,128,128),
    CV_RGB(160,160,160),
    CV_RGB(  0,  0,255),
    CV_RGB(  0,255,  0),
    CV_RGB(  0,255,255),
    CV_RGB(255,  0,  0),
    CV_RGB(255,  0,255),
    CV_RGB(255,255,  0),
    CV_RGB(255,255,255),
};
Scalar color;
void gamma_correct(Mat& img, Mat& dst, double gamma) {
	Mat	temp;
	CvMat tmp;

	img.convertTo(temp,	CV_32FC1, 1.0/255.0, 0.0);
	tmp=temp;
	cvPow(&tmp,	&tmp, gamma);
	temp.convertTo(dst , CV_8UC1 , 255.0	, 0.0);
}
int main() {
    cvNamedWindow("display",1);
    img=imread("image.jpg",1);
    r.x=img.cols/10;
    r.y=img.rows/3;
    r.width=img.cols*8/10;
    r.height=img.rows*2/3;
    smallImg=img(r);
    cvtColor(smallImg,gray,CV_BGR2GRAY);
//  medianBlur(gray,gray,5);
    equalizeHist(gray,gray);
    gamma_correct(gray,gray,4.0);
    imshow("display",gray);
    waitKey(0);

	bw=(gray>threshval);
    imshow("display",bw);
    waitKey(0);

	Mat	Structure0=getStructuringElement(MORPH_RECT,Size(3,3));
    erode(bw,bw,Structure0,Point(-1,-1));
	Mat	Structure1=getStructuringElement(MORPH_RECT,Size(6,6));
    dilate(bw,bw,Structure1, Point(-1,-1));
    imshow("display",bw);
    waitKey(0);

    findContours(bw,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
    if (!contours.empty()&&!hierarchy.empty()) {
        idx=0;
        n=0;
        vector<Point> approx;
        for (;idx>=0;idx=hierarchy[idx][0]) {
            color=colors[idx%15];
//          drawContours(smallImg,contours,idx,color,1,8,hierarchy);
            approxPolyDP(Mat(contours[idx]), approx, arcLength(Mat(contours[idx]), true)*0.005, true);//0.005为将毛边拉直的系数
            const Point* p = &approx[0];
            int m=(int)approx.size();
            polylines(smallImg, &p, &m, 1, true, color);
            circle(smallImg,Point(p[0].x,p[0].y),2,color);
            circle(smallImg,Point(p[1].x,p[1].y),1,color);

            n++;
            if (1==n) {
                maxrect=boundingRect(Mat(contours[idx]));
            } else {
                brect=boundingRect(Mat(contours[idx]));
                CvRect mr(maxrect),br(brect);
                maxrect=cvMaxRect(&mr,&br);
            }
        }
        circle(smallImg,Point(maxrect.x+maxrect.width/2,maxrect.y+maxrect.height/2),2,CV_RGB(255,0,0));
    }
    imshow("display",smallImg);
    waitKey(0);
    cvDestroyWindow("display");
    return 0;
}
赵老师,这个主要是应用了哪方面的知识呢?因为小白看不太懂。另外我运行了这个程序发现图像前后并没有什么变化是什么原因呢? 谢谢赵老师啦。

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧