Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Array sorting in MATLAB

                 Sorting an array and retrieving the minimum and maximum values with the index may seem simpler but this helped me in many ways especially when working with arrays. So this is just a refresh on how to sort a 1 and 2d arrays.

Single dimensional array sorting

To sort the array and display the position (index):

A = [5 8 9 10 100 7 50 8 88 6;];

Before sorting:
A =   5     8     9    10   100     7    50     8    88     6

[sorted_array,pos]= sort(A)

After sorting: Here the sorted array and the array position of each value will be displayed.
By default, sorting type is ‘ascending’.


sorted_array =     5     6     7     8     8     9    10    50    88   100


pos =     1    10     6     2     8     3     4     7     9     5


To find the maximum value:
A =   5     8     9    10   100     7    50     8    88     6

[max_value,max_pos]=max(A)

max_value  =    100


max_pos  =      5

To find the minimum value:

A =   5     8     9    10   100     7    50     8    88     6


[min_value,min_pos]=min(A)

min_value =      5


min_pos =      1


Two dimensional array sorting:

B=[17 5 8 9; 45 23 50 6; 7 49 21 11]


Before sorting:

B =     17     5     8     9
           45    23    50     6
            7    49    21    11


[sorted_array, pos]=sort(B(:))

After sorting:

sorted_array =

     5
     6
     7
     8
     9
    11
    17
    21
    23
    45
    49
    50


pos =

     4
    11
     3
     7
    10
    12
     1
     9
     5
     2
     6
     8

Maximum value:
[sorted_array, pos]=max(B(:))

sorted_array =

    50


pos =

     8


Minimum Value:
[sorted_array, pos]=min(B(:))

sorted_array =

     5


pos =

     4

To retrieve the minimum value: B(4)
ans =      5
like button Like "IMAGE PROCESSING" page

Cone effect in MATLAB

The image shrinks towards the center, forming a cone effect. Here, I first found the mid point of the image, since the image expands from the center. Then the angle and the radius of the points are found by converting into polar co-ordinates from Cartesian.  The square root (radius * K) is obtained, where K  can be changed to make the image size differ.
                                        Again convert from polar to Cartesian and display the final image.
For more fun, try this effect with your own photography image .




MATLAB CODE:


A=imread('square.jpg');
B=uint8(zeros(size(A)));
figure,imshow(A);

%FIND THE MID VALUES
midx=ceil((size(A,1)+1)/2);
midy=ceil((size(A,2)+1)/2);

%CHANGE THE VALUE OF 'K' 
K=180;
x2=zeros([size(A,1) size(A,2)]);
y2=zeros([size(A,1) size(A,2)]);

%COMPUTATION TO GET CONE EFFECT
for i=1:size(A,1)
    x=i-midx;
    for j=1:size(A,2)
           [theta,rho]=cart2pol(x,j-midy);
           sqtrho=sqrt(rho*K);
           [l,m]=pol2cart(theta,sqtrho);
           x2(i,j)=ceil(l)+midx;
           y2(i,j)=ceil(m)+midy;
    end
end
       
        % IF THE ARRAY CONTAINS VALUES LESS THAN 1 REPLACE IT WITH 1 AND
        % IF GREATER THAN 1 REPLACE WITH SIZE OF THE ARRAY
        x2(x2<1)=1;
        x2(x2>size(A,1))=size(A,1);
     
        y2(y2<1)=1;
        y2(y2>size(A,2))=size(A,2);
     
        for i=1:size(A,1)
            for j=1:size(A,2)
                B(i,j,:)=A(x2(i,j),y2(i,j),:);
            end
        end
     
     figure,   imshow(B);





K=100
K=180

SEE ALSO : Oil Painting  Swirl effect   Glassy effect   Tiling effect   Paint application
  Reference:
FOR FUN:  create your own Photoshop effects
    like button Like "IMAGE PROCESSING" page

    Glassy effect in MATLAB

    To obtain a glassy effect, define a window of size m by n. Obtain a random pixel value from the window matrix and add it with the current position value.


    MATLAB CODE:
    A=imread('aish.jpg');
    %WINDOW SIZE
    m=6;                                                                  
    n=7;
    Image=uint8(zeros([size(A,1)-m,size(A,2)-n,3]));

    for i=1:size(A,1)-m
        for j=1:size(A,2)-n

            mymask=A(i:i+m-1,j:j+n-1,:);
    %Select a pixel value from the neighborhood.
    x2=ceil(rand(1)*m);
    y2=ceil(rand(1)*n);
       
            Image(i,j,:)=mymask(x2,y2,:);
        end
    end

    figure,imshow(Image);
        We can change the matrix size and see the difference in the result.

    SEE ALSO : Oil Painting  Swirl effect   Cone effect     Tiling effect   Paint application

      Reference:
    FOR FUN:  Create your own Photoshop effects
      like button Like "IMAGE PROCESSING" page
      Previous Post Next Post Home
      Google ping Hypersmash.com