Opencv角点检测
#include "stdafx.h" #define max_corners 20 int main() { int cornerNum = max_corners; vector<Point2f>corner; double qualityLevel = 0.05; double minDistance = 5; double scalar = 0.5; Mat srcImg,srcImg1; Mat grayImg,grayImg1,grayImg2; Mat disImg; int i; //VideoCapture video("E:\\C_VC_code\\Text_Photo\\feini.flv"); VideoCapture video(0); if(!video.isOpened()) { return -1; } video>>srcImg; resize(srcImg,srcImg1,Size(srcImg.cols*scalar,srcImg.rows*scalar),1,1,3); cvtColor(srcImg1,grayImg,CV_BGR2GRAY, 1); Rect rect; double up,down,left,right; while(1) { video>>srcImg; resize(srcImg,srcImg1,Size(srcImg.cols*scalar,srcImg.rows*scalar),1,1,3); //disImg = srcImg(Rect(200,150,130,130)); cvtColor(srcImg1,grayImg1,CV_BGR2GRAY, 1); absdiff(grayImg1,grayImg,grayImg2); goodFeaturesToTrack(grayImg2,corner,cornerNum,qualityLevel,minDistance,Mat(),3,false,0.04); resize(srcImg,srcImg1,Size(srcImg.cols*scalar,srcImg.rows*scalar),1,1,3); cvtColor(srcImg1,grayImg,CV_BGR2GRAY, 1); up = left = 0x3f3f3f3f; down = right = -0x3f3f3f3f; for(i=0;i<corner.size();i++) { up = min(up, corner[i].y*1.0); left = min(left, corner[i].x*1.0); down = max(down, corner[i].y*1.0); right = max(right, corner[i].x*1.0); circle(srcImg1,Point(corner[i].x,corner[i].y),2,Scalar(0,255,0),2); } rect.x = left; rect.y = up; cout<< right<<" " <<down<<endl; rect.width = right-left; rect.height = up-down; rectangle(srcImg1,rect,Scalar(255,0,0),2); imshow("gray",grayImg2); imshow("x",srcImg1); if(waitKey(33)>0) break; } return 0; }