图像数据流识别圆形_人工智能大赛视觉处理(一)图形识别
生活随笔
收集整理的這篇文章主要介紹了
图像数据流识别圆形_人工智能大赛视觉处理(一)图形识别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
圖形識別也可以理解為輪廓識別。輪廓可以簡單認為成將連續的點(連著邊界)連在一起的曲線,具有相同的顏色或者灰度。
(1)為了更加準確,要使用二值化圖像。
(2)在尋找輪廓之前,要進行閾值化處理或者 Canny 邊界檢測。
(3)繪制輪廓
(4)輪廓的近似
(5)查找輪廓的不同特征 ①角點數 ②面積
通過檢測角點來判定是什么形狀,當角點無數多時認為為圓形。
當然此思路不適合做復雜環境下的圖像檢測容易誤判,在識別特殊規則的圖形比較準確,還有待改進。如有不足,感謝指出。
import cv2 as cv import numpy as np global a class ShapeAnalysis:def __init__(self):self.shapes = {'three': 0, 'four': 0, 'five':0, 'six': 0, 'circles': 0}def analysis(self, frame):h, w, ch = frame.shaperesult = np.zeros((h, w, ch), dtype=np.uint8)print("start to detect lines...n")# 二值化圖像gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)cv.imshow("input image", frame)#函數cv.findContours有三個參數,第一個是輸入圖像,第二個是輪廓檢索模式,第三個是輪廓近似方法out_binary, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)#shape數組為面積范圍areas = np.zeros(shape=[1, 7000])areaj = np.zeros(shape=[1, 7000])aread = np.zeros(shape=[1, 7000])areaw = np.zeros(shape=[1, 7000])areal = np.zeros(shape=[1, 7000])areayuan = np.zeros(shape=[1, 7000])i = 1global afor cnt in range(len(contours)):# 提取與繪制輪廓cv.drawContours(result, contours, cnt, (0, 255, 0), 2)# 輪廓逼近#epsilon,它是從原始輪廓到近似輪廓的最大距離。它是一個準確度參數epsilon = 0.01 * cv.arcLength(contours[cnt], True)#角點計算approx = cv.approxPolyDP(contours[cnt], epsilon, True)# 分析幾何形狀corners = len(approx)shape_type = ""if corners == 3:count = self.shapes['three']count = count+1self.shapes['three'] = count#shape_type = "三角形"area_3 = cv.contourArea(contours[cnt])areas[0, i] = area_3if corners == 4:count = self.shapes['four']count = count + 1self.shapes['four'] = count#shape_type = "four形"area_4 = cv.contourArea(contours[cnt])areaj[0, i] = area_4if corners == 5:count = self.shapes['five']count = count + 1self.shapes['five'] = count#shape_type = "five形"area_5 = cv.contourArea(contours[cnt])areaw[0, i] = area_5#if corners == 6:# count = self.shapes['six']# count = count + 1# self.shapes['six'] = count# #shape_type = "six形"# area_6 = cv.contourArea(contours[cnt])# areal[0, i] = area_6if corners >= 50:count = self.shapes['circles']count = count + 1self.shapes['circles'] = count#shape_type = "圓形"area_y = cv.contourArea(contours[cnt])areayuan[0, i] = area_yi = i + 1area_S = areas.max()area_J = areaj.max()area_Y = areayuan.max()area_D = aread.max()area_W = areaw.max()area_L = areal.max()shape_types = "T" #三角形shape_typej = "F" #矩形shape_typey = "Y" #圓形shape_typew = "F" #five形shape_typel = "S" #six形if area_S >5000:a = shape_typesif area_J >5000:a = shape_typejif area_Y >5000:a = shape_typeyif area_W>5000:a = shape_typewif area_L >5000:a = shape_typelreturn self.shapesif __name__ == "__main__":cap = cv.VideoCapture(0)success, img = cap.read()cv.imshow('frame',img)cv.imwrite("/home/car/AI_match/4.jpg", img)src = cv.imread("/home/car/AI_match/4.jpg")ld = ShapeAnalysis()ld.analysis(src)print(a)# cv.waitKey(0)# cv.destroyAllWindows() 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的图像数据流识别圆形_人工智能大赛视觉处理(一)图形识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 曼彻斯特解密_曼彻斯特编码解码方法与流程
- 下一篇: 查询 oracle_关于oracle和m