简体   繁体   English

如何在图像上找到仅棕色区域的轮廓?

[英]How to find contours of only brown areas on an image?

I have been trying to find contours of brown areas on my frame.我一直试图在我的框架上找到棕色区域的轮廓。

框架

I tried to find these contours in a for loop.我试图在for循环中找到这些轮廓。

    for c in cntrs:
        area = cv2.contourArea(c)
        if area > 1200:   # i obtanied threshold with trial end error
            x, y, w, h = cv2.boundingRect(c) #find coordinates of brown area
            roi2 = results[y:y + h, x:x + w] #copy these parts to the another image

but i can see only one contour as result但结果我只能看到一个轮廓

结果

i couldn't understand why i see only one contour.我不明白为什么我只看到一个轮廓。 because when i print number of thresholded contours i see 2因为当我打印阈值轮廓的数量时,我看到2

here is what i tried这是我试过的

import cv2
import numpy as np
cv2.destroyAllWindows()
capture = cv2.VideoCapture(0) ##masaustunde 1, laptopta 0


def nothing(x):
    pass

roi = cv2.imread('savedImage.jpg')
cv2.imshow('input', roi)
hsv = cv2.cvtColor(roi, cv2.COLOR_RGB2HSV)

lower_green = np.array([0, 0, 0])
upper_green = np.array([40, 255, 255])

mask = cv2.inRange(hsv, lower_green, upper_green)
mask_inv = cv2.bitwise_not(mask)

fg = cv2.bitwise_and(roi, roi, mask=mask_inv)

gray = cv2.cvtColor(fg, cv2.COLOR_BGR2GRAY)
gray = cv2.cvtColor(fg, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 32, 255, 0)[1]
cntrs = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]
area_thresh = 0
syc = 0
results = fg.copy()
for c in cntrs:
    area = cv2.contourArea(c)
    if area > 1200: #esige dikkat
        syc = syc + 1
        x, y, w, h = cv2.boundingRect(c)
        roi2 = results[y:y + h, x:x + w]
cv2.imshow('roi2', roi2)

cv2.waitKey(0)


capture.release()  # release the camera from video capture
cv2.destroyAllWindows()

You are writing over the results (roi2) in your loop without waiting to view each one.您正在循环中写入结果 (roi2),而无需等待查看每个结果。 You have to put the waitKey() into the loop.您必须将 waitKey() 放入循环中。

Also as I said before, your area threshold was too high.同样正如我之前所说,您的区域阈值太高了。

I also chose better lower and upper thresholds.我还选择了更好的上下阈值。 (I save the hsv file and then measured its values in GUI tool) (我保存了 hsv 文件,然后在 GUI 工具中测量了它的值)

This works for me in Python/OpenCV.这在 Python/OpenCV 中对我有用。 Press the return key after each roi2 is displayed to go on to the next one.显示每个 roi2 后按返回键继续下一个。

import cv2
import numpy as np

roi = cv2.imread('green_areas.jpg')
#cv2.imshow('input', roi)

hsv = cv2.cvtColor(roi, cv2.COLOR_RGB2HSV)
#cv2.imshow('hsv', hsv)

lower_green = np.array([30, 30, 30])
upper_green = np.array([60, 140, 100])

mask = cv2.inRange(hsv, lower_green, upper_green)
#cv2.imshow('mask', mask)

mask_inv = cv2.bitwise_not(mask)

fg = cv2.bitwise_and(roi, roi, mask=mask_inv)
#cv2.imshow('fg', fg)

gray = cv2.cvtColor(fg, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 32, 255, 0)[1]
#cv2.imshow('thresh', thresh)

cntrs = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]
area_thresh = 0
syc = 0
results = fg.copy()
for c in cntrs:
    area = cv2.contourArea(c)
    if area > 500: #esige dikkat
        syc = syc + 1
        x, y, w, h = cv2.boundingRect(c)
        roi2 = results[y:y + h, x:x + w]
        cv2.imshow('roi2', roi2)
        cv2.waitKey(0)

cv2.destroyAllWindows()

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

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