Python,OpenCV轮廓属性、轮廓检测及绘制
生活随笔
收集整理的這篇文章主要介紹了
Python,OpenCV轮廓属性、轮廓检测及绘制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python,OpenCV輪廓屬性、輪廓檢測及繪制
- 1. 效果圖
- 2. 源碼
- 2.1 輪廓屬性
- 2.2 輪廓特征
- 參考
這篇博客將介紹OpenCV中的輪廓,輪廓的特征及屬性(質心,面積,輪廓,近似輪廓,周長,最小外接圓,面積最小外接矩形,外接直角矩形,凸包);
計算輪廓屬性提取物體的一些常用特性,如堅固性、等效直徑、掩模圖像、平均強度等;
- 寬高比(外接矩形寬/高的比率)
- Extent(面積/外接矩形面積的比率)
- 堅固性(面積/凸包面積的比率)
- 等效直徑(與輪廓面積相同的圓的直徑)
- 擬合橢圓的長軸、短軸、方向;
- 均值(平均顏色或平均強度)
- 極值點是指物體的最上面、最下面、最右邊和最左邊的點
1. 效果圖
外接矩形效果圖如下:紅色為面積最小矩形(它考慮了旋轉),綠色為外接邊界直角矩形
最小外接圓效果圖如下:
擬合橢圓效果圖如下:
擬合線效果圖如下:
2. 源碼
2.1 輪廓屬性
# 找出輪廓的不同特征,如面積、周長、質心、邊界盒等
# moments矩可以計算物體的質心、面積等特征;函數cv2.moments()提供了一個計算出所有力矩值的字典。import cv2
import numpy as npimage = cv2.imread('opencv_logo.jpg')
img = cv2.imread('opencv_logo.jpg', 0)
ret, thresh = cv2.threshold(img, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, 1, 2)
# print(contours)cnt = contours[0]
cv2.drawContours(image, cnt, -1, (255, 255, 255), -1)
M = cv2.moments(cnt)
# 力矩
print('力矩:', M)# 質心
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
print('質心:', (cx, cy))
cv2.circle(image, (cx, cy), 3, (255, 255, 255), -1)# 面積
area = cv2.contourArea(cnt)
print('面積:', area, M['m00'])# 等高線周長也稱為弧長??梢允褂胏v2.arcLength(cnt,True),True標識閉合區域,False表示曲線
perimeter = cv2.arcLength(cnt, True)
print('周長:', perimeter)# 輪廓近似:它將一個輪廓形狀近似為另一個具有較少頂點數的形狀,具體取決于我們指定的精度。它是Douglas-Peucker算法的一個實現。
epsilon = 0.1 * cv2.arcLength(cnt, True)
# epsilon表示近似輪廓10%,True表示閉合區域
approx = cv2.approxPolyDP(cnt, epsilon, True)
print('近似輪廓:', approx)# 凸包:cv2.convexHull()函數檢查曲線的凸度缺陷并進行校正。
hull = cv2.convexHull(cnt)
print('凸包:', hull)# 檢查凸性,即檢查曲線是否凸,cv2.isContourConvex()
k = cv2.isContourConvex(cnt)
print('檢查是否凸:', k)# 邊框(邊界矩形) cv2.boundingRect()計算得到直角矩形,(x,y)為矩形的左上角坐標,(w,h)為矩形的寬度和高度。)
x, y, w, h = cv2.boundingRect(cnt)
aspect_ratio = float(w) / h
print('寬高比: ', aspect_ratio)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)# cv2.minarearct(),旋轉矩形(邊界矩形是以最小面積繪制的,考慮了旋轉。) (中心(x,y), 寬度高度(width, height), 旋轉角度);
# 使用cv2.boxPoints()得到矩形的4個角點
# 兩個矩形都顯示在一個圖像中。綠色矩形顯示法線邊界矩形。紅色矩形是旋轉的矩形。
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(image, [box], 0, (0, 0, 255), 2)# cv2.minEnclosingCircle()面積最小外接圓
(x, y), radius = cv2.minEnclosingCircle(cnt)
center = (int(x), int(y))
radius = int(radius)
cv2.circle(image, center, radius, (0, 255, 0), 2)# 擬合橢圓,返回橢圓內接的旋轉矩形。
ellipse = cv2.fitEllipse(cnt)
cv2.ellipse(image, ellipse, (0, 255, 0), 2)# 擬合線
rows, cols = img.shape[:2]
[vx, vy, x, y] = cv2.fitLine(cnt, cv2.DIST_L2, 0, 0.01, 0.01)
lefty = int((-x * vy / vx) + y)
righty = int(((cols - x) * vy / vx) + y)
cv2.line(image, (cols - 1, righty), (0, lefty), (0, 255, 0), 2)
cv2.imshow("img", image)
cv2.waitKey(0)
2.2 輪廓特征
import cv2
import numpy as npimg = cv2.imread('opencv_logo.jpg', 0)
ret, thresh = cv2.threshold(img, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, 1, 2)
# print(contours)cnt = contours[0]x, y, w, h = cv2.boundingRect(cnt)
aspect_ratio = float(w) / h
print('寬高比:', aspect_ratio)area = cv2.contourArea(cnt)
x, y, w, h = cv2.boundingRect(cnt)
rect_area = w * h
extent = float(area) / rect_area
print('extent: ', extent)area = cv2.contourArea(cnt)
hull = cv2.convexHull(cnt)
hull_area = cv2.contourArea(hull)
solidity = float(area) / hull_area
print('堅固性: ', solidity)area = cv2.contourArea(cnt)
equi_diameter = np.sqrt(4 * area / np.pi)
print('等面積圓的直徑:', equi_diameter)(x, y), (MA, ma), angle = cv2.fitEllipse(cnt)
print('方向:', angle)
print('長軸,短軸:', MA, ma)# Numpy以(行,列)格式給出坐標,OpenCV以(x,y)格式給出坐標。
imgray = img
mask = np.zeros(imgray.shape, np.uint8)
cv2.drawContours(mask, [cnt], 0, 255, -1)
pixelpoints = np.transpose(np.nonzero(mask))
# pixelpoints = cv2.findNonZero(mask)
print('遮罩mask: ', mask)
print('組成輪廓的所有像素點:', pixelpoints)min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(imgray, mask=mask)
print('最小,最大,最小位置,最大位置:', min_val, max_val, min_loc, max_loc)mean_val = cv2.mean(imgray, mask=mask)
print('平均顏色/平均強度:', mean_val)leftmost = tuple(cnt[cnt[:, :, 0].argmin()][0])
rightmost = tuple(cnt[cnt[:, :, 0].argmax()][0])
topmost = tuple(cnt[cnt[:, :, 1].argmin()][0])
bottommost = tuple(cnt[cnt[:, :, 1].argmax()][0])
print('極值點:', leftmost, rightmost, topmost, bottommost)
參考
- https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_contours/py_table_of_contents_contours/py_table_of_contents_contours.html#table-of-content-contours
總結
以上是生活随笔為你收集整理的Python,OpenCV轮廓属性、轮廓检测及绘制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百鸟朝凰图是谁画的呢?
- 下一篇: 求一个带傻的QQ网名。