Day1-PM


learn the following

  • python introduction
  • basic programs
    • file IO/camera IO
    • blur, morphology
    • masking of simple color

python introduction

  • Indentation is very important in Python programming. Indentation level is used to determine the coding block (the grouping of statements) and scope of variables.
  • Variable is accessible from same block or nested block. Variable doesn't need to declare before using. Type of variable is determined by value to be assigned. Variable declared "global" has globally scope.
  • A comment starts with a hash character (#)

py.png

>python ./basic.py
0:0
1:1
2:3
3:6
4:10
5:15
6:21
7:28
8:36
9:45
sum is between 30 and 50

basic programs

About color, file IO, blur, morphology, and masking of simple color

  • Digital color image is usually constructed with 3 color components, Red, Green, Blue. But, in case of object detection from image, it is not convenient to use RGB color component. Therefore, HSV components is often utilized (H means Hue. S means Saturation. V means Value (luminance)).
  • In Python programming, you can import the image, separate color components, convert to HSV, with following very simple code by using OpenCV package.
  • Line 134 (import the image)
    img = cv2.imread("./objects.jpg")
  • Line 154 to 162 (separate color components)
    • Variable img is arrayed in a "height" x "width" x "the number of color channels" matrix. In the RGB image, color channels are arranged in order B, G, R.
  • Line 170 (convert to HSV (Hue, Saturation, Value(Brightness))
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  • Execute the Sample01 program, and click any position on the target image, you can observe the HSV values ​​in around area of clicked position.

web camera IO

To capture video from cameras, we can use VideoCapture Class. It is included in the module “cv2: OpenCV 3.2.0”. (cf. Getting Started with Videos @ OpenCV 3.2.0)

  • Line 01-02 (import module “cv2:OpenCV” and “numpy”)
    import cv2
    import numpy as np
    • “np” is an alias pointing to numpy
  • Line 04 (create a VideoCapture object)
    cap = cv2.VideoCapture(0)
    • “0” is Device index (Second camera’s index is 1)
  • Line 07 (capture a frame)
    ret, frame = cap.read()
    • “ret” is bool (If frame is read correctly, it will be ‘Ture’)
    • “frame” is a image data (numpy.ndarray object)
  • Line 09 (display the image on a window)
    cv2.imshow(‘video frame’, frame)
    • ‘video frame’ is window name
    • If the same name window exists, overwrite the image
  • Line 06 (create a loop, and capture frame-by-frame)
    while cap.isOpened():
    • “cap.isOpened()” can check whether “cap (VideoCapture object)” is initialized or not
    • If it is initialized, this loop becomes infinite loop
  • Line 11-12 (stop the loop when pressing the ‘q’ key)
    if cv2.waitKey(1) & 0xFF == ord(‘q’):
      break
    • “cv2.waitKey(1)” waits for a key event for 1 millisecond, and return the code of the pressed key (or -1 if no key was pressed)
    • “cv2.waitKey(1) & 0xFF” is just used to mask off the last 8bits (& means AND)
    • “ord(‘q’)” return the code of ‘q’ key
  • We can add some image processing at line 08.
    ex.
      ret, frame = cap.read()
      gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)   # <- change color space (RGB2GRAY)
      cv2.imshow(‘video frame’, gray)   # <- display the gray image

FrontPage
Day1-PM
Day2
Day4
Day5-AM
Day5-PM


  • counter: 108
  • today: 1
  • yesterday: 0
  • online: 1

edit