walkingmask 2015-04-29 02:47 采纳率: 0%
浏览 1579

我调用opencv中的函数计算出的hu矩没有旋转、缩放不变性,请大家帮我看下程序哪出问题了。

#include
#include
#include

using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{//读入图片预处理
Mat image=imread("F:\vs2010 project2\1.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();

}


图片如下

  • 写回答

1条回答 默认 最新

  • 久绊A 全栈领域新星创作者 2023-01-21 15:21
    关注
    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();
    
    

    有问题

    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示