// mytesr1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <cvaux.h>
#include <iostream>
#include <string>
#include <strstream>
#include <iomanip>
#include <cmath>
using namespace std;
void imRotate(IplImage *src,IplImage *dst,double angle , CvPoint2D32f center,double scale = 1 );
int _tmain(int argc, _TCHAR* argv[])
{
time_t StartTime = clock();
IplImage *src = cvLoadImage("d:\\1.bmp",CV_LOAD_IMAGE_GRAYSCALE);
cvShowImage("src",src);
CvMemStorage *storage = cvCreateMemStorage();
CvSeq *contour = NULL;
CvRNG rng = cvRNG(-1);
while(1){
double scalar = cvRandReal(&rng)*0.8;
if(scalar < 0.3 )
continue;
double angle = cvRandReal(&rng)*360;
IplImage *dst = cvCreateImage(cvSize(src->width,src->height),8,1);
imRotate(src,dst,angle,cvPoint2D32f(src->width/2,src->height/2),scalar);
cvShowImage("dst",dst);
cvThreshold(dst,dst,100,255,CV_THRESH_BINARY);
cvFindContours(dst,storage,&contour);
CvMoments moments;
CvHuMoments huMoments;
cvMoments(dst,&moments,1);
cvGetHuMoments(&moments, &huMoments);
cout<<"the huMoments is: "<<endl;
for(int i = 0;i<7;++i){
cout<<i<<":"<<((double*)&huMoments)[i]<<endl;
}
cout<<endl;
cvReleaseImage(&dst);
cvClearSeq(contour);
if(cvWaitKey() == 27)
break;
}
time_t EndTime = clock();
cout<<double(EndTime - StartTime)/CLOCKS_PER_SEC<<endl;
}
void imRotate(IplImage *src, IplImage *dst,double angle , CvPoint2D32f center,double scale)
{
assert(src->width == dst->width && src->height == dst->height &&src->depth == dst->depth &&src->nChannels == dst->nChannels);
CvMat *mapMatrix = cvCreateMat(2,3,CV_32FC1);
cv2DRotationMatrix(center,angle,scale,mapMatrix); //旋转缩放为仿射变换,此处求变换矩阵
cvWarpAffine(src,dst,mapMatrix);
}
opencv 对hu不变矩的测试程序cvGetHuMoments
最新推荐文章于 2022-01-26 00:28:08 发布