简体   繁体   中英

Image Rotation gives grayscale image

I got a problem with my Rotate image function in C++, Using OpenCV and Qt. It kinda does his job, but not as expected, apart of being in grayscale, a part of the image seems to be duplicated at the top right.

Before

After

void ImgProcessing::rotate(cv::Mat &img, cv::Mat &tmp, int angle){

    float rads = angle*3.1415926/180.0;
    float cs = cos(-rads);
    float ss = sin(-rads);

    float xcenter = (float)(img.cols)/2.0;
    float ycenter = (float)(img.rows)/2.0;

    for(int i = 0; i < img.rows; i++)
        for(int j = 0; j < img.cols; j++){

            int rorig = ycenter + ((float)(i)-ycenter)*cs - ((float)(j)-xcenter)*ss;
            int corig = xcenter + ((float)(i)-ycenter)*ss + ((float)(j)-xcenter)*cs;
            int pixel = 0;
            if (rorig >= 0 && rorig < img.rows && corig >= 0 && corig < img.cols) {
                     tmp.at<int>(i ,j) = img.at<int>(rorig, corig);
                  }else tmp.at<int>(i ,j) = 0;
        }

}

Can the problem be in accessing to the image pixels?

It depends on how you read in the image but I think you are accessing it incorrectly. It should be something like this:

Vec3b intensity = image.at<Vec3b>(j, i);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM