[英]OpenCV Python, remove shadow and get contour
I need to get circles' contour from the image.我需要从图像中获取圆圈的轮廓。 But there is a shadow gradient.(top is bright, bottom is dark) So topside background's GL is same as bottom side circle's GL.
但是有一个阴影渐变。(顶部是亮的,底部是暗的)所以顶部背景的 GL 与底部圆形的 GL 相同。
I also have the back side image without circle and try this code我也有没有圆圈的背面图像并尝试此代码
import cv2
import matplotlib.pyplot as plt
src = './test.jpg'
img = cv2.imread(src, cv2.IMREAD_GRAYSCALE)
back_src = './back.jpg'
back = cv2.imread(back_src, cv2.IMREAD_GRAYSCALE)
diff_img = 255-cv2.absdiff(img, back)
cv2.normalize(diff_img, norm_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
th, threshed = cv2.threshold(norm_img, 245, 255, cv2.THRESH_BINARY)
In this image, if i set threshold 245, i can get perfect binary image.在这张图片中,如果我设置阈值 245,我可以获得完美的二值图像。 But in the real images(grayscale), they are noisy so it's hard to set each threshold.
但在真实图像(灰度)中,它们很嘈杂,因此很难设置每个阈值。
I think there's a better way.我认为有更好的方法。 please help me....
请帮我....
You can use adaptive thresholding for this:您可以为此使用自适应阈值:
gray = cv2.imread('/path/to/your/image/circles_0001.jpeg', cv2.COLOR_BGR2GRAY)
th = cv2.adaptiveThreshold(gray,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,11,2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.