Reputation: 179
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?
Upvotes: 1
Views: 214
Reputation: 979
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);
Upvotes: 1