本文整理匯總了Python中cv2.absdiff方法的典型用法代碼示例。如果您正苦於以下問題:Python cv2.absdiff方法的具體用法?Python cv2.absdiff怎麽用?Python cv2.absdiff使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cv2
的用法示例。
在下文中一共展示了cv2.absdiff方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: segment
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def segment(image, threshold=25):
global bg
# find the absolute difference between background and current frame
diff = cv2.absdiff(bg.astype("uint8"), image)
# threshold the diff image so that we get the foreground
thresholded = cv2.threshold(diff, threshold, 255, cv2.THRESH_BINARY)[1]
# get the contours in the thresholded image
(_, cnts, _) = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# return None, if no contours detected
if len(cnts) == 0:
return
else:
# based on contour area, get the maximum contour which is the hand
segmented = max(cnts, key=cv2.contourArea)
return (thresholded, segmented)
#-----------------
# MAIN FUNCTION
#-----------------
示例2: main
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def main():
capture = cv2.VideoCapture(0)
_, image = capture.read()
previous = image.copy()
while (cv2.waitKey(1) < 0):
_, image = capture.read()
diff = cv2.absdiff(image, previous)
#image = cv2.flip(image, 3)
#image = cv2.norm(image)
_, diff = cv2.threshold(diff, 32, 0, cv2.THRESH_TOZERO)
_, diff = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY)
diff = cv2.medianBlur(diff, 5)
cv2.imshow('video', diff)
previous = image.copy()
capture.release()
cv2.destroyAllWindows()
示例3: prediction
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def prediction(self, image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.GaussianBlur(image, (21, 21), 0)
if self.avg is None:
self.avg = image.copy().astype(float)
cv2.accumulateWeighted(image, self.avg, 0.5)
frameDelta = cv2.absdiff(image, cv2.convertScaleAbs(self.avg))
thresh = cv2.threshold(
frameDelta, DELTA_THRESH, 255,
cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
cnts = cv2.findContours(
thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
self.avg = image.copy().astype(float)
return cnts
示例4: diff_frames
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def diff_frames(self,previous_frame,image):
'''
diff value for two value,
determin if to excute the detection
:param previous_frame: RGB array
:param image: RGB array
:return: True or False
'''
if previous_frame is None:
return True
else:
_diff = cv2.absdiff(previous_frame, image)
diff=np.sum(_diff)/previous_frame.shape[0]/previous_frame.shape[1]/3.
if diff>self.diff_thres:
return True
else:
return False
示例5: diff_rect
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def diff_rect(img1, img2, pos=None):
"""find counters include pos in differences between img1 & img2 (cv2 images)"""
diff = cv2.absdiff(img1, img2)
diff = cv2.GaussianBlur(diff, (3, 3), 0)
edges = cv2.Canny(diff, 100, 200)
_, thresh = cv2.threshold(edges, 0, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
if not contours:
return None
contours.sort(key=lambda c: len(c))
# no pos provide, just return the largest different area rect
if pos is None:
cnt = contours[-1]
x0, y0, w, h = cv2.boundingRect(cnt)
x1, y1 = x0+w, y0+h
return (x0, y0, x1, y1)
# else the rect should contain the pos
x, y = pos
for i in range(len(contours)):
cnt = contours[-1-i]
x0, y0, w, h = cv2.boundingRect(cnt)
x1, y1 = x0+w, y0+h
if x0 <= x <= x1 and y0 <= y <= y1:
return (x0, y0, x1, y1)
示例6: get_match_confidence
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def get_match_confidence(img1, img2, mask=None):
if img1.shape != img2.shape:
return False
## first try, using absdiff
# diff = cv2.absdiff(img1, img2)
# h, w, d = diff.shape
# total = h*w*d
# num = (diff<20).sum()
# print 'is_match', total, num
# return num > total*0.90
if mask is not None:
img1 = img1.copy()
img1[mask!=0] = 0
img2 = img2.copy()
img2[mask!=0] = 0
## using match
match = cv2.matchTemplate(img1, img2, cv2.TM_CCOEFF_NORMED)
_, confidence, _, _ = cv2.minMaxLoc(match)
# print confidence
return confidence
示例7: segment
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def segment(image, threshold=25):
global bg
# find the absolute difference between background and current frame
diff = cv2.absdiff(bg.astype("uint8"), image)
# threshold the diff image so that we get the foreground
thresholded = cv2.threshold(diff, threshold, 255, cv2.THRESH_BINARY)[1]
# get the contours in the thresholded image
(_, cnts, _) = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# return None, if no contours detected
if len(cnts) == 0:
return
else:
# based on contour area, get the maximum contour which is the hand
segmented = max(cnts, key=cv2.contourArea)
return (thresholded, segmented)
#--------------------------------------------------------------
# To count the number of fingers in the segmented hand region
#--------------------------------------------------------------
示例8: matchAB
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def matchAB(fileA, fileB):
# 讀取圖像數據
imgA = cv2.imread(fileA)
imgB = cv2.imread(fileB)
# 轉換成灰色
grayA = cv2.cvtColor(imgA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imgB, cv2.COLOR_BGR2GRAY)
# 獲取圖片A的大小
height, width = grayA.shape
# 取局部圖像,尋找匹配位置
result_window = np.zeros((height, width), dtype=imgA.dtype)
for start_y in range(0, height-100, 10):
for start_x in range(0, width-100, 10):
window = grayA[start_y:start_y+100, start_x:start_x+100]
match = cv2.matchTemplate(grayB, window, cv2.TM_CCOEFF_NORMED)
_, _, _, max_loc = cv2.minMaxLoc(match)
matched_window = grayB[max_loc[1]:max_loc[1]+100, max_loc[0]:max_loc[0]+100]
result = cv2.absdiff(window, matched_window)
result_window[start_y:start_y+100, start_x:start_x+100] = result
plt.imshow(result_window)
plt.show()
示例9: segment
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def segment(image, threshold=25):
global bg
# find the absolute difference between background and current frame
diff = cv2.absdiff(bg.astype("uint8"), image)
# threshold the diff image so that we get the foreground
thresholded = cv2.threshold(diff,
threshold,
255,
cv2.THRESH_BINARY)[1]
# get the contours in the thresholded image
(cnts, _) = cv2.findContours(thresholded.copy(),
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# return None, if no contours detected
if len(cnts) == 0:
return
else:
# based on contour area, get the maximum contour which is the hand
segmented = max(cnts, key=cv2.contourArea)
return (thresholded, segmented)
開發者ID:SparshaSaha,項目名稱:Hand-Gesture-Recognition-Using-Background-Elllimination-and-Convolution-Neural-Network,代碼行數:25,代碼來源:ContinuousGesturePredictor.py
示例10: segment
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def segment(image, threshold=25):
global bg
# find the absolute difference between background and current frame
diff = cv2.absdiff(bg.astype("uint8"), image)
# threshold the diff image so that we get the foreground
thresholded = cv2.threshold(diff,
threshold,
255,
cv2.THRESH_BINARY)[1]
# get the contours in the thresholded image
(cnts, _) = cv2.findContours(thresholded.copy(),
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# return None, if no contours detected
if len(cnts) == 0:
return
else:
# based on contour area, get the maximum contour which is the hand
segmented = max(cnts, key=cv2.contourArea)
return (thresholded, segmented)
開發者ID:SparshaSaha,項目名稱:Hand-Gesture-Recognition-Using-Background-Elllimination-and-Convolution-Neural-Network,代碼行數:25,代碼來源:PalmTracker.py
示例11: background_subtraction
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def background_subtraction(previous_frame, frame_resized_grayscale, min_area):
"""
This function returns 1 for the frames in which the area
after subtraction with previous frame is greater than minimum area
defined.
Thus expensive computation of human detection face detection
and face recognition is not done on all the frames.
Only the frames undergoing significant amount of change (which is controlled min_area)
are processed for detection and recognition.
"""
frameDelta = cv2.absdiff(previous_frame, frame_resized_grayscale)
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
im2, cnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
temp = 0
for c in cnts:
# if the contour is too small, ignore it
if cv2.contourArea(c) > min_area:
temp = 1
return temp
示例12: motion1
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def motion1(new_frame, base):
motion = cv2.absdiff(base, new_frame)
gray = cv2.cvtColor(motion, cv2.COLOR_BGR2GRAY)
cv2.imshow('motion', gray)
ret, motion_mask = cv2.threshold(gray, 25, 255, cv2.THRESH_BINARY_INV)
blendsize = (3,3)
kernel = np.ones(blendsize,'uint8')
motion_mask = cv2.erode(motion_mask, kernel)
# lots
motion_mask /= 1.1429
motion_mask += 16
# medium
#motion_mask /= 1.333
#motion_mask += 32
# minimal
#motion_mask /= 2
#motion_mask += 64
cv2.imshow('motion1', motion_mask)
return motion_mask
示例13: motion3
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def motion3(frame, counter):
global last_frame
global static_mask
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if last_frame is None:
pass
else:
diff = cv2.absdiff(gray, last_frame)
cv2.imshow('motion3', diff)
if static_mask is None:
static_mask = np.float32(diff)
else:
if counter > 1000:
c = float(1000)
else:
c = float(counter)
f = float(c - 1) / c
static_mask = f*static_mask + (1.0 - f)*np.float32(diff)
mask_uint8 = np.uint8(static_mask)
cv2.imshow('mask3', mask_uint8)
ret, newmask = cv2.threshold(mask_uint8, 2, 255, cv2.THRESH_BINARY)
cv2.imshow('newmask', newmask)
last_frame = gray
# average of frames (the stationary stuff should be the sharpest)
示例14: FMCenterSurroundDiff
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def FMCenterSurroundDiff(self, GaussianMaps):
dst = list()
for s in range(2, 5):
now_size = GaussianMaps[s].shape
now_size = (now_size[1], now_size[0]) # (width, height)
tmp = cv2.resize(GaussianMaps[s + 3], now_size, interpolation=cv2.INTER_LINEAR)
nowdst = cv2.absdiff(GaussianMaps[s], tmp)
dst.append(nowdst)
tmp = cv2.resize(GaussianMaps[s + 4], now_size, interpolation=cv2.INTER_LINEAR)
nowdst = cv2.absdiff(GaussianMaps[s], tmp)
dst.append(nowdst)
return dst
# Constructing a Gaussian pyramid + taking center-surround differences
示例15: motionDetected
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import absdiff [as 別名]
def motionDetected(self, new_frame):
frame = self.preprocessInputFrame(new_frame)
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
gray = cv.GaussianBlur(gray, (21, 21), 0)
if self.prevFrame is None:
self.prevFrame = gray
return False
frameDiff = cv.absdiff(gray, self.prevFrame)
# kernel = np.ones((5, 5), np.uint8)
opening = cv.morphologyEx(frameDiff, cv.MORPH_OPEN, None) # noqa
closing = cv.morphologyEx(frameDiff, cv.MORPH_CLOSE, None) # noqa
ret1, th1 = cv.threshold(frameDiff, 10, 255, cv.THRESH_BINARY)
height = np.size(th1, 0)
width = np.size(th1, 1)
nb = cv.countNonZero(th1)
avg = (nb * 100) / (height * width) # Calculate the average of black pixel in the image
self.prevFrame = gray
# cv.DrawContours(currentframe, self.currentcontours, (0, 0, 255), (0, 255, 0), 1, 2, cv.CV_FILLED)
# cv.imshow("frame", current_frame)
ret = avg > self.threshold # If over the ceiling trigger the alarm
if ret:
self.updateMotionDetectionDts()
return ret