opencv 对hu不变矩的测试程序cvGetHuMoments

// 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);  
}  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值