角点检测是用于检测图像中的兴趣点的技术。兴趣点基本上是可以在图像中唯一检测到的东西。兴趣点有助于我们描绘图像,广泛用于目标跟踪、图像分类和视觉搜索等应用。
角点是两条边的交点,是兴趣点的特定情况,有一种流行的角点检测技术称为Harris角点检测器,该技术主要基于灰度图像的偏导数构造2*2矩阵,然后分析特征值。
以图像中的一小块区域为例,目标是确定该区域是否有一个角点,即考虑所有相邻区域并计算区域和所有相邻区域之间的强度差异,如果在所有方向上的差异都很大,那么该区域内有一个角点。这是对实际算法的过度简化。
角是沿两个方向具有强烈强度差异的点。
Harris和Stephens的原始论文http://www.bmva.org/bmvc/1988/avc-88-023.pdf。
int main(int argc, char* argv[])
{
//Read the input value for the size of the block
int blockSize;
istringstream iss(argv[1]);
iss >> blockSize;
//Check if 'blockSize' is smaller than 2
if (blockSize < 2)
blockSize = 2;
//Detector parameters
int apertureSize = 5;
double k = 0.04;
int thresh = 200;
RNG rng(12345);
string windowName = "Harris Corner Detector";
//Current frame
Mat frame, frameGray;
char ch;
//Create the capture object
//0->input arg that specifies it should take the input from the webcam
VideoCapture cap(0);
if (!cap.isOpened()) {
cerr << "Unable to open the webcam. Exiting!" << endl;
return -1;
}
//Scaling factor to resize the input frames from the webcam
float scalingFactor = 0.75;
Mat dst, dst_norm, dst_norm_scaled;
//Iterate until the user presses the Esc key
while (true) {
//Capture the current frame
cap >> frame;
//Resize the frame
resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER_AREA);
dst = Mat::zeros(frame.size(), CV_32FC1);
//Convert to grayscale
cvtColor(frame, frameGray, COLOR_BGR2GRAY);
//Detecting corners
cornerHarris(frameGray, dst, blockSize, apertureSize, k, BORDER_DEFAULT);
//Normalizing
normalize(dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
convertScaleAbs(dst_norm, dst_norm_scaled);
//Drawing a circle around corners
for (int j = 0; j < dst_norm.rows; j++) {
for (int i = 0; i < dst_norm.cols; i++) {
if ((int)dst_norm.at<float>(j, i) > thresh)
circle(frame, Point(i, j), 8, Scalar(0, 255, 0), 2, 8, 0);
}
}
imshow(windowName, frame);
ch = waitKey(30);
if (ch == 27)
break;
}
cap.release();
destroyAllWindows();
return 0;
}
代码有点问题,成功失败一半一半,出现全屏绿色情况,待修改。