2012-12-15 74 views
3

我想逆时针旋转90度,但看起来旋转点是错误的。我如何找到源图像的旋转中心?openCV图像旋转中心C

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

回答

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。这些值根据输出图像大小调整结果。

P.S: 这不是一个通用的解决方案。它仅适用于90度的旋转。

+0

非常感谢,非常感谢! – user1477955