【问题标题】:How to mark the center and calculate the diameter of an image using opencv - python?如何使用opencv-python标记中心并计算图像的直径?
【发布时间】:2018-11-13 18:32:30
【问题描述】:

我正在研究中心的识别和图像渲染。我正在使用cv2.findContours 来划定感兴趣图像的边缘。并使用cv.minEnclosingCircle (cnt) 环绕我感兴趣的区域。下面的代码我可以识别每个 ROI 的中心,但是我无法在图像的输出中标记与我要计算的图像相对应的圆圈,并且我想用 pqno 点标记确切的位置算法确定了中心。

import cv2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredText


thresh = cv2.imread('IMD044.png',0)
_, contours,hierarchy = cv2.findContours(thresh,2,1)
print (len(contours))
cnt = contours

for i in range (len(cnt)):
    (x,y),radius = cv2.minEnclosingCircle(cnt[i])
    center = (int(x),int(y))
    radius = int(radius)
    cv2.circle(thresh,center,radius,(0,255,0),2)
    print ('Circle: ' + str(i) + ' - Center: ' + str(center) + ' -     Radius: ' + str(radius))
plt.text(x-15, y+10, '+', fontsize=25, color = 'red')
plt.text(10, -10, 'Centro: '+str(center), fontsize=11, color = 'red')
plt.text(340, -10, 'Diametro: '+str((radius*2)/100)+'mm', fontsize=11, color = 'red')
plt.Circle(x, y, color='red', fill=False)
plt.imshow(thresh, cmap='gray')
plt.show()

我使用Opencv文档来划分轮廓并获取区域,但没有出现绿色圆圈标记。

退出:

预期退出:

更新问题我可以添加信息,只需要添加圆圈并检查直径是否正确。

【问题讨论】:

    标签: python image opencv image-processing


    【解决方案1】:

    您正在使用单通道图像,但尝试显示 3 通道颜色。添加以下内容:

    ...
    thresh = cv2.imread('IMD016.png',0)
    _, contours,hierarchy = cv2.findContours(thresh,2,1)
    thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB)
    print (len(contours))
    ...
    

    另外,在混合使用OpenCVPyPlot 绘图例程时要格外小心。 OpenCV 默认使用BGR,而PyPlot 使用RGB。此外,cv2 绘图例程不会添加额外的通道,而plt 会。只需要记住这一点

    【讨论】:

    • 我看到我更新了问题。上面的代码测量感兴趣区域的直径是否正确?是的,我只需要将圆圈添加到情节中,我在这里尝试
    • 我明白了,我现在正在学习使用 opencv。而对于图像的直径,上面描述的代码是否正确?你能帮我解决这个问题吗?
    • 我相信这是以像素表示的轮廓直径。像素不会自动转换为mm
    • 嘿家伙,我发现我的 cv2.imshow 不再显示任何输出图像,这是在我将 opencv 的版本更新到 4 后发生的。会发生什么?我已经研究了几个角落,有些人经历过,但没有答案。嗯,我无法在上面的算法中得到输出。
    • 如果要将直径转换为毫米,则需要知道图像的dpidiameter_in_mm = dpi * 3.93701 * diameter_in_pixels。当然,这是假设1 dpi = 0.393701 pixel/cm
    猜你喜欢
    • 2020-06-20
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多