图像处理之图像距

来源:互联网 发布:伊斯兰教知乎 编辑:程序博客网 时间:2024/06/05 06:00

本实验仅使用轮廓距来寻找轮廓质心。

#include "cv.h"#include "highgui.h"using namespace cv;using namespace std;Mat srcGray;int lThreshold=100;void barChange(int pos, void* userdata);int main(int argc,char *argv[]){Mat src;src=imread("src.jpg");cvtColor(src,srcGray,CV_BGR2GRAY);blur(srcGray,srcGray,Size(3,3));namedWindow("srcGray");imshow("srcGray",srcGray);createTrackbar("lThreshold:","srcGray",&lThreshold,255,barChange);barChange(0,0);waitKey(0);destroyAllWindows();return 0;}void barChange(int pos, void* userdata){Mat srcCanny,dstContours;vector<vector<Point>> contours;vector<Vec4i> hierarchy;Canny(srcGray,srcCanny,lThreshold,lThreshold*2);findContours(srcCanny,contours,hierarchy,CV_RETR_TREE,CHAIN_APPROX_SIMPLE);cout<<"size:"<<contours.size()<<endl;vector<Moments> mu(contours.size());for (int i=0;i<contours.size();i++){mu[i]=moments(contours[i],false);}vector<Point2f> mc(contours.size());//储存轮廓质心位置for (int i=0;i<contours.size();i++){mc[i]=Point2f(mu[i].m10/mu[i].m00,mu[i].m01/mu[i].m00);}dstContours=Mat::zeros(srcGray.size(),CV_8UC3);for (int i=0;i<contours.size();i++){printf("contor[%d]-Area=%.2f-Area(Opencv)=%.2f\n",i,mu[i].m00,contourArea(contours[i]));drawContours(dstContours,contours,i,Scalar(0,255,255),2,8,vector<Vec4i>(),0);circle(dstContours,mc[i],4,Scalar(0,0,255),-1);}imshow("dstContours",dstContours);}
效果:

部分数据

0 0