diff --git a/bit2image.py b/bit2image.py index edcda1ebb4f294eec139727248ba78b8c85dee75..e7b78b7bd96c2e612cba85c344d000019313fb8f 100644 --- a/bit2image.py +++ b/bit2image.py @@ -1,15 +1,130 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Copyright 2019 +# Author: Gonzalo Belcredi, gbelcredi@fing.edu.uy +# Instituto de Ingenieria Electrica, Facultad de Ingenieria, +# Universidad de la Republica, Uruguay. +# +# This is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# This software is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this software; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. + from PIL import Image import numpy as np +import math +filename_bin = 'dat/recepcion_2.dat' +img_height = 800 +img_width = 549 +sync_start = [0,128,255]*10 +sync_size = 30 -array_rx = np.fromfile('dat/recepcion.dat', dtype='uint8') -columnas = 830 -filas = len(array_rx)/830 -array_rx = array_rx[0:filas*columnas] +array_rx = np.fromfile(filename_bin, dtype='uint8') +columnas = img_height + sync_size +filas = len(array_rx)/columnas +DECODE_IMAGE = False -image_rx=np.reshape(array_rx,(filas, columnas)) +def get_PSNR(image1,image2): + value = 0 + + matrix = np.asarray(image1, dtype="int8" ) + matrix_rx = np.asarray(image2, dtype="int8" ) + + M = matrix.shape[0] + N = matrix.shape[1] + + for i in range(0,M): + for j in range(0,N): + value += (float(matrix[i,j])-float(matrix_rx[i,j])) **2 + + MSE = value /(M * N) + if MSE < 10e-6: + MSE = 10e-6 + PSNR = 10 * math.log10((2**8-1)/MSE) + + return MSE,PSNR + +def get_sync_peaks(signal, sync): + """!@brief Devuelve los indices de comienzo del cuadro de sincronización. + @param signal Señal + @param sync Vector de sincronización + @result peaks Indices de sincronización. + """ + # list of maximum correlations found: (index, value) + peaks_corr = [(0, 0)] + + # minimum distance between peaks + mindistance = columnas*img_width - 1000 + + # need to shift the values down to get meaningful correlation values + signalshifted = [x-128 for x in signal] + sync = [x-128 for x in sync] + + for i in range(0,len(signalshifted) - len(sync)): + + corr = np.dot(sync, signalshifted[i : i+len(sync)]) + + # if previous peak is too far, keep it and add this value to the + # list as a new peak + if i - peaks_corr[-1][0] > mindistance: + peaks_corr.append((i, corr)) + + # else if this value is bigger than the previous maximum, set this + # one + elif corr > peaks_corr[-1][1]: + peaks_corr[-1] = (i, corr) + peaks = [i[0] for i in peaks_corr] # retrieve peak indexes from peaks_corr + + return peaks + + +''' Visualización de señal en recepción ''' + +array_rx = array_rx[0:filas*columnas] +image_rx=np.reshape(array_rx,(filas, columnas)) image = Image.fromarray(image_rx.astype('uint8')) image.save('images/imagen_recibida.png') image.show() + + +if DECODE_IMAGE: + peaks = get_sync_peaks(array_rx,sync_start) + nr_images = len(peaks) + print "Se identificaron ", nr_images, " imágenes." + print "Indices de comienzo de imagen:", peaks + + for i in range(0,nr_images): + if (peaks[i]+img_width*columnas) < len(array_rx): + array = array_rx[peaks[i]:peaks[i]+img_width*columnas] + matrix = np.reshape(array,(img_width, columnas)) + + ''' Quito el frame de sincronización para recuperar la imagen original''' + matrix_deframed = matrix[:,30:] + image = Image.fromarray(matrix_deframed.astype('uint8')) + image.save('images/imagen_recibida_'+str(i+1)+'.png') + image.show() + + + ''' Calculo MSE y PSNR ''' + img = Image.open('images/image.png' ) + img_rx = Image.open('images/imagen_recibida_1.png') #Seleccionar aquí la imagen correspondiente + img.load() + img_rx.load() + + MSE, PSNR = get_PSNR(img,img_rx) + print "MSE: ", MSE + print "PSNR (dB): ", PSNR \ No newline at end of file diff --git a/dat/recepcion.dat b/dat/recepcion.dat index b5fe8e392b088fc1705b1a39894e91efd7af1b06..3490eb8ecad54f07fb4d9f5100e1ce96125b5c90 100644 Binary files a/dat/recepcion.dat and b/dat/recepcion.dat differ diff --git a/dat/recepcion_1.dat b/dat/recepcion_1.dat deleted file mode 100644 index 3490eb8ecad54f07fb4d9f5100e1ce96125b5c90..0000000000000000000000000000000000000000 Binary files a/dat/recepcion_1.dat and /dev/null differ diff --git a/images/image.png b/images/image.png new file mode 100644 index 0000000000000000000000000000000000000000..071cc7d70876646b6aefc3c4e4ac6e291f92442d Binary files /dev/null and b/images/image.png differ diff --git a/images/imagen_frame_recibida.png b/images/imagen_frame_recibida.png new file mode 100644 index 0000000000000000000000000000000000000000..65244ad491245e21011393201a53648c907ffd8c Binary files /dev/null and b/images/imagen_frame_recibida.png differ diff --git a/images/imagen_recibida.png b/images/imagen_recibida.png index de4b789271acfb68f77fa029fff858bc28741f51..68adca893e73fa925ede7d8e6892f5c2884267e6 100644 Binary files a/images/imagen_recibida.png and b/images/imagen_recibida.png differ diff --git a/images/imagen_recibida_1.png b/images/imagen_recibida_1.png new file mode 100644 index 0000000000000000000000000000000000000000..904579094341dc291dd66e8c296a3e4c3f0ded04 Binary files /dev/null and b/images/imagen_recibida_1.png differ diff --git a/images/imagen_recibida_2.png b/images/imagen_recibida_2.png new file mode 100644 index 0000000000000000000000000000000000000000..a2f3681b20eb898e54f5d196a60f264150cc91f3 Binary files /dev/null and b/images/imagen_recibida_2.png differ diff --git a/images/imagen_recibida_3.png b/images/imagen_recibida_3.png new file mode 100644 index 0000000000000000000000000000000000000000..0fbc39d7dbca555f26502b0b733d32caa3058ea1 Binary files /dev/null and b/images/imagen_recibida_3.png differ diff --git a/images/imagen_recibida_4.png b/images/imagen_recibida_4.png new file mode 100644 index 0000000000000000000000000000000000000000..5f8825f242c35e8f0dc0bd3f518bad348cea6734 Binary files /dev/null and b/images/imagen_recibida_4.png differ diff --git a/images/imagen_recibida_5.png b/images/imagen_recibida_5.png new file mode 100644 index 0000000000000000000000000000000000000000..5f8825f242c35e8f0dc0bd3f518bad348cea6734 Binary files /dev/null and b/images/imagen_recibida_5.png differ diff --git a/images/imagen_recibida_6.png b/images/imagen_recibida_6.png new file mode 100644 index 0000000000000000000000000000000000000000..5f8825f242c35e8f0dc0bd3f518bad348cea6734 Binary files /dev/null and b/images/imagen_recibida_6.png differ