예제 #1
0
def meanshift(image_pk,test_pk,grid_size,distance,sigma):
    image_selected = Imagen.objects.get(pk=image_pk)
    image_for_pil = Image.open("."+image_selected.archivo.url)
    image_for_pil = image_for_pil.filter(ImageFilter.GaussianBlur(sigma))

    maxsize = (200, 200)

    image_for_pil.thumbnail(maxsize, PIL.Image.ANTIALIAS)
    ms_data = grid.puntos_interseccion(np.array(image_for_pil),int(grid_size))
    mean_shift = ms.MeanShift(kernel='epanechnikov_kernel')
    mean_shift_result = mean_shift.cluster(ms_data,distance,kernel_bandwidth=[100,255])
    original_points =  mean_shift_result.original_points
    shifted_points = mean_shift_result.shifted_points
    cluster_assignments = mean_shift_result.cluster_ids

    original_points_file = open('op.npy','wb+')
    shifted_points_file = open('sp.npy','wb+')
    cluster_assignments_file = open('ca.npy','wb+')
    np.save(original_points_file,original_points)
    np.save(shifted_points_file,shifted_points)
    np.save(cluster_assignments_file,cluster_assignments)
    image_for_grid = Image.fromarray(grid.visualizar_clusteres(np.array(image_for_pil),mean_shift_result)[0])
    original_points_file.close()
    shifted_points_file.close()
    cluster_assignments_file.close()

    image_for_grid.save("./imagenes/tmp/ms/ms_{}.png".format(image_selected.nombre),"png")
    fil = open("./imagenes/tmp/ms/ms_{}.png".format(image_selected.nombre), 'rb')
    rd = fil.read()
    content_file = ContentFile(rd)
    fil.close()
    content_file.name="./edited/edited.png"
    image_edited = Imagen(nombre="./imagenes/tmp/ms/ms_{}.png".format(image_selected.nombre),archivo=content_file,formato="png",editada=True)
    image_edited.save()
    test = Prueba.objects.get(pk=test_pk)
    original_points_file = open('op.npy','rb')
    cf_op = ContentFile(original_points_file.read())
    cf_op.name="op.npy"
    test.op_file = cf_op
    original_points_file.close()
    shifted_points_file = open('sp.npy','rb')
    cf_sp = ContentFile(shifted_points_file.read())
    cf_sp.name = "sp.npy"
    test.sp_file = cf_sp
    shifted_points_file.close()
    cluster_assignments_file = open('ca.npy','rb')
    cf_ca = ContentFile(cluster_assignments_file.read())
    cf_ca.name = "ca.npy"
    test.ca_file = cf_ca
    cluster_assignments_file.close()
    test.resultado=image_edited
    print(test.op_file)
    test.save()

    return image_edited.archivo.url
예제 #2
0
#Extraer los puntos de la imagen que tienen intersección con el Grid
#ORIGINALMENTE ERAN 100
GAUSSIAN_KERNELS = [1, 3, 5]


nombre_imagen = "krapfen.jpg"

#RGB = np.array(io.imread("jakob-nielsen-thumbs-up.jpg"))

#RGB2 = PIL.Image.open("angel.jpg")
#RGB2 = RGB2.filter(ImageFilter.GaussianBlur(GAUSSIAN_KERNELS[1]))

thumbnail = grid.thumbnail(nombre_imagen,(200,200))

RGB = np.array(io.imread(thumbnail))
LISTA_PUNTOS = grid.puntos_interseccion(RGB, 19)
print(np.shape(RGB))
print("Numero de puntos: {}".format(len(LISTA_PUNTOS)))
import mean_shift_epanechnikov as ms
#Utilizar el algoritmo meanshift sobre la lista de puntos. 5 Dimensiones + 1 de la clasificación
mean_shifter = ms.MeanShift(kernel = 'epanechnikov_kernel')
#ORIGINALMENTE ERA [0.2,0.2]
mean_shift_result = mean_shifter.cluster(LISTA_PUNTOS,40, kernel_bandwidth = [100,128])

# Muestra las asignaciones de cada uno de los puntos
cluster_assignments = mean_shift_result.cluster_ids
print(cluster_assignments)
print(mean_shift_result.original_points)
print("Puntos asignados: {}".format(len(cluster_assignments)))

(res,colores) = grid.visualizar_clusteres(RGB,mean_shift_result)
예제 #3
0
RGB2.thumbnail(maxsize, PIL.Image.ANTIALIAS)

RGB2 = np.array(RGB2)
print(np.shape(RGB2))
# LISTA_PUNTOS =  list([])
# alto = RGB2.shape[0]
# ancho = RGB2.shape[1]
# for i in range(alto):
#      for j in range(ancho):
#          LISTA_PUNTOS.append([i,j,RGB2[i][j][0],RGB2[i][j][1],RGB2[i][j][2]])
# LISTA_PUNTOS = np.array(LISTA_PUNTOS)
# np.shape(LISTA_PUNTOS)

# La lista de puntos ahora son las intersecciones del grid
LISTA_PUNTOS = grid.puntos_interseccion(RGB2, 10)
print("Numero de puntos: {}".format(len(LISTA_PUNTOS)))
import mean_shift_epanechnikov as ms
#Utilizar el algoritmo meanshift sobre la lista de puntos. 5 Dimensiones + 1 de la clasificación
mean_shifter = ms.MeanShift(kernel='epanechnikov_kernel')
#ORIGINALMENTE ERA [0.2,0.2]
mean_shift_result = mean_shifter.cluster(LISTA_PUNTOS,
                                         kernel_bandwidth=[100, 255])

# Muestra las asignaciones de cada uno de los puntos
cluster_assignments = mean_shift_result.cluster_ids
print(cluster_assignments)
#print(bayesian.get_small_clusters(cluster_assignments,1))
new_assignments = bayesian.reduce_clusters(mean_shift_result, 10)
print(new_assignments)
mean_shift_result.cluster_ids = new_assignments