OpenCV:简单的图像旋转和缩小算法 [英] OpenCV : algorithm for simple image rotation and reduction

查看:157
本文介绍了OpenCV:简单的图像旋转和缩小算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have tried image rotation and reduction(JPEG) with getRotationMatrix2D(center, angle, scale); and warpAffine(image1, image3, rotation, image3.size()); I got the result I wanted(image as below)

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/如何在不使用d3d或opengl的情况下旋转位图 任何人都可以将简单的线性代数一点一点地分解,以对我的伪代码向我解释吗?

but I want to learn some simple rotation and reduction algorithm ( simple for beginner like me ) without using any library to get the same result. After searching for various solutions, i ended up with this from https://gamedev.stackexchange.com/questions/67613/how-can-i-rotate-a-bitmap-without-d3d-or-opengl Can anyone break up bit by bit of the simple linear algebra to explain to me in regards to my pesudo code?

还原码

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

推荐答案

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

In this example I have loaded two images. One is in gray scale and other is color image. Both are same image so you can understand easily how to handle rotation with mathematical equation. Please see this example which is very easy to understand. Also in the similar manner you can add scaling and reduction. Here each point is converted according to the equation and color value set on new location. Here is the code:

#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;
}

这篇关于OpenCV:简单的图像旋转和缩小算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