[英]Program to convert image and rotate
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. 我一直在尝试使用opencv在c ++中创建这个程序,将图像转换为灰度并随后旋转图像,但我得到的输出是各种混乱。
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 代码: http : //pastebin.com/FSJKyaeU
Also, here's a picture of the output I get http://i.imgur.com/qpYm1.jpg 另外,这是我得到的输出图片http://i.imgur.com/qpYm1.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. 然后更新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);
}
}
}
I think at least there is something wrong on this line: 我认为至少这一行有问题:
imRotate.at<unsigned char>(i,j) = im.at<unsigned char>(NewXPixel,NewYPixel);
... because i
and j
are supposed to loop over the original image, right? ...因为i
和j
应该在原始图像上循环,对吗?
Perhaps you meant something like: 也许你的意思是:
imRotate.at<unsigned char>(NewXPixel,NewYPixel) = im.at<unsigned char>(i,j);
Also, x
and y
are not used in FuncRotate
. 此外,在FuncRotate
中不使用x
和y
。
Moreover, I believe you should initialize imRotate
to zero. 而且,我相信你应该将imRotate
初始化为零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.