openMv入手
openMv入手
- 前言
- openmv是什么
- 一些基本參數
- 感光元件(也就是你IDE窗口看到的圖像)
- 統計信息
- 畫圖
- 測距
- 顏色形狀同時識別
- 忠告忠告!
前言
之前學習了OpenCV的一些基本操作,但終究是圖片處理,有些干,回學校后拿到openmv模塊開始進行一些相關操作。
openmv是什么
還有一些擴展模塊可選用,but
價格勸退,選用了標配版
參數來源淘寶,它有沒有夸張就不知道了
openmv入手教程
上面這個是官方教程有很多例程講的也十分詳細,對新手十分友好
進階的openmv教程
一些基本參數
感光元件(也就是你IDE窗口看到的圖像)
import sensor#引入感光元件的模塊# 設置攝像頭 sensor.reset()#初始化感光元件 sensor.set_pixformat(sensor.RGB565)#設置為彩色 sensor.set_framesize(sensor.QVGA)#設置圖像的大小 sensor.skip_frames()#跳過n張照片,在更改設置后,跳過一些幀,等待感光元件變穩定。# 一直拍照 while(True):img = sensor.snapshot()#拍攝一張照片,img為一個image對象sensor.set_pixformat() 設置像素模式。
sensor.GRAYSCALE: 灰度,每個像素8bit。
sensor.RGB565: 彩色,每個像素16bit。
從這里可以看出若轉換為灰度像素是減半的
以下列出了圖像的一些參數
我認為,mv比較nb的地方就是自動增益了,以下開啟
sensor.set_auto_gain() 自動增益開啟(True)或者關閉(False)。在使用顏色追蹤時,需要關閉自動增益。sensor.set_auto_whitebal() 自動白平衡開啟(True)或者關閉(False)。在使用顏色追蹤時,需要關閉自動白平衡。sensor.set_auto_exposure(enable[\, exposure_us])enable 打開(True)或關閉(False)自動曝光。默認打開。 如果 enable 為False, 則可以用 exposure_us 設置一個固定的曝光時間(以微秒為單位)。設置翻轉
sensor.set_hmirror(True)
水平方向翻轉
sensor.set_vflip(True)
垂直方向翻轉 vflip的英文就是垂直鏡像
image.difference(image)
從這張圖片減去另一個圖片。比如,對于每個通道的每個像素點,取相減絕對值操作。這個函數,經常用來做移動檢測。
統計信息
使用統計信息
畫圖
畫線
image.draw_line(line_tuple, color=White) 在圖像中畫一條直線。line_tuple的格式是(x0, y0, x1, y1),意思是(x0, y0)到(x1, y1)的直線。
顏色可以是灰度值(0-255),或者是彩色值(r, g, b)的tupple。默認是白色
畫框
image.draw_rectangle(rect_tuple, color=White) 在圖像中畫一個矩形框。rect_tuple 的格式是 (x, y, w, h)。
畫圓
image.draw_circle(x, y, radius, color=White) 在圖像中畫一個圓。x,y是圓心坐標
radius是圓的半徑
畫十字
image.draw_cross(x, y, size=5, color=White) 在圖像中畫一個十字x,y是坐標
size是兩側的尺寸
寫字
image.draw_string(x, y, text, color=White) 在圖像中寫字 8x10的像素x,y是坐標。使用\n, \r, and \r\n會使光標移動到下一行。
text是要寫的字符串。
測距
# Measure the distance # # This example shows off how to measure the distance through the size in imgage # This example in particular looks for yellow pingpong ball.import sensor, image, time# For color tracking to work really well you should ideally be in a very, very, # very, controlled enviroment where the lighting is constant... yellow_threshold = ( 56, 83, 5, 57, 63, 80) # You may need to tweak the above settings for tracking green things... # Select an area in the Framebuffer to copy the color settings.sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.RGB565) # use RGB565. sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed. sensor.skip_frames(10) # Let new settings take affect. sensor.set_auto_whitebal(False) # turn this off. clock = time.clock() # Tracks FPS.K=5000#the value should be measuredwhile(True):clock.tick() # Track elapsed milliseconds between snapshots().img = sensor.snapshot() # Take a picture and return the image.blobs = img.find_blobs([yellow_threshold])if len(blobs) == 1:# Draw a rect around the blob.b = blobs[0]img.draw_rectangle(b[0:4]) # rectimg.draw_cross(b[5], b[6]) # cx, cyLm = (b[2]+b[3])/2length = K/Lmprint(length)#print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while# connected to your computer. The FPS should increase once disconnected.具體原理
測距
顏色形狀同時識別
import sensor, image, timesensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(time = 2000) sensor.set_auto_gain(False) # must be turned off for color tracking sensor.set_auto_whitebal(False) # must be turned off for color tracking clock = time.clock()while(True):clock.tick()img = sensor.snapshot().lens_corr(1.8)for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,r_min = 2, r_max = 100, r_step = 2):area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())#area為識別到的圓的區域,即圓的外接矩形框statistics = img.get_statistics(roi=area)#像素顏色統計print(statistics)#(0,100,0,120,0,120)是紅色的閾值,所以當區域內的眾數(也就是最多的顏色),范圍在這個閾值內,就說明是紅色的圓。#l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的眾數。if 0<statistics.l_mode()<100 and 0<statistics.a_mode()<127 and 0<statistics.b_mode()<127:#if the circle is redimg.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))#識別到的紅色圓形用紅色的圓框出來else:img.draw_rectangle(area, color = (255, 255, 255))#將非紅色的圓用白色的矩形框出來print("FPS %f" % clock.fps())忠告忠告!
實驗室有個師兄就這樣燒壞一個openmv4,心疼啊T T
這個很重要
后續再補神經網絡,如果有興趣的小伙伴等不及可聯系我
總結
- 上一篇: matlab 坐标系转换
- 下一篇: three.js根据高德地图的经纬度信息