terça-feira, 4 de junho de 2019

OpenCV e Convex Hull

Rapaaaaaaaaaaaz, eu vou postar esse código aqui porque eu não achei em nenhum canto na internet uma pagina mostrando uma demo de convex hull

Podem usar a vontade!
Nosso código pra mostrar mostrar na tela a imagem da webcam e o Convex Hull, no caso eu to fazendo de objetos da cor amarela ou da cor verde
import cv2
import numpy as np


#epsilon = 0.1*cv2.arcLength(cnt,True)
# approx = cv2.approxPolyDP(cnt,epsilon,True)

#criando uma funcao
def nothing(x):
pass

cap = cv2.VideoCapture(0)

def make_1080p():
cap.set(3, 1920)
cap.set(4, 1080)

def make_720p():
cap.set(3, 1280)
cap.set(4, 720)
def make_480p():
cap.set(3, 640)
cap.set(4, 480)

def change_res(width, height):
cap.set(3, width)
cap.set(4, height)

make_480p()

loweryellow = np.array([13, 118, 100])
upperyellow = np.array([35, 255, 255])

lowergreen = np.array([33, 79, 51])
uppergreen = np.array([81, 255, 255])

while True:
_, frame = cap.read()
blurred_frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(blurred_frame, cv2.COLOR_BGR2HSV)

#yellow
maskyellow = cv2.inRange(hsv, loweryellow, upperyellow)
cntyellow,hierarchyyellow = cv2.findContours(maskyellow, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
cntyellow = sorted(cntyellow, key=cv2.contourArea, reverse=True)
totalyellow = len(cntyellow)

#green
maskgreen = cv2.inRange(hsv, lowergreen, uppergreen)
cntgreen,hierarchygreen = cv2.findContours(maskgreen, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
cntgreen = sorted(cntgreen, key=cv2.contourArea, reverse=True)
totalgreen = len(cntgreen)

x, y, w, h, cor = -1, -1, -1, -1, "nenhuma"
areaYellow, areaGreen = 0, 0

if(totalyellow > 0):
hullyellow = cv2.convexHull(cntyellow[0])
cv2.drawContours(frame, [hullyellow], -1, (0, 232, 255), 4)
areaYellow = cv2.contourArea(hullyellow)
#agora isso aqui com certeza eh lixo, mas nao tem como saber a posicao sem isso
posxYellow,posyYellow,wYellow,hYellow = cv2.boundingRect(hullyellow)

if(totalgreen > 0):
hullgreen = cv2.convexHull(cntgreen[0])
cv2.drawContours(frame, [hullgreen], -1, (0, 255, 0), 4)
areaGreen = cv2.contourArea(hullgreen)
#agora isso aqui com certeza eh lixo, mas nao tem como saber a posicao sem isso
posxGreen,posyGreen,wGreen,hGreen = cv2.boundingRect(hullgreen)


cv2.imshow("frame", frame)
key = cv2.waitKey(1)
if key == 27:
break

if (areaGreen > areaYellow):
cor = "verde"
x = posxGreen
y = posyGreen
w = wGreen
h = hGreen
elif(areaGreen < areaYellow):
cor = "amarelo"
x = posxYellow
y = posyYellow
w = wYellow
h = hYellow
elif(areaGreen == 0 and areaYellow):
cor = "nenhuma"

print (cor)
cap.release()
cv2.destroyAllWindows()

Nosso código pra identificação de cor de caixa (se é verde ou amarela)
import cv2
import numpy as np

#criando uma funcao
def nothing(x):
pass

cap = cv2.VideoCapture(0)

def make_1080p():
cap.set(3, 1920)
cap.set(4, 1080)

def make_720p():
cap.set(3, 1280)
cap.set(4, 720)
def make_480p():
cap.set(3, 640)
cap.set(4, 480)

def change_res(width, height):
cap.set(3, width)
cap.set(4, height)

#make_480p()

def identificacor(img):
frame = img
blurred_frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(blurred_frame, cv2.COLOR_BGR2HSV)

loweryellow = np.array([13, 118, 100])
upperyellow = np.array([35, 255, 255])

lowergreen = np.array([33, 79, 51])
uppergreen = np.array([81, 255, 255])


#yellow
maskyellow = cv2.inRange(hsv, loweryellow, upperyellow)
cntyellow,hierarchyyellow = cv2.findContours(maskyellow, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
cntyellow = sorted(cntyellow, key=cv2.contourArea, reverse=True)
totalyellow = len(cntyellow)

#green
maskgreen = cv2.inRange(hsv, lowergreen, uppergreen)
cntgreen,hierarchygreen = cv2.findContours(maskgreen, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
cntgreen = sorted(cntgreen, key=cv2.contourArea, reverse=True)
totalgreen = len(cntgreen)

x, y, w, h, cor = -1, -1, -1, -1, "nenhuma"

if(totalyellow > 0):
hullyellow = cv2.convexHull(cntyellow[0])
cv2.drawContours(frame, [hullyellow], -1, (0, 232, 255), 4)
areaYellow = cv2.contourArea(hullyellow)
#agora isso aqui com certeza eh lixo, mas nao tem como saber a posicao sem isso
posxYellow,posyYellow,wYellow,hYellow = cv2.boundingRect(hullyellow)

if(totalgreen > 0):
hullgreen = cv2.convexHull(cntgreen[0])
cv2.drawContours(frame, [hullgreen], -1, (0, 255, 0), 4)
areaGreen = cv2.contourArea(hullgreen)
#agora isso aqui com certeza eh lixo, mas nao tem como saber a posicao sem isso
posxGreen,posyGreen,wGreen,hGreen = cv2.boundingRect(hullgreen)

if (areaGreen > areaYellow):
cor = "verde"
x = posxGreen
y = posyGreen
w = wGreen
h = hGreen
elif(areaGreen < areaYellow):
cor = "amarelo"
x = posxYellow
y = posyYellow
w = wYellow
h = hYellow
elif(areaGreen == 0 and areaYellow):
cor = "nenhuma"

w = w/2
h = h / 2
x = x + w
y = y + h

return cor, x, y, w, h

def identificacorimagem (imagem):
img = cv2.imread(imagem, cv2.IMREAD_COLOR)
cor, x, y, w, h = identificacor(img)
return cor, x, w, w, h


def identificacorimgcamera ():
make_480p()
_, frame = cap.read()
cor, x, y, w, h = identificacor(frame)
return cor, x, w, w, h


testecor, testex, testey, testew, testeh = identificacorimgcamera ()
print(testecor)

if testecor == "amarelo":

testecor, testex, testey, testew, testeh = identificacorimagem ('quad8.jpg')
print(testecor)


Referencias:
https://www.youtube.com/watch?v=nspSQSU_zvk&t=21s
https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_contours/py_contour_features/py_contour_features.html
https://www.pyimagesearch.com/2014/08/04/opencv-python-color-detection/
https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html

Nenhum comentário:

Postar um comentário