-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre_processing.py
More file actions
64 lines (51 loc) · 1.54 KB
/
pre_processing.py
File metadata and controls
64 lines (51 loc) · 1.54 KB
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
# -*- coding: utf8 -*-
__author__ = 'PowerDi'
__address__ = '578661971@qq.com'
import cv2
import numpy as np
def change_size(filename):
'''change the initial picture to the size 1280*967
Need openCV and numpy module(cv2)
:param filename: eg: test.bmp
:return: None
'''
fn = filename
image = cv2.imread(fn)
newimage = cv2.resize(image, (1280, 967), interpolation=cv2.INTER_AREA)
cv2.imwrite('1280_967.bmp', newimage)
return newimage
def change_to_gray(image):
'''
Change a RGB picture to a Gray picture
:param image: narray, 3-dimension picture
:return: narray, 2-dimension, gray picture
'''
myimg1 = image.copy()
gray_img = cv2.cvtColor(myimg1, cv2.COLOR_BGR2GRAY)
cv2.imwrite("gray.bmp", gray_img)
return gray_img
def cut(img, mode=1):
'''
Cut a picture to get its upper part or lower part
:param img: narray
:param mode: int, '1' for upper part, '2' for lower part, Default:'1'
:return: narray, picture after cutting
'''
h = img.shape[0]
w = img.shape[1]
if mode == 1:
cutimg = img[int(h / 2.2):int(h / 1.7), int(w / 3.8):int(w / 1.8) + 15] # up
else:
cutimg = img[int(h / 1.7):int(h / 1.4) + 7, int(w / 3.8):int(w / 1.8) + 15] # down
cv2.imwrite("after_cut.bmp", cutimg)
return cutimg
def pre_process(filename, mode=1):
'''
:param filename: string, name of the picture, eg: 'test.bmp'
:param mode: int, '1' for upper part, '2' for lower part, Default:'1'
:return: narray, picture after pre-processing
'''
newimage = change_size(filename)
gray_img = change_to_gray(newimage)
cutimg = cut(gray_img,mode)
return cutimg