kar*_*lip 20 c++ opencv image-processing rotation
我正在尝试使用OpenCV 的C++ API将1296x968图像旋转90度,我遇到了一些问题.
输入:

旋转:

如您所见,旋转的图像存在一些问题.首先,它具有与原始尺寸相同的尺寸,即使我专门Mat使用原始尺寸的反转尺寸创建目的地.结果,目标图像被裁剪.
我怀疑这种情况正在发生,因为我正在调用warpAffine()并传递原始Mat大小而不是目标大小Mat.但我这样做是因为我遵循了这个答案,但现在我怀疑答案可能是错的.所以这是我的第一个怀疑/问题.
第二,是,warpAffine()是在一定的书面形式向目的地偏移(可能在旋转的数据复制到图像中)和该操作留下了可怕的大黑边的图像周围.
我该如何解决这些问题?
我正在分享以下源代码:
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
void rotate(Mat& image, double angle)
{
Point2f src_center(image.cols/2.0F, image.rows/2.0F);
Mat rot_matrix = getRotationMatrix2D(src_center, angle, 1.0);
Mat rotated_img(Size(image.size().height, image.size().width), image.type());
warpAffine(image, rotated_img, rot_matrix, image.size());
imwrite("rotated.jpg", rotated_img);
}
int main(int argc, char* argv[])
{
Mat orig_image = imread(argv[1], 1);
if (orig_image.empty())
{
cout << "!!! Couldn't load " << argv[1] << endl;
return -1;
}
rotate(orig_image, 90);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
kar*_*lip 27
我找到了一个不涉及的解决方案warpAffine().
但在此之前,我需要说明(对于将来的参考)我的怀疑是正确的,你需要在调用时传递目标的大小warpAffine():
warpAffine(image, rotated_img, rot_matrix, rotated_img.size());
Run Code Online (Sandbox Code Playgroud)
据我所知,这个函数绘制的黑色边框(由偏移写入引起)似乎是它的标准行为.我已经注意到了C接口以及使用2.3.1a和2.3.0版本在Mac和Linux上运行的OpenCV的C++接口.
我最终使用的解决方案比所有这些扭曲的东西简单得多.您可以使用和将图像旋转90度.这里是:cv::transpose()cv::flip()
Mat src = imread(argv[1], 1);
cv::Mat dst;
cv::transpose(src, dst);
cv::flip(dst, dst, 1);
imwrite("rotated90.jpg", dst);
Run Code Online (Sandbox Code Playgroud)
---- I> 
use*_*710 11
由于偏移等原因,许多人在旋转图像或图像块方面遇到了问题.因此,我发布了一个解决方案,允许您旋转图像的区域(或整体)并将其粘贴到另一个图像或具有该功能计算一切都适合的图像.
// ROTATE p by R
/**
* Rotate p according to rotation matrix (from getRotationMatrix2D()) R
* @param R Rotation matrix from getRotationMatrix2D()
* @param p Point2f to rotate
* @return Returns rotated coordinates in a Point2f
*/
Point2f rotPoint(const Mat &R, const Point2f &p)
{
Point2f rp;
rp.x = (float)(R.at<double>(0,0)*p.x + R.at<double>(0,1)*p.y + R.at<double>(0,2));
rp.y = (float)(R.at<double>(1,0)*p.x + R.at<double>(1,1)*p.y + R.at<double>(1,2));
return rp;
}
//COMPUTE THE SIZE NEEDED TO LOSSLESSLY STORE A ROTATED IMAGE
/**
* Return the size needed to contain bounding box bb when rotated by R
* @param R Rotation matrix from getRotationMatrix2D()
* @param bb bounding box rectangle to be rotated by R
* @return Size of image(width,height) that will compleley contain bb when rotated by R
*/
Size rotatedImageBB(const Mat &R, const Rect &bb)
{
//Rotate the rectangle coordinates
vector<Point2f> rp;
rp.push_back(rotPoint(R,Point2f(bb.x,bb.y)));
rp.push_back(rotPoint(R,Point2f(bb.x + bb.width,bb.y)));
rp.push_back(rotPoint(R,Point2f(bb.x + bb.width,bb.y+bb.height)));
rp.push_back(rotPoint(R,Point2f(bb.x,bb.y+bb.height)));
//Find float bounding box r
float x = rp[0].x;
float y = rp[0].y;
float left = x, right = x, up = y, down = y;
for(int i = 1; i<4; ++i)
{
x = rp[i].x;
y = rp[i].y;
if(left > x) left = x;
if(right < x) right = x;
if(up > y) up = y;
if(down < y) down = y;
}
int w = (int)(right - left + 0.5);
int h = (int)(down - up + 0.5);
return Size(w,h);
}
/**
* Rotate region "fromroi" in image "fromI" a total of "angle" degrees and put it in "toI" if toI exists.
* If toI doesn't exist, create it such that it will hold the entire rotated region. Return toI, rotated imge
* This will put the rotated fromroi piece of fromI into the toI image
*
* @param fromI Input image to be rotated
* @param toI Output image if provided, (else if &toI = 0, it will create a Mat fill it with the rotated image roi, and return it).
* @param fromroi roi region in fromI to be rotated.
* @param angle Angle in degrees to rotate
* @return Rotated image (you can ignore if you passed in toI
*/
Mat rotateImage(const Mat &fromI, Mat *toI, const Rect &fromroi, double angle)
{
//CHECK STUFF
// you should protect against bad parameters here ... omitted ...
//MAKE OR GET THE "toI" MATRIX
Point2f cx((float)fromroi.x + (float)fromroi.width/2.0,fromroi.y +
(float)fromroi.height/2.0);
Mat R = getRotationMatrix2D(cx,angle,1);
Mat rotI;
if(toI)
rotI = *toI;
else
{
Size rs = rotatedImageBB(R, fromroi);
rotI.create(rs,fromI.type());
}
//ADJUST FOR SHIFTS
double wdiff = (double)((cx.x - rotI.cols/2.0));
double hdiff = (double)((cx.y - rotI.rows/2.0));
R.at<double>(0,2) -= wdiff; //Adjust the rotation point to the middle of the dst image
R.at<double>(1,2) -= hdiff;
//ROTATE
warpAffine(fromI, rotI, R, rotI.size(), INTER_CUBIC, BORDER_CONSTANT, Scalar::all(0));
//& OUT
return(rotI);
}
Run Code Online (Sandbox Code Playgroud)
也许这可以帮助某人。
变量是
img :原始图像
角度:度数
比例
dst :目标图像
int width = img.size().width,
height = img.size().height;
Mat rot = getRotationMatrix2D(Point2f(0,0), angle, scale)/scale; //scale later
double sinv = rot.at<double>(0,1),
cosv = rot.at<double>(0,0);
rot.at<double>(1,2) = width*sinv; //adjust row offset
Size dstSize(width*cosv + height*sinv, width*sinv + height*cosv);
Mat dst;
warpAffine(img, dst, rot, dstSize);
resize(dst, dst, Size(), scale, scale); //scale now
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37279 次 |
| 最近记录: |