def removeBackground(self, image):
gray = np.float32(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)) / 255
if self.background_gaussian is None or self.background_gaussian.shape[0] != Configuration.gaussian_kernel_size:
self.background_gaussian = cv2.getGaussianKernel(Configuration.gaussian_kernel_size, -1, cv2.CV_32F)
background = cv2.sepFilter2D(gray, cv2.CV_32F, self.background_gaussian, self.background_gaussian)
result = gray - background
result = result * self.mask
mi = np.min(result)
ma = np.max(result)
#result = (result - mi) / (ma - mi)
return result / ma
python类getGaussianKernel()的实例源码
def FrameSmoth(frame):
''' In this stage of algorithm we impliment the 'bluring' procces -
the function clculate the score of each frame of the interval (0.25 s) by execute the gaussian.
The goal of this proccess is to avoid 'False Positive' of ths frames hat we recognized as diffrent. '''
gaussian =cv2.getGaussianKernel(5,10)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray=cv2.filter2D(gray,-1,gaussian)
#gray=signal.convolve2d(gray, gaussian,mode='same')
gray=normalize(gray)
return gray
def modifiedLaplacian(img):
''''LAPM' algorithm (Nayar89)'''
M = np.array([-1, 2, -1])
G = cv2.getGaussianKernel(ksize=3, sigma=-1)
Lx = cv2.sepFilter2D(src=img, ddepth=cv2.CV_64F, kernelX=M, kernelY=G)
Ly = cv2.sepFilter2D(src=img, ddepth=cv2.CV_64F, kernelX=G, kernelY=M)
FM = np.abs(Lx) + np.abs(Ly)
return cv2.mean(FM)[0]
def detect(self, image):
floatimage = cv2.cvtColor(np.float32(image), cv2.COLOR_BGR2GRAY) / 255
if self.gaussian is None or self.gaussian.shape[0] != Configuration.log_kernel_size:
self.gaussian = cv2.getGaussianKernel(Configuration.log_kernel_size, -1, cv2.CV_32F)
gaussian_filtered = cv2.sepFilter2D(floatimage, cv2.CV_32F, self.gaussian, self.gaussian)
# LoG
filtered = cv2.Laplacian(gaussian_filtered, cv2.CV_32F, ksize=Configuration.log_block_size)
# DoG
#gaussian2 = cv2.getGaussianKernel(Configuration.log_block_size, -1, cv2.CV_32F)
#gaussian_filtered2 = cv2.sepFilter2D(floatimage, cv2.CV_32F, gaussian2, gaussian2)
#filtered = gaussian_filtered - gaussian_filtered2
mi = np.min(filtered)
ma = np.max(filtered)
if mi - ma != 0:
filtered = 1 - (filtered - mi) / (ma - mi)
_, thresholded = cv2.threshold(filtered, Configuration.log_threshold, 1.0, cv2.THRESH_BINARY)
self.debug = thresholded
thresholded = np.uint8(thresholded)
contours = None
if int(cv2.__version__.split('.')[0]) == 2:
contours, _ = cv2.findContours(thresholded, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
else:
_, contours, _ = cv2.findContours(thresholded, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
candidates = []
for i in range(len(contours)):
rect = cv2.boundingRect(contours[i])
v1 = rect[0:2]
v2 = np.add(rect[0:2], rect[2:4])
if rect[2] < Configuration.log_max_rect_size and rect[3] < Configuration.log_max_rect_size:
roi = floatimage[v1[1]:v2[1], v1[0]:v2[0]]
_, _, _, maxLoc = cv2.minMaxLoc(roi)
maxLoc = np.add(maxLoc, v1)
candidates.append(maxLoc)
self.candidates = candidates
return candidates