Mi repositorio:
https://github.com/vane90/visioncomput/blob/master/lab10.py
En esta ocasión primeramente se realizo el movimiento en 2 dimensiones de 3 rectángulos estos se mueven en direcciones de izquierda, derecha, arriba y abajo esto simulando el movimiento.
Así es con la prueba de movimiento con los 3
Aquí va parte del codigo
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
baja_izq = 1 #direcciones | |
baja_der = 5 | |
sube_izq = 7 | |
sube_der = 9 | |
mueverect = 4 | |
negro = (0, 0, 0) #colores del rectangulo | |
rojo = (255, 0, 0) | |
verde = (0, 255, 0) | |
azul = (0, 0, 255) | |
b1 = {'rect':pygame.Rect(300, 80, 50, 100), 'color':rojo, 'dir':sube_der} #crea rectanculo de colores | |
b2 = {'rect':pygame.Rect(300, 80, 60, 100), 'color':verde, 'dir':sube_izq} | |
b3 = {'rect':pygame.Rect(300, 80, 60, 100), 'color':azul, 'dir':baja_izq} | |
rectans = [b1, b2, b3] | |
#rectans = [b1] | |
ventanaSurface.fill(negro) | |
for b in rectans: #mueve rects | |
if b['dir'] == baja_izq: | |
b['rect'].left -= mueverect | |
b['rect'].top += mueverect | |
if b['dir'] == baja_der: | |
b['rect'].left += mueverect | |
b['rect'].top += mueverect | |
if b['dir'] == sube_izq: | |
b['rect'].left -= mueverect | |
b['rect'].top -= mueverect | |
if b['dir'] == sube_der: | |
b['rect'].left += mueverect | |
b['rect'].top -= mueverect | |
if b['rect'].top < 0: #sube | |
if b['dir'] == sube_izq: | |
b['dir'] = baja_izq | |
if b['dir'] == sube_der: | |
b['dir'] = baja_der | |
if b['rect'].bottom > ancho_ventana: | |
# se mueve a lo ancho | |
if b['dir'] == baja_izq: | |
b['dir'] = sube_izq | |
if b['dir'] == baja_der: | |
b['dir'] = sube_der | |
if b['rect'].left < 0: | |
# el bloque se mueve mas a la izq | |
if b['dir'] == baja_izq: | |
b['dir'] = baja_der | |
if b['dir'] == sube_der: | |
b['dir'] = sube_der | |
if b['rect'].right > alto_ventana: | |
# el bloque se mueve mas a la der | |
if b['dir'] == baja_der: | |
b['dir'] = baja_izq | |
if b['dir'] == sube_der: | |
b['dir'] = sube_izq | |
Solo pude realizarlo con un solo objeto no con los tres mostrados anteriormente.
- Obtenemos la primer imagen de movimiento y la procesamos de la siguiente manera:
- escala de grises
- filtro
- convolución
- formas
- Ya obteniendo la imagen en formas debemos de tener el centro de esta forma
Escala de grises

