0

I am trying to change a part of an image with another image I couldn't find the merging function so I just occur that can I change the rgb values of the part I want to change with the other images RGB values is it possible

Thanks for suggestions

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
eomer
  • 3,514
  • 7
  • 30
  • 42

2 Answers2

3

If by change you mean replace, then you can use the image ROI (region of interest) functions to directly replace a rectangular region of your original image with a rectangular region from another image very efficiently.

Suppose your original image was stored in A and you want to change a part of it (a rectangular region) using pixels from an image B.

UPDATE: Here's the code in C

/**** C ****/

// Acquire Image A and B (here as an example, I'm reading from disk)
IplImage* A = cvLoadImage("image_A.jpg");
IplImage* B = cvLoadImage("image_B.jpg");

// Set the region-of-interest (ROI) for the two images
// such that only the ROI of A and B will be handled
cvSetImageROI(A,cvRect(200,200,128,128));
cvSetImageROI(B,cvRect(0,0,128,128));

// Copy the ROI in B to the ROI in A
cvCopy(B,A);

// Reset the ROI (now the entire image will be handled)
cvResetImageROI(A);
cvResetImageROI(B); 

// Display A
cvNamedWindow("Modified A");
cvShowImage("Modified A",A);
cvWaitKey();

// Release the images
cvReleaseImage(&A);
cvReleaseImage(&B);

Using OpenCV 2.0:

// C++ //

// Images A and B have already been loaded .....

// Region in image A starting from (100,100) of width 200 and height 200
Rect RegionA(100,100,200,200);
// Region in image B starting from (50,50) of width 200 and height 200
Rect RegionB(50,50,200,200);

// No copying, just a reference to the ROI of the image
Mat A_ROI(A,RegionA);
Mar B_ROI(B,RegionB);
// Copy all the pixels in RegionB in B to RegionA to A
B.copyTo(A);
Jacob
  • 34,255
  • 14
  • 110
  • 165
  • hmmm thanks but can you write a C code i'm not good at C++ i learned that cvSetImageROI copy that specified rectangular to another image i heard ROI function itself first time. as i undertand your comments near the it will just do what i am looking for – eomer Oct 20 '09 at 06:10
0

You could try something like this:

CvScalar s = cvGet2D(original_cvimage, x, y); // get the (x,y) pixel value
cvSet2D(new_cvimage, x, y, s); // set the (x,y) pixel value
anyone
  • 11
  • 1
  • i dont understand what does it do can you explain – eomer Oct 19 '09 at 09:47
  • cvGet2D returns a CvScalar object which represents a pixel. If you say: s = cvGet2D(original_cvimage, 0, 0), it'll return the RGB values for pixel (0,0). s[0] = bluevalue, s[1] = green and s[2] = red. But that's non of your concern. All you need to know is that after you've called s = cvGet2D(original_cvimage, x, y); , s will hold the pixel value for 1 pixel of your original image. By calling cvSet2D(new_cvimage, x, y, s); you're loading 1 pixel from your original image in the new image. So you need to add a nested loop to go through all the pixels in your image. – anyone Oct 19 '09 at 10:48
  • That's going to be **very slow**. – Jacob Oct 19 '09 at 14:13
  • Yes it is, but I read this thread: http://stackoverflow.com/questions/1571683/opencv-image-on-image and was under the impression that the OP didn't want to use cvSetImageROI and cvCopy – anyone Oct 19 '09 at 14:30