-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess_image.py
57 lines (42 loc) · 1.25 KB
/
preprocess_image.py
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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 16 18:30:46 2018
This file contains functions to preprocess an image
@author: deric
"""
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
def get_sep_data(dataset):
"""Set the column 'label' and the pixels apart.
Returns:
-labels
-pixels
"""
labels = dataset['label']
dataset_images = dataset.drop('label', axis=1)
return labels, dataset_images
def vec_to_matrix(row):
"""Transform a vector representation of an image into a matrix one"""
image = np.reshape(row, (-1, 28)).astype(np.uint8)
return image
def image_smoother(img, t=70):
"""
Parameters
img: numpy.array
Matrix containing the pixel values of an image (eventually
containing noise)
Returns
denoised: numpy.array
img without noise
"""
# Denoising (to handle "stains")
img = cv.medianBlur(img, 5)
# Thresholding the image
_, img = cv.threshold(img, t, 255, cv.THRESH_BINARY)
return img
def plot_preprocessed(img):
"""Apply the preprocessing and plot the result"""
img = image_smoother(img, 25)
plt.imshow(img, cmap="Greys")
plt.show()