- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 OpenCV 中的 cv2.drawContours
函数显示填充轮廓。我已经根据 Canny
检测得出的边缘图像开发了一个轮廓列表,并且正在为层次结构定义启用 RETR_EXTERNAL
找到轮廓。然而,我遇到了一个问题,尽管在 cv2.drawContours
命令中使用了 -1
标志来指示填充轮廓,但只有轮廓本身(即边缘)是显示。例如:
mask = np.zeros(rawimg.shape, np.uint8)
cv2.drawContours(mask, contours[246], -1, (0,255,255), -1)
导致仅显示轮廓 246 的轮廓。因为我只是检索外部轮廓,所以我不认为我只看到在每个边缘发现的内部和外部轮廓之间的差异,所以我对它为什么显示轮廓而不是填充感到有点困惑-1
标志会建议它应该这样做。
编辑:完整代码包含在下面。问题在于该行: cv2.drawContours(mask, cnt, 2, (0,255,255), -1)虽然这是按照 Dan 建议的方式进行格式化,但结果如下图:
. cnt 是单个轮廓,因此它指的是轮廓中的单个点是有道理的。当行更改为:
cv2.drawContours(mask, cnt, -1, (0,255,255), -1)
轮廓像以前一样打印,但是轮廓仍然没有填充,因为命令末尾的 -1 标志表明应该填充。
测试图片:
import os
import cv2
import numpy as np
from matplotlib import pyplot as plt
import copy as cp
path = 'C:\\Users\\...deleted...\\Desktop\\testimage6.jpg'
#Determine largest contour in the image
def maxContour(contours):
cnt_list = np.zeros(len(contours))
for i in range(0,len(contours)):
cnt_list[i] = cv2.contourArea(contours[i])
max_value = np.amax(cnt_list)
max_index = np.argmax(cnt_list)
cnt = contours[max_index]
return cnt, max_index
if os.path.isfile(path):
# Import the raw image to a working location and save to an isolated variable
# Import the raw image to a working location and save to an isolated variable
img = cv2.imread(path)
rawimg = cv2.imread(path)
saveimg = cv2.imread(path)
imgray = cv2.cvtColor(saveimg, cv2.COLOR_BGR2GRAY)
saveimgray = cp.copy(imgray)
f1 = plt.figure(1)
f1.set_size_inches(8,10)
plt.title('Original Image')
plt.xticks([]), plt.yticks([])
plt.imshow(rawimg, cmap='gray')
plt.savefig('output1.jpg', dpi=300)
cv2.imshow('Raw Image',rawimg)
cv2.waitKey(0)
cv2.destroyWindow('Raw Image')
# Impose an opening function as a filter
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
f2 = plt.figure(2)
f1.set_size_inches(8,10)
plt.title('Opened Image')
plt.xticks([]), plt.yticks([])
plt.imshow(opening, cmap='gray')
plt.savefig('output2.jpg', dpi=300)
cv2.imshow('Opened Image', opening)
cv2.waitKey(0)
cv2.destroyWindow('Opened Image')
#Extract the edges from the filtered image
edges = cv2.Canny(opening,10,100)
cv2.imshow('Edges', edges)
cv2.waitKey(0)
cv2.destroyWindow('Edges')
f3=plt.figure(3)
f3.set_size_inches(16,8)
plt.title('Edge Image')
plt.xticks([]), plt.yticks([])
plt.imshow(edges, cmap='gray')
plt.savefig('output3.jpg', dpi=300)
#Detect contours in the edge image
image, contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(img, contours, -1, (0,255,255), 2)
cv2.imshow('Contours Image', img)
cv2.waitKey(0)
cv2.destroyWindow('Contours Image')
f4=plt.figure(4)
f4.set_size_inches(16,8)
plt.title('Contour Image')
plt.xticks([]), plt.yticks([])
plt.imshow(img)
plt.savefig('output2.jpg', dpi=300)
#Find maximum area contour
cnt, max_index = maxContour(contours)
print(max_index)
# Calculate contour-based statistics
# TBD
#Test of removing max contour
#grayimg = cv2.cvtColor(rawimg, cv2.COLOR_BGR2GRAY)
mask = np.zeros(rawimg.shape, np.uint8)
cv2.drawContours(mask, cnt, 2, (0,255,255), -1)
#ret, mask = cv2.threshold(grayimg, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
cv2.imshow('Mask Image', mask)
cv2.waitKey(0)
cv2.destroyWindow('Mask Image')
cv2.imshow('Mask Image', mask_inv)
cv2.waitKey(0)
cv2.destroyWindow('Mask Image')
#Fit ellipse to contour and calculate ellipse statistics
(x,y), (w,h), angle = cv2.fitEllipse(cnt)
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
x = np.int0(x)
y = np.int0(y)
w = np.int0(0.5*w)
h = np.int0(0.5*h)
#output2 = cv2.ellipse(img, center, dim, angle, 0, 360, (255,0,0), 12)
output2 = cv2.ellipse(img, (x,y), (w,h), angle, 0, 360, (255,0,0), 2)
output3 = cv2.drawContours(output2, [box], 0, (0,255,0), 2)
cv2.imshow('Ellipse Image',output2)
cv2.waitKey(0)
cv2.destroyWindow('Ellipse Image')
else:
print('file does not exist')`
最佳答案
function drawContours
takes a list of contours as input .尝试:
cv2.drawContours(
image=mask,
contours=[cnt],
contourIdx=-1,
color=(0,255,255),
thickness=cv2.FILLED)
代替:cv2.drawContours(mask, cnt, -1, (0,255,255), -1)
。
关于python - cv2.drawContours 不会绘制填充轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45246036/
我有一个不规则形状的元素(比方说图标)。 我想要围绕它的某种轮廓,以符合特定颜色的形状。此轮廓的颜色必须均匀地围绕形状,即与形状各处的距离相同,并且没有颜色渐变。 我发现使用的是 css 选项 fil
这部分代码我总是出错 &contours = ((contours.h_next) -> h_next); contours.h_next = ((contours.h_next) -> h_next
我通过 css (:after) 创建了 3 个圆圈,使用一些背景颜色,边框看起来不规则。有什么解决办法吗? 在这里您可以看到问题:https://flowersliving.com/cpt_01/a
使用这个: background: -moz-linear-gradient(315deg, transparent 10px, black 10px); 如何在不使用 border 的情况下围绕它创
我想计算二元 NxM 矩阵中某个形状周围的凸包。凸包算法需要一个坐标列表,所以我采用 numpy.argwhere(im) 来获得所有形状点坐标。然而,这些点中的大多数对凸包没有贡献(它们位于形状的内
如何删除从下拉菜单中选择元素时显示的虚线边框/轮廓? 您可以看到显示了虚线边框/轮廓,我想删除它(在 Firefox 中截取的屏幕截图)。 尝试下面的解决方案并没有删除它: select:focus,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
我正在使用 Qt4 GraphicsView 框架绘制一些多边形,并且允许用户放大和缩小绘图。我希望多边形随着用户在 View 中更改缩放级别(比例)而变得越来越小,但是有没有办法使轮廓厚度始终保持不
我在 C# 中有一个 Vector3 点列表,我需要计算这些点的凹轮廓。确实有很多引用资料,尤其是 -convex- 分辨率(我已经成功实现了,多亏了 graham 的算法), 但是,由于我现在需要有
注: r 中的解决方案, python , java ,或者如果需要,c++或 c#是需要的。 我正在尝试根据运输时间绘制轮廓。更清楚地说,我想将具有相似旅行时间(比如说 10 分钟间隔)的点聚集到特
假设我在图像上找到了轮廓。在图像 2 上找到此轮廓位置的最佳方法是什么? 我看到两个选项:要么我用白线绘制轮廓并匹配图像 2 上的图像,要么我以某种方式(这甚至可能吗?)直接匹配图像 2 上的轮廓。
我一直在研究细菌的图像,希望从图像中获取细菌的数量,还需要根据特定的形状和大小对细菌进行分类。 我正在使用opencv python。现在,我使用轮廓法。 contours,hierarchy
我无法区分以下两个轮廓。 cv2.contourArea两者的值相同。在Python中有什么功能可以区分它们吗? 最佳答案 要区分填充轮廓和未填充轮廓,可以在使用 cv2.findContours 查
是否可以根据 Activity 配置文件的某些表达式来注册bean前任。 @Profile(!prod) @Profile(name!="test") 我有一种情况,我需要根据许多不同的条件配
我有一个由多个 CAShapeLayer 组成的 3D 相似图形对象。必须抚摸所有形状(天花板和墙壁)。有些形状共享一条边 - 这似乎是问题的根源。 然而,轮廓似乎是围绕另一个形状的现有轮廓绘制的。所
有谁知道,是否可以在用户使用顺序导航(TAB 按钮)时在输入元素周围显示轮廓,并在用户用鼠标单击此输入元素时隐藏轮廓?有没有人实现过这种行为? 我在 CSS 文件中的 :focus 选择器上使用这个属
这是我在 StackOverflow 上的第一个问题,所以我会尝试以正确的方式格式化它。 基本上,我有一个带有边框和轮廓的 div。悬停时,div 也会有一个阴影,当然,它应该在轮廓之外。这适用于所有
我在 Opencv 2.9 (C++) 中使用 findContours。我得到的是一个 vector> contours,它描述了我的轮廓。假设我有一个矩形,其轮廓存储在 vector 中。接下来我
我有一个 div,它有附加的子 div,定位在父 div 之外。 我希望父 div 有一个轮廓 onclick,但轮廓延伸到子 div 周围。 有没有办法让轮廓完全围绕父 div。 我不能使用边框,因
我正在尝试在彩色图标周围设置实线边框。 应该足够直截了当,显然它适用于字形,但我无法让它适用于 我试过... // like this fiddle: http://jsfiddle.net/9s
我是一名优秀的程序员,十分优秀!