具体代码如下
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 15 10:48:09 2019
@author: 01
"""
#!/usr/bin/env python
import cv2
import numpy as np
ph1 = "img/1.jpg"
ph2 = "img/2.jpg"
ph3 = "img/3.jpg"
ph4 = "img/4.jpg"
ph5 = "img/5.jpg"
ph6 = "img/6.jpg"
ph7 = "img/7.jpg"
ph8 = "img/8.jpg"
threshod= 40
s1 = cv2.imread(ph1,0)
s2 = cv2.imread(ph2,0)
s3 = cv2.imread(ph3,0)
s4 = cv2.imread(ph4,0)
s5 = cv2.imread(ph5,0)
s6 = cv2.imread(ph6,0)
s7 = cv2.imread(ph7,0)
s8 = cv2.imread(ph8,0)
emptyimg1 = np.zeros(s1.shape,np.uint8)
emptyimg2 = np.zeros(s1.shape,np.uint8)
emptyimg3 = np.zeros(s1.shape,np.uint8)
emptyimg4 = np.zeros(s1.shape,np.uint8)
emptyimg5 = np.zeros(s1.shape,np.uint8)
emptyimg6 = np.zeros(s1.shape,np.uint8)
emptyimg7 = np.zeros(s1.shape,np.uint8)
emptyimg8 = np.zeros(s1.shape,np.uint8)
emptyimg9 = np.zeros(s1.shape,np.uint8)
def pic_sub(dest,s1,s2):
for x in range(dest.shape[0]):
for y in range(dest.shape[1]):
if(s2[x,y] > s1[x,y]):
dest[x,y] = s2[x,y] - s1[x,y]
else:
dest[x,y] = s1[x,y] - s2[x,y]
if(dest[x,y] < threshod):
dest[x,y] = 0
else:
dest[x,y] = 255
def pic_add(dest,s1,s2):
for x in range(dest.shape[0]):
for y in range(dest.shape[1]):
if(s2[x,y] > s1[x,y]):
dest[x,y] = s2[x,y] - s1[x,y]
else:
dest[x,y] = s1[x,y] - s2[x,y]
if(dest[x,y] < threshod):
dest[x,y] = 0
else:
dest[x,y] = 255
pic_sub(emptyimg1,s1,s3)
pic_sub(emptyimg2,s2,s4)
pic_sub(emptyimg3,s3,s5)
pic_sub(emptyimg4,s4,s6)
pic_add(emptyimg5,emptyimg1,emptyimg2)
pic_add(emptyimg6,emptyimg3,emptyimg4)
pic_add(emptyimg7,emptyimg5,emptyimg6)
cv2.namedWindow("s1")
cv2.namedWindow("s2")
cv2.namedWindow("result")
cv2.imshow("s1",emptyimg1)
cv2.imshow("s2",emptyimg2)
cv2.imshow("result",emptyimg7)
cv2.waitKey(0)
cv2.destroyAllWindows()
如果有一个视频,可以从中拿出相邻的两帧图像,最简单的就是进行图像相减,可以找到边界点效果
(LK算法更好)