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 157 158 159 160 161 162 163 164 165 166 167 168 169
| #include <opencv2/opencv.hpp> #include <ctime> using namespace std; using namespace cv; double M[7] = {0}; vector<Mat> cutImage(Mat image) { vector<Mat> roi; int rwidth = image.cols/3; int rheight = image.rows/3; for (int y = 0;y < image.rows-10;y +=rheight) { for (int x = 0;x < image.cols-10;x += rwidth) { Rect rect(x, y, rwidth, rheight); rect &= Rect(0, 0, image.cols, image.rows);; roi.push_back(image(rect)); } } return roi; } void calcHu(Mat image) { int bmpWidth = image.cols; int bmpHeight = image.rows; int bmpStep = image.step; int bmpChannels = image.channels(); uchar* pBmpBuf = image.data; double m00=0,m11=0,m20=0,m02=0,m30=0,m03=0,m12=0,m21=0; double x0=0,y0=0; double u20=0,u02=0,u11=0,u30=0,u03=0,u12=0,u21=0; double t1=0,t2=0,t3=0,t4=0,t5=0; int Center_x=0,Center_y=0; int i,j; double s10=0,s01=0,s00=0; for(j=0;j<bmpHeight;j++) { for(i=0;i<bmpWidth;i++) { s10+=i*pBmpBuf[j*bmpStep+i]; s01+=j*pBmpBuf[j*bmpStep+i]; s00+=pBmpBuf[j*bmpStep+i]; } } Center_x=(int)(s10/s00+0.5); Center_y=(int)(s01/s00+0.5); m00=s00; for(j=0;j<bmpHeight;j++) { for(i=0;i<bmpWidth;i++) { x0=(i-Center_x); y0=(j-Center_y); m11+=x0*y0*pBmpBuf[j*bmpStep+i]; m20+=x0*x0*pBmpBuf[j*bmpStep+i]; m02+=y0*y0*pBmpBuf[j*bmpStep+i]; m03+=y0*y0*y0*pBmpBuf[j*bmpStep+i]; m30+=x0*x0*x0*pBmpBuf[j*bmpStep+i]; m12+=x0*y0*y0*pBmpBuf[j*bmpStep+i]; m21+=x0*x0*y0*pBmpBuf[j*bmpStep+i]; } } u20=m20/pow(m00,2); u02=m02/pow(m00,2); u11=m11/pow(m00,2); u30=m30/pow(m00,2.5); u03=m03/pow(m00,2.5); u12=m12/pow(m00,2.5); u21=m21/pow(m00,2.5); t1=(u20-u02); t2=(u30-3*u12); t3=(3*u21-u03); t4=(u30+u12); t5=(u21+u03); M[0]=u20+u02; M[1]=t1*t1+4*u11*u11; M[2]=t2*t2+t3*t3; M[3]=t4*t4+t5*t5; M[4]=t2*t4*(t4*t4-3*t5*t5)+t3*t5*(3*t4*t4-t5*t5); M[5]=t1*(t4*t4-t5*t5)+4*u11*t4*t5; M[6]=t3*t4*(t4*t4-3*t5*t5)-t2*t5*(3*t4*t4-t5*t5); } int compareHu(double mo[9][7]) { int no = 0; double sum = 0, min = 100, max = 0; for (int i = 0;i<9;i++) { sum += mo[i][0]; if(mo[i][0]>max) max = mo[i][0]; if(mo[i][0]<min) min = mo[i][0]; } sum /= 9; if(sum - min > max - sum) max = min; for (int i = 0;i<9;i++) if (mo[i][0]==max){ no = i;break;} return no; } void drawCross(int n, Mat image) { int centerx = 0, centery = 0, widstep = 0, heistep = 0; widstep = image.cols/6; heistep = image.rows/6; centerx = n%3 * widstep * 2 + widstep; centery = n/3 * heistep * 2 + heistep; Scalar color(0, 0, 255); line(image, Point(centerx-20, centery), Point(centerx+20, centery), color, 2); line(image, Point(centerx, centery-10), Point(centerx, centery+10), color, 2); } int main() { Mat src, binary; vector <Mat> srcRoi; int count = 12; double moment[9][7] = {0}; while (--count) { char imageName[10]; sprintf_s(imageName, "%d.jpg", count); src = imread(imageName, 1); Canny(src, binary, 50, 100); srcRoi = cutImage(binary); for (int i = 0;i < 9;i++) { calcHu(srcRoi[i]); for (int j = 0;j<7;j++) moment[i][j] = M[j]; } int no = compareHu(moment); drawCross(no, src); imshow(imageName, src); waitKey(0); } return 0; }
|