python - 如何使用opencv-python标记中心并计算图像的直径?
问题描述
我正在研究中心的识别和图像渲染。我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文档来划定轮廓并获取区域,但没有出现绿色圆圈标记。
出口:
更新我能够添加信息的问题,只需要添加圆圈并检查直径是否正确。
解决方案
您正在使用单通道图像,但尝试显示 3 通道颜色。添加以下内容:
...
thresh = cv2.imread('IMD016.png',0)
_, contours,hierarchy = cv2.findContours(thresh,2,1)
thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB)
print (len(contours))
...
OpenCV
此外,在混合和PyPlot
绘图例程时要格外小心。OpenCV
默认使用BGR
,而PyPlot
使用RGB
. 此外,cv2
绘图例程不会添加额外的通道,而plt
会添加。只需要记住这一点
推荐阅读
- node.js - 像 Youtube 一样的视频流,在 Node js 中播放的主要视频之间有广告视频
- android - 如何在项目中找到墙纸 URL?
- asp.net - Global.asax Application_Error IIS7WorkerRequest.RaiseCommunicationError.ExplicitFlush()
- java - 如何用新字符替换字符串中的第 n 个字符?
- php - php mailer Getting SMTP connect() failed 突然出错,休息正常
- javascript - 让 taskRunner 运行时遇到问题
- bash - 如何在子 shell 中使用 grep 命令?
- php - 每 3 小时运行一次请求
- ruby - 安装自制软件时出现LoadError
- vbscript - VBScript 文件重命名:“文件已存在”错误