我有一个图像,是规则的图案。我希望可以有一个 Python 代码来帮助指示“Miss-Moving”位置的边界框,是目标,例如:
目标
我曾尝试在侵蚀后再次进行灰色,侵蚀,阈值,侵蚀,结果为:
加里
加里侵蚀
阈值灰蚀
侵蚀阈值-灰色-侵蚀
我不知道下一步该怎么做,或者欢迎对这项工作提出任何意见。谢谢。
这是我的 Python 代码:
# Define Display & Save image function
def displayIMG(img, windowName):
cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
cv2.imshow(windowName, img)
cv2.imwrite(windowName + '.jpg', img)
#1.Read image file & Show out
image = cv2.imread(image_filename)
displayIMG(image, "Original")
#2. Gray
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
name = "Gray"
draw = gray
displayIMG(draw, name)
#3. Erosion
kernel = np.ones((3, 3), np.uint8)
erosion = cv2.erode(gray, kernel, iterations=2)
name = "Gary_Erosion"
draw = erosion
hist = plotHistogram(draw, name)
max_gray = max(hist)
print('Max Gray = ', max_gray)
displayIMG(draw, name)
#4. Threshold
(T, thresh) = cv2.threshold(gray, 120, 255, cv2.THRESH_TOZERO)
name = "Threshold_Gray"
draw = thresh
displayIMG(draw, name)
plotHistogram(draw, name, max_gray)
#5. Erosion again
kernel = np.ones((3, 3), np.uint8)
erosion2 = cv2.erode(draw, kernel, iterations=5)
name = "Gary_Threshold_Erosion"
draw = erosion2
hist = plotHistogram(draw, name, max_gray)
displayIMG(draw, name)