在图像上放置图像

时间:2009-10-15 11:09:46

标签: c++ image-processing opencv

我想在我确定的坐标处将图像放在捕获的视频帧上。

之前我问过,我被告知要使用cvCopycvSetImageROI,但我不想在那些坐标上裁剪我想要添加另一张图像。 也许这是正确的方式,但我不理解它(如果正确请解释)。

4 个答案:

答案 0 :(得分:7)

我刚刚使用SetRoi做过这个,就像这样。我有两个图像,一个是一个名为thumb_frame的缩略图,它是我将在大图像中包含的小图片show_frame

//I set the ROI to the same size as the thumb_frame
cvSetImageROI(show_frame.image, cvRect(thumbnail_x_pos,
                    thumbnail_y_pos, thumb_frame->width, thumb_frame->height));

//I add the image to the designated ROI
cvAddWeighted(thumb_frame, alpha, show_frame, beta, 0, show_frame);

就是这样。

答案 1 :(得分:6)

void cvOverlayImage(IplImage* src, IplImage* overlay, CvPoint location, CvScalar S, CvScalar D)
{
 int x,y,i;

  for(x=0;x < overlay->width -10;x++)     
//replace '-10' by whatever x position you want your overlay image to begin. 
//say '-varX'
    {
        if(x+location.x>=src->width) continue;
        for(y=0;y < overlay->height -10;y++)  
//replace '-10' by whatever y position you want your overlay image to begin.
//say '-varY'
        {
            if(y+location.y>=src->height) continue;
            CvScalar source = cvGet2D(src, y+location.y, x+location.x);
            CvScalar over = cvGet2D(overlay, y, x);
            CvScalar merged;
            for(i=0;i<4;i++)
            merged.val[i] = (S.val[i]*source.val[i]+D.val[i]*over.val[i]);
            cvSet2D(src, y+location.y, x+location.x, merged);
        }
    }
}

使用它

cvOverlayImage(largerimage, overlayimage, cvPoint(10, 10), cvScalar(0.5,0.5,0.5,0.5), cvScalar(0.5,0.5,0.5,0.5)); 
//The cvPoint(10,10) can be the cvPoint(varX,varY) depending on how you write the function 
//and how you want to use it. 
//You cannot choose values less than 'varX' and 'varY' in this case
//else you would see a runtime error.

答案 2 :(得分:0)

您必须逐个像素地从源复制到目标。下面的代码恰好与坐标xy相关。我实际上没有尝试过这个,但我相当肯定它应该或多或少地按照你的预期工作。

确保目标图像至少是源的大小加上偏移量!

void drawImage(IplImage* target, IplImage* source, int x, int y) {
    for (int ix=0; x<source->width; x++) {
        for (int iy=0; y<source->height; y++) {
            int r = cvGet2D(source, iy, ix).val[2];
            int g = cvGet2D(source, iy, ix).val[1];
            int b = cvGet2D(source, iy, ix).val[0];
            CvScalar bgr = cvScalar(b, g, r);
            cvSet2D(target, iy+y, ix+x, bgr);
        }
    }
}

答案 3 :(得分:0)

不幸的是,“Paul Lammertsma”代码在这里混合了索引,你有固定代码:

void drawImage(IplImage* target, IplImage* source, int x, int y) {

    for (int ix=0; ix<source->width; ix++) {
        for (int iy=0; iy<source->height; iy++) {
            int r = cvGet2D(source, iy, ix).val[2];
            int g = cvGet2D(source, iy, ix).val[1];
            int b = cvGet2D(source, iy, ix).val[0];
            CvScalar bgr = cvScalar(b, g, r);
            cvSet2D(target, iy+y, ix+x, bgr);
        }
    }
}