1

There is an image X of N*M size row M,column N. There are other 2 images A,B of same size as X. The objective is to shuffle the rows of X with the rows extracted from A and shuffle the columns of X with the columns extracted from B resulting in a totally modified img.

I am stuck at the point where simultaneously this is occuring. I am aware about the colon operator with which the code runs but for a square image. Please help how to go about it.

X=imread('picture.jpg');
[r c]=size(X);
[dummy,rowscrambleIdx]=sort(A,1);
X_shuffled=X;
[dummy,colscrambleIdx]=sort(B,2);

EDIT: The following code works for square image and I want to do similar operation for a rectangular sized image. However, this code does not work for rectangular sized image. I have tried to make the first code follow a similar logic but it does not work for a non-square RGB image having say 256*240*3 size

    X=imread('picture.jpg');

    [dummy,scrambleIdx]=sort(A(:));
    X_shuffled=X;
    X_shuffled(:)=A(scrambleIdx);

%now unscrambling
[dummy,unscrambleIdx] = sort(scrambleIdx);
X_recovered=X;
X_recovered(:)=X_shuffled(unscrambleIdx);
1
  • (Another case where the question has been substantially modified some time after an answer is received - rolling back). Commented Jun 20, 2016 at 10:08

1 Answer 1

1

Why not use randi to randomly create some number of indicies to pull from A, and some to pull from B?

Example:

m=10;
n=5;
A=rand(m,n)
B=ones(m,n)

%3x1 vector of random ints b/w 1 and 10
index=randi([1 10],3,1);

rand_row = A(index,:);
B(index,:)=rand_row

Then output is:

index =

    10
     9
     4

A =

0.9797    0.1174    0.7303    0.6241    0.2619
0.4389    0.2967    0.4886    0.6791    0.3354
0.1111    0.3188    0.5785    0.3955    0.6797
0.2581    0.4242    0.2373    0.3674    0.1366
0.4087    0.5079    0.4588    0.9880    0.7212
0.5949    0.0855    0.9631    0.0377    0.1068
0.2622    0.2625    0.5468    0.8852    0.6538
0.6028    0.8010    0.5211    0.9133    0.4942
0.7112    0.0292    0.2316    0.7962    0.7791
0.2217    0.9289    0.4889    0.0987    0.7150


B =

     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1


B =

    1.0000    1.0000    1.0000    1.0000    1.0000
    1.0000    1.0000    1.0000    1.0000    1.0000
    1.0000    1.0000    1.0000    1.0000    1.0000
    0.2581    0.4242    0.2373    0.3674    0.1366
    1.0000    1.0000    1.0000    1.0000    1.0000
    1.0000    1.0000    1.0000    1.0000    1.0000
    1.0000    1.0000    1.0000    1.0000    1.0000
    1.0000    1.0000    1.0000    1.0000    1.0000
    0.7112    0.0292    0.2316    0.7962    0.7791
    0.2217    0.9289    0.4889    0.0987    0.7150
Sign up to request clarification or add additional context in comments.

6 Comments

@Chris: All the rows in the matrix B needs to be replaced by all the rows of A and similarly for the columns. Also, it is desired to get the matrix B in its original form that is make the operation reversible. So how will this be reversible since the pixel information in B will be lost unless it is stored? From your answer index=randi([1 10],3,1); should this be modified as index=randi([1 256],(256*256),1); Please clarify.
If you have an original matrix X as per your OP, and you replace all the columns of X with all of those of A, X will now be identical to A. If you then replace all the columns of X (which looks like A now) with all the columns of B, X will now be identical to B. Unless you are doing some adding/subtracting/etc of overlapping values. Perhaps I've misunderstood the question?
@Chris : I have editted the Question and put the code which is working for sqaure image but not for the one in question. Perhaps, on seeing the operation it will bemore clear.
@Chris: Kindly have a look at the edited question with a second code which works for square image. I wanted to do a similar operation for a rectangular sized image. Hope I have made myself clear. Sorry for the trouble.
Does it crash on rectangular images, or does it produce incorrect results?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.