C++ OpenCV:一种简单的图像旋转和压缩算法

C++ OpenCV:一种简单的图像旋转和压缩算法,c++,image,algorithm,opencv,rotation,C++,Image,Algorithm,Opencv,Rotation,我用getRotationMatrix2D(中心、角度、比例)尝试了图像旋转和缩小(JPEG) 和warphafine(image1,image3,rotation,image3.size()) 我得到了我想要的结果(如下图所示) for(int r=0;r

我用
getRotationMatrix2D(中心、角度、比例)尝试了图像旋转和缩小(JPEG)
warphafine(image1,image3,rotation,image3.size())
我得到了我想要的结果(如下图所示)

for(int r=0;r
但是我想学习一些简单的旋转和归约算法(对于像我这样的初学者来说很简单),而不需要使用任何库 得到同样的结果。 在寻找各种解决方案之后,我最终得到了这个 从…起 有人能一点一点地分解简单的线性代数来解释我的伪码吗

编辑: 归约码

      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);
        }
     }
 }
void reduction(Mat image1)
{
for(int r=0;r

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

#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/highgui.h”
#包括“opencv2/imgproc/imgproc.hpp”
#包括“opencv2/objdetect/objdetect.hpp”
使用名称空间std;
使用名称空间cv;
#定义PIPI3.14156
int main()
{
Mat img=imread(“C:/Users/dell2/Desktop/DSC00587.JPG”,0);//加载灰度图像
Mat imgC=imread(“C:/Users/dell2/Desktop/DSC00587.JPG”,1);//加载彩色图像
Mat rotC(imgC.cols、imgC.rows、imgC.type());
rotC=标量(0,0,0);
Mat rotG(img.cols、img.rows、img.type());
rotG=标量(0,0,0);
浮动角度=90.0*PIPI/180.0;

对于(int r=0;r在这个例子中,我加载了两个图像。一个是灰度图像,另一个是彩色图像。这两个图像都是相同的,所以你可以很容易地理解如何用数学方程处理旋转。请看这个很容易理解的例子。同样,你也可以用类似的方式添加缩放和缩小。这里每个点都是转换的d根据新位置上设置的方程式和颜色值。以下是代码:

#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/highgui.h”
#包括“opencv2/imgproc/imgproc.hpp”
#包括“opencv2/objdetect/objdetect.hpp”
使用名称空间std;
使用名称空间cv;
#定义PIPI3.14156
int main()
{
Mat img=imread(“C:/Users/dell2/Desktop/DSC00587.JPG”,0);//加载灰度图像
Mat imgC=imread(“C:/Users/dell2/Desktop/DSC00587.JPG”,1);//加载彩色图像
Mat rotC(imgC.cols、imgC.rows、imgC.type());
rotC=标量(0,0,0);
Mat rotG(img.cols、img.rows、img.type());
rotG=标量(0,0,0);
浮动角度=90.0*PIPI/180.0;

for(int r=0;r感谢您的帮助!虽然此代码只能支持90度吗?正如我尝试输入各种其他金额,例如(180度,270度),但它只会给我错误。什么是“-”符号-new_px?-ve符号是由于x值变为负值,对于正常坐标系是可以的。但在图像处理中,你知道顶部,左点是(0,0),底部,右点是(w,h)…对于自定义角度,这将很复杂,因为您需要计算角度以及图像尺寸。如果旋转45度并希望在相同的画布大小下适合旋转的图像,则必须缩小图像的比例。对于90270度,图像尺寸也将交换。通常图像尺寸保持不变,并将旋转的图像缩小到适合在Canvash下,谢谢你的回答,我刚刚添加了我的简化代码(编辑的帖子),但它不起作用,我犯了什么错误?没有分配像素值。只是计算了点。你应该在输出Mat的新位置分配像素值。你为什么使用Mat rotC(img.cols,img.rows,img.type());而不是使用Mat rotC;请您向我解释一下好吗?谢谢您的帮助!虽然此代码只能支持90度吗?因为我尝试输入各种其他数值,例如(180度,270度),但它只会给我错误。什么是“-”符号-new_px?-ve符号是由于x值变为负值,对于正常坐标系是可以的。但在图像处理中,你知道顶部,左点是(0,0),底部,右点是(w,h)…对于自定义角度,这将很复杂,因为您需要计算角度以及图像尺寸。如果旋转45度并希望在相同的画布大小下适合旋转的图像,则必须缩小图像的比例。对于90270度,图像尺寸也将交换。通常图像尺寸保持不变,并将旋转的图像缩小到适合在Canvash下,谢谢你的回答,我刚刚添加了我的简化代码(编辑的帖子),但它不起作用,我犯了什么错误?没有分配像素值。只是计算了点。你应该在输出Mat的新位置分配像素值。你为什么使用Mat rotC(img.cols,img.rows,img.type());而不是使用Mat rotC;请您向我解释一下好吗?