Deteccion de foco
De ZHID, la enciclopedia libre.
| Tabla de contenidos |
Prueba 1
Diseño de algoritmo, capturas de 320x320, procesamiento en host.
Algoritmo (en Python)
import Image
import struct
count = 0
w = 320
h = 320
length = w*h
origdepth = 16
enddepth = 8
colors = 2**enddepth
threshold = 0.99*colors
sumx = 0
sumy = 0
sumc = 0
npot = 0 #Number of pixels over threshold
depthrel = 2.0 ** enddepth / 2.0 ** origdepth
imagen = Image.new("P", (w,h))
f = open("read2.000000")
i = f.read(2)
while count < length:
x = count % w
y = count / w
color = int(struct.unpack('H',i)[0] * depthrel)
if color < threshold:
color = 0
else:
sumx = sumx + (x * color)
sumy = sumy + (y * color)
sumc = sumc + color
npot = npot + 1
imagen.putpixel((x,y), color)
count = count + 1
i = f.read(2)
print "Spotlight center: (", sumx/sumc, ",", sumy/sumc, ") Intensity: ", sumc/npot
imagen.show()
Captura original
(Umbral 0)
Resultados
Umbral 0.5
Spotlight center: ( 164 , 170 ) Intensity: 171
Umbral 0.75
Spotlight center: ( 164 , 193 ) Intensity: 225
Umbral 0.90
Spotlight center: ( 153 , 210 ) Intensity: 253
Prueba 2
Utilización de imágenes anteriores para la eliminación de ruido de fondo.
Algoritmo (en Python)
No tiene en cuenta puntos que estaban iluminados en la captura anterior, ni puntos solos, es decir, aquellos que no tienen vecinos iluminados.
import Image
import struct
w = 320
h = 320
length = w*h
origdepth = 16
enddepth = 8
colors = 2 ** enddepth
threshold = 0.99 * colors
sumx = 0
sumy = 0
sumc = 0
npot = 0 #Number of pixels over threshold
depthrel = 2.0 ** enddepth / 2.0 ** origdepth
imagen = Image.new("P", (w,h))
f = open("read4.000000")
fant = open("read3.000000")
count=1
pixels = []
pixelsant = []
while count <= length:
i = f.read(2)
iant = fant.read(2)
pixels.append(int(struct.unpack('H',i)[0] * depthrel))
pixelsant.append(int(struct.unpack('H', iant)[0] * depthrel))
count = count + 1
count = 0
while count < length:
x = count % w
y = count / w
color = pixels[count]
colorant = pixelsant[count]
if colorant < threshold:
colorant = 0
else:
colorant = 255
color = color - colorant #Delete it If anterior pixel was iluminated
if color < threshold:
color = 0
else:
try:
round = (pixels[count-1] + pixels[count+1] + pixels[count-w] + pixels[count+w])/4
except(IndexError):
round = 0
if round > threshold: #Alone pixels are deleted
sumx = sumx + (x * color)
sumy = sumy + (y * color)
sumc = sumc + color
npot = npot + 1
else:
color = 0
imagen.putpixel((x,y), color)
count = count + 1
print "Spotlight center: (", sumx/sumc, ",", sumy/sumc, ") Intensity: ", sumc/npot
imagen.show()
Capturas Originales
Capturas en las que existe un foco de luz fijo generado por reflejos en el entorno.
Imagen previa
Imagen actual
Resultado (Umbral 0.99)
Con el algoritmo de la prueba 1 podemos ver que el punto se aleja mucho del foco:
Spotlight center: ( 26 , 239 ) Intensity: 254
Con el nuevo algoritmo:
Spotlight center: ( 83 , 172 ) Intensity: 254
Prueba 3
Algoritmo de prueba2 portado a C. Utilizando las mismas capturas.
readimage.c
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
char *image1_file = argv[1];
char *image2_file = argv[2];
int w = 320;
int h = 320;
int length = w*h;
int origdepth = 16;
int enddepth = 8;
int colors = 1 << enddepth;
int threshold = 0.99 * colors;
int sumx = 0;
int sumy = 0;
int sumc = 0;
int npot = 0; //Number of pixels over threshold
int count = -1;
int x, y, color1, color2, round;
float depthrel = pow(2.0, enddepth - origdepth);
unsigned short *image1 = (unsigned short *)calloc(w*h, sizeof(unsigned short));
unsigned short *image2 = (unsigned short *)calloc(w*h, sizeof(unsigned short));
int fd1 = open(image1_file, O_RDONLY);
int fd2 = open(image2_file, O_RDONLY);
if(read(fd1, image1, length) < length)
{
perror("image1 cannot be read");
exit(-1);
}
if(read(fd2, image2, length) < length)
{
perror("image2 cannot be read");
exit(-1);
}
close(fd1);
close(fd2);
while(count++<length)
{
x = count % w;
y = count / w;
color1 = image1[count] * depthrel;
color2 = image2[count] * depthrel;
//printf("color1: %d\tcolor2: %d\n", color1, color2);
if (color1 > threshold)
{
color2 = 0; //Delete this pixel if anterior pixel was iluminated
continue;
}
if (color2 > threshold)
{
if(x > 1 && y > 1 && x < w - 1 && y < h - 1)
if((image2[count-1]+image2[count+1]+image2[count-w]+image2[count+w]) > threshold<<2){
sumx += (x * color2);
sumy += (y * color2);
sumc += color2;
npot ++;
}
}
}
printf("Spotlight center: (%d, %d) Intensity: %d\n", sumx/sumc, sumy/sumc, sumc/npot);
}
Resultados
$ time python prueba2/readImage.py
Spotlight center: ( 83 , 172 ) Intensity: 254
real 0m2.519s
user 0m2.416s
sys 0m0.044s
$ time ./readimage read3.000000 read4.000000
Spotlight center: (92, 157) Intensity: 254
real 0m0.019s
user 0m0.007s
sys 0m0.002s
- Punto Rojo (83, 172): ejecución en Python
- Punto Verde (92, 157): ejecución en C
Se puede observar una importante reducción de tiempos y una mayor precisión (punto verde en prácticamente posición buscada), esto último debido a una corrección en la obtención de puntos "solos".
Prueba 4
Ejecución en Zaurus
readimage.c
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <sys/time.h>
int main(int argc, char *argv[])
{
char *image1_file = argv[1];
char *image2_file = argv[2];
int w = 320;
int h = 320;
int length = w*h;
int colors = 1 << 16;
int threshold = 0.999 * colors;
int sumx = 0;
int sumy = 0;
int sumc = 0;
int npot = 0; //Number of pixels over threshold
int count = -1;
int x, y, color1, color2, round;
struct timeval *timev = (struct timeval *)malloc(sizeof(struct timeval));
suseconds_t time0;
unsigned short *image1 = (unsigned short *)calloc(w*h, sizeof(unsigned short));
unsigned short *image2 = (unsigned short *)calloc(w*h, sizeof(unsigned short));
int fd1 = open(image1_file, O_RDONLY);
int fd2 = open(image2_file, O_RDONLY);
if(read(fd1, image1, length) < length)
{
perror("image1 cannot be read");
exit(-1);
}
if(read(fd2, image2, length) < length)
{
perror("image2 cannot be read");
exit(-1);
}
close(fd1);
close(fd2);
gettimeofday(timev, NULL);
time0 = timev->tv_usec;
while(count++<length)
{
x = count % w;
y = count / w;
color1 = image1[count];
color2 = image2[count];
//printf("color1: %d\tcolor2: %d\n", color1, color2);
if (color1 > threshold)
{
color2 = 0; //Delete this pixel if anterior pixel was iluminated
continue;
}
if (color2 > threshold)
{
if(x > 1 && y > 1 && x < w - 1 && y < h - 1)
if((image2[count-1]+image2[count+1]+image2[count-w]+image2[count+w]) > threshold<<2){
sumx += (x * color2);
sumx2 += (x * x * color2);
sumy += (y * color2);
sumy2 += (y * y * color2);
sumc += color2;
npot ++;
}
}
}
gettimeofday(timev, NULL);
printf("Spotlight center: (%d, %d) Intensity: %d Dispersion: (%d, %d) Time: %dms\n",
sumx/sumc, sumy/sumc, sumc/npot, sumx2/sumc, sumy2/sumc, timev->tv_usec-time0);
}
Resultado
320x320
Spotlight center: (91, 158) Intensity: 65532 Dispersion: (8281, 24964) Time: 127336ms
240x240
Spotlight center: (124, 81) Intensity: 65535 Dispersion: (-883, 6642) Time: 68727ms









![[Portada]](/stylesheets/images/wiki.png)