0
votes

I have a set of two monochrome images [attached] where I want to put rectangular bounding boxes for both the persons in each image. I understand that cv2.dilate may help, but most of the examples I see are focusing on detecting one rectangle containing the maximum pixel intensities, so essentially they put one big rectangle in the image. I would like to have two separate rectangles.

image1

image2

UPDATE: This is my attempt:

import numpy as np
import cv2

im = cv2.imread('splinet.png',0)
print im.shape
kernel = np.ones((50,50),np.uint8)
dilate = cv2.dilate(im,kernel,iterations = 10)
ret,thresh = cv2.threshold(im,127,255,0)
im3,contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
plt.imshow(im,cmap='Greys_r')
#plt.imshow(im3,cmap='Greys_r')

for i in range(0, len(contours)):
    if (i % 2 == 0):
       cnt = contours[i]
       #mask = np.zeros(im2.shape,np.uint8)
       #cv2.drawContours(mask,[cnt],0,255,-1)
       x,y,w,h = cv2.boundingRect(cnt)
       cv2.rectangle(im,(x,y),(x+w,y+h),(255,255,0),5)
       plt.imshow(im,cmap='Greys_r')
       cv2.imwrite(str(i)+'.png', im)

cv2.destroyAllWindows()

And the output is attached below: As you see, small boxes are being made and its not super clear too.

output

1
'most of the examples' what do mean by this? And what is it that you want?Jeru Luke
I mean that the tutorials I find online foucs on detecting one rectangle containing the maximum pixel intensities. I want to put bounding boxes on both the persons in both the imagesGKS
As a starting point you should search for contour bounding box binary image and get some code here, The idea is to threshold the images and find contours and then bounding boxesZdaR
@ZdaR done that! The output is messy.GKS
@JeruLuke the image come from a cascade of processes, and the image I attached is the final output. I have to use the final output onlyGKS

1 Answers

0
votes

The real problem in your question lies in selection of the optimal threshold from the monochrome image.

In order to do that, calculate the median of the gray scale image (the second image in your post). The threshold level will be set 33% above this median value. Any value below this threshold will be binarized.

This is what I got:

enter image description here

Now performing morphological dilation followed by contour operations you can highlight your region of interest with a rectangle.

Note:

Never set a manual threshold as you did. Threshold can vary for different images. Hence always opt for a threshold based on the median of the image.