Is there a way to make my picture RGB again? Trying to test hamming code
조회 수: 2 (최근 30일)
이전 댓글 표시
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
DGM
2024년 1월 24일
편집: DGM
2024년 1월 24일
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.
채택된 답변
DGM
2024년 1월 24일
편집: DGM
2024년 1월 24일
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)
I don't know if that's helpful or not.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Hamming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!