Why do I receive an error in image write in the imwrite saying I cannot write jpeg file because of signed integer, appreciate it a lot, thanks.

6 views (last 30 days)
%read image file
A = imread('success.jpg');
%the seed for random noise for some random signal experience
S = RandStream('mt19937ar','Seed',5489);
%signal to noise ratio
snr = -10;
%M-ary Quadrature Amplitude Modulation
M = 8;
k = log2(M);
%data of image to vector
imgvector = A(:);
%vector input into 16-bit binary data where the msb identifies the sign of
%signal
bitt = dec2bin(typecast(int16(imgvector),'uint16'));
%convert binary matrix into an array (column or row array) SERIAL TYPE
[m n] = size(bitt);
array = reshape(bitt,1,m*n);
vector = num2str(array)-'0';
%bits divisible by k by adding zeros
data = length(vector);
r = rem(data,k);
while r~= 0
data = data+1;
r = rem(data,k);
end
conc = horzcat(vector, zeros(1,data-length(vector)));
%binary data to integer valued symbols for qammod where k bit becomes decimal
val = bin2dec(num2str(reshape(conc,k,[])'));
%modulator using QAM
mod = qammod(val,M);
%additive white gaussian noise
noisy = awgn(mod,snr,'measured',S,'db');
%demodulator in QAM
demod = qamdemod(mod,M);
%integer values into binary
demodat = num2str(dec2bin(demod))-'0';
%reshape into serial type
resh = reshape(demodat',1,[]);
%removes the added zero of modulation process
if length(vector) ~= length(resh)
resh(length(vector)+1:data) = [];
end
%original form - array to matrix
org = reshape(resh,m,n);
mat = num2str(org);
%matrix to decimal
dec = typecast(uint16(bin2dec(mat)),'int16');
imwrite(dec,"Rogando_8qam_neg10.jpg");

Answers (1)

Walter Roberson
Walter Roberson on 27 May 2022
https://petapixel.com/2014/01/22/jpeg-standard-gets-boost-will-support-12-bit-color-depth-lossless-compression/
The maximum bits per pixel component for jpeg is 12. 16 bit unsigned images are not supported. Signed integer images are not supported.
Your code typecast whatever the original data was to 16 bit unsigned but makes no attempt to typecast back to the original datatype, which would have been uint8 for jpeg.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!