opencv 旋转图片
来源:互联网 发布:mac os sierra安装教程 编辑:程序博客网 时间:2024/05/22 13:35
旋转图像
有几种方法旋转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)
Returns IplImage* points to the cloned image
Arguements -
- IplImage* src - source image to be cloned
- cvCreateMat(int rows, int columns, int types)
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>
<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)
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)
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, CvArr* dst, const CvMat* mapMatrix)
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;}
- OpenCV旋转图片
- opencv 旋转图片
- opencv 旋转图片
- opencv图片旋转剪裁
- opencv 图片旋转
- OpenCv图片旋转
- OpenCV 图片旋转,缩放
- 旋转图片之opencv
- opencv 图片旋转90度
- OpenCV下的图片旋转
- java 结合opencv图片旋转
- 【OpenCV】通过旋转图片增加训练集
- opencv实现图片的裁剪以及旋转
- Python Opencv旋转图片90度
- python+opencv图片旋转矩形分割
- openCV-图片的旋转加放缩函数
- opencv图片旋转后图片无法填满窗口有黑边解决方法
- 【图像处理】基于OpenCV底层实现的图片旋转
- hibernate id Generator的类型
- unity3D与网页的交互
- websocket协议10版本
- AS3 Vector
- iphone——日期处理
- opencv 旋转图片
- sql & oracle 查询表的字段名
- 如何正确利用超链接调用 JavaScript 函数
- Android异步处理三:Handler+Looper+MessageQueue深入详解 .
- 基于距离传感器的自动锁屏程序
- glib
- oracle regexp_like介绍和例子
- 手把手教你编译MOSES机器翻译系统
- 探秘widget之launcher添加widget的流程分析