#include<iostream>#include<opencv2/opencv.hpp>#include<vector>usingnamespace std;usingnamespace cv;intmain(){
Mat img =imread("手.png");if(img.empty()){
cout <<"输入为空";return-1;}//二值化
Mat gray, binary;cvtColor(img, gray, COLOR_BGR2GRAY);threshold(gray, binary,105,200, THRESH_BINARY);//开运算消除细小区域
Mat k =getStructuringElement(MORPH_RECT,Size(3,3),Point(-1,-1));morphologyEx(binary, binary, MORPH_OPEN, k);//发现轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;findContours(binary, contours, hierarchy,0,2,Point());//计算hu矩//;遍历发现的轮廓for(int n =0; n < contours.size(); n++){//计算图像矩
Moments M;
M =moments(contours[n],true);//计算hu矩
Mat hu;//HuMoments(图像矩,输出的hu矩)//hu矩由七个向量组成,具有旋转不变性HuMoments(M, hu);
cout << hu << endl;}return0;}
‘’‘’‘’‘’‘’‘’‘’‘’‘’‘
输出结果
[0.1668624965843623;6.968771756270732e-05;2.244941673115611e-06;1.356927248733923e-08;-2.367181753737709e-15;-1.077726087210472e-10;-7.28870474031461e-17]