在OpenCV中逐像素复制图像

时间:2015-12-03 10:55:18

标签: c++ image opencv

我正在尝试将较小的图像复制到较大的图像中。我这样做时出错了。我不想使用ROI方法,因为我将使用此代码进行多次迭代,并且每次为图像选择ROI效率都不高,并且每次都会改变ROI。 我不想使用copyTo()函数复制图像,因为我的下一步是检查图像像素是否为0(即黑色),如果是,则不复制它。 我可以读取像素的值,但当我尝试将其复制到另一个图像时,我得到一个错误。我查看过所有以前的帖子并尝试进行更改,但没有一个能够正常工作。 我会附上我的代码以及我得到的错误。

#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img1, img2, output;

    img1 = imread("E:/Image2.jpg", 1);
    img2 = imread("E:/Marker2.PNG", 1);

    int startrow, startcol;
    startrow = 20;
    startcol = 20;

    int rows, cols,i,j,r=1,c;
    cv::Size s = img2.size();
    rows = s.height;
    cols = s.width;
for (i = startrow; i <= startrow + rows; i++)
    {
        c = 1;
        for (j = startcol; j <= startcol + cols; j++)
        {
            output.at<uchar>(i, j) = img2.at<uchar>(r, c);
            c++;
        }
        r++;
    }
    imshow("Output", output);
    waitKey(1000);
    return 0;
}

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:4)

不要使用复杂的像素位置参数。相反,只需使用投资回报率中的子图像:

cv::Mat input = cv::imread("../inputData/Lenna.png");
cv::Mat output = input.clone();
cv::Mat marker = cv::imread("../inputData/marker.png");

// subimage dimensions:
cv::Point startPosition = cv::Point(20,20);
cv::Size size = marker.size();

// ROI:
cv::Rect subImageRect = cv::Rect(startPosition, size);

// limit the roi if roi is bigger than the original image:
cv::Rect fullImageRect = cv::Rect(cv::Point(0,0), input.size());

// intersection of both rois
subImageRect = subImageRect & fullImageRect;
if(subImageRect.width == 0 || subImageRect.height == 0)
{
    std::cout << "marker position isn't within the image dimensions" << std::endl;
    return 0;
}

// subimage = reference to image part of original image:
cv::Mat outputSubImage = output(subImageRect);
// marker subimage should be the whole marker, but might be reduced.
cv::Mat markerSubImage = marker(cv::Rect(0,0,subImageRect.width, subImageRect.height));

// now just copy the data:
markerSubImage.copyTo(outputSubImage);
// if you don't want to use .copyTo, just use a loop over 0 .. subImage.width/height and copy from same pixel location to same pixel location.
cv::imshow("output", output);
cv::waitKey(0);

使用此输入图像:

enter image description here

和此标记图像:

enter image description here

它会生成此输出:

enter image description here

如果您确定标记位于图像中,则可以删除完整性检查以简化代码:

cv::Mat input = cv::imread("../inputData/Lenna.png");
cv::Mat output = input.clone();
cv::Mat marker = cv::imread("../inputData/marker.png");

// subimage dimensions:
cv::Point startPosition = cv::Point(20,20);
cv::Size size = marker.size();

// ROI:
cv::Rect subImageRect = cv::Rect(startPosition, size);
// subimage = reference to image part of original image:
cv::Mat outputSubImage = output(subImageRect);

// now just copy the data:
marker.copyTo(outputSubImage);
// if you don't want to use .copyTo, just use a loop over 0 .. subImage.width/height and copy from same pixel location to same pixel location.

答案 1 :(得分:2)

这对我来说很好用:

#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img1, img2, output;

    img1 = imread("10.jpg", 1);
    img2 = imread("13.jpg", 1);
    output=img1.clone();
    int startrow, startcol;
    startrow = 20;
    startcol = 20;

    int rows, cols,i,j,r=0,c;
    cv::Size s = img2.size();
    rows = s.height;
    cols = s.width;
for (i = startrow; i < startrow + rows; i++)
    {
        c = 0;
        for (j = startcol; j < startcol + cols; j++)
        {
            output.at<Vec3b>(i, j) = img2.at<Vec3b>(r, c);
            c++;
        }
        r++;
    }
    imshow("Output", output);
    waitKey(0);
    return 0;
}

enter image description here

答案 2 :(得分:0)

如果没有实际的错误消息,我不确定到底发生了什么。

对我来说,看起来你的问题与分配输出有关。尝试将其初始化为正确的大小,然后再开始填写其值。

output = Mat::zeros(numRows, numCols, type) // something like this

查看文档: enter image description here