gpt4 book ai didi

python - 如何执行条件指令以识别 Open CV 中较大轮廓内的轮廓

转载 作者:太空宇宙 更新时间:2023-11-03 22:38:36 25 4
gpt4 key购买 nike

我正在尝试识别更大轮廓内的轮廓,因为我试图做到这一点,当白色在黄色内时,程序会打印一条警告,提示已检测到物体,我该如何做到这一点工作?

def dibujaramarillo(maskamarillo,color):


_,contornos,hierarchy_=cv2.findContours(maskamarillo, cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)#Procedemos a encontrar los contornos de nuestra mascara en HSV con contornos externos
#y un contorno simple por vertices
for c in contornos: #para ello recorremos 1 por 1 "contornos" y almacenamos en la variable "c"
area = cv2.contourArea(c) #Guardamos en "area" los contornos recorridos con la funcion mostrada
if area > 1000: #si esa area de captura es mayor de 3000 lo va a tomar como positivo y lo contorneará

nuevocontorno = cv2.convexHull(c) #con convexHull suavizamos nuestros contornos y eliminamos el ruido
cv2.drawContours(frame, [nuevocontorno], 0, color,3)
#dibujamos los contornos en nuestro video con las caracteristicas establecidas

Yellow Taxi and inside white color

最佳答案

你可以使用 hsv 蒙版图像,首先蒙版黄色:

enter image description here

比得到轮廓和 mask 白色:

enter image description here

import cv2
import numpy as np

img = cv2.imread("2NrfJ.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_hsv = np.array([10, 100, 100])
upper_hsv = np.array([34, 255, 255])
mask = cv2.inRange(hsv, lower_hsv, upper_hsv)
contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
if cv2.contourArea(contour ) >800: # filter small contours
x,y,w,h = cv2.boundingRect(contour )
cv2.rectangle(hsv,(x,y),(x+w,y+h),(0,255,0),2)
hsv_contour = hsv[y:y+h,x:x+w]
if np.array(cv2.mean(hsv_contour)).astype(np.uint8)[0] in range(30,40) \
and np.array(cv2.mean(hsv_contour)).astype(np.uint8)[1] in range(100,150)\
and np.array(cv2.mean(hsv_contour)).astype(np.uint8)[2] in range(140,160):
cv2.imshow('cutted contour',img[y:y+h,x:x+w])
cv2.imshow("mask", mask)
print("TAXI HAS BEEN RECOGNIZED")
cv2.waitKey(0)

关于python - 如何执行条件指令以识别 Open CV 中较大轮廓内的轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56095122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com