Image_Processing

opencv中感兴趣区域以及mask的使用

不规则区域提取

使用预先定义的ROI Matlab进行图像裁剪

CV2

RGB =(height, width, 3)

图片读取cv2.imread()第一个参数是文件路径,第二个参数指定读取方式

点击显/隐内容
1
2
3
4
import numpy as np
import cv2
# 以灰度模式读取图片
img = cv2.imread('lena.jpg',0) // cv2.IMREAD_GRAYSCALE
  • cv2.IMREAD_COLOR:加载彩色图片,这个是默认参数。
  • cv2.IMREAD_GRAYSCALE:以灰度模式加载图片。
  • cv2.IMREAD_UNCHANGED:包括alpha。
  • 上面三个flag分别对应的值为1,0,-1

使用cv2.imshow()显示一个图像窗口,窗口大小根据图像自动调整。第一个参数是窗口名称,第二个参数是图片。

使用函数cv2.imwrite()保存图像到文件,第一个参数是文件名,第二个参数是你要保存的文件

使用cv2.resize(img,(width, height))来改变图像大小。

VIDEOS

从视频中提取帧 ref

点击显/隐内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# coding=utf-8
# 全局变量
VIDEO_PATH = './1.avi' # 视频地址
EXTRACT_FOLDER = './extract_folder' # 存放帧图片的位置
EXTRACT_FREQUENCY = 100 # 帧提取频率
def extract_frames(video_path, dst_folder, index):
# 主操作
import cv2
video = cv2.VideoCapture()
if not video.open(video_path):
print("can not open the video")
exit(1)
count = 1
while True:
_, frame = video.read()
if frame is None:
break
if count % EXTRACT_FREQUENCY == 0:
save_path = "{}/{:>03d}.jpg".format(dst_folder, index)
cv2.imwrite(save_path, frame)
index += 1
count += 1
video.release()
# 打印出所提取帧的总数
print("Totally save {:d} pics".format(index-1))
def main():
# 递归删除之前存放帧图片的文件夹,并新建一个
import shutil
try:
shutil.rmtree(EXTRACT_FOLDER)
except OSError:
pass
import os
os.mkdir(EXTRACT_FOLDER)
# 抽取帧图片,并保存到指定路径
extract_frames(VIDEO_PATH, EXTRACT_FOLDER, 1)
if __name__ == '__main__':
main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import cv2
import shutil
import os
dataset = "handwaving"
inpath = "/Users/daniel/Desktop/%s/"%dataset
outpath = "/Users/daniel/Downloads/Nutstore/PythonFile/DATASET/KTH/%s/"%dataset
def extract_frames(video_path,dst_folder,index):
video = cv2.VideoCapture()
if not video.open(video_path):
print("can not open the video")
exit(1)
success = True
while success:
success, frame = video.read()
save_path = dst_folder+"%d.png"%index
cv2.imwrite(save_path, frame)
index += 1
video.release()
# 打印出所提取帧的总数
print("Totally save {:d} pics".format(index-1))
return index
def main():
index = 1
try:
shutil.rmtree(outpath)
except OSError:
pass
if not os.path.exists(outpath):
os.makedirs(outpath)
for i in range(1, 21):
for j in range(1, 5):
video_path = inpath + "person%02d_handwaving_d%d_uncomp.avi" % (i, j)
index = extract_frames(video_path,outpath,index)
if __name__=="__main__":
main()

Depth Images

rgbd images processing

Loading and Visualizing Kinect Depth images in Matlab

MSR3D-Daily Activities Dataset

点击显/隐内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
clear;close all;clc;
binPath = '/Users/daniel/Downloads/Nutstore/PythonFile/DATASET/msr3d/Depth/';
output_path = '/Users/daniel/Downloads/Nutstore/PythonFile/DATASET/msr3d/Depth_Image/';
idx = 0;
for si = 1:1
for ai = 1:1
for ei = 1:1
%%%%%%%%%%%%%
%%%%%%%%%%%%%
acsr = num2str(ai,'%02d');
susr = num2str(si,'%02d');
exsr = num2str(ei,'%02d');
%%%%%% getsr(ai,si,ei) convert ai,si,ei to double bits
%%%%%% for example, if ai=3, acsr is 03
%%%%%%%%%%%
binfile = [binPath,'a',acsr,'_s',susr,'_e',exsr,'_depth.bin'];
if ~exist(binfile,'file')
disp('error');
continue;
end
fileread = fopen(binfile);
if fileread<0
disp('no such file.');
return;
end
header = fread(fileread,3,'uint=>uint');
nnof = header(1); ncols = header(2); nrows = header(3);
depths = zeros(ncols, nrows, nnof);
for f = 1:nnof
idx = idx+1;
fprintf('Processing %d/10, %d/16, %d/2, %d/%d\n',si,ai,ei,f,nnof);
frame = zeros( ncols, nrows);
for row = 1:nrows
tempRow = fread(fileread, ncols, 'uint=>uint');
tempRowID = fread(fileread, ncols, 'uint8');%%%%%
frame(:,row) = tempRow;
end
depth(:,:,f) = frame;
[m,n] = size(depth(:,:,f));
pic_name = [output_path,num2str(idx),'.jpg'];
pic = imrotate(depth(:,:,f),270);
%imshow(depth(:,:,f));
%imwrite(uint8(pic),pic_name);
%plot3k(depth(:,:,1));
% fig = imagesc(depth(:,:,1)); axis off; % truesize;
% title('Depth in color map');
% colormap parula;
% c = colorbar; c.Label.String = 'Distance in mm';
% clear tempRow tempRowID;
% [counts,binLocations] = imhist(depth(:,:,1),4000);
% binLocations = binLocations(1:4000,1);
% bar( binLocations,counts); xlim([450 4000])
% title('Histogram of depth values');
end
fclose(fileread);
end
end
end