how to save a matrix as an image

조회 수: 2 (최근 30일)
Adel Hafri
Adel Hafri 2022년 3월 5일
답변: Walter Roberson 2022년 3월 5일
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

답변 (1개)

Walter Roberson
Walter Roberson 2022년 3월 5일
imwrite(M1, M2, '07.tif')

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by