我在 OpenCV C++ 中做这个项目,我在其中制作给定图像的反射,就像翻转函数一样,但每个像素的坐标。问题是我得到的图像输出都是蓝色的,水平有一条线,我相信我的代码只影响第一个 channel 。
我试着做imageReflectionFinal.at<Vec3b>(r,c) = image.at<Vec3b>(r,c);
为了解决它,但没有任何改变。我会在下面留下代码,在此先感谢。
Mat image = imread("image_dir/image.jpg");
Mat imageReflectionFinal = Mat::zeros(image.size(), image.type());
for(unsigned int r=0; r<image.rows; r++) {
for(unsigned int c=0; c<image.cols; c++) {
imageReflectionFinal.at<Vec3b>(r,c) = image.at<Vec3b>(r,c);
Vec3b sourcePixel = image.at<Vec3b>(r,c);
imageReflectionFinal.at<Vec3b>(r, c) = (uchar)(c, -r + (220)/2);
}
}
最佳答案
如果您不想使用 flip函数,您可以镜像更改每行的 x 坐标(列)。这是代码:
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
int main() {
//You can change as "Mat3b" for the 3-channel images
Mat1b image = imread("/ur/image/directory/image.jpg",CV_LOAD_IMAGE_GRAYSCALE);
Mat1b imageReflectionFinal = Mat::zeros(image.size(), image.type());
for(unsigned int r=0; r<image.rows; r++) {
for(unsigned int c=0; c<image.cols; c++) {
imageReflectionFinal(r, c) = image(r, image.cols - 1 - c);
//y-axis(r) doesnt change only x-axis(cols) mirroring
}
}
imshow("Result",imageReflectionFinal);
waitKey(0);
return 0;
}
This answer is also my reference.
关于c++ - 如何在 OpenCV 中将图像与另一个图像相关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60045476/