How to Convert the Two dimensional Image to a one dimensional Array using ZigZag Scanning
26 次查看(过去 30 天)
显示 更早的评论
ImageArray=toZigzag(QuantizedImage);
am getting zigzag not defined
0 个评论
回答(2 个)
John D'Errico
2018-3-24
编辑:John D'Errico
2018-3-24
(Note: I would not have answered this at all, because it is surely homework. But you already have gotten an answer. So you might as well get a good answer that I hope you can learn from.)
This is one of those things that should be written in vectorized form. Well, it can be. And it is not that difficult to do. But how? The trick is to think in terms of arrays, and how the elements of an nxm array are stored in memory.
I'll first create a small array, just to show how it works.
n = 7;
m = 8;
testimage = reshape(1:n*m,[n,m])
testimage =
1 8 15 22 29 36 43 50
2 9 16 23 30 37 44 51
3 10 17 24 31 38 45 52
4 11 18 25 32 39 46 53
5 12 19 26 33 40 47 54
6 13 20 27 34 41 48 55
7 14 21 28 35 42 49 56
So we want a zig-zag raster scan. That should yield elements in this order:
[1 2 8 15 9 3 4 10 16 22 ...]
I imagine that might seem daunting at first. Start out like this:
[c,r] = meshgrid(1:m,1:n);
cr = c(:) + r(:);
If you don't know what meshgrid does, this is a great time to read the docs!
[~,tags] = sortrows([cr , -rem(cr+1,2).*c(:)/(m+1)])
zigzagscannedimage = testimage(tags)
zigzagscannedimage =
1
2
8
15
9
3
4
10
16
22
29
23
17
11
5
6
12
18
24
30
36
43
37
31
25
19
13
7
14
20
26
32
38
44
50
51
45
39
33
27
21
28
34
40
46
52
53
47
41
35
42
48
54
55
49
56
The trick was to use meshgrid to create arrays of row and column indices. Then by adding the indices, we get a result that allows us to single out the various anti-diagonals of the array.
Finally, I used sortrows, with a trick, to force it to sort each alternating diagonal in the opposite direction from the previous one.
If you want it to work on a general array of unknown size, then use size to extract the number of rows and columns.
Again, the idea is to think in terms of arrays. Use them. But all of what, 4 lines of code? No loops required. And that code will run quickly too.
There are other variations of zig-zag scan. Suppose we wanted to scan across the rows in alternating order? This would yield a sequence like:
[1 8 15 22 29 36 43 50 51 44 37 30 23 16 9 2 3...]
In fact, this one is even easier than the first scan I wrote. Again, start the same way...
[n,m] = size(testimage);
[c,r] = meshgrid(1:m,1:n);
c(2:2:end,:) = fliplr(c(2:2:end,:));
reshape(testimage(sub2ind([n,m],r,c))',[],1)
ans =
1
8
15
22
29
36
43
50
51
44
37
30
23
16
9
2
3
10
17
24
31
38
45
52
53
46
39
32
25
18
11
4
5
12
19
26
33
40
47
54
55
48
41
34
27
20
13
6
7
14
21
28
35
42
49
56
I am sure I could have found at least a couple of different ways to do each of these variations of zig-zag scans. For example, a sort based solution for the alternating row scan:
[n,m] = size(testimage);
[c,r] = meshgrid(1:m,1:n);
c(2:2:end,:) = fliplr(c(2:2:end,:));
[~,tags] = sort(r(:) + c(:)./(m+1));
zigzagscanimage = testimage(tags);
The trick is often to look for solutions based on arrays. Your MATLAB code will begin to improve once you do.
Finally, while I did not do so, a very good idea is to explain in comment lines exactly what these code blocks do. Otherwise, you will have problems debugging your code in the future, when you forget how you did it.
1 个评论
mariem khlifi
2018-3-24
编辑:mariem khlifi
2018-3-24
There is no predefined function in matlab that allows you to do the zig-zag scanning. You have to find a way that allows you get the scan manually.
Here is a code that I developed for the matter:
function d=zig_or_zag(A)
d=[];
n=length(A);
w=1;
for i=1:n
C=A(1:i,1:i);
if w==1
d=[d diag(flipud(C))'];
w=0;
else
d=[d diag(flipud(C'))'];
w=1;
end
end
for i=2:n
C=A(i:n,i:n);
if w==1
d=[d diag(flipud(C))'];
w=0;
else
d=[d diag(flipud(C'))'];
w=1;
end
end
end
If you have any question regarding the code, do not hesitate :)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!