【问题标题】:OpenCV : algorithm for simple image rotation and reductionOpenCV:简单的图像旋转和缩小算法
【发布时间】:2017-03-09 14:10:42
【问题描述】:

我已尝试使用 getRotationMatrix2D(center, angle, scale); 进行图像旋转和缩小 (JPEG) 和warpAffine(image1, image3, rotation, image3.size()); 我得到了我想要的结果(如下图)

for (int r = 0;r < image1.rows;r++) {

    for (int c = r + 1;c < image1.cols;c++) {
        Point center  = Point(image1.cols / 2, image1.rows / 2);
        Point center1 = Point(image1.cols / 2, image1.rows / 2);
        double angle = 90.0;
        double scale = 1;
        double angle1 = 90.0;
        double scale1 = 0.5;
        rotation = getRotationMatrix2D(center, angle, scale);
        rotation1 = getRotationMatrix2D(center1, angle1, scale1);

但我想在不使用任何库的情况下学习一些简单的旋转和缩减算法(对于像我这样的初学者来说很简单) 得到相同的结果。 在寻找各种解决方案之后,我最终得到了这个 来自https://gamedev.stackexchange.com/questions/67613/how-can-i-rotate-a-bitmap-without-d3d-or-opengl 谁能一点一点地分解简单的线性代数来向我解释我的伪代码?

编辑: 减码

      void reduction(Mat image1)
   {
        for (int r = 0;r < imgC.rows;r++)
        {
        for (int c = 0;c < imgC.cols;c++)
    {


        int new_x = c * (125 / 256);
        int new_y = r * (125 / 256);
        imgC.at<uchar>(r, c) = imgC.at<uchar>(new_y, new_x);
        }
     }
 }

【问题讨论】:

    标签: c++ image algorithm opencv rotation


    【解决方案1】:

    在此示例中,我加载了两个图像。一个是灰度图像,另一个是彩色图像。两者都是相同的图像,因此您可以轻松理解如何使用数学方程处理旋转。请看这个很容易理解的例子。同样以类似的方式,您可以添加缩放和缩小。这里每个点都根据新位置上设置的方程式和颜色值进行转换。代码如下:

    #include <iostream>
    #include <string>
    #include "opencv/highgui.h"
    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/objdetect/objdetect.hpp"
    
    using namespace std;
    using namespace cv;
    
    #define PIPI 3.14156
    
    int main()
    {
        Mat img = imread("C:/Users/dell2/Desktop/DSC00587.JPG",0);//loading gray scale image
        Mat imgC = imread("C:/Users/dell2/Desktop/DSC00587.JPG",1);//loading color image
    
        Mat rotC(imgC.cols, imgC.rows, imgC.type());
        rotC = Scalar(0,0,0);
    
        Mat rotG(img.cols, img.rows, img.type());
        rotG = Scalar(0,0,0);
    
        float angle = 90.0 * PIPI / 180.0;
    
        for(int r=0;r<imgC.rows;r++)
        {
            for(int c=0;c<imgC.cols;c++)
            {
                float new_px = c * cos(angle) - r * sin(angle);
                float new_py = c * sin(angle) + r * cos(angle);
    
                Point pt((int)-new_px, (int)new_py);
    
                //color image
                rotC.at<Vec3b>(pt) = imgC.at<Vec3b>(r,c);//assign color value at new location from original image
    
                //gray scale image
                rotG.at<uchar>(pt) = img.at<uchar>(r,c);//assign color value at new location from original image
    
            }
        }
    
    
        imshow("color",rotC);
        imshow("gray",rotG);
        waitKey(0);
    
        return 0;
    }
    

    【讨论】:

    • 感谢您的帮助!虽然这段代码只能支持 90 度吗?正如我尝试输入各种其他数量一样,例如(180 度,270 度),但它只会给我错误。 -new_px 的“-”符号是什么?
    • -ve 符号是由于 x 值为负,正常坐标系可以。但是在图像处理中,您知道顶部,左侧是(0,0),底部,右侧是(w,h)......对于自定义角度,它会很复杂,因为您需要计算角度和图像尺寸.如果旋转 45 度并且想要在相同的画布尺寸下适合旋转的图像,那么您必须缩小图像。同样对于 90,270 度的图像尺寸将交换。通常图像尺寸保持不变并缩小旋转图像以适合画布下
    • 哦,谢谢你的回答,我刚刚在我的还原代码中添加了(编辑后的帖子),它不起作用,我犯了什么错误?
    • 没有分配像素值。刚刚计算了点。 U 应该在输出垫的新位置分配像素值
    • 你为什么使用 Mat rotC(img.cols, img.rows, img.type());而不是使用 Mat rotC;你能解释一下吗?
    猜你喜欢
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    相关资源
    最近更新 更多