图像旋转 OpenCV 错误
当我使用此代码旋转图像时,目标图像大小保持不变,因此图像被剪裁。请为我提供一种方法/代码片段来相应地调整大小(就像Matlab在imrotate中所做的那样),以便图像不会被剪裁并且离群像素会被全白色而不是黑色填充。我不希望缩小图像以适应原始尺寸。我只想要旋转,不需要缩放。
void imrotate(std::string imgPath,std::string angleStr,std::string outPath) {
size_t found1,found2;
found1=imgPath.find_last_of('/');
found2=imgPath.size()-4;
IplImage* src=cvLoadImage(imgPath.c_str(), -1);;
IplImage* dst;
dst = cvCloneImage( src );
int angle = atoi(angleStr.c_str());
CvMat* rot_mat = cvCreateMat(2,3,CV_32FC1);
CvPoint2D32f center = cvPoint2D32f(
src->width/2,
src->height/2
);
double scale = 1;
cv2DRotationMatrix( center, angle, scale, rot_mat );
cvWarpAffine( src, dst, rot_mat);
char angStr[4];
sprintf(angStr,"%d",angle);
cvSaveImage(string(outPath+imgPath.substr(found1+1,found2-found1-1)+"_"+angStr+".jpg").c_str(),dst);
cvReleaseImage(&src);
cvReleaseImage(&dst);
cvReleaseMat( &rot_mat );
}
原始图像:
替代文本 http://freeimagehosting.in/images/185_ElectricalG.jpg
旋转图像:
替代文本http://freeimagehosting.in/images/638_ElectricalG_60.jpg
When I use this code to rotate the image, the destination image size remains same and hence the image gets clipped. Please provide me a way/code snippet to resize accordingly (like Matlab does in imrotate) so that image does not get clipped and outlier pixels gets filled with all white instead of black. I don't want image to be scaled down to fit in original size. I just want rotation, no scaling.
void imrotate(std::string imgPath,std::string angleStr,std::string outPath) {
size_t found1,found2;
found1=imgPath.find_last_of('/');
found2=imgPath.size()-4;
IplImage* src=cvLoadImage(imgPath.c_str(), -1);;
IplImage* dst;
dst = cvCloneImage( src );
int angle = atoi(angleStr.c_str());
CvMat* rot_mat = cvCreateMat(2,3,CV_32FC1);
CvPoint2D32f center = cvPoint2D32f(
src->width/2,
src->height/2
);
double scale = 1;
cv2DRotationMatrix( center, angle, scale, rot_mat );
cvWarpAffine( src, dst, rot_mat);
char angStr[4];
sprintf(angStr,"%d",angle);
cvSaveImage(string(outPath+imgPath.substr(found1+1,found2-found1-1)+"_"+angStr+".jpg").c_str(),dst);
cvReleaseImage(&src);
cvReleaseImage(&dst);
cvReleaseMat( &rot_mat );
}
Original Image:
alt text http://freeimagehosting.in/images/185_ElectricalG.jpg
Rotated Image:
alt text http://freeimagehosting.in/images/638_ElectricalG_60.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不必将源图像克隆为目标,而是必须创建一个足够大的图像来拍摄最终的旋转图像,该图像将是一个边长为源宽度或高度最大值的 1.5 倍的正方形。
编辑:
您需要将目的地放大的量是 1 + sin(旋转角度),其最大值在 45 度处为 1.414...这必须向上舍入到合适的值
Instead of cloning the source image as the dest you are going to have to create an image big enough to take the final rotated image, which will be a square with sides 1.5 times the biggest of the source width or height.
Edit:
The amount you need to enlarge the destination by is 1 + sin(angle of rotation), which has a maximum value at 45 degrees of 1.414... This must be rounded up to a suitable value
在旋转之前,您需要通过填充来使源图像变大。
出于显而易见的原因,此处图像放大了 1.5,而不是 1.414。
You need to make the source image bigger by padding before doing the rotation.
Here the image is 1.5 bigger instead of 1.414 for obvious reasons.
这将满足您的要求甚至更多。 OpenCV 2.4 及以上版本:
This will do what you ask and more. OpenCV 2.4 and above: