hichamjanati / pyldpc

Creation of LDPC codes & simulation of coding and decoding binary data. Applications to sound and image files.
BSD 3-Clause "New" or "Revised" License
119 stars 32 forks source link

solve height width when image is rectangular #19

Closed lingr7 closed 4 years ago

lingr7 commented 4 years ago

Testing noisy_img with a rectangular gray image test_gray.png from Bing wallpaper 20200615 imageonline-co-grayscaledimage

# -*- coding: utf-8 -*-
"""
Created on Sun Jun 14 21:42:25 2020

@author: ligong
"""
import numpy as np
import cv2
import matplotlib.image as io
from pyldpc import make_ldpc, ldpc_images
from pyldpc.utils_img import gray2bin, rgb2bin
from pyldpc import utils
from matplotlib import pyplot as plt
from PIL import Image

from time import time

eye = Image.open("data/test_gray.png")
plt.imshow(eye,'gray')
plt.show()

eye = np.asarray(eye.convert('LA'))[:, :, 0]

eye_bin = gray2bin(eye)
print("Eye shape: (%s, %s)" % eye.shape)
print("Binary Eye shape: (%s, %s, %s)" % eye_bin.shape)
n = 200
d_v = 3
d_c = 4
seed = 42

##################################################################
# First we create a small LDPC code i.e a pair of decoding and coding matrices
# H and G. H is a regular parity-check matrix with d_v ones per row
# and d_c ones per column

H, G = make_ldpc(n, d_v, d_c, seed=seed, systematic=True, sparse=True)

##################################################################
# Now we simulate the transmission with Gaussian white noise
# and recover the original image via belief-propagation.

snr = 8
eye_coded, eye_noisy = ldpc_images.encode_img(G, eye_bin, snr, seed=seed)
outFile_coded = "data/test_gray_coded.png"
outFile_noisy = "data/test_gray_noisy.png"
# io.imsave(outFile_noisy,eye_noisy)
plt.imshow(eye_noisy,'gray')
plt.show()
plt.imshow(eye_coded,'gray')
cv2.imwrite(outFile_noisy,eye_noisy)
cv2.imwrite(outFile_coded,eye_coded)
plt.show()
codecov[bot] commented 4 years ago

Codecov Report

Merging #19 into master will not change coverage. The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #19   +/-   ##
=======================================
  Coverage   84.00%   84.00%           
=======================================
  Files          14       14           
  Lines         650      650           
  Branches      111      111           
=======================================
  Hits          546      546           
  Misses         82       82           
  Partials       22       22           
Impacted Files Coverage Δ
pyldpc/ldpc_images.py 86.20% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 2eeea26...51f8790. Read the comment docs.

hichamjanati commented 4 years ago

Indeed, thanks a lot @lingr7 :) Would like to add your example to the documentation ? if so you can name it with a plot_ prefix and put it in the examples folder. Or I can merge this

lingr7 commented 4 years ago

The commit only exchanges the order of height width parameter transfer in pyldpc/ldpc_images.py, because the square image exchange has no effect, but in order to correctly read out the image information (take out the information bits from the coding), the rectangle needs to exchange. The code given in my description validates the effect of the modification. image

lingr7 commented 4 years ago

I do it.

hichamjanati commented 4 years ago

Thanks @lingr7