在界面上使用OpenCV和C ++旋转图像

时间:2017-12-14 10:39:34

标签: c++ opencv

我正在开发在接口项目中使用C ++中的OpenCV旋转图像。所以,我对这段代码有一些问题。有没有办法解决这个代码......?

            IplImage* source_image = cvLoadImage(ip, 1);

            IplImage *rotate_image = cvCreateImage(cvGetSize(source_image), IPL_DEPTH_8U, 1);
            cvNamedWindow("rotate_image", CV_WINDOW_FREERATIO);

             int angle = 180;

             cvCreateTrackbar("Angle", rotate_image,&angle,360);

             int image_height = source_image.rows / 2;
             int image_width = source_image.cols / 2;


             IplImage *rotatetion = cvCreateImage(cvGetSize(source_image), IPL_DEPTH_8U, 1);
             rotatetion = cv2DRotationMatrix(Point(image_height,image_width),(angle - 180), 1);

             IplImage *rotated_image = cvCreateImage(cvGetSize(rotatetion), IPL_DEPTH_8U, 1);
             cvWarpAffine(dialateImage,Rotated_Image,Rotatetion,dialateImage.size());

             cvShowImage("rotateImage", rotated_image);

2 个答案:

答案 0 :(得分:1)

来自https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html

Mat rot_mat( 2, 3, CV_32FC1 );
Mat warp_mat( 2, 3, CV_32FC1 );
Mat src, warp_dst, warp_rotate_dst;

/// Load the image
src = imread( argv[1], 1 );

/// Set the dst image the same type and size as src
warp_dst = Mat::zeros( src.rows, src.cols, src.type() );

/// Compute a rotation matrix with respect to the center of the image
Point center = Point( warp_dst.cols/2, warp_dst.rows/2 );
double angle = -50.0;
double scale = 0.6;

/// Get the rotation matrix with the specifications above
rot_mat = getRotationMatrix2D( center, angle, scale );

/// Rotate the warped image
warpAffine( warp_dst, warp_rotate_dst, rot_mat, warp_dst.size() );

不要像@Miki那样使用过时的C api并提出一个明确的问题,而不是“这段代码没有用”。

答案 1 :(得分:0)

试试这个。工作。

                         IplImage* source_image;                     
                         IplImage *dest = cvCloneImage(source_image);
                         CvPoint2D32f center;
                         center.x = dest->width / 2;
                         center.y = dest->height / 2;
                         CvMat *mapMatrix = cvCreateMat(2, 3, CV_32FC1);


                         double angle = System::Convert::ToDouble(numericUpDown1->Value);
                         cv2DRotationMatrix(center, angle, 1.0, mapMatrix);

                         cvWarpAffine(source_image, dest, mapMatrix, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll(0));
                         cvReleaseMat(&mapMatrix);
                         cvShowImage("Rotated", dest);