openmv4形狀識別


本次實現了openmv4圓形、矩形、直線的形狀識別

 

圓形識別:

代碼如下:x_margin保持不變,更改threshold的閾值

1、threshold=500,x_margin=10

import sensor, image, time

sensor.reset()                      #復位攝像頭
sensor.set_pixformat(sensor.RGB565) # grayscale is faster
sensor.set_framesize(sensor.QQVGA)  #160*120
sensor.skip_frames(time = 2000)
clock = time.clock()

while(True):
    clock.tick()                    #捕獲幀率
    img = sensor.snapshot().lens_corr(1.8)      #畸變矯正,但是會影響幀率

# Circle對象有四個值: x, y, r (半徑), 和 magnitude。
    # magnitude是檢測圓的強度。越高越好

    # roi 是一個用以復制的矩形的感興趣區域(x, y, w, h)。x和y是以左下角為圓心的(0,0)
    # ROI 即圖像矩形。操作范圍僅限於roi區域內的像素。

    # x_stride 是霍夫變換時需要跳過的x像素的數量。若已知圓較大,可增加
    # x_stride 。簡述為有效兩點之間的距離跨度

    # y_stride 是霍夫變換時需要跳過的y像素的數量。若已知直線較大,可增加
    # y_stride 。

    # threshold 控制從霍夫變換中監測到的圓。只返回大於或等於閾值的圓。
    # 應用程序的閾值正確值取決於圖像。注意:一條圓的大小是組成圓所有
    # 索貝爾濾波像素大小的總和。

    # x_margin 控制所檢測的圓的合並。 圓像素為 x_margin 、 y_margin 和
    # r_margin的部分合並。

    #以設定的x_margin為邊界,大於它的合並為一個圓,小於它則消失

    # y_margin 控制所檢測的圓的合並。 圓像素為 x_margin 、 y_margin 和
    # r_margin 的部分合並。

    # r_margin 控制所檢測的圓的合並。 圓像素為 x_margin 、 y_margin 和
    # r_margin 的部分合並。

    # r_min,r_max和r_step控制測試圓的半徑。
    # threshold = 3500比較合適。如果視野中檢測到的圓過多,請增大閾值;
    # 相反,如果視野中檢測到的圓過少,請減少閾值。

    #注意:物體閾值大於設定的threshold,或物體的像素大於x—_margin才能被框中(y_margin和r_margin理論上也行,但是實現不了)

    for c in img.find_circles(threshold = 2000, x_margin = 10, y_margin = 10, r_margin = 10,
            r_min = 10, r_max = 100, r_step = 2):
        img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
        print(c)

    #image.find_circles[roi,x_stride=2,y_stride=1,threshold=2000,x_margin=10,y_margin=10,r_margin=10,r_min=2,r_max,r_step=2)
    #返回圓的x位置。返回圓的y位置。返回圓的半徑。返回圓的模(magnitude)。----對應於c

    print("FPS %f" % clock.fps())

運行結果:密密麻麻的圓

 

2、threshold=2000,x_margin=10

運行結果:識別了兩個圓

 

3、threshold=3000,x_margin=10

運行結果:只識別了一個圓

 

threshold的閾值,更改x_margin:

1、x_margin=10,threshold=2000

運行結果:識別了兩個圓

2、x_margin=1000,threshold=2000

運行結果:只識別了一個圓

修改threshold或x_margin都能改變識別的范圍

 

矩形識別:

代碼如下:

import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.RGB565) # 灰度更快(160x120 max on OpenMV-M7)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()

    # 下面的`threshold`應設置為足夠高的值,以濾除在圖像中檢測到的具有
    # 低邊緣幅度的噪聲矩形。最適用與背景形成鮮明對比的矩形。

    for r in img.find_rects(threshold = 10000):
        img.draw_rectangle(r.rect(), color = (255, 0, 0))
        for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
        print(r)

    print("FPS %f" % clock.fps())

運行結果:將矩形和它的 四個角分別框起來

 

線段識別:

代碼如下:

enable_lens_corr = False # turn on for straighter lines...打開以獲得更直的線條…
import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.RGB565) #灰度更快
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()

# 所有的線對象都有一個`theta()`方法來獲取它們的旋轉角度。
# 您可以根據旋轉角度來過濾線條。

min_degree = 0
max_degree = 179

# 所有線段都有 `x1()`, `y1()`, `x2()`, and `y2()` 方法來獲得他們的終點
# 一個 `line()` 方法來獲得所有上述的四個元組值,可用於 `draw_line()`.

while(True):
    clock.tick()
    img = sensor.snapshot()
    if enable_lens_corr: img.lens_corr(1.8) # for 2.8mm lens...

    # `threshold` controls how many lines in the image are found. Only lines with
    # edge difference magnitude sums greater than `threshold` are detected...

    # `threshold`控制從霍夫變換中監測到的直線。只返回大於或等於閾值的
    # 直線。應用程序的閾值正確值取決於圖像。注意:一條直線的大小是組成
    # 直線所有索貝爾濾波像素大小的總和。

    # `theta_margin`和`rho_margin`控件合並相似的直線。如果兩直線的
    # theta和ρ值差異小於邊際,則它們合並。

    for l in img.find_lines(threshold = 1000, theta_margin = 25, rho_margin = 25):
        if (min_degree <= l.theta()) and (l.theta() <= max_degree):
            img.draw_line(l.line(), color = (255, 0, 0))
            # print(l)

    print("FPS %f" % clock.fps())

運行結果:通過theta_margin和rho_margin控件合並相似的直線

注意:通過theta_margin和rho_margin控件合並相似的直線,這樣可以防止直線太散亂

 

 

 

 

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


免責聲明!

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



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