How to replace part of the image with another image ROI
using opencv2.4.11 java API i am trying to truncate part of an image and then place it on another image.
the code posted below shows, the truncate Mat object is a truncated part of the img_1 posted below. and img_2 is the image that should host the truncated part of the "truncated" Mat object.
When i run the code, the result is the img_3, and i exepected to see img_2 overlapped with the "truncated" Mat object img_3.
please let me know how to achieve this properly.
Highgui.imwrite(path2, destImg);
static final String PATH5 = "c:.../images/CannyDest.jpg";
static final String PATH6 = "c:.../images/BlurredDest.jpg";
public static void main(String[] args) {
....
....
....
Mat temp = Highgui.imread(PATH5);//img_1
Mat trunctaed = temp.submat(new Rect(0, 0, 30, 30));
Mat target = Highgui.imread(PATH6);//img_2
trunctaed.copyTo(target);
Highgui.imwrite(PATH6, trunctaed);//img_3
}
update:
Mat destImg = Highgui.imread(path0, Highgui.IMREAD_COLOR);
Mat srcImg = Highgui.imread(path1);
srcImg.copyTo(destImg.submat(0, 30, 0, 30));
Highgui.imwrite(path2, destImg);
/* //Also i tried the following
* srcImg.submat(new Rect(0, 0, 30, 30)).copyTo(destImg.submat(0,30,0,30));
Highgui.imwrite(path2, destImg);
srcImg.submat(new Rect(0, 0, 30, 30)).copyTo(destImg.submat(new Rect(0, 0, 30, 30)));
Highgui.imwrite(path2, destImg);*/
img_1
img_2
img_3
I think you really need to iterate through all the pixels you want to change and assign them a new value.
Could you also post the result you want? I am not sure I am understanding what you want. Is it: You want the result to be img_2 with a cropped region, or img_2 with img_3 inside in a specific place, or something else?