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
| #include <iostream> #include <opencv2/opencv.hpp>
using namespace std; using namespace cv;
void getHuMoments(Mat &src1,Mat &src2){ Mat gray1, binary1, dst1; Mat gray2, binary2, dst2; cvtColor(src1, gray1, COLOR_BGR2GRAY); cvtColor(src2, gray2, COLOR_BGR2GRAY);
imshow("src1", src1); imshow("src2", src2);
threshold(gray1, binary1, 127, 255, THRESH_BINARY | THRESH_OTSU); imshow("binary1", binary1); threshold(gray2, binary2, 127, 255, THRESH_BINARY | THRESH_OTSU); imshow("binary2", binary2);
Moments ms1,ms2; ms1 = moments(binary1, false); ms2 = moments(binary2, false); double hu1[7],hu2[7]; HuMoments(ms1, hu1); HuMoments(ms2, hu2);
cout << endl << endl; cout << " img1:" << endl; for (size_t i = 0; i < 7; i++) { hu1[i] = -1 * copysign(1.0, hu1[i]) * log10(abs(hu1[i])); cout << " hu1[" << i+1 << "]=" << hu1[i] << endl; } cout << endl;
cout << " img2:" << endl; for (size_t i = 0; i < 7; i++) { hu2[i] = -1 * copysign(1.0, hu2[i]) * log10(abs(hu2[i])); cout << " hu2[" << i+1 << "]=" << hu2[i] << endl; }
double d1 = matchShapes(binary1, binary2, CONTOURS_MATCH_I1, 0); double d2 = matchShapes(binary1, binary2, CONTOURS_MATCH_I2, 0); double d3 = matchShapes(binary1, binary2, CONTOURS_MATCH_I3, 0);
cout << " hu矩匹配值:" << endl; cout << " CONTOURS_MATCH_T1 :" << d1 << endl; cout << " CONTOURS_MATCH_T2 :" << d2 << endl; cout << " CONTOURS_MATCH_T3 :" << d3 << endl; }
int main() { Mat src1 = imread("1.jpg"); Mat src2 = imread("1.png");
if (src1.empty()||src2.empty()) { cout << " input the image error!" << endl; } getHuMoments(src1, src2);
waitKey(0); return 0; }
|