python处理mat数据和处理png的区别_Python---利用scipy.misc等库对jpg以及png等图像数据预处理(用于深度学习喂数据)...

转载自:http://blog.csdn.net/qq_16949707/article/details/56306720import os

import glob

image_dir = "D:\\data\\ADEChallengeData2016"

file_glob = os.path.join(image_dir, "images", "training", '*.' + 'jpg')

file_list = []

file_list.extend(glob.glob(file_glob))

1

2

3

4

5

6

file_list_test = file_list[0:10]

1

import random

random.shuffle(file_list_test)

1

2

import numpy as np

import scipy.misc as misc

1

2

def read_and_transform(filename):

image = misc.imread(filename)

resize_image = misc.imresize(image,[224, 224], interp='nearest')

return np.array(resize_image)

1

2

3

4

images = np.array([read_and_transform(filename) for filename in file_list_test])

1

def find_annotations_image_path(image_path):

return image_path.replace("images","annotations").replace("jpg","png")

1

2

annotations_file_list_test = [find_annotations_image_path(image_path) for image_path in file_list_test]

1

annotations = np.array([np.expand_dims(read_and_transform(filename), axis=3) for filename in annotations_file_list_test])

1

batch_offset = 0

1

def next_batch(batch_size,batch_offset,images,annotations,epochs_completed):

start = batch_offset

batch_offset += batch_size

if batch_offset > images.shape[0]:

# Finished epoch

epochs_completed += 1

print("****************** Epochs completed: " + str(epochs_completed) + "******************")

# Shuffle the data

perm = np.arange(images.shape[0])

np.random.shuffle(perm)

images = images[perm]

annotations = annotations[perm]

# Start next epoch

start = 0

batch_offset = batch_size

end = batch_offset

print start,end

# return images[start:end], self.annotations[start:end]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

2 测试

2.1 测试next_batch送数据

next_batch(2,0,images,annotations,0)

1

0 2

1

2

next_batch(2,2,images,annotations,0)

1

2 4

1

2

next_batch(2,10,images,annotations,0)

1

****************** Epochs completed: 1******************

0 2

1

2

3

2.2 测试shuffle打乱数据

perm = np.arange(images.shape[0])

1

print perm

1

[0 1 2 3 4 5 6 7 8 9]

1

2

np.random.shuffle(perm)

1

print perm

1

[5 3 9 4 8 6 2 1 7 0]

1

2

images[0]

1

array([[[ 92, 57, 27],

[181, 145, 113],

[227, 189, 153],

...,

[100, 77, 71],

[200, 177, 171],

[194, 174, 167]],

[[ 82, 45, 18],

[ 85, 46, 17],

[176, 136, 101],

...,

[200, 177, 171],

[192, 172, 165],

[180, 160, 153]],

[[ 88, 47, 25],

[ 93, 53, 28],

[131, 90, 60],

...,

[202, 182, 175],

[201, 181, 174],

[163, 144, 137]],

...,

[[ 99, 48, 31],

[ 84, 33, 16],

[ 81, 30, 13],

...,

[170, 134, 108],

[166, 130, 104],

[163, 127, 101]],

[[ 67, 16, 0],

[ 98, 47, 30],

[ 97, 46, 29],

...,

[168, 132, 106],

[173, 137, 111],

[168, 132, 106]],

[[ 62, 11, 0],

[ 83, 32, 15],

[ 65, 14, 0],

...,

[169, 133, 107],

[164, 128, 102],

[165, 129, 103]]], dtype=uint8)

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

images = images[perm]

annotations = annotations[perm]

1

2

images[0]

1

array([[[ 50, 75, 80],

[ 50, 75, 80],

[ 51, 76, 81],

...,

[135, 165, 176],

[133, 163, 174],

[133, 163, 174]],

[[ 50, 75, 80],

[ 50, 75, 80],

[ 51, 76, 81],

...,

[135, 165, 176],

[133, 163, 174],

[133, 163, 174]],

[[ 49, 74, 79],

[ 49, 74, 79],

[ 50, 75, 80],

...,

[135, 165, 176],

[133, 163, 174],

[133, 163, 174]],

...,

[[ 4, 16, 12],

[ 4, 16, 12],

[ 4, 16, 12],

...,

[176, 196, 205],

[177, 197, 206],

[178, 198, 207]],

[[ 4, 16, 12],

[ 4, 16, 12],

[ 4, 16, 12],

...,

[175, 195, 204],

[176, 196, 205],

[176, 196, 205]],

[[ 4, 16, 12],

[ 4, 16, 12],

[ 4, 16, 12],

...,

[177, 197, 206],

[176, 196, 205],

[176, 196, 205]]], dtype=uint8)

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

