opencv 旋转图片
2013-08-20 11:45
218 查看
旋转图像
有几种方法旋转OpenCV的图像。旋转图像中的主要问题是它会留下一些空白图像的四个角。有几种方法来应对的空白区域
下面是一个例子,旋转图像,同时保留它的大小。
Here are the new OpenCV functions, found in the above example.
cvCloneImage(const IplImage* src)
Makes a identical(cloned) copy of the 'src' image including header and region of interrest(ROI)
Returns IplImage* points to the cloned image
Arguements -
IplImage* src - source image to be cloned
cvCreateMat(int rows, int columns, int types)
Creates matrix header and allocates matrix data
Returns CvMat* points
to the matrix
Arguements -
int rows - number of rows of the matrix
int columns - number of columns of the matrix
int types - CV_<no. of bits of the image data><data type of image data>C<no. of channel>
<no. of bits of the image data> = 8,16,32, ....
<data type of image data> = 'S' for signed data, 'U' for unsigned data and 'F' for float data
<no. of channel> = 1,2,3
eg. : CV_8UC1 = 8 bit unsigned image with a single channel
CV_32FC3 = 32 bit float image with 3 channels
cvPoint2D32f (float x, float y)
It is a basic structure for 2D point with floating point cordinates
typedef struct CvPoint2D32f
{
float x;
float y;
}
CvPoint2D32f;
/* Constructor */
inline CvPoint2D32f cvPoint2D32f( double x, double y );
cv2DRotationMatrix(CvPoint2D32f center, double angle, double scale, CvMat* mapMatrix)
Computes affine matrix of 2D rotation
Arguements -
CvPoint2D32f center - specifies the ceter of rotation
double angle - rotation angle in degrees (Positive value means counter clockwise rotation and negative value means clockwise rotation)
double scale -Isotrophic scale factor
CvMat* mapMatrix - the pointer to the resultant matrix which should be 2x3 matrix
cvWarpAffine(const CvArr* src, [b]CvArr* dst, const
CvMat* mapMatrix)[/b]
Applies an affine transformation to the source image
Arguements -
const CvArr* src - source image
CvArr* dst - destination image which is the affine transformed image of the source image
const CvMat* mapMatrix - 2x3 transformation matrix
旋转填充式:
有几种方法旋转OpenCV的图像。旋转图像中的主要问题是它会留下一些空白图像的四个角。有几种方法来应对的空白区域
下面是一个例子,旋转图像,同时保留它的大小。
#include <opencv/cv.h> #include <opencv/highgui.h> IplImage *rotateImage(const IplImage *src, int angleDegrees) { IplImage *imageRotated = cvCloneImage(src); if(angleDegrees!=0){ CvMat* rot_mat = cvCreateMat(2,3,CV_32FC1); // Compute rotation matrix CvPoint2D32f center = cvPoint2D32f( cvGetSize(imageRotated).width/2, cvGetSize(imageRotated).height/2 ); cv2DRotationMatrix( center, angleDegrees, 1, rot_mat ); // Do the transformation cvWarpAffine( src, imageRotated, rot_mat ); } return imageRotated; } int main() { IplImage* img; IplImage* rotated_img; int angle=0; //creating the window with a track bar cvNamedWindow("MyWindow"); cvCreateTrackbar("Angle", "MyWindow", &angle, 360, 0); while(true){ //load the original image img = cvLoadImage("d:/1.jpg"); //rotate the image rotated_img=rotateImage(img,angle); //display the rotated image cvShowImage("MyWindow", rotated_img); //clean up cvReleaseImage(&img); cvReleaseImage(&rotated_img); //if user press 'ESC' button, program quit the while loop int c=cvWaitKey(50); if(c==27) break; } cvDestroyWindow("MyWindow"); return 0; }
Explanation
Here are the new OpenCV functions, found in the above example.cvCloneImage(const IplImage* src)
Makes a identical(cloned) copy of the 'src' image including header and region of interrest(ROI)
Returns IplImage* points to the cloned image
Arguements -
IplImage* src - source image to be cloned
cvCreateMat(int rows, int columns, int types)
Creates matrix header and allocates matrix data
Returns CvMat* points
to the matrix
Arguements -
int rows - number of rows of the matrix
int columns - number of columns of the matrix
int types - CV_<no. of bits of the image data><data type of image data>C<no. of channel>
<no. of bits of the image data> = 8,16,32, ....
<data type of image data> = 'S' for signed data, 'U' for unsigned data and 'F' for float data
<no. of channel> = 1,2,3
eg. : CV_8UC1 = 8 bit unsigned image with a single channel
CV_32FC3 = 32 bit float image with 3 channels
cvPoint2D32f (float x, float y)
It is a basic structure for 2D point with floating point cordinates
typedef struct CvPoint2D32f
{
float x;
float y;
}
CvPoint2D32f;
/* Constructor */
inline CvPoint2D32f cvPoint2D32f( double x, double y );
cv2DRotationMatrix(CvPoint2D32f center, double angle, double scale, CvMat* mapMatrix)
Computes affine matrix of 2D rotation
Arguements -
CvPoint2D32f center - specifies the ceter of rotation
double angle - rotation angle in degrees (Positive value means counter clockwise rotation and negative value means clockwise rotation)
double scale -Isotrophic scale factor
CvMat* mapMatrix - the pointer to the resultant matrix which should be 2x3 matrix
cvWarpAffine(const CvArr* src, [b]CvArr* dst, const
CvMat* mapMatrix)[/b]
Applies an affine transformation to the source image
Arguements -
const CvArr* src - source image
CvArr* dst - destination image which is the affine transformed image of the source image
const CvMat* mapMatrix - 2x3 transformation matrix
#include <opencv/cv.h> #include <opencv/highgui.h> IplImage *rotateImage(const IplImage *src, int angleDegrees, double zoom) { IplImage *imageRotated = cvCloneImage(src); CvMat* rot_mat = cvCreateMat(2,3,CV_32FC1); // Compute rotation matrix CvPoint2D32f center = cvPoint2D32f( cvGetSize(imageRotated).width/2, cvGetSize(imageRotated).height/2 ); cv2DRotationMatrix( center, angleDegrees, zoom, rot_mat ); // Do the transformation cvWarpAffine( src, imageRotated, rot_mat ); return imageRotated; } int main() { IplImage* img; IplImage* rotated_img; int angle=0; int zoom=24; //creating the window with 2 track bars cvNamedWindow("MyWindow"); cvCreateTrackbar("Angle", "MyWindow", &angle, 360, 0); cvCreateTrackbar("Zoom", "MyWindow", &zoom, 99, 0); while(true){ //load the original image img = cvLoadImage("d:/lena.jpg"); //rotate the image rotated_img=rotateImage( img, angle, (zoom+1)/25.0 ); //display the rotated image cvShowImage("MyWindow", rotated_img); //clean up cvReleaseImage(&img); cvReleaseImage(&rotated_img); //if user press 'ESC' button, program quit the while loop int c=cvWaitKey(50); if(c==27) break; } cvDestroyWindow("MyWindow"); return 0; }
旋转填充式:
#include <opencv/cv.h> #include <opencv/highgui.h> IplImage* rotateImage(const IplImage* src, int angleDegrees) { //take the dimention of original image int w = src->width; int h = src->height; // Make a new image for the result CvSize newSize; newSize.width = cvRound(w); newSize.height = cvRound(h); IplImage *imageRotated = cvCreateImage( newSize, src->depth, src->nChannels ); // Create a map_matrix, where the left 2x2 matrix is the transform and the right 2x1 is the dimensions. float m[6]; CvMat M = cvMat(2, 3, CV_32F, m); float angleRadians = angleDegrees * ((float)CV_PI / 180.0f); m[0] = (float)( cos(angleRadians) ); m[1] = (float)( sin(angleRadians) ); m[3] = -m[1]; m[4] = m[0]; m[2] = w*0.5f; m[5] = h*0.5f; // Transform the image cvGetQuadrangleSubPix( src, imageRotated, &M); return imageRotated; } int main() { IplImage* img; IplImage* rotated_img; int angle=0; //creating the window with a track bar cvNamedWindow("MyWindow"); cvCreateTrackbar("Angle", "MyWindow", &angle, 360, 0); while(true){ //load the original image img = cvLoadImage("d:/lena.jpg"); //rotate the image rotated_img=rotateImage(img,angle); //display the rotated image cvShowImage("MyWindow", rotated_img); //clean up cvReleaseImage(&img); cvReleaseImage(&rotated_img); //if user press 'ESC' button, program quit the while loop int c=cvWaitKey(50); if(c==27) break; } cvDestroyWindow("MyWindow"); return 0; }
相关文章推荐
- Qt creator5.7 OpenCV249之图片旋转(含源码下载)
- OpenCV 图片旋转,缩放
- opencv 图片旋转
- openCV-图片的旋转加放缩函数
- Python Opencv旋转图片90度
- python opencv实现图片旋转矩形分割
- opencv 图片旋转
- 【OpenCV】通过旋转图片增加训练集
- 使用opencv提取单据轮廓并旋转后生成图片
- opencv图片旋转90度,180度,270度
- 【图像处理】基于OpenCV底层实现的图片旋转
- opencv 图片旋转90度
- python+opencv图片旋转矩形分割
- python opencv对图像进行旋转且不裁剪图片的方法
- OpenCV实现图片旋转
- OpenCV下的图片旋转
- 【OpenCV_12】旋转视频以及图片 Rotate Image & Video
- 【图像处理】基于OpenCV底层实现的图片旋转
- OpenCV Mat结构的图片 旋转顺时针90度 180度 270度 逆时针90度
- OpenCV2.4.13中warpAffine函数理解,旋转,仿射变换,缩放,保持完整图片