OpenCV 18物件測量

2021-09-28 13:55:54 字數 1897 閱讀 5184

# -*- 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, cv.thresh_binary | 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)

# 求輪廓外接矩形

mm = cv.moments(contour)

# 求幾何矩,返回字典型別

# 求得圖形的重心座標

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)4,

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: cv.drawcontours(dst, contours, i,

(255,0

,0),

2)cv.imshow(

"measure-contours"

, dst)

# 讀取

src = cv.imread(

"d:\python\projects\opencv_toturial\images\detect_blob.png"

)# 建立opencv的gui視窗

cv.namedwindow(

"input image"

, cv.window_autosize)

# 將放入指定名字的視窗中顯示出來

cv.imshow(

"input image"

, src)

measure_object(src)

# 設定waitkey中的delay為0,程式會等待使用者操作後關閉視窗

opencv學習筆記 24物件測量

輪廓發現 可以用來進行數字的邊界查詢,進行區域的劃分 import cv2 as cv import numpy as np defmeasure object image 灰度化 gray cv.cvtcolor image,cv.color bgr2gray 二值化 ret,binary cv....

OpenCV 物件測量

二值化影象 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...

OpenCV 93 物件檢測 LBP特徵介紹

區域性二值模式 local binary pattern 主要用來實現2d影象紋理分析。其基本思想是用每個畫素跟它周圍的畫素相比較得到區域性影象結構,假設中心畫素值大於相鄰畫素值則則相鄰畫素點賦值為1,否則賦值為0,最終對每個畫素點都會得到乙個二進位制八位的表示,比如11100111。假設3x3的視...