-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyfia_area_w0.py
executable file
·105 lines (84 loc) · 3.8 KB
/
pyfia_area_w0.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
#! /usr/bin/python
# -*- coding: utf-8 -*-
import math
import fnmatch
import os
from PIL import Image # install from repositories
import numpy # install from repositories
import mahotas # see pyFIA's wiki for installing
#
# This is a version of pyFIA for area quantification, instead densitometry, for each object in a determinated path:
# 1. Reads the matrix of pixels from green canal (it can be modified to red or blue) or the only channel if it is grayscale.
# 2. Estimates the threshold between the object and the background by Otsu's method using the mahotas library.
# 3. Counts the pixels with a value lower the threshold.
#
def algoritmo(ruta, foto):
def calcular_iod(pixmap):
count_pix = 0
for pixels in pixmap:
if int(pixels) <= umbral: # pixels lower the threshold and different of 0
count_pix += 1 # count 1 for each pixel
return count_pix, umbral
direccion = ruta +"/" + foto # Full path of the image
if fnmatch.fnmatch(foto, '*.tif'):
imagen = Image.open(str(direccion)) # open TIFF image
if imagen.mode == "L":
pixmap = list(imagen.getdata()) # make a list with the values of one channel
elif imagen.mode == "RGB" or "RGBA":
pixmap = list(imagen.getdata(1)) # make a list with the values of indicated channel
# you can change de value: 0 = red, 1 = green, 2 = red
dna = numpy.array(pixmap, dtype='uintc') # from lista to numpy array
umbral = mahotas.thresholding.otsu(dna) # threshold by Otsu's method
(count_pix, umbral) = calcular_iod(pixmap) # applies the algorithm
if fnmatch.fnmatch(foto, '*.jpg'):
imagen = Image.open(str(direccion)) # open JPEG image
if imagen.mode == "L":
pixmap = list(imagen.getdata()) # make a list with the values of one channel
elif imagen.mode == "RGB" or "RGBA":
pixmap = list(imagen.getdata(1)) # make a list with the values of indicated channel
# you can change de value: 0 = red, 1 = green, 2 = red
dna = numpy.array(pixmap, dtype='uintc') # from lista to numpy array
# umbral = mahotas.thresholding.otsu(dna) # threshold by Otsu's method
umbral = 150
(count_pix, umbral) = calcular_iod(pixmap) # applies the algorithm
elif fnmatch.fnmatch(foto, '*.txt'):
imagen = open(str(direccion), 'r') # opens text file
leer = imagen.read() # reads
pixmap = leer.split() # list with the values of the file
dna = numpy.array(pixmap, dtype='uintc') # from lista to numpy array
umbral = mahotas.thresholding.otsu(dna) # threshold by Otsu's method
# umbral = 230
(count_pix, umbral) = calcular_iod(pixmap) # apply the algorithm
return count_pix, umbral
def abre_salida(ruta):
f_salida = ruta + "/output"
salida = open(f_salida ,"w")
salida.write("%s \t %s \t %s" % ("imagen", "pixels" , "umbral" "\n"))
salida.write("-" * 40)
return salida
def aplicar_algoritmo(ruta):
# Walks all the tree of folders since the path
for root, dirs, files in os.walk(ruta):
print '\n@@@@@@@@@@@@@@@@'
print 'Analyzing the path: ' + root # Path where it is
# Sort alphabetically the list of files
files.sort()
primer_fichero = True;
for current_file in files:
if fnmatch.fnmatch(current_file, '*.txt') \
or fnmatch.fnmatch(current_file, '*.tif') \
or fnmatch.fnmatch(current_file, '*.jpg') :
# If it is the first file, it create the output file
# Thus, it does not create file in the folders
if primer_fichero is True:
salida = abre_salida(root);
primer_fichero = False
print "Analyzing " + current_file
(count_pix, umbral) = algoritmo(root, current_file)
salida.write("\n%s \t %s \t %s" % (current_file, str(count_pix), umbral))
if primer_fichero is False:
salida.close()
print "\nThis is the Funny Program based in pyFIA for area measurement by Image Analysis"
url = str(raw_input("\n>>>Type the path of the images: "))
aplicar_algoritmo(url)
print "\n FINISH \n"