在现实应用中我们的角点往往并不是整数,所以为了提高我们寻找角点的精确程度,我们需要进行处理,一般有三种方法
1.插值方法
2.基于图像矩计算
3.图线拟合(高斯曲面,多项式,椭圆曲面)其中高斯曲面最为常用
具体的使用过程就是我们先用Shi-Tomasi角点检测 把角点找出来,然后再进行精确
cornerSubPix(InputArray image,
InputOutputArray corners,
Size winSize, Size zeroZone,
TermCriteria criteria)
这里介绍一下TermCriteria用于迭代算法的终止条件,该类变量需要3个参数,一个是类型,第二个参数为迭代的最大次数,最后一个是特定的阈值。类型有CV_TERMCRIT_ITER、CV_TERMCRIT_EPS、CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,分别代表着迭代终止条件为达到最大迭代次数终止,迭代到阈值终止,或者两者都作为迭代终止条件。以上的宏对应的c++的版本分别为TermCriteria::COUNT、TermCriteria::EPS,这里的COUNT也可以写成MAX_ITER。
(https://blog.csdn.net/u010606097/article/details/20905153)
#include
#include
#define Rc Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255))
using namespace std;
using namespace cv;
const char* output_tile = "SubPixcel Result";
Mat src, gray_src;
RNG rng(12345);
int max_corners = 15;
int max_count = 500;
void subPixel_Demo(int, void*);
int main(int argc, char** argv) {
src = imread("C:/Users/pbiha/Desktop/image/b1.jpg");
if (src.empty()) {
puts("Can't find the img...");
return -1;
}
cvtColor(src, gray_src, COLOR_BGR2GRAY);
namedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input", src);
namedWindow(output_tile, CV_WINDOW_AUTOSIZE);
createTrackbar("Corners:", output_tile, &max_corners, max_count, subPixel_Demo);
subPixel_Demo(0, 0);
waitKey(0);
return 0;
}
void subPixel_Demo(int, void*) {
if (max_corners < 5)max_corners = 5;
vectorcorners;
double minDistance = 0;
double qualityLevel=0.001;
Mat resultImg = src.clone();
goodFeaturesToTrack(gray_src, corners, max_corners, qualityLevel, minDistance,Mat(),3,false,0.4);
for (size_t t=0; t < corners.size(); t++) {
circle(resultImg, corners[t], 2, Rc, 2);
}
imshow(output_tile, resultImg);
TermCriteria tc = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 40, 0.001);
cornerSubPix(gray_src, corners, Size(5, 5), Size(-1, -1), tc);
for (size_t t = 0; t < corners.size(); t++) {
printf("%f %f\n", corners[t].x, corners[t].y);
}
}