[英]OpenCV - Adaptive-thresholding / effective noise reduction?
I am new to OpenCV and I just read about cv2.adaptiveThreshold()
and decided to try it out.我是 OpenCV 的新手,我刚刚阅读了
cv2.adaptiveThreshold()
并决定尝试一下。 Sadly there is this huge amount of noise I cannot seem to get rid of.可悲的是,我似乎无法摆脱这种巨大的噪音。
What are some effective ways to reduce noise so I can draw proper contours?有哪些有效的方法可以减少噪音,以便绘制正确的轮廓? What is the best practice and why?
最佳做法是什么,为什么?
Here is the snippet:这是片段:
import cv2 import numpy as np ##################################### winWidth = 640 winHeight = 840 brightness = 100 cap = cv2.VideoCapture(0) cap.set(3, winWidth) cap.set(4, winHeight) cap.set(10, brightness) kernel = (5, 5) ########################################### def preprocessing(frame): imgGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # mask = cv2.inRange(imgHsv, lower, upper) imgBlurred = cv2.GaussianBlur(imgGray, kernel, 1) gaussC = cv2.adaptiveThreshold(imgBlurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) imgDial = cv2.dilate(gaussC, kernel, iterations=3) imgErode = cv2.erode(imgDial, kernel, iterations=1) return imgDial def getcontours(imPrePro): contours, hierarchy = cv2.findContours(imPrePro, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) for cnt in contours: cv2.drawContours(imgCon, cnt, -1, (255, 0, 0), 3) ################################################### while (cap.isOpened()): success, frame = cap.read() if success == True: frame = cv2.flip(frame, 1) imgCon = frame.copy() imPrePro = preprocessing(frame) getcontours(imPrePro) cv2.imshow("Preprocessed", imPrePro) cv2.imshow("Original", imgCon) if cv2.waitKey(1) & 0xFF == ord("q"): break
I think it is best if we look at the blockSize
and C
parameters.我认为最好看一下
blockSize
和C
参数。
blockSize: Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
blockSize:像素邻域的大小,用于计算像素的阈值:3、5、7 等。
C: Constant subtracted from the mean or weighted mean (see the details below).
C:从平均值或加权平均值中减去常数(详见下文)。 Normally, it is positive but may be zero or negative as well.
通常,它是正数,但也可能为零或负数。
In your example you set C
to 2:在您的示例中,您将
C
设置为 2:
gaussC = cv2.adaptiveThreshold(imgBlurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
As you can see, you need to play with blockSize
and C
parameters to get the desired result from adaptive-threshold.如您所见,您需要使用
blockSize
和C
参数来从自适应阈值获得所需的结果。
In this question, we achieve less-noise by increasing C
parameter.在这个问题中,我们通过增加
C
参数来减少噪音。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.