Is there a way to make my picture RGB again? Trying to test hamming code

2 views (last 30 days)
I am doing assignment about hamming code and its ability to repaire itself. i've read that to encode picure it should be a BW picture but is there a way to restore colours after decoding? it would really benefit my project.
i will add the code and the picture as an archive. I have wrote coments to help you look at it.
The site didnt let upload archive as RAR so please rename it before openning
Best regards, sorry for bad english.
  2 Comments
DGM
DGM on 24 Jan 2024
Edited: DGM on 24 Jan 2024
Binarizing an image discards a massive amount of information. It is not a reversible process.
I don't know what you read, but I suspect that when it said something along the lines of encoding "a binary image", they meant to encode the entire file as a bitstream, not to binarize an RGB numeric array by thresholding its luma.
Recommending a different approach would require a better description of the goals/requirements.
zakhar
zakhar on 24 Jan 2024
Thank you. My task was to simply encode an image add some noise to it and decode it. The goal was to observe how Hamming code will restore corrupted image. It is my first time using MATLAB so i did not know how to do it properly

Sign in to comment.

Accepted Answer

DGM
DGM on 24 Jan 2024
Edited: DGM on 24 Jan 2024
Well, here's one example. It might not be the most practical sort of thing, but it's probably fairly instructive. I imagine this is more of an educational task than a practical application, so let's not complicate the error observation by having the JPG encoding inside the loop.
% read image
img = imread('scale_1200.jpg');
% measure the picture
[rows, cols, channels] = size(img);
% get k & n for hamming (value in brackets may be changed)
[h,g,n,k] = hammgen(2);
% image to binary
% we're assuming img is always uint8
binary_img = dec2bin(img(:),8); % convert to 8-bit char representation of binary
binary_img = binary_img == '1'; % convert from char to logical
% change size to fit the "k"
% bear in mind that we're not checking
% that numel(img) is integer-divisible by k
binary_img = reshape(binary_img.', [], k);
% encode image using hamming
hamming_encoded_img = encode(binary_img, n, k, 'hamming/binary');
% add noise
noisy_hamming_encoded_img = imnoise(hamming_encoded_img, 'salt & pepper');
% decode the image
decoded_noisy_img = decode(noisy_hamming_encoded_img, n, k, 'hamming/binary');
% reshape to regain original size
decoded_noisy_img = reshape(decoded_noisy_img, 8, []).'; % convert back to a Mx8 logical array
decoded_noisy_img = char(decoded_noisy_img + '0'); % convert back to char representation of binary
decoded_noisy_img_norm = bin2dec(decoded_noisy_img); % convert back to numeric
decoded_noisy_img_norm = reshape(decoded_noisy_img_norm, rows, cols, channels); % reshape back to a MxNx3 RGB array
decoded_noisy_img_norm = uint8(decoded_noisy_img_norm); % cast back to original class
% show images
subplot(1,2,1), imshow(img), title('original image');
subplot(1,2,2), imshow(decoded_noisy_img_norm), title('decoded image with noise');
% show the error
figure
imshow(imfuse(img,decoded_noisy_img_norm,'diff'),'border','tight')
immse(img,decoded_noisy_img_norm)
ans = 40.6133
I don't know if that's helpful or not.

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!