对于放射线扫描,我已经能够获得轮廓。
我有兴趣找到中心轴。我怎么能在python中做到这一点?
这是我的轮廓代码:
import cv2
img = cv2.imread("A.png")
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(img,60,200)
contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
hierarchy = hierarchy[0]
cv2.drawContours(img, contours, -1, (255,0,0), 3)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud) 正如@Silencer所建议的,我使用他在这里发布的代码来绘制图像中数字的轮廓.在某些时候,处理数字,就像0,6,8,9我看到他们的内部轮廓(圆圈)也被填充.我怎么能阻止这个?是否有为cv2.drawContours()设置的最小/最大动作区域,所以我可以排除内部区域?
我试图传递cv2.RETR_EXTERNAL但是使用此参数只考虑整个外部区域.
代码就是这样(再次感谢Silencer.几个月来一直在寻找这个......):
import numpy as np
import cv2
im = cv2.imread('imgs\\2.png')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#contours.sort(key=lambda x: int(x.split('.')[0]))
for i, cnts in enumerate(contours):
## this contour is a 3D numpy array
cnt = contours[i]
res = cv2.drawContours(im, [cnt], 0, (255, 0, 0), 1)
cv2.imwrite("contours.png", res)
'''
## Method 1: crop the region
x,y,w,h = cv2.boundingRect(cnt)
croped = res[y:y+h, x:x+w] …Run Code Online (Sandbox Code Playgroud) I have character images like this:

After getting contours and convexHull the output is like this:

For that I used following code:
import cv2
img = cv2.imread('input.png', -1)
ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
127, 255, cv2.THRESH_BINARY)
image, contours, hier = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
# get convex hull
hull = cv2.convexHull(cnt)
cv2.drawContours(img, [hull], -1, (0, 0, 255), 1)
cv2.imwrite("output.png", img)
Run Code Online (Sandbox Code Playgroud)
As you can see in the following image there are identified contours which are vertically aligned with …
我有一组元组:
a = [(375, 193)
(364, 113)
(277, 20)
(271, 16)
(52, 106)
(133, 266)
(289, 296)
(372, 282)]
Run Code Online (Sandbox Code Playgroud)
如何在 OpenCV 中的点之间画线?
这是我的代码不起作用:
for index, item in enumerate(a):
print (item[index])
#cv2.line(image, item[index], item[index + 1], [0, 255, 0], 2)
Run Code Online (Sandbox Code Playgroud) 我试图隔离验证码中的字母,我设法过滤了验证码,结果得到了这张黑白图像:
但是当我尝试使用 OpenCV 的 findContours 方法分离字母时,它只是发现了一个包裹我整个图像的外部轮廓,从而产生了这个图像(图像外的黑色轮廓)。
我将此代码与 Python 3 和 OpenCV 3.4.2.17 一起使用:
img = threshold_image(img)
cv2.imwrite("images/threshold.png", img)
image, contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for i, contour in enumerate(contours):
area = cv2.contourArea(contour)
cv2.drawContours(img, contours, i, (0, 0, 0), 3)
cv2.imwrite('images/output3.png', img)
Run Code Online (Sandbox Code Playgroud)
我只希望我的最终结果是每个字符外有 5 个轮廓。
今天我试图识别一个物体的边缘。
通过这样做,我得到了很好的结果。
import cv2
img = cv2.imread("0.png")
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img2 = cv2.equalizeHist(img2)
img2 = cv2.GaussianBlur(img2, (7, 7), 0)
edges = cv2.Canny(img2, 180, 300)
im2, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 255, 0), 1)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
我的最终目标是找到边缘之间的中心线,(每个 X 上的 (MaxY+MinY)/2 的集合)理想的结果应该是这样的:(抱歉手绘不好)
谁能让我知道我该怎么做?非常感谢。
我需要分组contours并绘制一个bounding rectangle包含所有轮廓的单曲,就像这样
from matplotlib import pyplot as plt
import cv2 as cv
img = cv.imread('shapes1.png', 0)
imgRGB = cv.cvtColor(img.copy(), cv.COLOR_GRAY2RGB)
_, ctrs, _ = cv.findContours(img, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
boxes = []
for ctr in ctrs:
x, y, w, h = cv.boundingRect(ctr)
boxes.append([x, y, w, h])
for box in boxes:
top_left = (box[0], box[1])
bottom_right = (box[0] + box[2], box[1] + box[3])
cv.rectangle(imgRGB, top_left, bottom_right, (0,255,0), 2)
fig = plt.figure(figsize = (10, 10))
ax = fig.add_subplot(111)
ax.imshow(imgRGB, cmap='gray') …Run Code Online (Sandbox Code Playgroud) 我是计算机视觉的新手。因此,我不知道以下代码的内部实现,因此无法调试该错误。任何人都可以在以下代码中指出错误吗?
该代码使用Box Filter和Edge detection Kernel矩阵的组合将停车图像转换为二进制图像。然后我试图找到轮廓。现在我知道可以在二进制图像上找到轮廓,可以使用cv2.threshold()函数导出轮廓,从Filter和Kernel矩阵获得的图像也不是二进制图像吗?
import cv2
import numpy as np
import matplotlib.pyplot as plt
img=cv2.imread('parking spot1.jpg',1)
k3 = np.array(([-1,-1,-1],[-1,8,-1],[-1,-1,-1]))
low_filter = cv2.boxFilter(img, -1, (4,4))
output_low = cv2.filter2D(low_filter, -1, k3)
plt.subplot(2, 2, 1)
plt.imshow(img)
plt.title('Original Image')
plt.subplot(2, 2, 2)
plt.imshow(output_low)
plt.title('matrix1')
plt.show()
img, ret, heirarchy = cv2.findContours(output_low, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
Run Code Online (Sandbox Code Playgroud)
您认为我做错了什么?我非常感谢您对此问题的解释或指导。
非常感谢。
我面临的错误是:
()最近追溯(最近一次通话)----> 1 img,ret,heirarchy = cv2.findContours(output_low,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
错误:OpenCV(3.4.3)/io/opencv/modules/imgproc/src/contours.cpp:199:错误:(-210:不支持的格式或格式组合)[开始]当模式!=时,FindContours仅支持CV_8UC1图像。 CV_RETR_FLOODFILL否则仅在函数'cvStartFindContours_Impl中支持CV_32SC1图像
opencv convolution computer-vision lowpass-filter opencv-contour
在我应用了一些处理之后,例如cv2.Canny(),它现在看起来像这样:

如您所见,黑线变为空心。我试过腐蚀和膨胀,但如果我多次这样做,两个入口将被关闭(意味着成为连接线或封闭轮廓)。
我怎样才能使这些线条像下图一样牢固,同时保持 2 个入口不受影响?
更新 1
我已经用几张照片测试了以下答案,但代码似乎是定制的,只能处理这张特定的图片。由于 SOF 的限制,我不能上传大于 2MB 的照片,所以我将它们上传到我的 Microsoft OneDrive 文件夹中,以方便您测试。
https://1drv.ms/u/s!Asflam6BEzhjgbIhgkL4rt1NLSjsZg?e=OXXKBK
更新 2
我拿起@fmw42 的帖子作为答案,因为他的答案是最详细的。它没有回答我的问题,但指出了处理迷宫的正确方法,这是我的最终目标。我喜欢他的回答问题的方法,首先告诉您每个步骤应该做什么,以便您对如何完成任务有一个清晰的想法,然后从头到尾提供完整的代码示例。很有帮助。
由于SOF的限制,我只能挑出一个答案。如果允许多个答案,我也会选择 Shamshirsaz.Navid 的答案。他的回答不仅指出了解决问题的正确方向,而且形象化的解释对我来说真的很管用~!我想它对所有试图理解为什么需要每一行代码的人都同样有效。他也在评论中跟进了我的问题,这使得 SOF 有点互动:)
Ann Zen 的回答中的 Threshold track bar 也是一个非常有用的提示,可以帮助人们快速找到最佳值。
我正在尝试填充通过单独阈值处理 3 个颜色通道获得的轮廓。
image_original = cv2.imread(original_image_path)
image_contours = np.zeros((image_original.shape[0], image_original.shape[1], 1), dtype=np.uint8)
image_contour = np.zeros((image_original.shape[0], image_original.shape[1], 1), dtype=np.uint8)
image_binary = np.zeros((image_original.shape[0], image_original.shape[1], 1), dtype=np.uint8)
image_area = image_original.shape[0] * image_original.shape[1]
for channel in range(image_original.shape[2]):
ret, image_thresh = cv2.threshold(image_original[:, :, channel], 120, 255, cv2.THRESH_OTSU)
_, contours, hierarchy = cv2.findContours(image_thresh, 1, 1)
for index, contour in enumerate(contours):
if( cv2.contourArea( contour ) > image_area * background_remove_offset ):
del contours[index]
cv2.drawContours(image_contours, contours, -1, (255,255,255), 3)
_, contours, hierarchy = cv2.findContours(image_contours, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image_contour, max(contours, key …Run Code Online (Sandbox Code Playgroud) opencv ×10
opencv-contour ×10
python ×8
opencv3.0 ×2
python-3.x ×2
captcha ×1
convex-hull ×1
convolution ×1
cv2 ×1
x-ray ×1