我在 C++ 中使用 OpenCV 和 Qt 旋转图像功能时遇到问题。 它有点做他的工作,但不像预期的那样,除了灰度之外,图像的一部分似乎在右上角重复了。
之前
之后
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;
}
}
问题可能出在访问图像像素上吗?
最佳答案
这取决于您如何阅读图像,但我认为您访问图像的方式不正确。它应该是这样的:
Vec3b intensity = image.at<Vec3b>(j, i);
关于c++ - 图像旋转给出灰度图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56601966/