[英]Rectangular bounding boxes around objects in monochrome images in python?
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.
我知道cv2.dilate可能会有所帮助,但我看到的大多数示例都集中在检测包含最大像素强度的一个矩形,因此从本质上讲,它们在图像中放置了一个大矩形。 I would like to have two separate rectangles.
我想有两个单独的矩形。
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. 输出附在下面:如您所见,正在制作小盒子,它也不是很清晰。
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.
阈值水平将设置为高于此中间值33% 。 Any value below this threshold will be binarized.
低于此阈值的任何值将被二值化。
This is what I got: 这就是我得到的:
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.
因此,始终选择基于图像中位数的阈值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.