-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcounting_file.py
executable file
·138 lines (108 loc) · 4.07 KB
/
counting_file.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Created on Wed Feb 26 01:54:04 2020
# @author: Fernando Camussi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
""" Fichero de conteo """
import cv2 as cv
import os.path
from PyQt5.QtCore import QPoint
from PyQt5.QtGui import QImage, QPixmap
class CountingFile():
""" Carga y guarda la información relacionada con el conteo """
def __init__(self, file=None):
self.points = []
if file is None:
self.file = None
self.imageFile = None
self.imageMask = None
self.image = None
self.mask = None
self.ppm = None
else:
self.file = file
f = open(self.file, 'r')
imageFile = f.readline().rstrip()
maskFile = f.readline().rstrip()
ppm = int(f.readline().rstrip())
self.imageFile = os.path.dirname(self.file) + '/' + imageFile
self.maskFile = os.path.dirname(self.file) + '/' + maskFile
self.image = cv.imread(self.imageFile, cv.IMREAD_COLOR)
self.mask = cv.imread(self.maskFile, cv.IMREAD_GRAYSCALE)
self.ppm = ppm
for l in f:
p = list(map(int, l.split(',')))
self.points.append(QPoint(p[0],p[1]))
f.close()
def getFile(self):
""" Retorna el nombre del fichero de conteo """
return self.file
def getImage(self):
""" Retorna la imagen """
return self.image
def setImage(self, image):
""" Fija la imagen """
self.image = image
def getMask(self):
""" Retorna la máscara """
return self.mask
def setMask(self, mask):
""" Fija la máscara """
self.mask = mask
def getPoints(self):
""" Retorna una lista con los puntos de coordenadas de las plantas """
return self.points
def setPoints(self, points):
""" Fija la lista de puntos de coordenadas de las plantas """
self.points = points
def getPPM(self):
""" Retorna pixeles por metro """
return self.ppm
def setPPM(self, ppm):
""" Fija pixeles por metro """
self.ppm = ppm
def saveFile(self, file):
""" Guarda los puntos, la máscara y la imagen
"""
self.file = file
f = open(self.file, 'w')
if self.imageFile is None:
self.imageFile = os.path.splitext(self.file)[0] + '.image.tif'
self.maskFile = os.path.splitext(self.file)[0] + '.mask.tif'
cv.imwrite(self.imageFile, self.image)
cv.imwrite(self.maskFile, self.mask)
f.write(os.path.basename(self.imageFile) + '\n')
f.write(os.path.basename(self.maskFile) + '\n')
f.write(str(self.ppm) + '\n')
for p in self.points:
f.write(str(p.x()) + ',' + str(p.y()) + '\n')
f.close()
def cvImageToQPixmap(cv_image):
""" Convierte una imagen en formato OpenCV a una imagen en formato
QPixmap
"""
cv_image = cv.cvtColor(cv_image, cv.COLOR_BGR2RGB)
[h,w,_] = cv_image.shape
image = QImage(cv_image.data, w, h, w*3, QImage.Format_RGB888)
return QPixmap(image)
def isRGB(cv_image):
""" Retorna True si la imagen es RGB
"""
channel_R = cv_image[:,:,2]
channel_G = cv_image[:,:,1]
channel_B = cv_image[:,:,0]
return not ((channel_R == channel_G).all() and \
(channel_G == channel_B).all())