1

I have an image where I am creating rectangle over a specified area. The image is : enter image description here

I am reading this image passing it through yolo algorithm gives me co-ordinates for rectangle around this gesture the x1 , y1 , x2 , y2 values are

print(x1 , y1 , x2 , y2)
tensor(52.6865) tensor(38.8428) tensor(143.1934) tensor(162.9857)

Using these to add a rectangle over the image

box_w = x2 - x1
box_h = y2 - y1

color = bbox_colors[int(np.where(unique_labels == int(cls_pred))[0])]
# Create a Rectangle patch
bbox = patches.Rectangle((x1, y1), box_w, box_h, linewidth=2, edgecolor=color, facecolor="none")
# Add the bbox to the plot
ax.add_patch(bbox)

It results in the follwing image :

enter image description here

Now, I want to blacken everything around this square. For this purpose i am saving the above image and reading it back then using opencv to blacken the rest using following code.

x1 = int(x1)
y1 = int(y1)
x2 = int(x2)
y2 = int(y2)

# read image
img = cv2.imread(output_path)
#creating black mask
mask = np.zeros_like(img)
mask = cv2.rectangle(mask, (x1, y1), (x2,y2),  (255,255,255), -1)

# apply mask to image
result = cv2.bitwise_and(img, mask)
# save results
cv2.imwrite(output_path, result)

I am getting the following image as result :

enter image description here

There are 2 issues :

  1. cv2.rectangle only takes integer values as co-ordinates
  2. May be x, y axis has different direction in yolo and open cv. Just guessing cause integers co-ordinate values should not be giving such vast difference from the rectangle.

This is being done in Jupyter notebook on Win 10.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Palash Jhamb
  • 605
  • 6
  • 15
  • Maybe https://towardsdatascience.com/yolo2-walkthrough-with-examples-e40452ca265f or https://stackoverflow.com/a/57764063/7758804 will help – Trenton McKinney Nov 25 '21 at 05:24
  • This search seems promising: [convert tensor yolo value to image array location site:stackoverflow.com](https://www.google.com/search?q=convert+tensor+yolo+value+to+image+array+location+site:stackoverflow.com&rlz=1C1ONGR_enUS976US976&sxsrf=AOaemvJsUGC6W-TzVKdSMTSKGF6aFz13mg:1637817725074&sa=X&ved=2ahUKEwjrqda94rL0AhXCHzQIHePXDCoQrQIoBHoECAUQBQ&biw=2560&bih=1335&dpr=1) – Trenton McKinney Nov 25 '21 at 05:25
  • If you find something comment it as a duplicate, or post an answer with links to the sources if a combination of links help resolve the issue. Best of luck. – Trenton McKinney Nov 25 '21 at 05:27
  • 1
    I have figured out the issue. I was saving the image using plt.savefig and then reading this saved image back in open cv. The issue is that plt.savefig removes any whitespace while saving , resulting in difference in size of the image. To resolve, i did not save the image. Rather, the image was directly consumed by opencv and then final desired result image saved by opencv. – Palash Jhamb Nov 25 '21 at 10:08

0 Answers0