【问题标题】:Rectangular bounding boxes around objects in monochrome images in python?python中单色图像中对象周围的矩形边界框?
【发布时间】:2017-02-23 16:37:26
【问题描述】:

我有一组两个单色图像 [附加],我想在每个图像中为两个人放置矩形边界框。我知道 cv2.dilate 可能会有所帮助,但我看到的大多数示例都集中在检测一个包含最大像素强度的矩形,所以基本上他们在图像中放置了一个大矩形。我想要两个独立的矩形。

更新: 这是我的尝试:

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()

输出如下:如您所见,正在制作小盒子,但也不是很清楚。

【问题讨论】:

  • '大部分例子'这是什么意思?你想要什么?
  • 我的意思是我在网上找到的教程专注于检测一个包含最大像素强度的矩形。我想在两个图像中的两个人上放置边界框
  • 作为起点,您应该搜索contour bounding box binary image 并在此处获取一些代码,其想法是对图像进行阈值化并找到轮廓,然后找到边界框
  • @ZdaR 做到了!输出很乱。
  • @JeruLuke 图像来自级联进程,我附加的图像是最终输出。我只需要使用最终输出

标签: python opencv scikit-image


【解决方案1】:

您问题的真正问题在于从单色图像中选择最佳阈值

为此,请计算灰度图像(帖子中的第二张图像)的中值。 阈值水平将设置为高于此中值33%。低于此阈值的任何值都将被二值化。

这是我得到的:

现在执行形态膨胀和轮廓操作,您可以用矩形突出显示您感兴趣的区域。

注意:

永远不要像以前那样设置手动阈值。对于不同的图像,阈值可能会有所不同。因此,请始终根据图像的median选择阈值。

【讨论】:

  • 仍然没有为每个人获得一个矩形。它只为右边的人提供一个矩形
  • 近似值是你最好的选择,给定这样的图像
猜你喜欢
  • 2012-03-20
  • 2015-02-13
  • 2010-11-12
  • 2021-01-03
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
相关资源
最近更新 更多