如何使用图像处理找到叠加在 260 mm x 6.2 mm 条带上的液滴的平均直径?
How to find the average diameter of the drops, which are superimposed on a strip of size 260 mm x 6.2 mm using image processing?
如何计算图像中水滴的平均直径。图像上的水滴是雨滴,叠加水滴的条带大小为260mm x 6.2mm。该条带长度和宽度可被视为参考(透视)尺寸,可用于测量液滴直径或 x 轴和 y 轴的长度(以毫米为单位)。
另外,如何用直径标记每个水滴?
image = cv2.imread("1.png")
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# create a binary thresholded image
_, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
# show it
# find the contours from the thresholded image
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# draw all contours
for contour in contours:
if (cv2.contourArea(contour) < 3000) & (cv2.contourArea(contour) > 4):
image = cv2.drawContours(image, contours, -1, (1, 1, 1), 1)
circles = image.copy()
num_circles = len(contours)
ave = 0
rds = []
center, radius = cv2.minEnclosingCircle(contour)
cx = int(round(center[0]))
cy = int(round(center[1]))
rr = int(round(radius))
# draw enclosing circle over beads
# cv2.circle(circles, (cx,cy), rr, (1,1,1), 1)
# cumulate radii for average
ave = ave + radius
rds.append(rr)
# print average radius
ave_radius = ave / num_circles
print("average radius:", ave_radius)
print ("number of circles:", num_circles)
cv2.imwrite('beads_circles.jpg', circles)
plt.imshow(circles)
plt.show()
这是输出
import numpy as np
import matplotlib.pyplot as plt
import cv2
import glob
import os
img = cv2.imread("pip_ims/52.png")
image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# create a binary thresholded image
_, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
# find the contours from the thresholded image
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
dia_mm = []
for contour in contours:
#image = cv2.drawContours(image, contours, -1, (1, 1, 1), 3)
area = cv2.contourArea(contour)
dia = 2*(area/np.pi)**(0.5) # calculating diameter from area
print(dia)
diamm = dia*(6.2/64) # converting diameter from pixels to mm
dia_mm.append(diamm)
print(diamm)
diameter = list(filter(lambda num: num != 0, dia_mm)) # removing zeros
diameter = np.array(diameter)
print(diameter.mean(), diameter.max(), diameter.min())
如何计算图像中水滴的平均直径。图像上的水滴是雨滴,叠加水滴的条带大小为260mm x 6.2mm。该条带长度和宽度可被视为参考(透视)尺寸,可用于测量液滴直径或 x 轴和 y 轴的长度(以毫米为单位)。 另外,如何用直径标记每个水滴?
image = cv2.imread("1.png")
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# create a binary thresholded image
_, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
# show it
# find the contours from the thresholded image
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# draw all contours
for contour in contours:
if (cv2.contourArea(contour) < 3000) & (cv2.contourArea(contour) > 4):
image = cv2.drawContours(image, contours, -1, (1, 1, 1), 1)
circles = image.copy()
num_circles = len(contours)
ave = 0
rds = []
center, radius = cv2.minEnclosingCircle(contour)
cx = int(round(center[0]))
cy = int(round(center[1]))
rr = int(round(radius))
# draw enclosing circle over beads
# cv2.circle(circles, (cx,cy), rr, (1,1,1), 1)
# cumulate radii for average
ave = ave + radius
rds.append(rr)
# print average radius
ave_radius = ave / num_circles
print("average radius:", ave_radius)
print ("number of circles:", num_circles)
cv2.imwrite('beads_circles.jpg', circles)
plt.imshow(circles)
plt.show()
这是输出
import numpy as np
import matplotlib.pyplot as plt
import cv2
import glob
import os
img = cv2.imread("pip_ims/52.png")
image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# create a binary thresholded image
_, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
# find the contours from the thresholded image
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
dia_mm = []
for contour in contours:
#image = cv2.drawContours(image, contours, -1, (1, 1, 1), 3)
area = cv2.contourArea(contour)
dia = 2*(area/np.pi)**(0.5) # calculating diameter from area
print(dia)
diamm = dia*(6.2/64) # converting diameter from pixels to mm
dia_mm.append(diamm)
print(diamm)
diameter = list(filter(lambda num: num != 0, dia_mm)) # removing zeros
diameter = np.array(diameter)
print(diameter.mean(), diameter.max(), diameter.min())