简体   繁体   English

opencv 边界框问题

[英]opencv bounding box issue

I have an image I need to draw a bounding box around and I'm trying to use the code at bottom of this post.我有一张需要在周围绘制边界框的图像,并且我正在尝试使用本文底部的代码。 The issue I have is that I have tried to blur the blue box shape to remove its details eg我遇到的问题是我试图模糊蓝色框形状以删除其细节,例如

cv2.blur(img,(20,20)) 

but the blurred image doesnt seem to have a good enough edge to produce a bounding box.但是模糊的图像似乎没有足够好的边缘来产生边界框。

I have found that if I use my code below with a plain blue square with sharp edges of the same size as the image below, it works just fine.我发现,如果我在下面的代码中使用带有与下图相同大小的锐利边缘的纯蓝色正方形,它就可以正常工作。 It seems the detail within the blue area stops a boundary box being drawn.蓝色区域内的细节似乎阻止了边界框的绘制。

I was hoping someone could tell me how to get this to work please.我希望有人能告诉我如何让它工作。

在此处输入图像描述

import cv2

# Load the image - container completely blurred out so no hinges,locking bars , writing etc are visible, just a blank blue square
img = cv2.imread('blue_object.jpg')

# convert to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

edged = cv2.Canny(img, 120,890)
# Apply adaptive threshold
thresh = cv2.adaptiveThreshold(edged, 255, 1, 1, 11, 2)
thresh_color = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)

# apply some dilation and erosion to join the gaps - change iteration to detect more or less area's
thresh = cv2.dilate(thresh,None,iterations = 50)
thresh = cv2.erode(thresh,None,iterations = 50)

# Find the contours
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

# For each contour, find the bounding rectangle and draw it
for cnt in contours:
    area = cv2.contourArea(cnt)
    if area > 50000:
        x,y,w,h = cv2.boundingRect(cnt)
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)


cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

You're converting to gray, throwing all that valuable color information away.您正在转换为灰色,丢弃所有有价值的颜色信息 You're also Canny-ing, which is generally a bad idea.你也是个精明的人,这通常是个坏主意。 Beginners don't have the judgment to apply Canny sensibly.初学者没有明智地应用 Canny 的判断力。 Best stay away from it.最好远离它。

This can be solved with the usual approach:这可以通过通常的方法解决:

  • transform colorspace into something useful, say HSV将色彩空间转换成有用的东西,比如 HSV
  • inRange on well-saturated blue饱和度良好的蓝色上的inRange
  • some morphology operations to clean up debris清理碎片的一些形态学操作
  • bounding box边界框

That is assuming you are looking for a blue container, or any well-saturated color really.那是假设您正在寻找一个蓝色的容器,或者任何饱和度很高的颜色。

im = cv.imread("i4nPk.jpg")
hsv = cv.cvtColor(im, cv.COLOR_BGR2HSV)

lower = (90, 84, 0)
upper = (180, 255, 255)
mask1 = cv.inRange(hsv, lower, upper)

mask2 = cv.erode(mask1, kernel=None, iterations=2)

(x, y, w, h) = cv.boundingRect(mask2) # yes that works on masks too

canvas = cv.cvtColor(mask2, cv.COLOR_GRAY2BGR)
cv.rectangle(canvas, (x,y), (x+w, y+h), color=(0,0,255), thickness=3)

输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM