opencv學習筆記 24物件測量

2021-10-05 01:36:38 字數 2231 閱讀 8592

輪廓發現

可以用來進行數字的邊界查詢,進行區域的劃分

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, cv.thresh_binary_inv | cv.thresh_otsu)

# 顯示閾值

print

("threshold value : %s"

% ret)

# 顯示二值影象

cv.imshow(

"binary image"

, binary)

dst = cv.cvtcolor(binary, cv.color_gray2bgr)

# 查詢輪廓

for i, contour in

enumerate

(contours)

:# 求輪廓面積

area = cv.contourarea(contour)

x, y, w, h = cv.boundingrect(contour)

# 形狀的長寬比

rate =

min(w, h)

/max

(w, h)

print

("rectangle rate : %s"

% rate)

# 求取幾何矩

mm = cv.moments(contour)

print

(type

(mm)

)# 字典型別

# 獲取中心位置

cx = mm[

'm10'

]/ mm[

'm00'

] cy = mm[

'm01'

]/ mm[

'm00'

]# 引數: 影象 圓心 半徑 顏色 填充

cv.circle(dst,

(np.

int(cx)

, np.

int(cy)),

3,(0

,255

,255),

-1)# 繪製外接矩形 引數:影象 起點 終點 顏色 線寬

cv.rectangle(dst,

(x, y)

,(x+w, y+h),(

0,0,

255),2

)print

("contour area %s"

% area)

# 多邊形逼近 引數: 輪廓 epsilon close4,

true

)print0]

>6:

# 多邊形 可以看作圓形

cv.drawcontours(dst, contours, i,(0

,255,0

),2)

0]==4

:# 四邊形

cv.drawcontours(dst, contours, i,(0

,0,255),

2)0]

==3:# 三角形

C 學習筆記 014 物件

1 使用物件進行程式設計是c 的核心,也是我們常說的c 比c 高階 的重要根據之一。2 類 class 就像是一幅藍圖,它決定乙個物件將是什麼樣的 具備什麼樣的屬性和功能 3 oop過程的第一步是建立乙個類,而每個類跟變數一樣都有乙個名字。class myfirstclass 4 類名的第乙個字元採...

python學習筆記2(物件)

物件的屬性不能繼承。方法可以呼叫沒有的引數或方法,但執行時會報錯。property setter標記的方法可以當作屬性來用。class animal object def init self self.age yi def run self print self.name,is running cl...

Redis學習筆記(六) 物件

前面我們看了redis用到的主要資料結構,如簡單動態字串 sds 雙向鍊錶 字典 壓縮列表 整數集合等。但是redis並沒有直接使用這些資料結構來實現鍵值對,而是基於這些資料結構建立了乙個物件系統,這個系統包括字串物件 列表物件 雜湊物件 集合物件 有序集合物件,除此之外,redis的物件系統還實現...