91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡介

圖形識別代碼,可以識別多種顏色與形狀,還可以計算面積與周長,形狀包括三角形,正方形,長方形,梯形,平行四邊形,菱形,其他四邊形,五邊形,五角星,六邊形和其他多邊形,橢圓,圓等,顏色包括藍色,紫色,紅色,橙色等 python,opencv。如果沒有辦法運行,請私信我。

資源截圖

代碼片段和文件信息

import?cv2?as?cv
import?numpy?as?np
import?math
“““
????功能:實現多種幾何圖形的形狀及顏色的識別,并計算面積及周長
????可以識別的圖形:三角形,任意四邊形(梯形,平行四邊形,菱形,正方形,矩形,不特殊四邊形),
????????????????????五邊形,五角星,六邊形,多邊形,橢圓,半圓,圓
????可以識別的顏色:紅色,綠色,藍色,黃色,黑色,紫色,橙色,白色
????除了定義的可識別顏色外,其他返回其對應的rgb值,提示用戶根據rgb值去找尋相應顏色?????????????????
????除了定義的可識別圖形外,其他為未定義的圖形??
“““
noise_b?=?0.5


class?ShapeAnalysis:
????def?__init__(self):
????????self.shapes?=?{‘triangle‘:?0
???????????????????????‘quadrilateral‘:?0?‘trapezium‘:?0?‘parallelogram‘:?0
???????????????????????‘rectangle‘:?0?‘rhombus‘:?0?‘square‘:?0
???????????????????????‘pentagon‘:?0?‘pentagram‘:?0?‘hexagon‘:?0
???????????????????????‘polygons‘:?0?‘ellipse‘:?0?‘circle‘:?0
???????????????????????‘undefined‘:?0
???????????????????????}

????????self.colors?=?{‘white‘:?0?‘black‘:?0
???????????????????????‘red‘:?0?‘green‘:?0
???????????????????????‘blue‘:?0?‘yellow‘:?0
???????????????????????‘orange‘:?0?‘purple‘:?0
???????????????????????‘others‘:?0
???????????????????????}

????def?analysis(self?frame):
????????self.re_strover?=?‘識別結果:\n‘
????????h?w?ch?=?frame.shape??#?獲取圖形信息
????????result?=?np.zeros((h?w?ch)?dtype=np.uint8)??#?生成跟圖形相同大小的矩陣
????????cv.imshow(“input?image“?frame)

????????#?二值化圖像
????????print(‘start?to?binary?image?and?detect?edges...\n‘)
????????gray?=?cv.cvtColor(frame?cv.COLOR_BGR2GRAY)
????????#?threshold:固定閾值二值化?ret?dst輸出圖?=?cv2.threshold(src輸入圖?thresh閾值?maxval?type)
????????#?type:二值化操作的類型,包含以下5種類型:?cv2.THRESH_BINARY;
????????#?cv2.THRESH_BINARY_INV;?cv2.THRESH_TRUNC;?cv2.THRESH_TOZERO;cv2.THRESH_TOZERO_INV
????????#?adaptiveThreshold自適應閾值二值化
????????ret?binary?=?cv.threshold(gray?0?255?cv.THRESH_BINARY_INV?|?cv.THRESH_OTSU)

????????#?函數cv2.findContours()有三個參數。第一個是輸入圖像,第二個是輪廓檢索模式,第三個是輪廓近似方法。
????????#?輪廓的點集(contours)[Next?Previous?First_Child?Parent]
????????#?各層輪廓的索引(hierarchy)
????????contours?hierarchy?=?cv.findContours(binary?cv.RETR_TREE?cv.CHAIN_APPROX_SIMPLE)
????????if?len(contours)?????????????print(‘there?is?nothing?can?be?detected!?Please?check?the?input?image.?Thanks!‘)
????????else:
????????????shape_numbers?=?0
????????????for?cnt?in?range(len(contours)):

????????????????#?通過計算周長和面積來排除一些圖像干擾
????????????????#?計算面積與周長
????????????????p?=?cv.arcLength(contours[cnt]?True)
????????????????area?=?cv.contourArea(contours[cnt])

????????????????if?area?>?noise_b?and?p?>?noise_b:??#?如果面積和周長小于0.5判斷為噪聲干擾,不是圖形
????????????????????shape_numbers?+=?1???#?統計檢測到圖形

????????????????????#?為圖形,做統計分析
????????????????????cv.drawContours(result?contours?cnt?(0?255?0)?2)??#?畫出輪廓

????????????????????#?求解中心位置
????????????????????mm?=?cv.moments(contours[cnt])
????????????????????cx?=?int(mm[‘m10‘]?/?(mm[‘m00‘]?+?1e-12))
????????????????????cy?=?int(mm[‘m01‘]?/?(mm[‘m00‘]?+?1e-12))
????????????????????cv.circle(result?(cx?cy)?3?(0?0?255)?-1)

????????????????????#?獲取顏色信息
????????????????????color?=?fr

評論

共有 條評論