def console_run(ddepth, ksize, normalize, border): array = cv2.sqrBoxFilter(gui.vs, ddepth=ddepth, ksize=(ksize, ksize), normalize=normalize, borderType=border) gui.show(array)
def niBlackThreshold( src, blockSize, k ): mean = cv2.boxFilter(src,cv2.CV_32F,(blockSize, blockSize),borderType=cv2.BORDER_REPLICATE) sqmean = cv2.sqrBoxFilter(src, cv2.CV_32F, (blockSize, blockSize), borderType = cv2.BORDER_REPLICATE) variance = sqmean - (mean*mean) stddev = np.sqrt(variance) thresh = mean + stddev * float(-k) thresh = thresh.astype(src.dtype) k = (src>thresh)*255 k = k.astype(np.uint8) return k
def cv_SqrBoxFilter(input=('ImagePin', 0), ddepth=('IntPin', -1), xsize=('IntPin', 3), ysize=('IntPin', 3), normalize=('BoolPin', True), img=(REF, ('ImagePin', 0))): """ Blurs An image.""" image = cv2.sqrBoxFilter(input.image, ddepth, (xsize, ysize), normalize=normalize) img(image)
def niBlackThreshold( src, blockSize, k, binarizationMethod= 0 ): mean = cv2.boxFilter(src,cv2.CV_32F,(blockSize, blockSize),borderType=cv2.BORDER_REPLICATE) sqmean = cv2.sqrBoxFilter(src, cv2.CV_32F, (blockSize, blockSize), borderType = cv2.BORDER_REPLICATE) variance = sqmean - (mean*mean) stddev = np.sqrt(variance) thresh = mean + stddev * float(-k) thresh = thresh.astype(src.dtype) k = (src>thresh)*255 k = k.astype(np.uint8) return k # cv2.imshow()
def estimateOutputColorised(self): # get all local means in means = cv.boxFilter(src=self.input, ddepth=cv.CV_64F, ksize=tuple(self.filter_size), anchor=(-1, 1), normalize=True, borderType=cv.BORDER_REPLICATE) square_means = cv.sqrBoxFilter(src=self.input, ddepth=cv.CV_64F, ksize=tuple(self.filter_size), anchor=(-1, 1), normalize=True, borderType=cv.BORDER_REPLICATE) means2 = cv.multiply(means, means) variances = square_means - means2 avgVariance = cv.reduce(variances, 0, cv.REDUCE_SUM, -1) avgVariance = cv.reduce(avgVariance, 1, cv.REDUCE_SUM, -1) noiseVar = avgVariance / self.input.shape[0] * self.input.shape[ 1] * self.input.shape[2] noiseVar = np.reshape(noiseVar, (1, 3)) y = np.zeros(self.input.shape) for row in range(self.input.shape[0]): for col in range(self.input.shape[1]): y[row][col] = saturate_cast( means[row][col] + np.maximum(np.zeros(noiseVar[0].shape), variances[row][col] - noiseVar[0]) * (self.input[row][col] - means[row][col]) / np.maximum(variances[row][col], noiseVar[0])) return y
def estimateOutput(self): means = cv.boxFilter(src=self.input, ddepth=cv.CV_64F, ksize=tuple(self.filter_size), anchor=(-1, 1), normalize=True, borderType=cv.BORDER_REPLICATE) square_means = cv.sqrBoxFilter(src=self.input, ddepth=cv.CV_64F, ksize=tuple(self.filter_size), anchor=(-1, 1), normalize=True, borderType=cv.BORDER_REPLICATE) means2 = cv.multiply(means, means) # calculating the variance matrix variances = square_means - means2 # estimating noise variance by finding projections across length and width avgVarianceMat = cv.reduce(variances, 1, cv.REDUCE_SUM, -1) avgVarianceMat = cv.reduce(avgVarianceMat, 0, cv.REDUCE_SUM, -1) noiseVar = (avgVarianceMat / self.input.shape[0] * self.input.shape[1]).item(0) print(noiseVar) print(variances[0][0]) y = np.zeros(self.input.shape) for row in range(self.input.shape[0]): for col in range(self.input.shape[1]): y[row][col] = means[row][col] + max( 0, variances[row][col] - noiseVar) * (self.input[row][col] - means[row][col]) / max( variances[row][col], noiseVar) return y
def niBlackThreshold( src, maxValue = 255, binarizationtype = cv2.THRESH_BINARY, blockSize = 23, k = 1.0, binarizationMethod = BINARIZATION_METHOD.BINARIZATION_WOLF): #Input grayscale image assert len(src.shape) == 2 assert blockSize % 2 == 1 and blockSize > 1 if (binarizationMethod == BINARIZATION_METHOD.BINARIZATION_SAUVOLA): assert src.dtype == np.uint8 # Compute local threshold (T = mean + k * stddev) # using mean and standard deviation in the neighborhood of each pixel # (intermediate calculations are done with floating-point precision) # note that: Var[X] = E[X^2] - E[X]^2 mean = cv2.boxFilter(src, cv2.CV_32F, (blockSize,blockSize),src, (-1,-1), True, cv2.BORDER_REPLICATE) sqmean = cv2.sqrBoxFilter(src, cv2.CV_32F, (blockSize,blockSize), src, (-1,-1), True, cv2.BORDER_REPLICATE); variance = sqmean - mean * mean; variance[variance<0] = 0 stddev = np.sqrt(variance); if binarizationMethod == BINARIZATION_METHOD.BINARIZATION_NIBLACK: thresh = mean + stddev * k; elif binarizationMethod == BINARIZATION_METHOD.BINARIZATION_SAUVOLA: thresh = mean * (1. + k * (stddev / 128.0 - 1.)); elif binarizationMethod == BINARIZATION_METHOD.BINARIZATION_WOLF: srcMin = src.min() stddevMax = stddev.max() thresh = mean - k * (mean - srcMin - stddev * (mean - srcMin) / stddevMax); elif binarizationMethod == BINARIZATION_METHOD.BINARIZATION_NICK: sqrtVarianceMeanSum = np.sqrt(variance + sqmean) thresh = mean + k * sqrtVarianceMeanSum; else: assert "Unknown binarization method" thresh = thresh.astype(src.dtype) # Apply thresholding: ( pixel > threshold ) ? foreground : background if binarizationtype == cv2.THRESH_BINARY or binarizationtype == cv2.THRESH_BINARY_INV: # dst = (src > thresh) ? maxval : 0 # dst = (src > thresh) ? 0 : maxval cmpop = cv2.CMP_GT if binarizationtype == cv2.THRESH_BINARY else cv2.CMP_LE mask = cv2.compare(src, thresh, cmpop); mask = (mask==255) dst = np.zeros_like(src) dst[mask] = maxValue elif binarizationtype == cv2.THRESH_TRUNC: # dst = (src > thresh) ? thresh : src mask = cv2.compare(src, thresh, cv2.CMP_GT); mask = (mask==255) dst = src.copy() dst[mask] = thresh[mask] elif binarizationtype == cv2.THRESH_TOZERO or binarizationtype == cv2.THRESH_TOZERO_INV: # dst = (src > thresh) ? src : 0 cmpop = cv2.CMP_GT if binarizationtype == cv2.THRESH_TOZERO else cv2.CMP_LE mask = cv2.compare(src, thresh, cmpop) mask = (mask==255) dst = np.zeros_like(src) dst[mask] = src[mask] else: assert "Unknown threshold binarizationtype" return dst
img_32 = img.astype('float32') img_32 /= 255.0 # 方框滤波cv.boxFilter()和cv.sqrBoxFilter() # 进行归一化 img_box_norm = cv.boxFilter(img, -1, (3, 3), anchor=(-1, -1), normalize=True) # 不进行归一化 img_box = cv.boxFilter(img, -1, (3, 3), anchor=(-1, -1), normalize=False) # 进行归一化 points_sqr_norm = cv.sqrBoxFilter(points, -1, (3, 3), anchor=(-1, -1), normalize=True, borderType=cv.BORDER_CONSTANT) img_sqr_norm = cv.sqrBoxFilter(img, -1, (3, 3), anchor=(-1, -1), normalize=True, borderType=cv.BORDER_CONSTANT) # 不进行归一化 points_sqr = cv.sqrBoxFilter(points, -1, (3, 3), anchor=(-1, -1), normalize=False, borderType=cv.BORDER_CONSTANT) # 展示图像处理结果