0

How can I fill ROI, so that the image is placed in the centre of the ROI?

The ROI is found with (x, y, w, h) = cv2.boundingRect(contour) and I insert the input image with

                       image = cv2.resize(input_image, (w, h))
                       img2gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                       _, image_mask = cv2.threshold(img2gray, 1, 255, cv2.THRESH_BINARY)
                       roi = frame[y:y+h, x:x+w]
                       roi[np.where(image_mask)] = 0
                       roi += image

The h and w are the dimensions of my input image.

How can I add the offsets so that the ROI += image will result like shown in the image? This is the outcome I want

  • What error are you facing? – Jeru Luke Apr 27 '22 at 10:26
  • @JeruLuke I am not facing any error as it is, but I have hard time understanding how to input the image in the center. I can either overlay the entire ROI with an image or place the image in the upper left corner of the ROI. – Drawntogetha Apr 27 '22 at 10:39
  • What is `input_image` and `frame` in the code? – Jeru Luke Apr 27 '22 at 10:47
  • @JeruLuke Input image is the image I want to place inside the ROI. The frame is the webcam frame I am grabbing to do processing to it and then find a ROI in it. Problem is that my ROI is of the size of the detected object. I'd like to insert image in its place, but so that it does not take the whole ROI size, but as shown in the illustration in OP. – Drawntogetha Apr 27 '22 at 10:49
  • And on what basis are you finding `contour`? – Jeru Luke Apr 27 '22 at 10:51
  • @JeruLuke Contour is found by looking at the objects of particular color in the frame with cv2.findContours: `contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)` – Drawntogetha Apr 27 '22 at 10:53
  • So you want to replace the colored object with resized version of `input_image` – Jeru Luke Apr 27 '22 at 11:02
  • 1
    @JeruLuke I need to scale my `input_image` so that it occupies only a part of the colored object, while the ROI represents the entire object. – Drawntogetha Apr 27 '22 at 11:12
  • @Drawntogetha. Do you mind if you wanted to do with mouse event? It is straight forward. – toyota Supra Apr 27 '22 at 11:58
  • @toyotaSupra Unfortunately no, I need to be able to do it in realtime, as the program is associated with the webcam stream. – Drawntogetha Apr 27 '22 at 12:03
  • can you post original image? – toyota Supra Apr 27 '22 at 12:05

1 Answers1

1

As per my understanding, you want to place the resized image within the center of the ROI.

You have already obtained the ROI roi = frame[y:y+h, x:x+w].

# using this as background, hence making it white
roi[:] = (255, 255, 255)

# Obtain the spatial dimensions of ROI and image
roi_h, roi_w = roi.shape[:2]
image_h, image_w = image.shape[:2]

# Calculate height and width offsets
height_off = int((roi_h - image_h)/2)
width_off = int((roi_w - image_w)/2)

# With Numpy slicing, place the image in the center of the ROI
roi[height_off:height_off+image_h, width_off:width_off+image_w] = image

I hope this gives you an idea on how to proceed further

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • The numpy slicing part is exactly what I was looking for, thank you! – Drawntogetha Apr 28 '22 at 06:59
  • What worked in the end is to adjust the ROI itself, so that I have `(x, y, w, h) = cv2.boundingRect(contour)` `centerx = int(x+30)` `centery = int(y+10)` `halfw = int(w/1.5)` `halfh = int(h/2)` `roi = frame[centery:centery+halfh, centerx:centerx+halfw]` And with trial & error I've placed my input_image into the ROI center – Drawntogetha Apr 28 '22 at 08:38
  • Sounds good. Glad I could help! – Jeru Luke Apr 28 '22 at 08:47