Search code examples
imagematlabmatrixoctavediagonal

using circshift to shift martix diagonally in matlab / octave


How can I shift a matrix cells diagonally?

I have an image with horizontal lines see below: Original horizontal lines Image

I use the snippet of code to shift the cells diagonally see below

reshaped_output = imresize(repmat_rgb, [640, 480]); %reshape output
imwrite(reshaped_output,strcat('/tmp/img/','orig','.png')); %will create file without borders and use any resize in repmat

[row, col, dim] = size(reshaped_output);

diag_shift_rgb=zeros(row, col, dim); %preallocate array
for ii=1:col
  bb_r=circshift(reshaped_output(:,ii,1),ii-1);
  bb_g=circshift(reshaped_output(:,ii,2),ii-1);
  bb_b=circshift(reshaped_output(:,ii,3),ii-1);
  diag_shift_rgb(:,ii,1)=[bb_r]; %over write array
  diag_shift_rgb(:,ii,2)=[bb_g]; %over write array
  diag_shift_rgb(:,ii,3)=[bb_b]; %over write array
end

imwrite(diag_shift_rgb,strcat('/tmp/img/','diag','.png')); %will create file without borders and use any resize in repmat

I do get shifted diagonal lines but the colors are off along with the shift what am I doing incorrectly?

Diagonal image

Ps: I'm using Octave 4.0 which is similar to matlab

Another example with numbers

Input Example with numbers
1                     1                     1                     1
2                     2                     2                     2
3                     3                     3                     3
4                     4                     4                     4
5                     5                     5                     5
6                     6                     6                     6
7                     7                     7                     7


Output example with numbers of what I'm trying to get with the image
1                     7                     6                     5
2                     1                     7                     6
3                     2                     1                     7
4                     3                     2                     1
5                     4                     3                     2
6                     5                     4                     3
7                     6                     5                     4

Solution

  • diag_shift_rgb is of type double but it should be of type uint8 to be saved correctly:

    diag_shift_rgb = zeros (row, col, dim, "uint8");