I've been trying to make this program in c++ with opencv that converts the image to greyscale and rotates the image afterwards, but the output I get is all kinds of messed up.
I have been searching for solutions and looking for help everywhere, but I haven't been able to find out what the heck I have done wrong so if any of you could help me it'd be great
Code: http://pastebin.com/FSJKyaeU
Also, here's a picture of the output I get https://i.sstatic.net/uMGuJ.jpg
Please replace:
Mat imRotate(im.cols * 2, im.rows * 2, im.type());
to use this one:
int size = static_cast<int>(sqrt(im.cols * im.cols/4 + im.rows * im.rows/4) * 2) + 1;
Mat imRotate(size, size , im.type());
Then update FuncRotate. I think you need to do something like this:
void FuncRotate(Mat im, Mat imRotate, int q, int x, int y, int rows, int columns)
{
double radians = (q * 3.1415)/180; // or try use M_PI instead of 3.1415
double cosr = cos(radians);
double sinr = sin(radians);
for(int i=0; i<columns; i++) //columns of the original image
{
for(int j=0; j<rows; j++) //rows of the original image
{
int NewXPixel = imRotate.cols/2 + ((i-x) * cosr) - ((j-y) * sinr);
int NewYPixel = imRotate.rows/2 + ((i-x) * sinr) + ((j-y) * cosr);
if(NewXPixel < 0 || NewYPixel < 0 || NewXPixel >= imRotate.cols || NewYPixel >= imRotate.rows)
continue;
imRotate.at<unsigned char>(NewYPixel,NewXPixel) = im.at<unsigned char>(j,i);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With