我想 粘贴 分部 镜像到另一个镜像上 为我正在做的一个项目的一部分。当我试图查看我目前所拥有的东西时,我看到调用 imshow()
在两张图片上进行检查,实际上并没有显示出两张图片在应该的点上有相同的BRG值。
这些数值几乎都是错误的,经常会有一半的屏幕显示为黑色。
IE:像素[0,1]的 firstIMG
将有[91,21,30],而 secondIMG
当鼠标悬停在像素上时,会有一些其他的值。imshow()
屏风
// Randomly create an image of 10x10 pixels
Mat firstIMG = Mat(10, 10, CV_8UC3);
randu(firstIMG, Scalar::all(0), Scalar::all(255));
imshow("First", firstIMG);
waitKey();
// Create a second image of same type as the first
Mat secondIMG = Mat::zeros(firstIMG.rows-5, firstIMG.cols-5, firstIMG.type());
// Iterate through all rows of secondIMG
for(int i = 0; i < secondIMG.rows; i++) {
// Iterate through all columns of secondIMG
for(int j = 0; j < secondIMG.cols; j++) {
secondIMG.at<unsigned char>(i, j) = firstIMG.at<unsigned char>(i, j);
}
}
imshow("Second", secondIMG);
waitKey();
如果我遍历每张图像的每个像素,它将为两张图像打印出相同的数据,但当我使用 imshow()
在...上 secondIMG
很多像素都是不正确的。尽管如此,我也试着访问并更改了BRG的值。secondIMG
以不同的方式,[就像这篇文章][1]。这仍然会导致同样的结果,即在它们共享的点上的数值完全不同。最后,我还尝试过直接用 clone()
的 firstIMG
用相同的大小和所有的东西,也导致了和上面一样的错误。
[1]: https://stackoverflow.com/questions/7899108/opencv-get-pixel-channel-value-from-mat-image
它应该是 cv::Vec3b
而不是 uchar
的彩色图像。
你可以使用优化的内置功能,而不是在像素上循环。
cv::Mat secondIMG(firstIMG.rows, firstIMG.cols, firstIMG.type());
cv::Rect rect(0, 0, secondIMG.cols, secondIMG.rows); // x, y, width, height
firstIMG(rect).copyTo(secondIMG);
// or
cv::Mat thirdIMG = firstIMG(rect).clone();