Question

Is it possible to place an image inside an image with OpenCv (JavaCv). For example i have a 1000x1000 image and a 100x100 image. And at position 600x600 i would like to place the smaller image inside the larger image.

lets say the blue box is the 1000x1000 IplImage and the red one is the 100x100 IplImage. Is it possible to put the red box in the blue box. Preferably computational rather efficient because it has to work in real time.

enter image description here

Thx in advance

Was it helpful?

Solution

This is in Python, but conversion to Java is going to be real easy. Use GetSubRect(), and Copy(). GetSubRect() returns a rectangular subarray of interest (specify top left point of interest, and the width and height). Then just copy over the image using Copy().

import cv
blue = cv.LoadImage("blue.jpg")
red = cv.LoadImage("red.jpg")

sub = cv.GetSubRect(blue, (100, 100, 50, 50))
cv.Copy(red,sub)

cv.ShowImage('blue_red', blue)
cv.WaitKey(0)

Alternatively, as karlphillip suggests you could specify the 'region of interest' using SetImageROI(), and do much the same thing:

cv.SetImageROI(blue,(100,100,50,50))
cv.Copy(red, blue)
cv.ResetImageROI(blue)

Its very important to reset the ROI, ResetImageROI, otherwise you will only display/save the ROI, and not the whole image.

Demo output:

blue: enter image description here red: enter image description here combined: enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top