views:

52

answers:

1

This is in continuation to a question Image Shuffling in one of my threads %# scramble autumn.tif with itself

img1 = imread('autumn.tif'); 

%# scramble 
[dummy,scrambleIdx] = sort(img1(:)); 
img2 = img1; 
img2(:) = img1(scrambleIdx); %# note the (:). If you don't use it, img2 becomes a vector 

%# unscramble 
[dummy2,unscrambleIdx] = sort(scrambleIdx); 
img3 = img2; 
img3(:) = img2(unscrambleIdx); 

Question 1:-The sort(X,dim) function arranges the columns of X in ascending order. Over here : implies all the dimensions of X? Question 2: In this code the columns and rows of img2 are shuffled or only the columns are shuffled? Plz clarify

+1  A: 
  1. sort(X,dim) sorts along dimension dim, i.e. sort(X,1) sorts the rows within each column, and sort(X,2) sorts the columns within each row. sortrows(X,4) sorts the rows according to the fourth row, and if you want to sort the columns, you have to transpose X first. sort(X(:) sorts all the elements of the array X.
  2. In this code, all the elements of img2 are shuffled.

Instead of using autumn.tif, you may want to try shuffling on e.g. magic(5), a 5x5 magic square, so that you can better see what is going on.

EDIT

Here are the examples with magic(5)

>> m = magic(5)
m =
    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

>> sort(m,1) %# sort rows in each column of m
ans =
     4     5     1     2     3
    10     6     7     8     9
    11    12    13    14    15
    17    18    19    20    16
    23    24    25    21    22

>> sort(m,2) %# sort columns in each row of m
ans =
     1     8    15    17    24
     5     7    14    16    23
     4     6    13    20    22
     3    10    12    19    21
     2     9    11    18    25

>> sortrows(m,3) %# sort the rows of m according to column 3
ans =
    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

>> mt = m' %'# transpose m
mt =
    17    23     4    10    11
    24     5     6    12    18
     1     7    13    19    25
     8    14    20    21     2
    15    16    22     3     9

>> sortrows(mt,2) %# sort the rows of the transpose of m according to col 2
ans =
    24     5     6    12    18
     1     7    13    19    25
     8    14    20    21     2
    15    16    22     3     9
    17    23     4    10    11

>> mm = m; %# assign an output array for the next operation
>> mm(:) = sort(m(:)) %# sort all elements of m
mm =
     1     6    11    16    21
     2     7    12    17    22
     3     8    13    18    23
     4     9    14    19    24
     5    10    15    20    25
Jonas
@Jonas.In your answer you have said that sort(X,2) sorts the columns within each row,then why do we need to do transpose in order to sort all the columns(in refernce tou your reply).Also,sort(X(:)) sorts all elements so i guess we dont have to write separately statements for sorting the rows and columns?Is this command an example of vectorization?Thank you once again.
gavishna
I have added a few examples. Please have a look how the original array changes. Maybe this gives you an idea how you want to shuffle your image.
Jonas
@Jonas.I really appreciate your efforts Jonas in making me understand what I intend to do with your examples.I tried these in Matlab.There are 2 logic which i cant understand.1.How the rows are sorted in each column in sort(m,2) .
gavishna
2. I want to re confirm the logic to check if i have clearly understood the role of img1.In the code posted as the question (which is actually replied by you), the elements of img1 are sorted both row wise and column wise by the command sort(img1(:)).The colon serves the dual purpose of fetching both the row and column index instead of separately fetching the row and column index by something like this command, img2=(:,colScramble,:); img2 = img2(rowScramble,:,:); So,the colon avoids these steps.Is it so?Then the sorted pixel positions form the index for shuffling the elements of img2.
gavishna
@Jonas,it it possible to shuffle the image without sorting the other image img1?How do we obtain the scrambleIdx in[dummy,scrambleIdx]=sort(img1(:)); without sorting/arranging img1?
gavishna
@gavishna/1: `sort(m,2)` sorts the values of each row. For example, a row `[1 4 3 2]` becomes `[1 2 3 4]`
Jonas
@gavishna/2: img(:) converts the image into a vector of all its elements by adding one column beneath the other. Thus, all pixels of `img1` contribute to shuffling, not only a single row or column.
Jonas
@gavishna/3: Yes, it is possible to shuffle the image without sorting. The only reason I suggested sorting was that you have been asking how to recover the original using only the result and the mask. Sorting gives you a vector of the exactly right length, and with all rows/columns appearing exactly once. This makes it possible to recover the original afterwards, and it is robust to e.g. repeated values in the shuffling mask.
Jonas
@Jonas,if img(:) is converted into a vector then how come during unscrambling it is reconverted to a matrix of 3MN dimension?Regarding the third answer,then i can re affirm that without sort it will not be possible to recover the original.
gavishna