how to reshape a single image

hey experts , i just wanna ask how to reshape a single matrix to an image in coding like this
dhsig2 = huffmandeco(hcode1,handles.dict);
hsl3=uint8(reshape(dhsig2));
thanks

 Accepted Answer

Walter Roberson
Walter Roberson on 24 Sep 2016
Edited: Walter Roberson on 24 Sep 2016
If you want a huffman encoding of an image to be decodable just from the output, without the user having to indicate what the image size is, then you have to put the size in the output. For example you could have
[uint8(ndims(handles.citra)), typecast(uint64(size(handles.citra)), 'uint8')]
and put that at the beginning of the output. This is the number of dimensions as a single byte, followed by that many uint64 that together specify the size of the image.
Writing the number of dimensions first allows you to support grayscale, RGB, CMYK, and multiband formats all with the same encoding routine.
Probably in practice you could get away with uint32 instead of uint64 but someone might ask to encode a multi-gigabyte image after reshaping it to a vector and uint32 would fail for that.
To decode from uint8 vector V:
bytes_per_dim = 8;
pic_dims = V(1);
num_dims_bytes = pic_dims * 8;
pic_size = double( typecast( V(2:num_dims_bytes + 1), 'uint64') );
Then apply huffman decoding starting from byte num_dims_bytes + 2, and reshape the result to pic_size

More Answers (1)

Image Analyst
Image Analyst on 22 Sep 2016
You have to pass in the number of rows and columns that you want the final array to be to the reshape() function. Otherwise how can it know what shape/dimensions you want? It won't know unless you tell it.

1 Comment

this the full coding of mine program but i can't reshape it when i encode it , i want to make a compression and decompression image using huffman coding
function pushbutton1_Callback(hObject, ~, handles)
[nama_file,nama_path] = uigetfile({'*.jpg','File Citra(*.jpg)';
'*.png','File PNG(*.png)';...
'*.tif','File TIF(*.tif)';...
'*.*','Semua File(*.*)'},...
'buka file citra asli');
if ~isequal(nama_file,0);
handles.citra=imread(fullfile(nama_path,nama_file));
red=handles.citra(:,:,1); %memisahkan warna merah dari image
r=red.';
green = handles.citra(:,:,2); % memisahkan warna hijau dari image
g=green.';
blue = handles.citra(:,:,3); % memisahkan warna biru dari image
b=blue.';
gbr = cat (3,r,g,b);
isequal(handles.citra,gbr)
gbr=double(gbr);
handles.hasilgbr=gbr;
guidata(hObject,handles);
[handles.baris handles.kolom handles.layer]=size(gbr);
handles.jum=handles.baris*handles.kolom*handles.layer;
handles.data=(reshape(gbr,1,handles.jum));
handles.pjgasli=(length(handles.data))*8;
info=imfinfo(fullfile(nama_path,nama_file));
size_file=info.FileSize/1000;
guidata(hObject,handles);
axes(handles.axes1);
imshow(handles.citra);
tester1=imshow(handles.citra);
att=imagemodel(tester1);
height = getImageHeight(att)
width = getImageWidth(att)
set(handles.width1,'String',width);
set(handles.height1,'String',height);
set(handles.ukuranfile,'String',size_file);
set(handles.ukuran,'String',handles.pjgasli);
else
return;
end
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[nama_file_simpan, path_simpan]=uiputfile(...
{'*.huff','File kompres(*.huff)'},...
'Menyimpan File Citra Hasil Kompresi JPEG');
tic
h = waitbar(0,'Harap Tunggu...');
for i=1:100,
waitbar(i/100)
end
close(h)
[frek,nil]=hist(handles.data,unique(handles.data));
frek=frek/sum(frek);
handles.dict = huffmandict(nil,frek); % Create the dictionary.
hcode = huffmanenco(handles.data,handles.dict);
if ~isequal(nama_file_simpan,0)
shuff=strcat(path_simpan, nama_file_simpan);
save (shuff,'hcode');
else
return;
end
guidata(hObject,handles);
pjghuff=length(hcode);
ratio2=100-((pjghuff/handles.pjgasli)*100);
t2=toc;
set(handles.huf_rat,'String',ratio2);
set(handles.huf_pro,'String',t2);
set(handles.huff_size,'String',pjghuff);
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[nama_file_huff,nama_path_huff] = uigetfile(...
{'*.huff','File kompres(*.huff)'},...
'Buka File Huffman');
if ~isequal(nama_file_huff,0)
hcode=importdata(fullfile(nama_path_huff, nama_file_huff));
else
return;
end
i
tic
h = waitbar(0,'Harap Tunggu...');
for i=1:100,
waitbar(i/100)
end
close(h)
dhsig = huffmandeco(hcode,handles.dict); % Decode the code.
hsl=uint8(reshape(dhsig,handles.baris,handles.kolom,handles.layer));
red = hsl(:,:,1); % memisahkan warna merah dari image
r=red.';
green = hsl(:,:,2); % memisahkan warna hijau dari image
g=green.';
blue = hsl(:,:,3); % memisahkan warna biru dari image
b=blue.';
hslhuff = cat (3,r,g,b);
isequal(hsl,hslhuff)
hasilhuff = double(hsl);
handles.hasilhuff=hasilhuff;
guidata(hObject,handles);
[handles.baris1 handles.kolom1 handles.layer1]=size(hsl);
handles.jum1=handles.baris1*handles.kolom1*handles.layer1;
handles.data1=(reshape(hsl,1,handles.jum1));
handles.pjgdeco=(length(handles.data1))*8;
guidata(hObject,handles);
axes(handles.axes3);
imshow(hslhuff);
tester2=imshow(hslhuff);
att2=imagemodel(tester2);
height1 = getImageHeight(att2)
width1 = getImageWidth(att2)
guidata(hObject,handles);
ratio3=100-((handles.pjgdeco/handles.pjgasli)*100);
t3=toc;
set(handles.dehuff_rat,'String',ratio3);
set(handles.dehuff_pro,'String',t3);
set(handles.dehuff_size,'String',handles.pjgdeco);
set(handles.width2,'String',width1);
set(handles.height2,'String',height1);
in Pushbutton 2 after hcode = huffmanenco(handles.data,handles.dict); i want to show image in axes with reshape with add list program ahead but i wont work , any advice ?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!