import cv2 as cv2 import numpy as np from PIL import Image
Video = "/run/media/ken/Data/Vlog/Tank_rasbbery/test.avi" Picture = "/home/ken/Pictures/test.gif"
Width_out = int(1920*.9) Height_out = int(1080*.9) PB_h_r = 1- 0.05
PB_img_wr = .1 img = cv2.imread(Picture,1)
point_color = (0, 0, 255) lineType = 8 thickness_r = 0.005
cap=cv2.VideoCapture(Video)
PB_H = int(Height_out*PB_h_r) Video_w = cap.get(cv2.CAP_PROP_FRAME_WIDTH) frame_total = cap.get(cv2.CAP_PROP_FRAME_COUNT) Num = 0
thickness = int(thickness_r * Height_out)
def img_deal(img_target, img_logo, row1, row2, col1, col2, THRE): img_logo_gray = cv2.cvtColor(img_logo, cv2.COLOR_BGR2GRAY) print(img_logo_gray[0]) ret, img_logo_mask = cv2.threshold(img_logo_gray, THRE, 255, cv2.THRESH_BINARY) img_logo_mask1 = cv2.bitwise_not(img_logo_mask)
img_roi = img_target[col1:col2, row1:row2].copy()
img_res0 = cv2.bitwise_and(img_roi, img_roi, mask=img_logo_mask) img_res1 = cv2.bitwise_and(img_logo, img_logo, mask=img_logo_mask1) img_res2 = cv2.add(img_res0, img_res1) img_target[col1:col2, row1:row2] = img_res2[:, :]
return img_target
List = [] im = Image.open(Picture) im.seek(1) try: while 1: img = cv2.cvtColor(np.asarray(im.convert()),cv2.COLOR_RGB2BGR) PB_img_hr = (img.shape[0]/ img.shape[1] )* PB_img_wr PB_img_w = int(PB_img_wr * Height_out) PB_img_h = int(PB_img_hr * Height_out) if PB_img_h%2 == 1: PB_img_h += 1 frame_img_h1 = PB_H -int(PB_img_h/2) frame_img_h2 = PB_H +int(PB_img_h/2)
List += [cv2.resize(img, (PB_img_w,PB_img_h), interpolation = cv2.INTER_AREA)] im.seek(im.tell()+1) except EOFError: pass
def CV_mask(img1, img2, rows1, rows2, cols1, cols2, THRE): roi = img1[rows1:rows2, cols1:cols2]
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) print(img2gray[0]) ret, mask = cv2.threshold(img2gray, THRE, 255, cv2.THRESH_BINARY_INV) mask_inv = cv2.bitwise_not(mask)
img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv) dst = cv2.add(img1_bg, img2) img1[rows1:rows2, cols1:cols2] = dst
return img1
while Num <= frame_total-1: Width = int((( Num/frame_total)*(Width_out-PB_img_w))) + int(PB_img_w*.5) Num +=1 ptLeftTop = (0, PB_H) img = List[int(Num%len(List))] img = cv2.flip(img,1 ) print(Num, frame_total, Width, PB_H, print(img[0][0])) ptRightBottom = (Width, PB_H) ret,frame=cap.read() frame = cv2.resize(frame, (Width_out,Height_out), interpolation = cv2.INTER_AREA) frame = cv2.rectangle(frame, ptLeftTop, ptRightBottom, point_color, thickness, lineType) Width_png = int((( Num/frame_total)*(Width_out-PB_img_w))) frame = img_deal(frame, img, 0+Width_png, PB_img_w+Width_png, frame_img_h1, frame_img_h2, 207) cv2.imshow("video",frame) if cv2.waitKey(25)&0xFF==ord('q'): cv2.destroyAllWindows() break
print("Mission Down")
|