Staty commited on
Commit
b540897
·
verified ·
1 Parent(s): b818573

Upload 3 files

Browse files
Files changed (3) hide show
  1. extract/1.py +28 -0
  2. extract/2.py +19 -0
  3. extract/getim.py +69 -0
extract/1.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import os
3
+ # 其它格式的图片也可以
4
+ img_array = []
5
+ path = "C:/Users/hasee/Downloads/Video/extract/image/" # 图片文件路径 # 获取该目录下的所有文件名
6
+ for i in range(250):
7
+ #挨个读取图片
8
+ img = cv2.imread(path+str(i)+".jpg")
9
+ #获取图片高,宽,通道数信息
10
+ height, width, layers = img.shape
11
+ #设置尺寸
12
+ size = (width, height)
13
+ #将图片添加到一个大“数组中”
14
+ img_array.append(img)
15
+ print("this is ok")
16
+ # avi:视频类型,mp4也可以
17
+ # cv2.VideoWriter_fourcc(*'DIVX'):编码格式,不同的编码格式有不同的视频存储类型
18
+ # fps:视频帧率
19
+ # size:视频中图片大小
20
+ fps=25
21
+ videopath='C:/Users/hasee/Downloads/Video/extract/test10.avi'#图片保存地址及格式
22
+ out1 = cv2.VideoWriter(videopath,cv2.VideoWriter_fourcc(*'DIVX'),fps, size)
23
+ for i in range(len(img_array)):
24
+ #写成视频操作
25
+ out1.write(img_array[i])
26
+ out1.release()
27
+ print("all is ok")
28
+
extract/2.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+
3
+ vc = cv2.VideoCapture("../v_c_0_10.mp4")
4
+
5
+ if vc.isOpened():
6
+ ret, frame = vc.read()
7
+ else:
8
+ ret = False
9
+
10
+
11
+ # loop read video frame
12
+ i=0
13
+ while ret:
14
+ ret, frame = vc.read()
15
+ if i<250:
16
+ image_path="./image/"+str(i)+".jpg"
17
+ cv2.imwrite(image_path, frame)
18
+ i=i+1
19
+ cv2.waitKey(40)
extract/getim.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import division
2
+ from PIL import Image
3
+ import argparse
4
+ import torch
5
+ import torchvision
6
+ import torch.nn as nn
7
+ import numpy as np
8
+ import torchvision.utils as vutils
9
+ import matplotlib.pyplot as plt
10
+ import numpy as np
11
+ import cv2
12
+
13
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
+
15
+
16
+ def remove_the_blackborder(image_p):
17
+ image = cv2.imread(image_p,cv2.IMREAD_COLOR) # 读取图片
18
+ img = cv2.medianBlur(image, 5) # 中值滤波,去除黑色边际中可能含有的噪声干扰
19
+ b = cv2.threshold(img, 3, 255, cv2.THRESH_BINARY) # 调整裁剪效果
20
+ binary_image = b[1] # 二值图--具有三通道
21
+ binary_image = cv2.cvtColor(binary_image, cv2.COLOR_BGR2GRAY)
22
+ try:
23
+ edges_y, edges_x = np.where(binary_image == 255) ##h, w
24
+ bottom = min(edges_y)
25
+ top = max(edges_y)
26
+ height = top - bottom
27
+
28
+ left = min(edges_x)
29
+ right = max(edges_x)
30
+ height = top - bottom
31
+ width = right - left
32
+
33
+ res_image = image[bottom:bottom + height, left:left + width]
34
+
35
+ except:
36
+ res_image=image
37
+ print(image_p)
38
+ return res_image
39
+
40
+
41
+
42
+ def load_image(image_path, transform=None, max_size=None, shape=None):
43
+ # image=Image.fromarray(image)
44
+ # image = cv2.imread(image_path)
45
+ image = Image.open(image_path)
46
+ #image = Image.open(image_path) # 读入图片, 下面是一些图片的预处理操作
47
+ if max_size:
48
+ scale = max_size / max(image.size)
49
+ size = np.array(image.size) * scale
50
+ # print(size) [400. 306.78733032]
51
+ image = image.resize(size.astype(int), Image.ANTIALIAS) # 改变图片大小
52
+
53
+ if shape:
54
+ image = image.resize(shape, Image.LANCZOS) # Image.LANCZOS是插值的一种方式
55
+
56
+ if transform:
57
+ # print(image) # PIL的JpegImageFile格式(size=(W,H))
58
+ image = transform(image).unsqueeze(0)
59
+ # print(image.shape) # [C, H, W]
60
+ return image
61
+
62
+
63
+
64
+ # fcontent = load_image("./image/0.jpg", transform, shape=[256, 256])
65
+ # for i in range(249):
66
+ # name="./image/"+str(i+1)+".jpg"
67
+ # content = load_image(name, transform, shape=[256, 256])
68
+ # fcontent=torch.cat((fcontent,content),-4)
69
+ # print(fcontent.shape)