Python + opencv for Dynamic Object Recognition, pythonopencv

Source: Internet
Author: User

Python + opencv for Dynamic Object Recognition, pythonopencv

Note: This method is very affected by light changes.

Figure of the result of your mobile phone shaking at home:

Source code:

#-*-Coding: UTF-8-*-"Created on Wed Sep 27 15:47:54 2017 @ author: tina" import cv2 import numpy as np camera = cv2.VideoCapture (0) # parameter 0 indicates the first camera # determine whether the video is enabled if (camera. isOpened (): print ('open') else: print ('camera not Open ') # for testing, view video size = (int (camera. get (cv2.CAP _ PROP_FRAME_WIDTH), int (camera. get (cv2.CAP _ PROP_FRAME_HEIGHT) print ('size: '+ repr (size) es = cv2.getStructuringElement (cv2.MORP) H_ELLIPSE, (9, 4) kernel = np. ones (5, 5), np. uint8) background = None while True: # Read the video stream grabbed, frame_lwpCV = camera. read () # pre-process the frame, first convert the grayscale image, and then perform Gaussian filtering. # Reasons for fuzzy processing using Gaussian filter: each input video will produce noise due to natural vibration, illumination changes, or camera itself. The noise is smoothed to avoid detection during motion and tracking. Direction = cv2.cvtColor (frame_lwpCV, cv2.COLOR _ BGR2GRAY) gray_lwpCV = direction (gray_lwpCV, (21, 21), 0) # set the first frame to the entire input background if background is None: background = gray_lwpCV continue # For each frame read from the background, the difference between the frame and Beijing is calculated and a difference map (different map) is obtained ). # You also need to apply a threshold value to get a black-and-white image, and use the following code to expand the image so that the hole and defect (imperfection) perform normalization diff = cv2.absdiff (background, gray_lwpCV) diff = cv2.threshold (diff, 148,255, cv2.THRESH _ BINARY) [1] # diff = cv2.dilate (diff, es, iterations = 2) # morphological expansion # display Rectangular Box image, contours, hierarchy = cv2.findContours (diff. copy (), cv2.RETR _ EXTERNAL, cv2.CHAIN _ APPROX_SIMPLE) # Calculate the target contour of an image for c in contours: if cv2.contourA Rea (c) <1500: # For the rectangular area, only the outlines greater than the given threshold are displayed, so some minor changes are not displayed. For cameras with unchanged illumination and low noise, do not set the minimum contour size threshold (continue (x, y, w, h) = cv2.boundingRect (c) # This function calculates the rectangle boundary box cv2.rectangle (frame_lwpCV, (x, y), (x + w, y + h), (0,255, 0), 2) cv2.imshow ('second s', frame_lwpCV) cv2.imshow ('dis ', diff) key = cv2.waitKey (1) & 0xFF # Press 'q' to exit the loop if key = ord ('q'): break # When everything done, release the capture camera. release () cv2.destroyAllWindows ()

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.