# 二值化影象
("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)
for cnt in
range
(len
(contours)):
# 提取與繪製輪廓
cv.drawcontours(result, contours, cnt,(0
,255,0
),2)
# 輪廓逼近
epsilon =
0.01
* cv.arclength(contours[cnt]
,true
), epsilon,
true
)# 分析幾何形狀
corners =
len shape_type =
""if corners ==3:
count = self.shapes[
'********'
] count = count+
1 self.shapes[
'********'
]= count
shape_type =
"三角形"
if corners ==4:
count = self.shapes[
'rectangle'
] count = count +
1 self.shapes[
'rectangle'
]= count
shape_type =
"矩形"
if corners >=10:
count = self.shapes[
'circles'
] count = count +
1 self.shapes[
'circles'
]= count
shape_type =
"圓形"if4
< corners <10:
count = self.shapes[
'polygons'
] count = count +
1 self.shapes[
'polygons'
]= count
shape_type =
"多邊形"
# 求解中心位置
mm = cv.moments(contours[cnt]
)if mm[
"m00"]!=
0:cx =
int(mm[
'm10'
]/ mm[
'm00'])
cy =
int(mm[
'm01'
]/ mm[
'm00'])
else
: cx, cy =0,
0 cv.circle(result,
(cx, cy),3
,(0,
0,255),-
1)# 顏色分析
color = frame[cy]
[cx]
color_str =
"("+
str(color[0]
)+", "
+str
(color[1]
)+", "
+str
(color[2]
)+")"# 計算面積與周長
p = cv.arclength(contours[cnt]
,true
) area = cv.contourarea(contours[cnt]
("周長: %.3f, 面積: %.3f 顏色: %s 形狀: %s "
OpenCV 18物件測量
coding utf 8 import cv2 as cv import numpy as np defmeasure object image gray cv.cvtcolor image,cv.color bgr2gray ret,binary cv.threshold gray,0,255,c...
opencv學習筆記 24物件測量
輪廓發現 可以用來進行數字的邊界查詢,進行區域的劃分 import cv2 as cv import numpy as np defmeasure object image 灰度化 gray cv.cvtcolor image,cv.color bgr2gray 二值化 ret,binary cv....
opencv 物體尺寸測量
1 原圖,已知左上角正方形的實際大小為 2cm 2 轉為灰度圖 高斯模糊 3 邊緣檢測 edged cv2.canny blur,50,100 開運算 4 根據左上角參考物體,計算單位長度中的畫素個數 pixel per cm dist in pixel dist in cm5 計算物體輪廓的最小外...