-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateLowResolution.py
More file actions
98 lines (70 loc) · 3.42 KB
/
Copy pathGenerateLowResolution.py
File metadata and controls
98 lines (70 loc) · 3.42 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#Importando as bibliotecas necessárias
import cv2 as cv
import imutils
import numpy as np
import random as rd
import os
import csv
import secrets
import cv2
#Criação da função
def GenerateLowResolution(folder, qty = 5, rf=2, k=(3,3), theta=(-1,1), tx=(-10,10), ty=(-10,10)):
#Varredura das imagens na pasta
for file in os.listdir(folder):
#Leitura da imagem
img = cv.imread(os.path.join(folder,file))
#Validação das imagens lidas
if img is not None:
#Criação da pasta LR
folder_name = f'LR - {file}'
path=os.path.join(folder,folder_name)
#Verificação da existência da pasta
if os.path.exists(path) == False:
#Criação da Pasta LR quando ainda não existir
os.makedirs(path)
for i in range(0,qty):
img_gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
#Gerando aleatóriamente o ângulo (em graus)
ang = rd.uniform(theta[0],theta[1])
#Rotacionando as imagens e
rotated = imutils.rotate(img_gray, angle=ang)
#Gerando aleatóriamente as componentes de tranlação em y e em x
transx = rd.randint(tx[0], ty[1])
transy = rd.randint(ty[0], ty[1])
#Definindo a matriz de translação
M = np.float32([[1, 0, transx], [0, 1, transy]])
#Transladando a imagem
shifted = cv.warpAffine(rotated, M, (rotated.shape[1], rotated.shape[0]))
#Leitura das dimensões da imagem
height = img.shape[0]
width = img.shape[1]
#Definição dos novos tamanhos da imagem (divisão inteira)
nheight = height//rf
nwidth = width//rf
#Redimensionamento 1: Alterando as dimensões da imagem usando os novos tamanhos
nimg = cv.resize(shifted, (nwidth,nheight))
#Aplicação de filtros de borramento
nimgG= cv.GaussianBlur(nimg, k, 0)
mu, sigma = 0, 0.1 # mean and standard deviation
s = np.random.normal(mu, sigma, (nheight,nwidth))
imgF= nimg + s
#Criação do id hexadecimal aleatório
id = secrets.token_hex(3)
#Salvando a imagem em uma pasta existente
cv.imwrite(f"{path}/{id}.png",imgF)
#Diretório do arquivo csv
arq_csv = 'LR.csv'
#Verificação da existência do arquivo
if os.path.exists(arq_csv) == True:
#Adição de dados no arquivo
csv_file = open('LR.csv', 'a',newline = '')
writer = csv.writer(csv_file)
writer.writerow([id, file, rf, k, ang, transx, transy])
else:
#Criação do arquivo e adição dos dados
with open('LR.csv', 'w', newline='') as csv_file:
writer = csv.writer(csv_file)
field = ["id", "original image", "rf", "k", "theta", "tx", "ty"]
writer.writerow(field)
writer.writerow([id, file, rf, k, ang, transx, transy])
GenerateLowResolution("imagens")