1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| #include <opencv/cv.h> #include <opencv/highgui.h> #include <math.h> #include <iostream> #include <fstream> using namespace std;
#pragma comment(lib, "opencv_core220.lib") #pragma comment(lib, "opencv_highgui220.lib") #pragma comment(lib, "opencv_imgproc220.lib")
struct Point { double fX; double fY; };
Point pts[4]; double fR;
int main() { ofstream outf("out.txt"); streambuf *default_buf=cout.rdbuf(); cout.rdbuf( outf.rdbuf() );
char* szZero = "仪表盘0.bmp"; char* szOne = "仪表盘1.bmp"; char* szRecog = "仪表盘.bmp"; IplImage* imgZero = cvLoadImage(szZero, 1); if (!imgZero) return -1; IplImage* imgOne = cvLoadImage(szOne, 1); if (!imgOne) return -1; IplImage* imgRecog = cvLoadImage(szRecog, 1); if (!imgRecog) return -1;
IplImage *imgEdge = NULL; imgEdge = cvCreateImage(cvGetSize(imgRecog),IPL_DEPTH_8U,1); IplImage *imgCanny = cvCreateImage(cvGetSize(imgRecog),IPL_DEPTH_8U,1);
CvMemStorage *storage = cvCreateMemStorage(0);
cvCvtColor(imgZero, imgEdge,CV_BGR2GRAY); cvCanny(imgEdge, imgCanny, 50, 100); cvClearMemStorage(storage); CvPoint* line; CvSeq* lines; lines = cvHoughLines2(imgCanny, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 10 ); line = (CvPoint*)cvGetSeqElem(lines, 0); cvLine(imgZero, line[0], line[1], CV_RGB(255,0,0), 3, CV_AA, 0 ); cout<<"端点1:"<< line[0].x << "," << line[0].y<<endl; cout<<"端点2:"<< line[1].y << "," << line[1].y<<endl; pts[1].fX = line[0].x; pts[1].fY = line[0].y;
cvCvtColor(imgOne, imgEdge,CV_BGR2GRAY); cvCanny(imgEdge, imgCanny, 50, 100); cvClearMemStorage(storage); lines = cvHoughLines2(imgCanny, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 10 ); line = (CvPoint*)cvGetSeqElem(lines, 0); cvLine(imgOne, line[0], line[1], CV_RGB(255,0,0), 3, CV_AA, 0 ); cout<<"端点1:"<< line[0].x << "," << line[0].y<<endl; cout<<"端点2:"<< line[1].y << "," << line[1].y<<endl; pts[2].fX = line[0].x; pts[2].fY = line[0].y;
cvCvtColor(imgRecog, imgEdge,CV_BGR2GRAY); cvCanny(imgEdge, imgCanny, 50, 100); cvClearMemStorage(storage); CvSeq * cir=NULL; cir = cvHoughCircles(imgCanny, storage, CV_HOUGH_GRADIENT, 1, imgRecog->width/10 ,80,40, 50); float * p=(float *)cvGetSeqElem(cir, 0); CvPoint pt = cvPoint(cvRound(p[0]),cvRound(p[1])); cvCircle(imgRecog,pt,cvRound(p[2]),CV_RGB(0,255,0)); cvCircle(imgRecog, pt, 5, CV_RGB(0,255,0), -1, 8, 0 ); cout<<"圆心:"<<pt.x<<","<<pt.y<<endl; cout<<"半径:"<<p[2]<<endl; pts[0].fX = pt.x; pts[0].fY = pt.y; fR = p[2];
lines = cvHoughLines2(imgCanny, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 10 ); line = (CvPoint*)cvGetSeqElem(lines, 0); cvLine(imgRecog, line[0], line[1], CV_RGB(255,0,0), 3, CV_AA, 0 ); cout<<"端点1:"<< line[0].x << "," << line[0].y<<endl; cout<<"端点2:"<< line[1].y << "," << line[1].y<<endl; pts[3].fX = line[0].x; pts[3].fY = line[0].y;
static const double PI = atan(1.0) * 4; double fKZero = (pts[1].fY - pts[0].fY) / (pts[1].fX - pts[0].fX); double fThetaZero = 180.0 / PI * atan(fKZero); if (pts[1].fX + 10 < pts[0].fX) { fThetaZero += 180.0; } fThetaZero += 360.0; if (fThetaZero > 360.0) { fThetaZero -= 360.0; }
double fKOne = (pts[2].fY - pts[0].fY) / (pts[2].fX - pts[0].fX); double fThetaOne = 180.0 / PI * atan(fKOne); if (pts[2].fX + 10 < pts[0].fX) { fThetaOne += 180.0; } fThetaOne += 360.0; if (fThetaOne > 360.0) { fThetaOne -= 360.0; }
double fKRecog = (pts[3].fY - pts[0].fY) / (pts[3].fX - pts[0].fX); double fThetaRecog = 180.0 / PI * atan(fKRecog); if (pts[3].fX + 10 < pts[0].fX) { fThetaRecog += 180.0; } fThetaRecog += 360.0; if (fThetaRecog > 360.0) { fThetaRecog -= 360.0; }
char szAns[100]; sprintf(szAns, "0刻度的角度为:%f,1刻度的角度为:%f,识别刻度的角度为:%f,识别结果为:%.2f", fThetaZero, fThetaOne, fThetaRecog, (fThetaRecog - fThetaZero) / (fThetaOne - fThetaZero)); cout << szAns << endl;
cvNamedWindow("检测", CV_WINDOW_AUTOSIZE); cvShowImage("检测", imgRecog); cvNamedWindow("刻度0", CV_WINDOW_AUTOSIZE); cvShowImage("刻度0", imgZero); cvNamedWindow("刻度1", CV_WINDOW_AUTOSIZE); cvShowImage("刻度1", imgOne); cvWaitKey(0);
cvDestroyWindow("检测"); cvDestroyWindow("刻度0"); cvDestroyWindow("刻度1"); cvReleaseImage(&imgRecog); cvReleaseImage(&imgZero); cvReleaseImage(&imgOne); cvReleaseImage(&imgEdge); cvReleaseImage(&imgCanny); cvReleaseMemStorage(&storage); }
|