如何使用图像像素在对象的多个点上计算直径?

发布于 2025-02-14 00:52:23 字数 278 浏览 2 评论 0原文

我试图使用计算机视觉来代替光学千分尺的使用,以在一定长度上在圆柱体的不同点上获取直径。

圆柱体的图像:

我如何在多个点(蓝线)上计算此对象的直径(气缸)如图像中所示的长度,使用OpenCV Python?

I am trying to get diameters on different points of a cylinder over a certain length using computer vision to replace the use of optical micrometer.

Image of a cylinder:
enter image description here

How can I calculate the diameter of this object (cylinder) on multiple points (blue lines) along its length as shown in the image using OpenCV python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的温暖 2025-02-21 00:52:23

OPENCV解决方案。主要想法是:

  1. 检测边缘
  2. 的边缘的轮廓
  3. 发现填充在轮廓区域
  4. 穿过图像中的每一列,并计算非零像素

1.,2。和3。可以通过单个阈值步骤来简化。您的用例

import numpy as np 
import cv2


src = cv2.imread('/path/to/src.jpg')
mask = np.zeros(src.shape, dtype=np.uint8)
w, h, c = src.shape

# edge detection
threshold = 100
gray = cv2.Canny(src, threshold, threshold * 2)

cv2.imshow('', gray)
cv2.waitKey(0)

# find contours
cnts = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
threshold_area = 0.5

# fill area withing contours with white color
for c in cnts:
    area = cv2.contourArea(c)
    if area > threshold_area:
        cv2.drawContours(mask, [c], -1, (255, 255, 255), -1)

cv2.imshow('', mask)
cv2.waitKey(0)

# get non zero values (height) of each column
column_pixels = [cv2.countNonZero(mask[:, i]) for i in range(0, w)]
print(column_pixels)

SRC映像:

”

canny结果:

“在此处输入图像说明”

用白色填充轮廓:

“在此处输入图像说明”

countnonzero在每个列的最后一个图像上应用

An OpenCV solution. The main idea is to:

  1. Detect edges
  2. Find the contours of the edges
  3. Fill in the contour areas
  4. Go through each column in the image and count the nonzero pixels

1., 2. and 3. could possibly be simplified by a single thresholding step depending on your use case

import numpy as np 
import cv2


src = cv2.imread('/path/to/src.jpg')
mask = np.zeros(src.shape, dtype=np.uint8)
w, h, c = src.shape

# edge detection
threshold = 100
gray = cv2.Canny(src, threshold, threshold * 2)

cv2.imshow('', gray)
cv2.waitKey(0)

# find contours
cnts = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
threshold_area = 0.5

# fill area withing contours with white color
for c in cnts:
    area = cv2.contourArea(c)
    if area > threshold_area:
        cv2.drawContours(mask, [c], -1, (255, 255, 255), -1)

cv2.imshow('', mask)
cv2.waitKey(0)

# get non zero values (height) of each column
column_pixels = [cv2.countNonZero(mask[:, i]) for i in range(0, w)]
print(column_pixels)

Src image:

enter image description here

Canny result:

enter image description here

After filling in contours with white color:

enter image description here

countNonZero is applied on this last image for each column

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文