问题描述
- 我调用opencv中的函数计算出的hu矩没有旋转、缩放不变性,请大家帮我看下程序哪出问题了。
-
#include
#include
#includeusing namespace std;
using namespace cv;int main(int argc, char *argv[])
{//读入图片预处理
Mat image=imread("F:vs2010 project21.jpg");
//image.create(480, 640, CV_8UC1);
namedWindow("show", CV_WINDOW_AUTOSIZE);
imshow("show",image);
cvtColor(image,image,CV_BGR2GRAY);
Mat img2;
threshold(image,img2,100,255,CV_THRESH_BINARY);
//vector> contours;
// find
//findContours(img2,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
// draw
// Mat result(image.size(),CV_8U,Scalar(0));
//drawContours(result,contours,-1,Scalar(255),2);
//namedWindow("show2", CV_WINDOW_AUTOSIZE);
//imshow("show2",result);//旋转缩放
cv::Point2f center = cv::Point2f(image.cols / 2, image.rows / 2); // 旋转中心double angle = 0; // 旋转角度
double scale = 1.0; // 缩放尺度
cv::Mat rotateMat; rotateMat = cv::getRotationMatrix2D(center, angle, scale); cv::Mat rotateImg; cv::warpAffine(img2, rotateImg, rotateMat, image.size()); cv::namedWindow("show2", CV_WINDOW_AUTOSIZE); cv::imshow("show2",rotateImg);
//求HU矩
IplImage img3 = rotateImg; cvShowImage("src",&img3); CvMoments moments; CvHuMoments hu; cvMoments(&img3,&moments,2); cvGetHuMoments(&moments, &hu); //cout<<hu.hu1<<"/"<<hu.hu2<<"/"<<hu.hu3<<"/"<<hu.hu4<<"/"<<hu.hu5<<"/"<<hu.hu6<<"/"<<hu.hu7<<"/"<<"/"<<endl; ofstream outfile; outfile.open("F:\vs2010 project2\tiquHU2\hu.txt",ios::app); //存放数据的文件名 if(outfile.is_open()) { outfile<<hu.hu1<<"/"<<hu.hu2<<"/"<<hu.hu3<<"/"<<hu.hu4<<"/"<<hu.hu5<<"/"<<hu.hu6<<"/"<<hu.hu7<<"/"<<"/"<<endl; //写入数据 outfile.close(); } else { cout<<"不能打开文件!"<<endl; } cvWaitKey();
}