在C ++中旋转图像时出现问题 [英] Problem rotating image in C++

查看:65
本文介绍了在C ++中旋转图像时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好;-)

我正在尝试使用用户输入在C ++中旋转图像

我不允许使用除导入之外的任何库图片

我真的尝试了所有东西,并在网上搜索了好几天,但要么它没有正确旋转,要么有像素错误

真的希望你可以帮助我

提前谢谢



我尝试过:



Hi There ;-)
I'm trying to rotate an image in C++ with user input
i'm not allowed to use any library other than to import the image
i've really tried everything and searched the web for days but either it's not rotating correctly or there is pixel errors
Really hope you can help me out
Thank you in advance

What I have tried:

#include <opencv2 core="" core.hpp="">
#include <opencv2 highgui="" highgui.hpp="">
#include <opencv2 imgproc="" imgproc.hpp="">
#include <iostream>
#include <math.h>

#define PI 3.14159265
using namespace std;
using namespace cv;

int main() {
    int rotateAmount = 90;
    
    Mat image = imread("/Users/Documents/Media/OpenCvProjects/mini/Final_Graphic_Square.jpg", CV_LOAD_IMAGE_COLOR);
    Mat imageRotated = imread("/Users/Documents/Media/OpenCvProjects/mini/Final_Graphic_Square.jpg", CV_LOAD_IMAGE_COLOR);
    double sinus = abs(sin(rotateAmount*PI/180));
    double cosinus = abs(cos(rotateAmount*PI/180));
    for (int y=0; y<image.rows; y++) {
        for (int x = 0; x< image.cols; x++) {
            
            int tempX = round((x*cosinus + y*sinus));
            //x*(cos(rotateAmount) - sin(rotateAmount));
            int tempY = round((y*cosinus + x*sinus));
            //y*( sin(rotateAmount) - cos(rotateAmount));
            imageRotated.at<vec3b>(y, x) = image.at<vec3b>(tempY, tempX);
        }
    }
    
    if (image.data && !image.empty()){
        namedWindow("Original Image");
        imshow("Original Image", image);
    }
    
    if (imageRotated.data && !imageRotated.empty()){
        namedWindow("Rotated Image");
        imshow("Rotated Image", imageRotated);
    }
    
 cvWaitKey(0);
}

推荐答案

您必须以另一种方式设置像素:

You must set the pixels in another way:
imageRotated.at<vec3b>(tempY, tempX) = image.at<vec3b>(y, x);

确保每个源像素都被翻译。由于四舍五入,你还必须处理从未设置的imageRotated中的像素。用背景颜色填充图片。



一个先进的解决方案是使用strang颜色(128,128,128)并在旋转后检查哪些点未着色并混合邻居的颜色。



看看我最喜欢的文章 CxImage ,它会拍很多图片操纵。至少你可以得到一些旋转好的图像。

That ensures that every source pixel is translated. Because of rounding you also must deal with pixel in imageRotated which are never set. Fill the pic with a background color.

An advanced solution is to use a strang color (128,128,128) and check after rotating which points are uncolored and mix a color from the neighbor.

Take a look at my favorite article CxImage which does a lot of image manipulation. At least you may get some well rotated images.


这篇关于在C ++中旋转图像时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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