我有这张具有3通道RGB(VARI指数计算的结果)的图像,我想在植物周围绘制边界框(矩形),此处以绿色表示。使用OpenCV / python的最佳和最简便的方法是什么?
对于OpenCV专家来说,这是一个容易解决的问题,但是我找不到在线的好教程来针对多个对象进行此操作。
我找到的最近的教程是: determining-object-color-with-opencv
边界框的假设应该/应该是:
谢谢!
答案 0 :(得分:0)
您需要进行HSV过滤
有关执行前2个代码的信息,请参见此页面
https://pythonprogramming.net/color-filter-python-opencv-tutorial/
请参阅https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html
import cv2
import numpy as np
img = cv2.imread('8FGo1.jpg',1)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_red = np.array([45,100,50])
upper_red = np.array([75,255,255])
mask = cv2.inRange(hsv, lower_red, upper_red)
kernel = np.ones((5,5),np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
x,y,w,h = cv2.boundingRect(contour)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow('img',img)
#cv2.imshow('mask',mask)
输出
答案 1 :(得分:0)
在踩到以下资源后,只需回答我自己的问题即可:https://docs.opencv.org/3.4/da/d0c/tutorial_bounding_rects_circles.html
可能不是最好的答案,但它以某种方式解决了我的问题!
import cv2
import numpy as np
image = cv2.imread('vari3.png')
# https://www.pyimagesearch.com/2016/02/15/determining-object-color-with-opencv/
# https://docs.opencv.org/3.4/da/d0c/tutorial_bounding_rects_circles.html
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
# mask: green is dominant.
thresh = np.array((image.argmax(axis=-1) == 1) * 255, dtype=np.uint8)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
contours_poly = [None] * len(cnts)
boundRect = [None] * len(cnts)
for i, c in enumerate(cnts):
contours_poly[i] = cv2.approxPolyDP(c, 3, True)
boundRect[i] = cv2.boundingRect(contours_poly[i])
for i in range(len(cnts)):
# cv2.drawContours(image, contours_poly, i, (0, 255, 0), thickness=2)
pt1 = (int(boundRect[i][0]), int(boundRect[i][1]))
pt2 = (int(boundRect[i][0] + boundRect[i][2]), int(boundRect[i][1] + boundRect[i][3]))
if np.sqrt((pt2[1] - pt1[1]) * (pt2[0] - pt1[0])) < 30:
continue
cv2.rectangle(image, pt1, pt2, (0, 0, 0), 2)
cv2.imwrite('result.png', image)
cv2.imshow("Image", image)
cv2.waitKey(0)