Ask Your Question
1

How to replace part of the image with another image ROI

asked 2015-03-26 06:27:15 -0600

RB gravatar image

updated 2015-03-27 03:41:52 -0600

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_1

img_2 img_2

img_3 img_3

edit retag flag offensive close merge delete

Comments

I think you really need to iterate through all the pixels you want to change and assign them a new value.

Pedro Batista gravatar imagePedro Batista ( 2015-03-26 06:45:32 -0600 )edit

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?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-03-26 07:32:56 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-03-26 07:30:58 -0600

bvbdort gravatar image

updated 2015-03-27 06:52:59 -0600

change

trunctaed.copyTo(target);   // erases all content  of target and keeps truncated image only.

to

trunctaed.copyTo(target(cv::Rect(0, 0, 30, 30))); // replace specified rect of target with truncate image.
Highgui.imwrite(PATH6, target); //img_3  result is in target.

above change gives.

image description

Update :

    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(cv::Rect(0, 0, 30, 30))); 
    Highgui.imwrite(PATH6, target); //img_3
edit flag offensive delete link more

Comments

i tried your answer but unfortunately i always receive the same result

RB gravatar imageRB ( 2015-03-26 08:07:08 -0600 )edit

This will create a ROI on target and then will copy truncated to that image. OP wants to replace pixels of ROI in the main image, keeping the other pixels the same.

Pedro Batista gravatar imagePedro Batista ( 2015-03-26 08:10:49 -0600 )edit

what is OP please

RB gravatar imageRB ( 2015-03-26 08:39:25 -0600 )edit

So what do you want? the inverse: colors in the edges? Or do you want edges on colors, but keeping the colors? Have you really changed the 2 lines of code in the answer? Strange if it gives the same result as in your case... Aren't you overwriting the image with just the ROI?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-03-26 10:09:02 -0600 )edit

please have alook at the update section..i still have the same problem to copy part of the image over anothe rimage, though, i have solved this issue with pixel by pixel replacement

RB gravatar imageRB ( 2015-03-27 03:43:18 -0600 )edit

i just put complete code lines, let me know it its working and gives you required result.

bvbdort gravatar imagebvbdort ( 2015-03-27 06:53:59 -0600 )edit

The submat is cropping the image, so you'll have just the copied region...Aren't simple parenthesis working (srcImg.submat(new Rect(0, 0, 30, 30)).copyTo(destImg(new Rect(0, 0, 30, 30)));)?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-03-27 11:46:59 -0600 )edit

On java you can't do destImg(new Rect(0, 0, 30, 30). I tried with destImg.adjustROI(dtop, dbottom, dleft, dright) before copyTo but still not work.

jotapdiez gravatar imagejotapdiez ( 2017-11-08 13:10:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-03-26 06:27:15 -0600

Seen: 25,311 times

Last updated: Mar 27 '15