Getting Started Guide: In-depth analysis of OpenCV's copyTo function and its application scenarios with rect

preface

OpenCV is a powerful open source computer vision library widely used in image processing and computer vision tasks. In OpenCV, the copyTo function is an important image processing function, which allows us to copy pixel data between different images, and combined with the use of rect (rectangle), more interesting functions can be achieved. This article will explain the use of the copyTo function in depth, and provide examples and application scenarios suitable for entry-level.

  1. CopyTo function overview
    In OpenCV, the prototype of the copyTo function is as follows:
void cv::Mat::copyTo(OutputArray dst, InputArray mask = noArray()) const

This function allows the current source image to be copied into the destination image (dst), optionally using a mask to specify the region of pixels to copy. It can handle images of different sizes and data types.

dst: The destination image, which must be a Mat object or image matrix that has been created and has the same size and data type as the source image.
mask: An optional parameter that specifies the area where pixels are copied. If a mask is provided, only pixels with a non-zero mask value will be copied into the destination image. If no mask is provided, the entire source image is copied by default.

Example of copyTo function

#include <opencv2/opencv.hpp>

int main() {
    
    
    // 读取源图像和目标图像
    cv::Mat srcImage = cv::imread("source.jpg", cv::IMREAD_COLOR);
    cv::Mat dstImage = cv::imread("destination.jpg", cv::IMREAD_COLOR);

    // 检查图像是否成功读取
    if (srcImage.empty() || dstImage.empty()) {
    
    
        std::cout << "无法读取图像文件" << std::endl;
        return -1;
    }

    // 将源图像复制到目标图像中(完全覆盖)
    srcImage.copyTo(dstImage);

    // 显示结果
    cv::imshow("源图像", srcImage);
    cv::imshow("目标图像", dstImage);
    cv::waitKey(0);

    return 0;
}

In the above example, we copied the source image (source.jpg) to the destination image (destination.jpg) and displayed both images in a window. Since no mask is used, the entire source image is copied to the corresponding position of the destination image.

Application scenario of copyTo function and rect

The copyTo function can be used in combination with rect to realize partial copying, pasting and merging of images. The following is an example using rect to copy a specific area of ​​the source image into the destination image.


#include <opencv2/opencv.hpp>

int main() {
    
    
    // 读取源图像和目标图像
    cv::Mat srcImage = cv::imread("source.jpg", cv::IMREAD_COLOR);
    cv::Mat dstImage = cv::imread("destination.jpg", cv::IMREAD_COLOR);

    // 检查图像是否成功读取
    if (srcImage.empty() || dstImage.empty()) {
    
    
        std::cout << "无法读取图像文件" << std::endl;
        return -1;
    }

    // 定义矩形区域(ROI)并复制到目标图像中
    cv::Rect rect(100, 100, 200, 200);
    cv::Mat roi = srcImage(rect);
    roi.copyTo(dstImage(rect));

    // 显示结果
    cv::imshow("源图像", srcImage);
    cv::imshow("目标图像", dstImage);
    cv::waitKey(0);

    return 0;
}

In the above example, we use the rectangle Rect(100, 100, 200, 200) to specify a specific area in the source image and copy it to the same location in the destination image. In this way, we are able to extract local regions of the source image in the target image.

in conclusion

This article introduces the copyTo function in OpenCV and its use in combination with rect. The copyTo function is a basic and important function in OpenCV image processing, and it is an essential skill for beginners. By understanding and mastering the copyTo function, you can implement operations such as copy, paste, and merge in image processing, expanding more interesting application scenarios. Hope this article helps you to better utilize OpenCV for image processing and computer vision tasks.

Guess you like

Origin blog.csdn.net/qq_46017342/article/details/131949503