Jump to content
admin

OpenCV. #2 Распознавание цветных объектов

Recommended Posts

Официальный поставщик Raspberry Pi в России: https://amperkot.ru/msk/catalog/mini-pc/raspberry-pi/
 
В ролике разберемся, как распознавать цветной объект в кадре.
Прошлый урок, с первыми шагами в OpenCV
Видео с конструктором робота

Скрипт из видео: 

# -*- coding: utf-8 -*-
import cv2
import video
import numpy
if __name__ == '__main__':
    def nothing(*arg):
        pass
    
    cv2.namedWindow( "out_window" )
    cap = video.create_capture(0)
    
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
    
    while True:
        flag, img = cap.read()
        
        height, width = img.shape[:2]
        edge = 10
        
        low_blue = numpy.array((90,20,20), numpy.uint8)
        high_blue = numpy.array((150,255,255), numpy.uint8)
        try:
            img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
            mask_blue = cv2.inRange(img_hsv,low_blue, high_blue)
            
            #result = cv2.bitwise_and(img_hsv,img_hsv,mask = mask_blue)
            #result = cv2.cvtColor(result, cv2.COLOR_HSV2BGR)
            
            moments = cv2.moments(mask_blue, 1)
            
            dM01 = moments['m01']
            dM10 = moments['m10']
            dArea = moments['m00']
  
            x=0
            
            if dArea > 150:
                x = int(dM10 / dArea)
                y = int(dM01 / dArea)
                cv2.circle(img, (x, y), 10, (255,0,0), -1)
            
            if (x>(width/2+edge)) and x!=0:
                cv2.rectangle(img, (0,0), (30,height), (0,255,0), -1)
            if (x<(width/2-edge)) and x!=0:
                cv2.rectangle(img, (width-30,0), (width,height), (0,255,0), -1)
                
            cv2.imshow("out_window", img)
        except:
            cap.release()
            raise
 
        ch = cv2.waitKey(50)
        #для выхода надо нажать esc
        if ch == 27:
            break
    cap.release()
    cv2.destroyAllWindows()

 

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...