文章目錄
- 前言
- 1. 灰度化(Grayscale)
- 2. 二值化(Thresholding)
- 3. 掩膜(Mask)
- 4. 腐蝕(Erosion)
- 5. 膨脹(Dilation)
- 6. 縮放(Scaling)
- 7. 旋轉(Rotation)
- 8. 平移(Translation)
- 9. 邊緣檢測(Edge Detection)
- 10. 輪廓檢測(Contour Detection)
- 11.總結
- 總結
前言
本文僅僅簡單介紹了Openmv中常見的圖像處理操作(灰度化、掩膜、二值化、腐蝕、膨脹、縮放、旋轉、平移、邊緣檢測、輪廓檢測)
1. 灰度化(Grayscale)
將彩色圖像轉換為灰度圖像,減少計算量。
實現方法:
import sensorsensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE) # 設置為灰度模式
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)while True:img = sensor.snapshot() # 捕獲灰度圖像
2. 二值化(Thresholding)
將灰度圖像轉換為黑白圖像,通過設定閾值分離目標區域。
實現方法:
import sensorsensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)threshold = (100, 255) # 閾值范圍while True:img = sensor.snapshot()img.binary([threshold]) # 二值化處理
3. 掩膜(Mask)
通過掩膜操作提取圖像中的特定區域。
實現方法:
import sensor, imagesensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)mask = image.Image(size=(100, 100), copy_to_fb=True) # 創建掩膜
mask.draw_rectangle(20, 20, 60, 60, color=255, fill=True) # 在掩膜上繪制白色矩形while True:img = sensor.snapshot()img.mask(mask) # 應用掩膜
4. 腐蝕(Erosion)
去除圖像中的細小噪聲,使目標區域縮小。
實現方法:
import sensor, imagesensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)while True:img = sensor.snapshot()img.erode(1) # 腐蝕操作,參數為腐蝕次數
5. 膨脹(Dilation)
填充目標區域中的空洞,使目標區域擴大。
實現方法:
import sensor, imagesensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)while True:img = sensor.snapshot()img.dilate(1) # 膨脹操作,參數為膨脹次數
6. 縮放(Scaling)
調整圖像大小。
實現方法:
import sensor, imagesensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)while True:img = sensor.snapshot()img.scale(x_scale=0.5, y_scale=0.5) # 縮放為原來的一半
7. 旋轉(Rotation)
旋轉圖像。
實現方法:
import sensor, imagesensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)while True:img = sensor.snapshot()img.rotation_corr(angle=45) # 旋轉45度
8. 平移(Translation)
平移圖像。
實現方法:
import sensor, imagesensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)while True:img = sensor.snapshot()img.translation(x_offset=10, y_offset=10) # 向右下方平移10像素
9. 邊緣檢測(Edge Detection)
檢測圖像中的邊緣。
實現方法:
import sensor, imagesensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)while True:img = sensor.snapshot()img.find_edges(image.EDGE_CANNY, threshold=(50, 80)) # Canny邊緣檢測
10. 輪廓檢測(Contour Detection)
檢測圖像中的輪廓。
實現方法:
import sensor, imagesensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)while True:img = sensor.snapshot()contours = img.find_contours(threshold=2000) # 查找輪廓for contour in contours:img.draw_rectangle(contour.rect(), color=127) # 繪制輪廓矩形框
11.總結
功能 方法 描述
灰度化 sensor.set_pixformat(sensor.GRAYSCALE) 將圖像轉換為灰度圖
二值化 img.binary([threshold]) 將灰度圖轉換為黑白圖
掩膜 img.mask(mask) 提取圖像中的特定區域
腐蝕 img.erode(iterations) 去除噪聲,縮小目標區域
膨脹 img.dilate(iterations) 填充空洞,擴大目標區域
縮放 img.scale(x_scale, y_scale) 調整圖像大小
旋轉 img.rotation_corr(angle) 旋轉圖像
平移 img.translation(x_offset, y_offset) 平移圖像
邊緣檢測 img.find_edges() 檢測圖像中的邊緣
輪廓檢測 img.find_contours() 檢測圖像中的輪廓
通過以上功能,OpenMV 可以實現豐富的圖像處理任務,適用于嵌入式機器視覺應用。
總結
以上就是今天要講的內容,本文僅僅簡單介紹了Openmv中常見的圖像處理操作(灰度化、掩膜、二值化、腐蝕、膨脹、縮放、旋轉、平移、邊緣檢測、輪廓檢測)