openmv4顏色識別


find_blobs函數:

通過find_blobs函數可以找到色塊.我們來討論一下,find_blobs的細節。

image.find_blobs(thresholds, roi=Auto, x_stride=2, y_stride=1, invert=False, area_threshold=10, 

pixels_threshold=10, merge=False, margin=0, threshold_cb=None, merge_cb=None)

這里的參數比較多。

  • thresholds是顏色的閾值,注意:這個參數是一個列表,可以包含多個顏色。如果你只需要一個顏色,那么在這個列表中只需要有一個顏色值,如果你想要多個顏色閾值,那這個列表就需要多個顏色閾值。注意:在返回的色塊對象blob可以調用code方法,來判斷是什么顏色的色塊。
  • red = (xxx,xxx,xxx,xxx,xxx,xxx)
    blue = (xxx,xxx,xxx,xxx,xxx,xxx)
    yellow = (xxx,xxx,xxx,xxx,xxx,xxx)
    
    img=sensor.snapshot()
    red_blobs = img.find_blobs([red])
    
    color_blobs = img.find_blobs([red,blue, yellow])
    

      

  • roi是“感興趣區”。

    left_roi = [0,0,160,240]
    blobs = img.find_blobs([red],roi=left_roi)

  • x_stride 就是查找的色塊的x方向上最小寬度的像素,默認為2,如果你只想查找寬度10個像素以上的色塊,那么就設置這個參數為10:

    blobs = img.find_blobs([red],x_stride=10)

  • y_stride 就是查找的色塊的y方向上最小寬度的像素,默認為1,如果你只想查找寬度5個像素以上的色塊,那么就設置這個參數為5:

    blobs = img.find_blobs([red],y_stride=5)

  • invert 反轉閾值,把閾值以外的顏色作為閾值進行查找
  • area_threshold 面積閾值,如果色塊被框起來的面積小於這個值,會被過濾掉
  • pixels_threshold 像素個數閾值,如果色塊像素數量小於這個值,會被過濾掉
  • merge 合並,如果設置為True,那么合並所有重疊的blob為一個。
    注意:這會合並所有的blob,無論是什么顏色的。如果你想混淆多種顏色的blob,只需要分別調用不同顏色閾值的find_blobs。
  • all_blobs = img.find_blobs([red,blue,yellow],merge=True)
    
    red_blobs = img.find_blobs([red],merge=True)
    blue_blobs = img.find_blobs([blue],merge=True)
    yellow_blobs = img.find_blobs([yellow],merge=True)
    

      

  • margin 邊界,如果設置為1,那么兩個blobs如果間距1一個像素點,也會被合並。

閾值:

一個顏色閾值的結構是這樣的:

red = (minL, maxL, minA, maxA, minB, maxB)

元組里面的數值分別是L A B 的最大值和最小值。

顏色閾值選擇工具:

OpenMV 的IDE里加入了閾值選擇工具,極大的方便了對於顏色閾值的調試。

首先運行hello world.py讓IDE里的framebuffer顯示圖案。

然后打開 工具 →機器視覺 → 閾值編譯器

點擊 Frame Buffer可以獲取IDE中的圖像,Image File可以自己選擇一個圖像文件。

拖動六個滑塊,可以實時的看到閾值的結果,我們想要的結果就是,將我們的目標顏色變成白色,其他顏色全變為黑色。

 

單顏色識別之紅色:

import sensor, image, time

#thresholds = [(66, 0, -51, -8, 6, 127)]  #green
thresholds = [(30, 100, 15, 127, 15, 127)]  #red
#thresholds = [(66, 0, -51, -8, 6, 127),(30, 100, 15, 127, 15, 127)]  #元組里面,包含列表,進行多種顏色識別



sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)     #關掉白平衡和自動增益
clock = time.clock()                # Create a clock object to track the FPS.

while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.
    #for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200,merge=True):
    #for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200):
    for blob in img.find_blobs([thresholds[0]],pixels_threshold=200,area_threshold=200):
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(),blob.cy())
    print(clock.fps())              # Note: OpenMV Cam runs about half as fast when connecte

 現象:

 

單顏色識別之綠色:

import sensor, image, time

thresholds = [(66, 0, -51, -8, 6, 127)]  #green
#thresholds = [(30, 100, 15, 127, 15, 127)]  #red
#thresholds = [(66, 0, -51, -8, 6, 127),(30, 100, 15, 127, 15, 127)]  #元組里面,包含列表,進行多種顏色識別



sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)     #關掉白平衡和自動增益
clock = time.clock()                # Create a clock object to track the FPS.

while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.
    #for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200,merge=True):
    #for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200):
    for blob in img.find_blobs([thresholds[0]],pixels_threshold=200,area_threshold=200):
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(),blob.cy())
    print(clock.fps())              # Note: OpenMV Cam runs about half as fast when connected

  現象:

 

多顏色識別之合並:

import sensor, image, time

#thresholds = [(66, 0, -51, -8, 6, 127)]  #green
#thresholds = [(30, 100, 15, 127, 15, 127)]  #red
thresholds = [(66, 0, -51, -8, 6, 127),(30, 100, 15, 127, 15, 127)]  #元組里面,包含列表,進行多種顏色識別



sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)     #關掉白平衡和自動增益
clock = time.clock()                # Create a clock object to track the FPS.

while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.
    for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200,merge=True):
    #for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200):
    #for blob in img.find_blobs([thresholds[0]],pixels_threshold=200,area_threshold=200):
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(),blob.cy())
    print(clock.fps())              # Note: OpenMV Cam runs about half as fast when connected

  現象:

 

多顏色識別之不合並:

import sensor, image, time

#thresholds = [(66, 0, -51, -8, 6, 127)]  #green
#thresholds = [(30, 100, 15, 127, 15, 127)]  #red
thresholds = [(66, 0, -51, -8, 6, 127),(30, 100, 15, 127, 15, 127)]  #元組里面,包含列表,進行多種顏色識別



sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)     #關掉白平衡和自動增益
clock = time.clock()                # Create a clock object to track the FPS.

while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.
    #for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200,merge=True):
    for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200):
    #for blob in img.find_blobs([thresholds[0]],pixels_threshold=200,area_threshold=200):
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(),blob.cy())
    print(clock.fps())              # Note: OpenMV Cam runs about half as fast when connected

  現象:

 

 

 

正是步行者,一步步登峰!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM