【问题标题】:Rotate an image using OpenCV with C++ on interface在界面上使用带有 C++ 的 OpenCV 旋转图像
【发布时间】:2017-12-14 10:39:33
【问题描述】:

我正在接口项目上使用 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);

【问题讨论】:

  • 不要使用过时的 C api!!!
  • 我知道它很旧,但我想解决这个模型。

标签: c++ opencv


【解决方案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,并提出一个明确的问题,而不是“这个代码不起作用”。

【讨论】:

  • 我可以旋转图像,问题是我想将 Mat 和 IplImage 结合起来。
【解决方案2】:

试试this.worked。

                         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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 2019-08-03
    • 2020-03-17
    相关资源
    最近更新 更多