how to save a matrix as an image

1 view (last 30 days)
Adel Hafri
Adel Hafri on 5 Mar 2022
Answered: Walter Roberson on 5 Mar 2022
Hello, i was assinged a .tif image and had to create a Run length encoder/decoder that compresses the image and decompresses it, i made it work, but now in the Decoding part, i want to know how i can save my compressed matrix as a tif image that works again
here is my code:
classdef RLE
properties
end
methods(Static)
function [Xc,MAPC] = RLEEnc(imlocation,filename)
[x,map]=imread([imlocation]);
f1=size(x);
f2=size(map);
h=uint16(1);
Xc={};
MAPC={};
for i=1:f1(1)
Xc{i}=[];
for j=1:f1(2)-1
if x(i,j)==x(i,j+1)
h = h + 1;
else
Xc{i}=[Xc{i} h x(i,j)];
h=uint16(1);
end
end
Xc{i} =[Xc{i} h x(i,f1(2))];
h=uint16(1);
end
h=1;
for i=1:f2(1)
MAPC{i}=[];
for j=1:f2(2)-1
if map(i,j)==map(i,j+1)
h = h + 1;
else
MAPC{i}=[MAPC{i} h map(i,j)];
h=1;
end
end
MAPC{i} =[MAPC{i} h map(i,f2(2))];
h=1;
end
save(filename,'Xc','MAPC','-nocompression')
end
function [] = RLEDec(filelocation)
load(filelocation,'Xc')
load(filelocation,'MAPC')
k1=size(Xc);
k2=size(MAPC);
z1={};
z2={};
for s=1:k1(2)
a=size(Xc{1,s});
L=1;
z1{s}=[];
while(L<a(2))
for n=1:Xc{1,s}(1,L);
z1{s}=[z1{s} Xc{1,s}(1,L+1)];
end
L = L+2;
end
end
for s=1:k2(2)
a=size(MAPC{1,s});
L=1;
z2{s}=[];
while(L<a(2))
for n=1:MAPC{1,s}(1,L);
z2{s}=[z2{s} MAPC{1,s}(1,L+1)];
end
L = L+2;
end
end
M1=cell2mat(z1(:));
M2=cell2mat(z2(:));
imshow(M1,M2)
save('07.tif','M1','M2')
end
end
end

Answers (1)

Walter Roberson
Walter Roberson on 5 Mar 2022
imwrite(M1, M2, '07.tif')

Tags

Community Treasure Hunt

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

Start Hunting!