Filtrado
convolución
Binarizar umbral =20
Detección de formas
Y estos son los movimientos que realizo
Código final
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
ima='nuevo.png' | |
ancho=300 | |
alto=400 | |
a_ventana=None | |
def open_i(): #imagenconmovimiento | |
imagen=Image.open('nuevo.png') | |
pixels=imagen.load() | |
return pixels | |
def detect_movi(p1,p2): #detecta_movi | |
global img,ancho,alto | |
pixt=[p1,p2] | |
for p in pixt: | |
print 'FORMA' | |
formas(p) | |
return | |
def deteccion(ima,im): | |
imagen,masa,centros=c_color(ima,im) | |
return masa,imagen,centros | |
def formas(pixt): #toma_formas | |
image = filtrar(pixt) | |
print 'salio de filtro' | |
raw_input() | |
image_cent,minimo,maximo,gx,gy,con = mascara(image) | |
pix_nor=normalizar(image_cent,minimo,maximo,con) | |
pix_bin= binar(pix_nor) | |
deteccion(pix_bin) | |
def c_color(im,imag): #aplica bfs | |
pixels=im | |
global ancho,alto | |
porcentaje=[] | |
fondos=[] | |
centro_m=[] | |
masa=[] | |
ancho,alto=im.size | |
t_pixels=ancho*alto | |
c=0 | |
pinta=[] | |
f=0 | |
m=[] | |
for i in range(ancho): | |
for j in range(alto): | |
pixn = pixels[i,j] | |
r,g,b= random.randint(0,255),random.randint(0,255), random.randint(0,255) | |
fondo=(r,g,b) | |
if (pixn==(0,0,0)): | |
c +=1 | |
origen=(i,j) | |
num_pixels,ab,od,puntos=bfs(pixn,origen,imag,fondo) | |
p=(num_pixels/float(t_pixels))*100 | |
if p>.3: | |
centro=(sum(ab)/float(num_pixels),sum(od)/float(num_pixels)) | |
masa.append(num_pixels) | |
#v=detectar_elipse(num_pixels,im,centro,puntos,fondo) | |
porcentaje.append(p) | |
fondos.append(fondo) | |
centro_m.append(centro) | |
print centro | |
imprimir_porcentajes(porcentaje) | |
return im,m,centro_m | |
def bfs(pix,origen,im,fondo): | |
pixels=pix | |
cola=list() | |
l=[-1,0,1] | |
ab=[] | |
od=[] | |
puntos=[] | |
cola.append(origen) | |
original = pixels[origen] | |
num=1 | |
while len(cola) > 0: | |
(i,j)=cola.pop(0) | |
actual = pixels[i,j] | |
if actual == original or actual==fondo: | |
for x in lista: | |
for y in l: | |
a= i+x | |
b = j+y | |
try: | |
if pixels[a,b]: | |
contenido = pixels[a,b] | |
if contenido == original: | |
pixels[a,b] = fondo | |
ab.append(a) | |
od.append(b) | |
num +=1 | |
cola.append((a,b)) | |
puntos.append((a,b)) | |
except IndexError: | |
pass | |
return num,ab,od,puntos | |
def main(): | |
anterior=None | |
siguiente=None | |
global img,ventanan | |
pygame.init() | |
ancho = 400 | |
alto = 400 | |
ventanan= pygame.display.set_mode((ancho, alto), 0, 32) | |
pygame.display.set_caption('Deteccion de Movimiento') | |
bajaizq = 1 | |
bajader = 3 | |
subeizq = 7 | |
subeder = 9 | |
mueverect = 4 | |
rojo = (25, 120, 0) | |
verde = (0, 255, 0) | |
azul = (0, 0, 255) | |
br = {'rect':pygame.Rect(300, 80, 60, 100), 'color':azul, 'dir':subeizq} | |
recta = [br] | |
while True: | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
pygame.quit() | |
sys.exit() | |
ventanan.fill((255,255,255)) | |
for b in recta: | |
if b['dir'] == bajaizq: | |
b['rect'].left -=mueverect | |
b['rect'].top += mueverect | |
if b['dir'] == bajader: | |
b['rect'].left += mueverect | |
b['rect'].top += mueverect | |
if b['dir'] == subeizq: | |
b['rect'].left -= mueverect | |
b['rect'].top -= mueverect | |
if b['dir'] == subeder: | |
b['rect'].left += mueverect | |
b['rect'].top -= mueverect | |
if b['rect'].top < 0: | |
if b['dir'] == subeizq: | |
b['dir'] = bajaizq | |
if b['dir'] == subeder: | |
b['dir'] = bajader | |
if b['rect'].bottom > ancho: | |
# block has moved past the bottom | |
if b['dir'] == bajaizq: | |
b['dir'] = subeder | |
if b['dir'] == bajader: | |
b['dir'] = subeder | |
if b['rect'].left < 0: | |
# block has moved past the left side | |
if b['dir'] == bajaizq: | |
b['dir'] = bajader | |
if b['dir'] == subeizq: | |
b['dir'] = subeder | |
if b['rect'].right > alto: | |
# block has moved past the right side | |
if b['dir'] == bajader: | |
b['dir'] = subeizq | |
if b['dir'] == subeder: | |
b['dir'] = subeizq | |
# draw the block onto the surface | |
#if anterior==None: | |
# anterior=imagen | |
pygame.draw.rect(ventanan, b['color'], b['rect']) | |
im='nuevo.png' | |
pygame.image.save(ventanan,ima) | |
#img='nuevo.png' | |
if anterior==None: | |
pix_a=open_i() | |
anterior=pix_a | |
print 'anterior',anterior | |
else: | |
pix_s=open_i() | |
siguiente=pix_s | |
print 'siguiente' | |
detect_movi(pix_a,pix_s) | |
anterior=siguiente | |
print 'detect' | |
main() |
Se me hace inconcluso este trabajo. Al inicio deja entender que fuese para múltiples objetos en 2D, pero al final es solamente uno que se procesa. 6 pts por el avance parcial.
ResponderEliminar