Module luminosity
[hide private]
[frames] | no frames]

Source Code for Module luminosity

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  #    luminosity.py 
  5  #        
  6  #    Copyright 2010 Victor Ramirez <virako.9@gmail.com> 
  7  #        
  8  #    This program is free software: you can redistribute it and/or modify 
  9  #    it under the terms of the GNU General Public License as published by 
 10  #    the Free Software Foundation, either version 3 of the License, or 
 11  #    (at your option) any later version. 
 12  # 
 13  #    This program is distributed in the hope that it will be useful, 
 14  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  #    GNU General Public License for more details. 
 17  # 
 18  #    You should have received a copy of the GNU General Public License 
 19  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 20   
 21   
 22   
 23  from opencv.highgui import * 
 24  from opencv.cv import * 
 25  from opencv import * 
 26   
 27   
 28   
29 -def luminosity(image, tablero, size, lum = 150):
30 """Funcion que busca los puntos luminosos de la imagen y los devuelve. 31 Esta función ya no se usa por que existian varios factores en contra. 32 Factor tiempo: tardaba mucho en recorrer muchos puntos (aunque con alg. 33 de busqueda se podía bajar ese tiempo). 34 Factor error: como cogía todos los puntos luminosos, cuando aparecía una 35 pequeña sombra se producían muchos errores y a la vez cientos de puntos 36 mas que recorrer""" 37 38 puntos = [] 39 # buscamos los 4 vértices del tablero para buscar solo dentro de ellos 40 x1 = tablero.matriz[0][0][0] - tablero.square[0] 41 y1 = tablero.matriz[0][1][1] - tablero.square[1] 42 x2 = tablero.matriz[size-1][size-1][0] + tablero.square[0] 43 y2 = tablero.matriz[size-1][size-1][1] + tablero.square[1] 44 45 for x in range(x1, x2): 46 for y in range(y1, y2): 47 if (image[x][y][0] + image[x][y][1] + image[x][y][2]) > lum: 48 puntos.append((x,y)) 49 return puntos
50
51 -def search_circle(image, tablero, size, dp = 1.6):
52 """Función que trata la imagen luminosa con los filtros Canny y Smooth y 53 busca cincunferencias. Devuelve los centros de las cincunferencias 54 encontradas """ 55 56 stones = [] 57 58 gray = cvCreateImage((image.width, image.height), IPL_DEPTH_8U, 1) 59 cvCvtColor(image, gray, CV_BGR2GRAY) 60 61 # creamos dos imagenes auxiliares para aplicar los filtros 62 gray_aux = cvCreateImage((gray.width, gray.height), gray.depth, \ 63 gray.nChannels) 64 gray_aux_2 = cvCreateImage((gray.width, gray.height), gray.depth, \ 65 gray.nChannels) 66 67 # buscamos los 4 vértices del tablero para buscar solo dentro de ellos # TODO 68 #x1 = tablero.matriz[0][0][0] - tablero.square[0] 69 #y1 = tablero.matriz[0][1][1] - tablero.square[1] 70 #x2 = tablero.matriz[size-1][size-1][0] + tablero.square[0] 71 #y2 = tablero.matriz[size-1][size-1][1] + tablero.square[1] 72 73 cvCanny(gray, gray_aux_2, 50,55,3) 74 cvSmooth(gray_aux_2, gray_aux, CV_GAUSSIAN, 5, 5) 75 76 # creo una matriz de para guardar los circulos encontrados 77 storage = cvCreateMat(1, gray_aux.height*gray_aux.width, cv.CV_32FC3) 78 79 # Buscamos circunferencias en la imagen con la transformada de Hough, 80 # el tamaño de las circunferencias estarán entre los radios dados 81 tablero.get_square() 82 circles = cvHoughCircles( gray_aux, storage, CV_HOUGH_GRADIENT, dp, \ 83 tablero.square[0]/2, 50, 55, tablero.square[0]/4, tablero.square[0] ) 84 85 try: 86 for n in range(0, storage.cols): 87 p = cvGet1D(storage, n) 88 pt = (cvRound(p[0]), cvRound(p[1])) 89 stones.append(pt) 90 cvCircle(gray, pt, cvRound(p[2]), cv.CV_RGB(255, 0, 0), 2) # pinta el circulo encontrado 91 print "Hay ", len(stones), " circulos en la imagen" 92 except: 93 print "No hemos podido recorrer el storage de cvHoughCircles.\ 94 No se han circunferencias" 95 96 #try: cvSaveImage("images/circunferencias.png", gray) 97 #except: print "Error al guardar la imagen con cincunferencias. " # para chequeo 98 99 #if storage.cols == 0 and dp <= 2 and len(stones) == 0: 100 # dp += 0.2 101 # print "variando profundidad para buscar mejor:", dp 102 103 # return search_circle(image, tablero, size, dp = dp+0.2) # TODO problema al hacer la recursividad y al variar el dp 104 # stones.append((100,100)) 105 106 return stones
107