2.3 测试np.expand_dims

annotations1 = np.array([read_and_transform(filename) for filename in annotations_file_list_test])

1

annotations1.shape

1

(10L, 224L, 224L)

1

2

annotations.shape

1

(10L, 224L, 224L, 1L)

1

2

annotations_file_list_test[0]

1

'D:\\data\\ADEChallengeData2016\\annotations\\training\\ADE_train_00000007.png'

1

2

file_list_test[0]

1

'D:\\data\\ADEChallengeData2016\\images\\training\\ADE_train_00000007.jpg'

1

2

image_path = annotations_file_list_test[0]

1

annotations_image = read_and_transform(image_path)

1

annotations_image.shape

1

(224L, 224L)

1

2

annotations_image

1

array([[ 0, 1, 1, ..., 6, 6, 6],

[135, 135, 135, ..., 6, 6, 6],

[ 0, 135, 135, ..., 6, 6, 6],

...,

[ 0, 11, 11, ..., 4, 4, 4],

[ 0, 11, 11, ..., 4, 4, 4],

[ 0, 11, 11, ..., 4, 4, 4]], dtype=uint8)

1

2

3

4

5

6

7

8

np.expand_dims(annotations_image,axis=3)

1

array([[[ 0],

[ 1],

[ 1],

...,

[ 6],

[ 6],

[ 6]],

[[135],

[135],

[135],

...,

[ 6],

[ 6],

[ 6]],

[[ 0],

[135],

[135],

...,

[ 6],

[ 6],

[ 6]],

...,

[[ 0],

[ 11],

[ 11],

...,

[ 4],

[ 4],

[ 4]],

[[ 0],

[ 11],

[ 11],

...,

[ 4],

[ 4],

[ 4]],

[[ 0],

[ 11],

[ 11],

...,

[ 4],

[ 4],

[ 4]]], dtype=uint8)

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

image_path.replace("images","annotations").replace("jpg","png")

1

'D:\\data\\ADEChallengeData2016\\annotations\\training\\ADE_train_00000007.png'

1

2

annotations_image_path = image_path.replace("images","annotations").replace("jpg","png")

1

annotations_image_dir = "D:\\data\\ADEChallengeData2016"

annotations_file_glob = os.path.join(image_dir, "annotations", "training", '*.' + 'png')

annotations_file_list = []

annotations_file_list.extend(glob.glob(file_glob))

1

2

3

4

len(annotations_file_list)

1

20210

1

2

annotations = np.array([np.expand_dims(read_and_transform(filename), axis=3) for filename in file_list_test])

1

annotations.shape

1

(10L, 224L, 224L, 3L, 1L)

1

2

len(images)

1

10

1

2

image_array = read_and_transform(file_list_test[0])

1

len(image_array)

1

224

1

2

image_array[0][:][:].shape

1

(224L, 3L)

1

2

image_array.shape

1

(224L, 224L, 3L)

1

2

2.4 测试misc的imread以及imresize

image = misc.imread(file_list_test[0])

1

image.shape

1

(735L, 512L, 3L)

1

2

resize_image = misc.imresize(image,[224, 224], interp='nearest')

1

import matplotlib.pyplot as plt

plt.figure()

plt.imshow(image)

plt.show()

1

2

3

4

import matplotlib.pyplot as plt

plt.figure()

plt.imshow(resize_image)

plt.show()

1

2

3

4

misc.imshow(image)

1

---------------------------------------------------------------------------

RuntimeError Traceback (most recent call last)

in ()

----> 1 misc.imshow(image)

D:\software\Anaconda2\lib\site-packages\scipy\misc\pilutil.pyc in imshow(arr)

440 os.unlink(fname)

441 if status != 0:

--> 442 raise RuntimeError('Could not execute image viewer.')

443

444

RuntimeError: Could not execute image viewer.

本文同步分享在 博客“Trent1985”(CSDN)。

如有侵权,请联系 support@oschina.cn 删除。

本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值