【问题标题】:Remove coins shadow using opencv使用opencv去除硬币阴影
【发布时间】:2020-05-08 11:11:22
【问题描述】:

我正在尝试使用最新版本的 OpenCV 计算图像中有多少硬币,但我正在努力解决阴影问题。

正在使用 Canny Edge 检测器方法,但正如您在第二张图片中看到的那样,由于阴影,它没有按预期工作......关于如何处理这个问题的任何想法?

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (7, 7), 0)
median = np.median(image)
lower = int(max(0, 0.67 * median))
upper = int(min(255, (1.33) * median))

canny = cv2.Canny(blurred, lower, upper)
contours, hierachy = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
coins = cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
cv2.imshow("Coins", coins)

【问题讨论】:

  • 您是否尝试过使用边缘检测器的阈值?也尝试过滤掉图像中的灰色。
  • 是的,我玩过门槛,但没有帮助。
  • 使用 HoughCircle 方法设法做到了
  • 是的,在大多数高对比度情况下,这是一个很好的解决方案。

标签: opencv image-processing opencv-contour opencv-python canny-operator


【解决方案1】:

您可以使用按颜色选择硬币。

import cv2 as cv
import numpy as np
low_H = 0
low_S = 50
low_V = 0
high_H = 255
high_S = 255
high_V = 255
frame = cv.imread('PzB9I.png')
frame_HSV = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
frame_threshold = cv.inRange(frame_HSV, (low_H, low_S, low_V), (high_H, high_S, high_V))
#         filling holes
im_floodfill = frame_threshold.copy()
h, w = frame_threshold.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
cv.floodFill(im_floodfill, mask, (0,0), 255);
im_floodfill_inv = cv.bitwise_not(im_floodfill)
mask = frame_threshold | im_floodfill_inv
#       find contours
contours, hierachy = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
coins = cv.drawContours(frame, contours, -1, (0, 255, 0), 1)
cv.imshow("Coins", coins)

【讨论】:

    猜你喜欢
    • 2018-05-24
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多