opencv - 使用opencv删除硬币阴影

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

我试图使用最新版本的OpenCV来计算图像中有多少枚硬币,但是我在阴影中挣扎。

正在使用Canny Edge检测器方法,但是正如您在第二幅图像中看到的那样,由于阴影,该方法无法正常工作...关于如何处理此问题的任何想法?

enter image description here

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)

enter image description here

最佳答案

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

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)

enter image description here

关于opencv - 使用opencv删除硬币阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61677816/

相关文章:

opencv - 如何去除图像数字之间不需要的噪声

python - 计算图像对象的中心点以确定方向

qt - cv::Mat 到 QImage 的转换

android - 如何获得轮廓的质心?安卓opencv

c++ - 对 CV_16S 类型的图像进行去噪

opencv - Haar Classifier 正像集澄清

image-processing - 带填充的平均池的期望行为是什么?

php - 将图像添加到自动完成建议

c - 如何为不同范围的值创建查找表