Define sections in a color recognition program

Asked

Viewed 120 times

1

I am working on a project, which next year will become my TCC. I am learning the language of Python, and for now I have had some advances. I was reading about computer vision, but I still have some questions. I managed to formalize a program (after many video lessons in English kk) that displays the Webcam video, and tracks any object with the color blue. I am having difficulty in, in this same program, defining sections. These sections would be to locate whether a piece entered the "house" or not. I will use this for an old game, so I need to define a section of 3x3 (really a # on the screen) and with that I was able to identify if an object entered this house or not. Follow the code I have for now. In case anyone has any idea how to do this would be of great help!

import cv2   
import numpy as np

cap=cv2.VideoCapture(0)

while(1):
    ret, img = cap.read()

#converter imagem(img i.e BGR) para HSV (hue-saturation-value)

hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

#definir dimensão da cor Azul
blue_lower=np.array([99,115,150],np.uint8)
blue_upper=np.array([110,255,255],np.uint8)

#encontrar as dimensões da cor Azul
blue=cv2.inRange(hsv,blue_lower,blue_upper)

#transformação e processamento  
kernal = np.ones((5 ,5), "uint8")

blue=cv2.dilate(blue,kernal)
res1=cv2.bitwise_and(img, img, mask = blue)

#Tracking da cor azul
(_,contours,hierarchy)=cv2.findContours(blue,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
    area = cv2.contourArea(contour)
    if(area>300):
        x,y,w,h = cv2.boundingRect(contour) 
        img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        cv2.putText(img,"Blue color",(x,y),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,0,0))


cv2.imshow("Cor",img)   
k = cv2.waitKey(30) & 0xFF
if k==27:
    break

    cap.release()
    cv2.destroyAllWindows()
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.