Created
May 16, 2013 05:29
-
-
Save alejandroave/5589588 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv | |
import pygame | |
import time | |
from pygame.locals import * | |
import math | |
from PIL import Image, ImageDraw,ImageFont | |
#from Tkinter import * | |
cv.NamedWindow("Deteccion", 0) | |
capture = cv.CreateCameraCapture(1) | |
width = None #leave None for auto-detection | |
height = None #leave None for auto-detection | |
if width is None: | |
width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH)) | |
else: | |
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,width) | |
if height is None: | |
height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT)) | |
else: | |
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,height) | |
###################################### | |
def cargar(imagen): | |
im = Image.open(imagen) | |
ancho, altura = im.size | |
pixels = im.load() | |
return ancho,altura,pixels,im | |
################################### | |
####################################################################### | |
def escala(imagen): | |
ancho,altura,pixels,im = cargar(imagen) | |
for i in range(ancho): | |
for j in range(altura): | |
(a,b,c) = pixels[i,j] | |
suma = a+b+c | |
prom = int(suma/3) | |
a = prom ##igualamos | |
b = prom ##igualamos | |
c = prom ##igualamos | |
pixels[i,j] = (a,b,c) ##igualamos | |
im.save(imagen) ##guardamos la imagen nueva | |
return | |
#return pygame.image.load(ngrises) ##enviamos la imagen cargada | |
######################################################################## | |
def convolucion(imagen): | |
tiempoi = time.time() | |
print "Iniciando convolucion: " | |
ancho,altura,pixels,im = cargar(imagen) | |
matrix = ([-1,0,1],[-2,0,2],[-1,0,1]) | |
matriy = ([1,2,1],[0,0,0],[-1,-2,-1]) | |
nueva = Image.new("RGB", (ancho, altura)) | |
npixels = nueva.load() | |
gx = [] ##gradientes de x | |
gy = [] ##gradientes de y | |
mxy = [] ##la convinacon de ambos | |
for i in range(altura): | |
gx.append([]) | |
gy.append([]) | |
mxy.append([]) | |
for j in range(ancho): | |
sumax = 0 | |
sumay = 0 | |
for y in range(-1,2): | |
for x in range(-1,2): | |
if j+x >= 0 and j+x < ancho and i+y >=0 and i+y < altura: | |
sumax += matrix[y+1][x+1] * pixels[j+x,i+y][1] | |
sumay += matriy[y+1][x+1] * pixels[j+x,i+y][1] | |
r = int(math.sqrt(sumax**2+sumay**2)) ##calculamos gradiente mayor | |
gx[i].append(sumax) ##guardamos los gradientes en x | |
gy[i].append(sumay) ##guardamos los gradientes en y | |
mxy[i].append(r) ##guardamos el resultado de los gradientes en x e y | |
if r < 0: | |
r = 0 | |
if r > 255: | |
r = 255 | |
npixels[j, i] = (r,r,r) | |
tiempof = time.time() | |
nueva.save(imagen) | |
print "Se tardo: ",tiempof-tiempoi,"segundos" | |
return | |
#return pygame.image.load(nconvolucion),gx,gy,mxynueva.save(nconvolucion)sumay += matriy[y+1][x+1] * pixels[j+x,i+y][1] | |
######################################################################################################################### | |
def normalisacion(imagen): | |
print "inicia normalizacion" | |
tiempoi = time.time() | |
ancho,altura,pixels,im = cargar(imagen) | |
prom = 0 | |
maxi = pixels[0,0][1] | |
mini = pixels[0,0][1] | |
for j in range(altura): | |
for i in range(ancho): | |
if maxi < pixels[i,j][1]: | |
maxi = pixels[i,j][1] | |
if mini > pixels[i,j][1]: | |
mini = pixels[i,j][1] | |
div = 256.0/(maxi-mini) | |
for j in range(altura): | |
for i in range(ancho): | |
prom = 0 | |
if i > 0 and j > 0 and i < ancho-1 and j < altura-1: | |
prom = int(math.floor((pixels[i,j][1] - mini)*div)) | |
pixels[i,j] = (prom,prom,prom) | |
if pixels[i,j][1] > 60: | |
pixels[i,j] = (255,255,255) | |
else: | |
pixels[i,j] = (0,0,0) | |
else: | |
pixels[i,j] = (0,0,0) | |
print "terminino normalizacion" | |
print "Binarisacion" | |
im.save(imagen) ##guardamos imagenes | |
#return pygame.image.load(nconvolucion)div = 256.0/(maxi-mini) | |
return | |
#########################################################3############### | |
def filtro(imagen): | |
prom = 0 | |
ancho,altura,pixels,im = cargar(imagen) | |
for x in range(altura): ### | |
for i in range(ancho): | |
suma = 0 | |
cont = 0 | |
for j in range(-1,2): ##altura | |
for k in range(-1,2): ##ancho | |
if i+k >= 0 and i+k < ancho and x+j >= 0 and x+j < altura: | |
suma += pixels[i+k,x+j][1] | |
cont += 1 | |
prom = int(suma/cont) | |
pixels[i,x] = (prom,prom,prom) | |
im.save(imagen) | |
#return pygame.image.load(nfiltro)prom = int(suma/cont) | |
return | |
######################################################################## | |
def diferencia(imagen1,imagen2): | |
ancho1,altura1,pixels1,im1 = cargar(imagen1) | |
ancho2,altura2,pixels2,im2 = cargar(imagen2) | |
nueva = Image.new("RGB", (ancho1, altura1)) | |
npixels = nueva.load() | |
for i in range(altura1): | |
for j in range(ancho1): | |
#diferencia = abs(pixels1[j,i][0] - pixels2[j,i][0]) | |
cont = 0 | |
for a in range(-2,3): | |
for b in range(-2,3): | |
if j+a >= 0 and j+a < ancho1 and i+b >= 0 and i+b < altura1: | |
diferencia = abs(pixels1[j+a,i+b][0] - pixels2[j+a,i+b][0]) | |
if diferencia == 0: | |
cont = 1 | |
if cont == 1: | |
npixels[j,i] = (0,0,0) | |
else: | |
diferencia = abs(pixels1[j,i][0] - pixels2[j,i][0]) | |
if diferencia != 0: | |
npixels[j,i] = (255,255,255) | |
for i in range(altura1): | |
for j in range(ancho1): | |
cont = 0 | |
for a in range(-10,11): | |
for b in range(-10,11): | |
if j+a >= 0 and j+a < ancho1 and i+b >= 0 and i+b < altura1: | |
if npixels[j,i][0] == 0: | |
cont = 1 | |
if cont == 1: | |
npixels[j,i] = (0,0,0) | |
nueva.save("mov.jpg") | |
return | |
def mov(viejo,diferencia): | |
ancho1,altura1,pixels1,im1 = cargar(viejo) | |
ancho2,altura2,pixels2,im2 = cargar(diferencia) | |
nueva = Image.new("RGB", (ancho1, altura1)) | |
npixels = nueva.load() | |
for i in range(altura1): | |
for j in range(ancho1): | |
if pixels2[j,i][0] != 0: | |
if pixels1[j,i] == pixels2[j,i]: | |
npixels[j,i] = (0,0,0) | |
else: | |
npixels[j,i] = (255,255,255) | |
else: | |
npixels[j,i] = (0,0,0) | |
nueva.save("detec.jpg") | |
return | |
####################################################################### | |
def pintar(viejo,detect): | |
ancho1,altura1,pixels1,im1 = cargar(viejo) | |
ancho2,altura2,pixels2,im2 = cargar(detect) | |
ancho3,altura3,pixels3,im3 = cargar("mostrar.jpg") | |
draw = ImageDraw.Draw(im3) | |
vecx = 1 | |
vecy = 1 | |
fuente = ImageFont.truetype('/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-C.ttf',20) | |
for i in range(altura2): | |
for j in range(ancho2): | |
a = 1 | |
b = 0 | |
final = 0 | |
if pixels2[j,i][0] == 255: | |
while a < ancho2: | |
if j+a >= 0 and j+a < ancho1-10 and i+b >= 0 and i+b < altura1-10: | |
if pixels1[j+a,i][0] != 0: | |
final = a | |
pixels1[j+a,i] = (0,0,0) | |
a = ancho2 | |
cont = 'der' | |
break | |
if j-a >= 0 and j-a < ancho1-10 and i+b >= 0 and i+b < altura1-10: | |
if pixels1[j-a,i][0] != 0: | |
final = a | |
pixels1[j-a,i] = (0,0,0) | |
a = ancho2 | |
cont = 'izq' | |
break | |
a += 1 | |
if final != 0: | |
xx,yy = j,i | |
# draw.line((j+final, i), fill=128) | |
draw.line((j, i, j+final, i), fill=128) | |
#if cont == 'der': | |
# draw.text((j, i), ' derecha ', fill=(255,0,0), font=fuente) | |
#else: | |
# draw.text((j, i), ' izquierda ', fill=(255,0,0), font=fuente) | |
draw.text((xx, yy), ' '+cont+' ', fill=(255,0,0), font=fuente) | |
im3.save("final.jpg") | |
def main(): | |
cont = 0 | |
pygame.init() | |
screen = pygame.display.set_mode((448,336),0,32) | |
pygame.display.set_caption('Juegos en Pygame') | |
while cont < 2: | |
img = cv.QueryFrame(capture) | |
k = cv.WaitKey(100); | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
sys.exit() | |
presionada = pygame.key.get_pressed() | |
if presionada[K_RIGHT]: | |
print "funca??" | |
cv.SaveImage("test"+str(cont)+".jpg",img) | |
cv.SaveImage("mostrar.jpg",img) | |
cv.SaveImage("img"+str(cont)+".jpg",img) | |
image=cv.LoadImage("test"+str(cont)+".jpg",cv.CV_LOAD_IMAGE_COLOR) | |
cont += 1 | |
cv.ShowImage("Deteccion", img) | |
escala("test0.jpg") | |
filtro("test0.jpg") | |
convolucion("test0.jpg") | |
normalisacion("test0.jpg") | |
escala("test1.jpg") | |
filtro("test1.jpg") | |
convolucion("test1.jpg") | |
normalisacion("test1.jpg") | |
print "sacando diferencia" | |
diferencia("test0.jpg","test1.jpg") | |
print "sacando movimienti" | |
mov("test0.jpg","mov.jpg") | |
print "pintando" | |
pintar("test0.jpg","detec.jpg") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment