簡體   English   中英

在OpenCV中逐像素復制圖像

[英]Copy image pixel by pixel in 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;
}

非常感謝您的幫助。

不要使用復雜的像素位置參數。 而是僅使用ROI中的子圖像:

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);

使用此輸入圖像:

在此處輸入圖片說明

和這個標記圖片:

在此處輸入圖片說明

它生成以下輸出:

在此處輸入圖片說明

如果您確定標記在圖像中,則可以刪除完整性檢查以簡化代碼:

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.

這對我來說很好:

#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;
}

在此處輸入圖片說明

沒有實際的錯誤消息,我不確定到底發生了什么。

對我來說,您的問題似乎與分配輸出有關。 開始填寫其值之前,請嘗試將其初始化為正確的大小。

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

請查看文檔: http : //docs.opencv.org/2.4/modules/core/doc/basic_structures.html#mat-create

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM