openCV

OpenCV的全稱是Open Source Computer Vision Library,是一個跨平台的電腦視覺庫。OpenCV是由英特爾公司發起並參與開發,以BSD授權條款授權發行,可以在商業和研究領域中免費使用。OpenCV可用於開發即時的圖像處理、電腦視覺以及圖型識別程式。
  1. Raspberry Pi安裝OpenCV

    sudo apt-get install libopencv-dev
    on Ubuntu: sudo apt-get install python3-opencv


    A.安裝 OpenCV 相關軟體。 步驟 1:先安裝好作業系統。
    可參考 安裝Linux 在Raspberry Pi4。
    步驟2:更新當前安裝的軟體。
    $ sudo apt-get update
    $ sudo apt-get upgrate
    步驟3:安裝 OpenCV 編譯所需的軟體。
    $ sudo apt-get install cmake build-essential pkg-config git
    步驟4:安裝常用圖像和視頻格式的支援軟體。
    $ sudo apt-get install libjpeg-dev libtiff-dev libjasper-dev libpng-dev libwebp-dev libopenexr-dev
    $ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libdc1394–22-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
    步驟5:安裝 OpenCV 界面所需的軟體。
    $ sudo apt-get install libgtk-3-dev libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5
    步驟6:安裝提高 OpenCV 運行速度的軟體。
    $ sudo apt-get install libatlas-base-dev liblapacke-dev gfortran
    步驟7:安裝 HDF5軟體。
    $ sudo apt-get install libhdf5-dev libhdf5–103
    步驟8:安裝 Python。
    $ sudo apt-get install python3-dev python3-pip python3-numpy
  2. OpenCV函數

    • imread("file")
    # coding=big5
    # Python program to use openCV functions
    import cv2
    src = cv2.imread("square-1.png")  #read-in an image file
    gray = cv2.imread("square-1.png",cv2.IMREAD_GRAYSCALE)  #convert it to a gray scale
    print(type(src))
    print(len(src))
    print(src.shape)
    (x,y,z)=src.shape
    print((x,y,z))
    for i in range(0,x,4):
        if(src[i][50][0] > 233 ): cc='White'
        elif(src[i][50][0] > 163 and src[i][50][2] < 220 ): cc='BLUE'
        else: cc='Black'
        print(i,50,src[i][50][0],src[i][50][1],src[i][50][2], cc)
    
    
    160
    (160, 202, 3)
    (160, 202, 3)
    0 50 255 255 255 White
    4 50 255 255 255 White
    8 50 255 255 255 White
    12 50 255 255 255 White
    16 50 255 255 255 White
    20 50 255 255 255 White
    24 50 255 255 255 White
    28 50 255 255 255 White
    32 50 0 0 0 Black
    36 50 0 0 0 Black
    40 50 232 162 0 BLUE
    44 50 232 162 0 BLUE
    48 50 232 162 0 BLUE
    52 50 232 162 0 BLUE
    56 50 232 162 0 BLUE
    60 50 232 162 0 BLUE
    64 50 232 162 0 BLUE
    68 50 232 162 0 BLUE
    72 50 232 162 0 BLUE
    76 50 232 162 0 BLUE
    80 50 232 162 0 BLUE
    84 50 232 162 0 BLUE
    88 50 232 162 0 BLUE
    92 50 232 162 0 BLUE
    96 50 232 162 0 BLUE
    100 50 232 162 0 BLUE
    104 50 232 162 0 BLUE
    108 50 232 162 0 BLUE
    112 50 232 162 0 BLUE
    116 50 232 162 0 BLUE
    120 50 0 0 0 Black
    124 50 0 0 0 Black
    128 50 255 255 255 White
    132 50 255 255 255 White
    136 50 255 255 255 White
    140 50 255 255 255 White
    144 50 255 255 255 White
    148 50 255 255 255 White
    152 50 255 255 255 White
    156 50 255 255 255 White
    
  3. RaspberryOpenCV 範例程式

    
    
    # Python program to use openCV functions
    import cv2
    src = cv2.imread("star.jpg")  #read-in image
    # Using cv2.COLOR_BGR2GRAY  convert to gray scale
    image = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY )
    cv2.imwrite("star_gray.jpg", image)  #write image to a new file
    # cv2.rectangle() method  draw a rectange
    start_point = (5, 5)
    end_point = (220, 220)
    color = (255, 0, 0) # Blue color in BGR
    thickness = 4  # Line thickness of 4 px
    image = cv2.rectangle(src, start_point, end_point, color, thickness)
    image = cv2.rectangle(image, (100,100),(400,400), (0,0,255), thickness)
    cv2.imwrite("star_rect.jpg", image)
    #---------------------------------------------------------
    # put 2 images in parallel(hstack) and serial(vstack)
    import numpy as np
    a=cv2.imread("star.jpg")
    b=cv2.imread("star_rect.jpg")
    c=np.hstack((a,b))
    d=np.vstack((a,b))
    cv2.imwrite("star_hstack.jpg", c)
    cv2.imwrite("star_vstack.jpg", d)