簡體   English   中英

如何計算邊緣的均值和標准差(OpenCV 中的 Canny 邊緣檢測)

[英]How to compute Mean and Standard Deviation of Edges (Canny Edge Detection in OpenCV)

我有一些圖像,您可以在其中看到打印的半色調,我想計算點的平均值和標准偏差。 我正在使用 OpenCV 和 Canny Edge Detection 來隔離這樣的點

import cv2

img = cv2.imread('img/color_dots.png')
img_blur = cv2.GaussianBlur(img, (3,3), 0)

edges = cv2.Canny(image=img_blur, threshold1=50, threshold2=100)

這是原圖:

在此處輸入圖片說明

這里是精明的邊緣圖像:

在此處輸入圖片說明

我認為並不是所有的點都被識別出來,尤其是黃色的點……而且我不確定我的方法是否適用於邊緣檢測或更好地找到輪廓? 無論如何,我如何計算這些點的平均大小?

這是在 Python/OpenCV 中執行此操作的一種方法。

閾值背景以隔離點。 然后您可以使用掩碼直接在 OpenCV 中計算顏色的平均值。 然后可以從方差的平方根得到標准偏差,即圖像的平方的均值減去圖像的均值的平方。 https://en.wikipedia.org/wiki/Standard_deviation

(或者,使用 Numpy 直接從 np.mean 和 np.std 計算均值和標准差)

輸入:

在此處輸入圖片說明

import cv2
import numpy as np

img = cv2.imread("color_dots.png")

# threshold on background color
lower = (215,215,215)
upper = (255,255,255)
thresh = cv2.inRange(img, lower, upper)

# invert so dots are white
thresh = 255 - thresh
#thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)

# get mean colors of dots using mask
mean_values = cv2.mean(img, mask=thresh)[0:3]
mean_values = list(mean_values)
print("mean:", mean_values)

# compute square of image (as floats) and compute mean of squared image
imgf = img.astype(np.float64)
imgf2 = imgf * imgf
mean2_values = cv2.mean(imgf2, mask=thresh)[0:3]

# convert mean of image and mean of image squared tuples to arrays
mean_values_arr = np.array([mean_values])
mean2_values_arr = np.array([mean2_values])

# compute the variance from the mean of image and mean of image squared arrays
variance_values_arr = mean2_values_arr - (mean_values_arr)*(mean_values_arr)

# compute sqrt to form std
std_values_arr = np.sqrt(variance_values_arr)

# convert array to simple list
std_values = list(std_values_arr[0])
print("std:", std_values)

# save result
cv2.imwrite("color_dots_threshold.png",thresh)

cv2.imshow("thresh", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

閾值圖像:

在此處輸入圖片說明

均值和標准差:

mean: [226.75895493644884, 209.73003594813198, 212.56531647194763]
std: [21.657486571574186, 19.53005158116894, 30.588081007741454]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM