使用openCV python在相同颜色的图像周围绘制轮廓

时间:2020-10-18 06:44:26

标签: python opencv

我有这张具有3通道RGB(VARI指数计算的结果)的图像,我想在植物周围绘制边界框(矩形),此处以绿色表示。使用OpenCV / python的最佳和最简便的方法是什么?

对于OpenCV专家来说,这是一个容易解决的问题,但是我找不到在线的好教程来针对多个对象进行此操作。

我找到的最近的教程是: determining-object-color-with-opencv

边界框的假设应该/应该是:

  • 绿色是主要颜色。
  • 它应该超过X个像素。

谢谢!

VARI Index

2 个答案:

答案 0 :(得分:0)

您需要进行HSV过滤

  1. 将图像颜色从BGR更改为HSV(色相饱和度值)。
  2. 过滤与绿色匹配的一定范围的饱和度和色相 阈值化。

有关执行前2个代码的信息,请参见此页面

https://pythonprogramming.net/color-filter-python-opencv-tutorial/

  1. 进行一些形态学操作,例如侵蚀,膨胀,开放, 关闭以删除不代表树木的绿色小点 并连接看起来破碎的树木。

请参阅https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html

  1. 检测轮廓,然后绘制矩形
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)

输出

enter image description here

答案 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)

enter image description here