[英]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.