gpt4 book ai didi

c++ - opencv c++中的copyTo如何工作?

转载 作者:行者123 更新时间:2023-12-02 10:04:42 27 4
gpt4 key购买 nike

我浏览了以下代码片段,想知道copyTo() in opencv函数如何工作。

 // Make a copy
Mat faceWithGlassesNaive1 = faceImage.clone();
// Take the eye region from the image
Mat roiFace = faceWithGlassesNaive1(Range(150,250),Range(140,440));
// Replace the eye region with the sunglass image
glassBGR.copyTo(roiFace);
copyTo()是否可以在 copyByreference上使用,以便对 roiface所做的任何修改都可以反射(reflect)在 faceWithGlassesNaive1中?还是我想念什么?需要帮忙 。
  • 下面是人脸图像

  • faceimage
  • 下面是glassBGR图片
    enter image description here
  • 以下是上述片段中glassBGR.copyTo(roiFace)的输出
    enter image description here
  • 最佳答案

    opencv的copyTo函数,如documentation所示:

    The method copies the matrix data to another matrix.



    因此,您不能仅仅通过它来实现这样的任务,而可以将其作为一种方法来实现:

    编码:
    #include <iostream>    
    #include <opencv2/imgproc.hpp>
    #include <opencv2/highgui.hpp>

    using namespace std;
    using namespace cv;

    int main()
    {

    Mat faceImage = imread("/ur/face/image/directory/elon.jpg");
    Mat glassBGR = imread("/ur/glass/image/directory/glass.png");

    resize(glassBGR,glassBGR,Size(300,100));
    // check each pixel of glass and if its white(255,255,255) then change it with face image pixels
    for(int i=0;i<glassBGR.cols;i++)
    {
    for(int j=0;j<glassBGR.rows;j++)
    {
    if(!(glassBGR.at<Vec3b>(j, i) == Vec3b(255,255,255)))
    {
    faceImage.at<Vec3b>(j+150,i+140) = glassBGR.at<Vec3b>(j, i);
    }
    }
    }

    imshow("result",faceImage);
    waitKey(0);
    }

    结果:

    enter image description here

    关于c++ - opencv c++中的copyTo如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60883395/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com