Image Reshape Error in MATLAB
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
How to correct the following error in MATLAB? Code attached.
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that
dimension.
Error in Rev2 (line 26)
decoded_image_low = reshape(decoded_low, size(lenna_gray));
clc;
clear all;
% Load the lenna image
lenna = imread('lenna.png');
% Convert image to grayscale
lenna_gray = rgb2gray(lenna);
% Convert pixel values to bits
lenna_bits = reshape(de2bi(lenna_gray), [], 1);
% BPSK modulation
Eb_No_low = 0; % Low SNR
Eb_No_high = 4; % High SNR
SNR_low = 10^(Eb_No_low/10);
SNR_high = 10^(Eb_No_high/10);
% Transmit and receive at low SNR
received_low = awgn(double(lenna_bits), SNR_low, 'measured');
% Demodulation
decoded_low = received_low < 0;
% Reshape decoded bits to original image size
decoded_image_low = reshape(decoded_low, size(lenna_gray));
% Plot original and received image at low SNR
figure;
subplot(1,2,1); imshow(lenna_gray); title('Original Image');
subplot(1,2,2); imshow(decoded_image_low); title('Received Image (0 dB SNR)');
% Transmit and receive at high SNR
received_high = awgn(double(lenna_bits), SNR_high, 'measured');
% Demodulation
decoded_high = received_high < 0;
% Reshape decoded bits to original image size
decoded_image_high = reshape(decoded_high, size(lenna_gray));
% Plot original and received image at high SNR
figure;
subplot(1,2,1); imshow(lenna_gray); title('Original Image');
subplot(1,2,2); imshow(decoded_image_high); title('Received Image (4 dB SNR)');
% Linear error detection code
% Example: Hamming (7,4) code
parityMatrix = [1 1 1 0 1 0 0; 1 1 0 1 0 1 0; 1 0 1 1 0 0 1];
generatorMatrix = [eye(4) parityMatrix'];
% Encode the data
encoded_data = mod(lenna_bits * generatorMatrix, 2);
% Add noise for linear error detection code
received_data = awgn(double(encoded_data), Eb_No_low, 'measured');
% Syndrome lookup table for error detection
syndrome_table = syndtable(parityMatrix);
% Decoding with error detection
decoded_data = zeros(size(encoded_data));
errors = zeros(size(encoded_data, 1), 1);
for i = 1:size(encoded_data, 1)
syndrome = mod(received_data(i, :) * parityMatrix', 2);
if sum(syndrome) ~= 0 % Error detected
errors(i) = 1;
else
decoded_data(i, :) = received_data(i, :);
end
end
% Count number of retransmission requests at different SNRs
SNRs = [0, 2, 4, 6, 8, 10];
retransmissions = zeros(size(SNRs));
for i = 1:length(SNRs)
SNR = 10^(SNRs(i)/10);
received_data = awgn(encoded_data, SNR, 'measured');
errors = zeros(size(encoded_data, 1), 1);
for j = 1:size(encoded_data, 1)
syndrome = mod(received_data(j, :) * parityMatrix', 2);
if sum(syndrome) ~= 0 % Error detected
errors(j) = 1;
retransmissions(i) = retransmissions(i) + 1;
end
end
end
% Plot number of retransmissions against SNR values
figure;
plot(SNRs, retransmissions, '-o');
xlabel('SNR (dB)');
ylabel('Number of Retransmissions');
title('Number of Retransmissions vs SNR');
% Error correction code
% Example: Reed-Solomon code
n = 255;
k = 223;
t = 16;
rs_encoder = comm.RSEncoder(n, k);
rs_decoder = comm.RSDecoder(n, k);
% Encode data
encoded_rs = step(rs_encoder, double(lenna_bits));
% Add noise for Reed-Solomon code
received_rs_low = awgn(double(encoded_rs), Eb_No_low, 'measured');
received_rs_high = awgn(double(encoded_rs), Eb_No_high, 'measured');
% Decode received data
decoded_rs_low = step(rs_decoder, received_rs_low);
decoded_rs_high = step(rs_decoder, received_rs_high);
% Reshape decoded bits to original image size
decoded_image_rs_low = reshape(decoded_rs_low, size(lenna_gray));
decoded_image_rs_high = reshape(decoded_rs_high, size(lenna_gray));
% Plot original and received images with error correction
figure;
subplot(1,2,1); imshow(decoded_image_low); title('Received Image (0 dB SNR, No Error Correction)');
subplot(1,2,2); imshow(decoded_image_rs_low); title('Received Image (0 dB SNR, Error Correction)');
figure;
subplot(1,2,1); imshow(decoded_image_high); title('Received Image (4 dB SNR, No Error Correction)');
subplot(1,2,2); imshow(decoded_image_rs_high); title('Received Image (4 dB SNR, Error Correction)');
0 Kommentare
Akzeptierte Antwort
DGM
am 26 Apr. 2024
decoded_low is a logical array representing the decoded bits of a uint8 image. As a consequence, numel(decoded_low) and numel(lenna_gray) differ by a factor of 8.
You're presuming that the input is integer-valued, but not checking. You're also presuming that de2bi() will produce a predictable word width. The output width will depend on the range of input values. You actually have to specify it if you want it to be consistent.
You can either force the output to be a consistent width as determined by the integer class:
% Load the lenna image
lenna = imread('peppers.png');
% Convert image to grayscale
lenna_gray = im2gray(lenna);
% Convert pixel values to bits
% either figure out the width programmatically and enforce integer inputs
% or recast to a known integer class
lenna_gray = im2uint8(lenna_gray); % now it's known
bpp = 8;
lenna_bits = reshape(de2bi(lenna_gray,bpp), [], 1);
% modulation stuff
% ...
% i'm going to ignore all that
decoded_low = lenna_bits; % just devectorize the input
% Reshape decoded bits to original image size
insize = size(lenna_gray); % just get this once
decoded_image_low = reshape(decoded_low, [], bpp); % this relies on knowing the width
decoded_image_low = bi2de(decoded_image_low); % it needs to be converted back to numeric
decoded_image_low = reshape(decoded_image_low, insize);
% Plot original and received image at low SNR
figure;
subplot(1,2,1); imshow(lenna_gray); title('Original Image');
subplot(1,2,2); imshow(decoded_image_low); title('Received Image (0 dB SNR)');
Or you can let it be whatever width is required to hold the range of values in the particular array.
% Load the lenna image
lenna = imread('peppers.png');
% Convert image to grayscale
lenna_gray = im2gray(lenna);
% or you can just let lenna_bits be whatever width is determined
% by the range of values therein
lenna_gray = im2uint8(lenna_gray); % now it's known
lenna_bits = reshape(de2bi(lenna_gray), [], 1);
% modulation stuff
% ...
% i'm going to ignore all that
decoded_low = lenna_bits;
% Reshape decoded bits to original image size
insize = size(lenna_gray); % just get this once
decoded_image_low = reshape(decoded_low, prod(insize), []); % don't need to know the width
decoded_image_low = bi2de(decoded_image_low);
decoded_image_low = reshape(decoded_image_low, insize);
% Plot original and received image at low SNR
figure;
subplot(1,2,1); imshow(lenna_gray); title('Original Image');
subplot(1,2,2); imshow(decoded_image_low); title('Received Image (0 dB SNR)');
Obviously, I cut out the modulation stuff, but this might help straighten out the vectorization/devectorization task.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Computer Vision with Simulink finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!