views:

2420

answers:

4

hi

i want to paste an image to a captured video frame on the coordinates which i determined

i asked that before and i have been told to use cvCopy and cvSetImageROI but i dont want to crop on those coordinates i want to add another image maybe its the right way but i didnt understand it (if its right pls explain it)

thank for suggestions

A: 

You will have to copy pixel by pixel from the source to the destination. The code below does precisely that, offseting with coordinates x and y. I haven't actually tried this, but am fairly certain it should work more or less as you'd expect.

Just make sure the target image is at least the size of the source plus the offset!

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);
        }
    }
}
Paul Lammertsma
thanks but i want to combine all of the source to x,y pixels of the target i think i must add 2 for loops to your code the for loops in your code will get the r,g,b values of the source and the other 2 i will add will change the targets rgb is it correct
eomer
The above code will cycle through all the pixels of the source image and copy them one by one to an offset position on the target image. There is no need for any additional loops unless you intend to copy multiple `IplImage`s onto the target.
Paul Lammertsma
ok i undertand your code thanks i already have a loop for multiple image i think you didnt understand me i want to do exactly opposite of your code what i want to do is replace a part of my source image with another image
eomer
Simply swap the `IplImage`s in the parameters to accomplish that.In essence what you need to do, is take the captured video as `target`, an image you want to overlay as `source`, and specify the `x` and `y` offset where you want the image placed on the video.Since the target video is passed with a pointer, you can then proceed to process the captured video frame after calling `drawImage()`.
Paul Lammertsma
paul i just use your code before your last comment i replace targets and sources with each other in the code however the image i want to put is put but on another coordinates and lenghten widely i want to put it on my eye detection program i put a black image to eyes but instead of eyes it appeared on the forheadif you want i can sen you the code
eomer
@eomer I've received your e-mail, but don't mind if I respond here. There's a simpler way of copying images pixel-by-pixel. The previous code was specific to three-channel images. I've updated my code accordingly. Give it a swing and let me know if it works or not.
Paul Lammertsma
Thats not fast. You can improve by using `real` get/set functions, better use @Ola's code.
Cfr
+3  A: 

I did this a while ago using SetRoi, it was something like this. I have two images, one is a thumbnail called thumb_frame that is the small picture I will include in the large image 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);

That's about it.

Ola
A: 

Bugfix: ix++ and iy++ Not: x++ and y++ in the loops!

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

  for(x=0;x < overlay->width -10;x++)
    {
        if(x+location.x>=src->width) continue;
        for(y=0;y < overlay->height -10;y++)
        {
            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);
        }
    }
}

To use it

cvOverlayImage(largerimage, overlayimage, cvPoint(10, 10), cvScalar(0.5,0.5,0.5,0.5), cvScalar(0.5,0.5,0.5,0.5));
virgoptrex
Thanks George. I really can't figure out how to use the editing tools here. I can write complicated code yet can't do these simple things :). What did you use to highlight my code. I tried <pre> and <code>
virgoptrex