33

是否有 OpenCV 函数可以在另一个图像上绘制图像?我有一个Mat类型的大图像。我有一个( )Mat类型的小图像。5x7我想在指定的大图像上绘制这个小图像coordinates

4

4 回答 4

53

使用Mat::rowRange()Mat::colRange()指定要在目标中绘制的区域Mat。代码:

Mat src( 5,  7, CV_8UC1, Scalar(1)); // 5x7
Mat dst(10, 10, CV_8UC1, Scalar(0)); // 10x10

src.copyTo(dst.rowRange(1, 6).colRange(3, 10));

结果如下:

之前copyTo()

dst:
    ( 0 0 0 0 0 0 0 0 0 0 )
    ( 0 0 0 0 0 0 0 0 0 0 )
    ( 0 0 0 0 0 0 0 0 0 0 )
    ( 0 0 0 0 0 0 0 0 0 0 )
    ( 0 0 0 0 0 0 0 0 0 0 )
    ( 0 0 0 0 0 0 0 0 0 0 )
    ( 0 0 0 0 0 0 0 0 0 0 )
    ( 0 0 0 0 0 0 0 0 0 0 )
    ( 0 0 0 0 0 0 0 0 0 0 )
    ( 0 0 0 0 0 0 0 0 0 0 )

之后copyTo()

dst:
    ( 0 0 0 0 0 0 0 0 0 0 )
    ( 0 0 0 1 1 1 1 1 1 1 )
    ( 0 0 0 1 1 1 1 1 1 1 )
    ( 0 0 0 1 1 1 1 1 1 1 )
    ( 0 0 0 1 1 1 1 1 1 1 )
    ( 0 0 0 1 1 1 1 1 1 1 )
    ( 0 0 0 0 0 0 0 0 0 0 )
    ( 0 0 0 0 0 0 0 0 0 0 )
    ( 0 0 0 0 0 0 0 0 0 0 )
    ( 0 0 0 0 0 0 0 0 0 0 )
于 2012-06-12T07:00:42.573 回答
40

在大图像中创建一个感兴趣区域,然后将小图像复制到该区域:

cv::Rect roi( cv::Point( originX, originY ), cv::Size( width, height ));
cv::Mat destinationROI = bigImage( roi );
smallImage.copyTo( destinationROI );

如果您确定小图像适合大图像,那么您可以简单地执行以下操作:

cv::Rect roi( cv::Point( originX, originY ), smallImage.size() );
smallImage.copyTo( bigImage( roi ) );
于 2012-12-07T12:34:38.250 回答
4

这是openCV的java版本中的解决方案

Rect roi= new Rect(originX,originY,smalImage.width(),smallImge.height());
smallImage.copyTo( new Mat(input,roi) );
于 2017-03-25T21:42:57.750 回答
0
void zoomImage(Mat &src, Mat &dst, int scale_percent)
{

    //# percent of original size
    int width = int(src.cols * scale_percent / 100);
    int height = int(src.rows * scale_percent / 100);
    Size dim = Size(width, height);
    //pyrUp(tmp, dst, Size(tmp.cols * 2, tmp.rows * 2));
    resize(src, dst, dim, 0.0, 0.0, INTER_CUBIC);

    if (scale_percent < 100)
    {

        Mat srcR =Mat::zeros(Size(640,480),src.type()) ;
        int rstart = (src.rows - height) / 2;
        int rend = height;
        int cstart = (src.cols - width) / 2;
        int cend = width;
        dst.copyTo(srcR.rowRange( rstart, dst.rows+ rstart).colRange(cstart,dst.cols+ cstart));
        dst = srcR.clone();

    }
    else
    {
        Mat  ROI(dst, Rect((width - src.cols) / 2, (height - src.rows) / 2, src.cols, src.rows));
          dst = ROI.clone();
    }

}
于 2019-02-06T10:34:10.967 回答