1
我只寫了一個函數來獲取最接近點的物體輪廓。我認爲並不難,但我不知道爲什麼我不能在這個功能中使用sqrt
。我得到的結果是很奇怪的,而不是假設的結果。OpenCV獲取最近的輪廓點
void FindContour(Mat a,Mat &image){
Mat temp;
int x=0, y=0;
int i;
int largest=65535;
a.copyTo(temp);
vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_NONE);
for (int index = 0; index >= 0; index = hierarchy[index][0]) {
Moments moment = moments(contours[index]);
double area = moment.m00;
int a = moment.m10/area;
int b = moment.m01/area;
i=std::sqrt((a-50)^2+(b-60)^2);
if (i<largest)
{ x=a;
y=b;
largest=i;
cout<<"x"<<x<<"y"<<y<<"i"<<i<<"/n";
}
}
Point2i pt(x,y);
circle(image,pt,20,Scalar(0,255,0),2);
putText(image,"object",Point(x,y+30),1,1,Scalar(0,255,0),2);
}