[英]OpenCV: Contours hidden behind mask. How to detect when a contour is about to move near mask edge?
我正在检测光源前的物体轮廓。 为了简化背景并消除噪音,我需要屏蔽不是光源的所有东西。 如何辨别物体何时位于蒙面区域的边缘?
假设形状没有特征(用于边缘检测的单色黑白)和模糊(图像1中的正方形可以是图像2中的圆)。
考虑光源前的轮廓。 它是独特的,我们可以告诉它嵌套在外轮廓内。 图1描绘了一个简化的案例。
在这种情况下,我们可以将外圈视为蒙版,并且很容易忽略轮廓内的所有内容。 图2描绘了具有一些边缘检测的简化情况。
一切都很美好,直到轮廓移动到光源的边缘。 突然间我们遇到了问题。 图3是边缘上的形状的示例。
轮廓与背景/遮罩区域的黑色无法区分。 OpenCV要么假设我们的光源的轮廓突然变得有趣并且没有其他物体被检测到。
我可以使用哪些工具来检测边缘形状是否存在某种中断? 是否有一种好的/计算的廉价方法来确定我们的轮廓是否与另一个轮廓相交?
HoughCircles
不起作用。 您可以使用cv2.log_polar
函数来展开圆形/椭圆形。
之后, np.argmax
可用于查找曲线。 尝试使用Scipy的signal.savgol_filter()
平滑曲线。 当对象阻挡光源时,平滑线和argmax数据之间会有很大差异:
这是我使用的代码:
import numpy as np
import cv2
# Read the image
img = cv2.imread('/home/stephen/Desktop/JkgJw.png', 0)
# Find the log_polar image
log_polar = cv2.logPolar(img, (img.shape[0]/2, img.shape[1]/2), 40, cv2.WARP_FILL_OUTLIERS)
# Create a background to draw on
bg = np.zeros_like(log_polar)
# Iterate through each row in the image and get the points on the edge
h,w = img.shape
points = []
for col in range(h-1):
col_slice = log_polar[col:col+1, :]
curve = np.argmax(255-col_slice)
cv2.circle(bg, (curve, col), 0, 255, 1)
points.append((curve, col))
cv2.imshow('log_polar', log_polar)
cv2.waitKey(0)
cv2.destroyAllWindows()
import scipy
from scipy import signal
x,y = zip(*points)
x_smooth = signal.savgol_filter(x,123,2)
import matplotlib.pyplot as plt
plt.plot(x)
plt.plot(x_smooth)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.