【问题标题】:openCV center of image rotation in CC中图像旋转的openCV中心
【发布时间】:2012-12-03 06:10:31
【问题描述】:

我想逆时针旋转90度,但是旋转点好像不对。如何找到源图像的旋转中心?

    img=cvLoadImage(argv[1],-1); 
height    = img->height;
width     = img->width;
step      = img->widthStep;
channels  = img->nChannels;
data      = (uchar *)img->imageData;

    IplImage *rotatedImg = cvCreateImage(cvSize(height,width), IPL_DEPTH_8U,img->nChannels);

    CvPoint2D32f center;
    center.x = width/2;
    center.y = height/2;
    CvMat *mapMatrix = cvCreateMat( 2, 3, CV_32FC1 );

    cv2DRotationMatrix(center, 90, 1.0, mapMatrix);
    cvWarpAffine(img, rotatedImg, mapMatrix, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll(0));


cvShowImage("My image", rotatedImg );

【问题讨论】:

    标签: c image opencv rotation


    【解决方案1】:

    你必须设置仿射变换矩阵的平移参数。

    要将图像旋转 90 度并使输出适合目标图像,您可以执行以下操作:

    IplImage *rotatedImg = cvCreateImage(cvSize(height,width), IPL_DEPTH_8U,img->nChannels);
    
    CvPoint2D32f center;
    center.x = width/2.0f;
    center.y = height/2.0f;
    CvMat *mapMatrix = cvCreateMat( 2, 3, CV_32FC1 );
    
    float x = width - 1.0f;
    float y = 0.0f;
    
    cv2DRotationMatrix(center, 90, 1, mapMatrix);
    cvmSet(mapMatrix,0,2,y);
    cvmSet(mapMatrix,1,2,x);
    cvWarpAffine(img, rotatedImg, mapMatrix, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll(0));
    

    注意偏移值 x 和 y。这些值会根据输出图像大小调整结果。

    附注: 这不是一个通用的解决方案。仅适用于旋转 90 度。

    【讨论】:

    • 非常感谢,帮了大忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    相关资源
    最近更新 更多