H
H
hardwellZero2020-04-04 20:46:36
go
hardwellZero, 2020-04-04 20:46:36

How to superimpose an image on another image (in the center)?

Hey!

There was a task to superimpose one image on the second using gocv.
The difficulty is that you need to calculate the center "background" of the image.
What am I doing now:

bgImg := gocv.IMRead("bg.jpg", gocv.IMReadColor)
inputImg := gocv.IMRead("input.jpg", gocv.IMReadColor)

bgHalfWidth := bgImg.Cols() / 2
bgHalfHeight := bgImg.Rows() / 2
imgHalfWidth := inputImg.Cols() / 2
imgHalfHeight := inputImg.Rows() / 2

minX := bgHalfWidth - imgHalfWidth
minY := bgHalfHeight - imgHalfHeight
maxX := bgHalfWidth + imgHalfWidth
maxY := bgHalfHeight + imgHalfHeight

rect := image.Rect(minX, minY, maxX, maxY)
roi := bgImg.Region(rect)
gocv.AddWeighted(roi, 0.0, bgImg, 1.0, 0.0, &roi)


Assume that the input background image has the following dimensions 1024x682 and the image to be overlaid is 512x341. For this case, we get:
minx, miny, maxx, maxy equal to 256, 171, 768, 511, but the correct ones are 256, 171, 768, 512.
Of course, the problem is in bisection and calculation, but alas, I can’t understand how I get rid of it. At some point, you need to shift or expand some image? Perhaps I just chose not the most successful approach to calculating minx, miny, maxx, maxy?

I'd love to hear any advice!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
U235U235, 2020-04-04
@hardwellZero

rect := image.Rect(minX, minY, maxX, maxY)
replace with
rect := image.Rect(minX, minY, minX+inputImg.Cols(), minY+inputImg.Rows())
or the same in maxX calculation /Y

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question