i am unable to zigzag scan image

6 Comments

Are you getting an error message? If so then please post the complete message.
What is size() of the image you are passing to the routine?
am using lena image
X=imread('lena1.jpg');
imshow(X);
function Vect=ZigZagscan(X)
% ZigZagscan Transform an matrix to a vector using Zig Zag Scan.
%
% VECT = ZIGZAGSCAN(MATRIX) reorganize the input Matrix and output it as a vector.
%
% Example:
% X=[1 2 3
% 4 5 6
% 7 8 9]
% X =
% 1 2 3
% 4 5 6
% 7 8 9
%
% ZigZagscan(X)=
% 1 2 4 7 5 3 6 8 9
%
%**************************************************************************
% Autors : Said BOUREZG
% Engineer on Electronics option: Communication .
% Date : 08.03.2016
% Filename ZigZagscan.m
% Please contribute if you find this software useful.
% Report bugs to: said.bourezg@yahoo.fr
% Adress: Said BOUREZG
% El Hodhna Street
% 28038 Tarmount
% M'sila - Algeria
% Email: said.bourezg@yahoo.fr
% Mobile: +213 796 018049
% http://
% If you can improve this code furtherly or add ...,
% please let me know. Thanks
%**************************************************************************
% X=imread('pingu.jpg');
% imshow(X);
[~, N]=size(X);
Vect=zeros(1,N*N);
Vect(1)=X(1,1);
v=1;
for k=1:2*N-1
if k<=N
if mod(k,2)==0
j=k;
for i=1:k
Vect(v)=X(i,j);
v=v+1;j=j-1;
end
else
i=k;
for j=1:k
Vect(v)=X(i,j);
v=v+1;i=i-1;
end
end
else
if mod(k,2)==0
p=mod(k,N); j=N;
for i=p+1:N
Vect(v)=X(i,j);
end
v=v+1;j=j-1;
else
p=mod(k,N);i=N;
for j=p+1:N
Vect(v)=X(i,j);
v=v+1;i=i-1;
end
end
end
end
end
What error message are you seeing?
as u said to apply on one channel i tried
am getting this error
Error in ZigZagscan (line 41)
Vect=zeros(1,N*N);
Error in zz (line 7)
Vect= ZigZagscan(X);
this way am applying one channel to zigzag scan
A= imread('lena1.jpg');
Red = A(:,:,1);
X = reshape(uint8(Red), [1 size(Red)]);
Vect= ZigZagscan(X);
i am unable to find the error please help me out
this is working very fine if i input this
X=[1 2 3
4 5 6
7 8 9]
Why are you reshaping Red into a 3D array? Why are you reshaping it at all? Why not just ZigZagscan(red) ?
thank you very much, it worked.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 28 Mar 2018
Edited: Guillaume on 28 Mar 2018
You cannot copy the body of a function into your code and expect it to work like a script. The function needs to be in its own m-file which you call from your script. Hence your script should only be:
X=imread('lena1.jpg');
imshow(X);
v = ZigZagScan(X);
Saying that, this ZigZagScan function is not particularly well written and assumes the input is a square matrix.
Below, a simpler code. Note this is also a function, so you'll have to put it into its own m-file named antizigzag.m
function v = antizigzag(X)
%author: G. de Sercey, University of Brighton
%BSD license
%input: X a 2D matrix, not necessarily square
%output: v a row vector of the anti-diagonals of the matrix scanned in a zigzag fashion
validateattributes(X, {'numeric'}, {'2d'});
X = fliplr(X); %flip X so the anti-diagonals become diagonals
v = arrayfun(@(d) diag(X, d), size(X, 2)-1:-1:1-size(X, 1), 'UniformOutput', false); %get the diagonals
v(1:2:end) = cellfun(@flip, v(1:2:end), 'UniformOutput', false); %flip odd diagonals
v = vertcat(v{:}).'; %concatenate the whole lot in a row vector
end

7 Comments

still am getting dimension error as
Index exceeds matrix dimensions. Error in ZigZagscan (line 55) Vect(v)=X(i,j);
Guillaume
Guillaume on 28 Mar 2018
Edited: Guillaume on 28 Mar 2018
As I said, I don't think that the code you found is of particularly good quality and I'm certainly not going to debug the mistakes that the author has made. In particular, the code assume that the input is a square matrix so if your input is not square, it will error.
Why don't you use the code I've provided which is much simpler and produces the same result (and works even if the input matrix is not square)?
can you please share your code link
It's right up there ^ in my answer!
Copy/paste the code starting at
function v = antizigzag(X)
%...
into a new file, that you'll name antizigzag.m. You can then call it from your own code
X=imread('lena1.jpg');
imshow(X);
v = antizigzag(X);
am getting error
Error using antizigzag (line 6)
Expected input to be two-dimensional.
jpeg images are 3 dimensional more than 99.9% of the time. You cannot apply zigzag or anti-zigzag routines to them as a whole: you can only apply it to one channel at a time, or else reshape the whole thing as if it were 2D and process the reshaped version. Or convert to grayscale and work with that.
as u said to apply on one channel i tried
am getting this error
Error in ZigZagscan (line 41)
Vect=zeros(1,N*N);
Error in zz (line 7)
Vect= ZigZagscan(X);
this way am applying one channel to zigzag scan
A= imread('lena1.jpg');
Red = A(:,:,1);
X = reshape(uint8(Red), [1 size(Red)]);
Vect= ZigZagscan(X);
i am unable to find the error please help me out
this is working very fine if i input this
X=[1 2 3
4 5 6
7 8 9]

Sign in to comment.

Tags

Asked:

on 28 Mar 2018

Commented:

on 31 Mar 2018

Community Treasure Hunt

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

Start Hunting!