# Insertion Sort Code in Matlab 2-D

clc, clear, close all;
tic
a=imread(‘lena_gray.tif’); imshow(a);
nf=.5;
ws=3;

b=imnoise(a,’salt & pepper’,nf);
figure,imshow(b);

[m1 n1]=size(a);
ll=floor(ws/2);
y=zeros(m1+ws-1); % b1 is zero matrix of 514*514
y=uint8(y); % b1 is a unsign 8 bit
[m n]=size(y); % Size of b1 matrix is 514*514
y(ll+1:m-ll,ll+1:n-ll)=b; % b1(2:513,2:513)

for i = 2:m1-1
for j = 2:n1-1
flg = 0;
n = size(b);
for l = 2:n;
key = y(l);
k = l-1;
while(k>0 && y(k)>key)
y(k+1) = y(k);
k = k-1;
y(k+1)=key;
end
end
if y(i,j) == 0 || y(i,j) == 255
flg = 1;
end
if flg == 0
if S(1) < y(i,j) && y(i,j) < S(5) && 0 < S(1) && S(9) < 255
y(i,j) = y(i,j);
end
else
if S(1) < S(5) && S(5) < S(9) && 0 < S(5) && S(5) < 255
y(i,j) = S(5);
end
if S(1) >= S(5) || S(5) >= S(9) || S(5) == 255 && S(5) == 0
y(i,j) = y(i,j-1);
end
end
end
end
y(1,:) = y(2,:);
y(R,:) = y(R-1,:);
y(:,1) = y(:,2);
y(:,C) = y(:,C-1);
b2=b1(ll+1:m-ll,ll+1:n-ll);
figure,imshow(b2);
toc Advertisements

# Asprince Sorting Code in MATLAB

tic
clc
clear all
a=imread(‘lena_gray.tif’);
imshow(a)
nf=.5;
ws=7;
b=imnoise(a,’salt & pepper’,nf);
figure,imshow(b)
[m1 n1]=size(a);
r=floor(ws/2);
b1=zeros(m1+ws-1);
b1=uint8(b1);
[m n]=size(b1);
b1(r+1:m-r,r+1:n-r)=b;
%figure,imshow(b3)
for i=r+1:m-r
for j=r+1:n-r
t1=b1(i-r:i+r,j-r:j+r);
t2=sortrows(t1);
t2=sort(t1(1:end,1:end));
le=length(t2);
len=ceil(le/2);
if (0<t1(r+1,r+1)&&t1(r+1,r+1)<255)
else
if t1(r+1,r+1)==255
for i1=len:-1:1
if (0<t2(i1)&&t2(i1)<255)
b1(i,j)=t2(i1);
break;
end
if (i1==1)
b1(i,j)=t2(len);
end
end
elseif t1(r+1,r+1)==0
for i1=len:le
if (0<t2(i1)&&t2(i1)<255)
b1(i,j)=t2(i1);
break;
end
if (i1==le)
b1(i,j)=t2(len);
end
end
end
end
end
end
b2=b1(r+1:m-r,r+1:n-r);
figure,imshow(b2)
[psnr,mse]=psnr_mse_maxerr(a,b2);
display(psnr);display(mse);
toc

# Ascending Sort Code With Effective Result in MATLAB – M. Ganesh Sai

tic
clc
clear all
a=imread(‘lena_gray.tif’);
imshow(a)
nf=.4;
ws=3;
b=imnoise(a,’salt & pepper’,nf);
figure,imshow(b)

[m1 n1]=size(a);
r=floor(ws/2);
b1=zeros(m1+ws-1);
b1=uint8(b1);
[m n]=size(b1);
b1(r+1:m-r,r+1:n-r)=b;
for i=r+1:m-r
for j=r+1:n-r
t1=b1(i-r:i+r,j-r:j+r);
t2=sort(t1(:));
le=length(t2);
len=ceil(le/2);
if (0<t1(r+1,r+1)&&t1(r+1,r+1)<255)
else
if t1(r+1,r+1)==255
for i1=len:-1:1
if (0<t2(i1)&&t2(i1)<255)
b1(i,j)=t2(i1);
break;
end
if (i1==1)
b1(i,j)=t2(len);
end
end
elseif t1(r+1,r+1)==0
for i1=len:le
if (0<t2(i1)&&t2(i1)<255)
b1(i,j)=t2(i1);
break;
end
if (i1==le)
b1(i,j)=t2(len);
end
end
end
end
end
end
b2=b1(r+1:m-r,r+1:n-r);
figure,imshow(b2)
toc

# Column Sorting Technique In 2D MATLAB – M. Ganesh Sai

Column sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted through the columns in the matrix  swapping each column if they are in the wrong order. The pass through the matrix is repeated until no swaps are needed, which indicates that the matrix is sorted. The algorithm gets its name from the way that the each column is sorted in the matrix.

Illustrative Example

Let us take the matrix form , and sort the each column step by step from lowest number to greatest number using row sort. In each step, elements written in bold are being sorted. Five steps will be required.

95       45       92       41       13

73       89       20       74       52

05       19     44       20       73

40       35     60       93       67

81       27     46       83       93

First step:

First sort the column 1 elements in the matrix form.

95     45     92   41     13                                                       05   45   92   41   13

73     89     20   74     52                                                       40   89   20   74   52

05     19     44   20     73                           →                        73   19   44   20   73

40     35     60   93     67                                                       81   35   60   93   67

81     27   46   83     93                                                         95     27   46   83   93

Second step:

Sort the column 2 elements in the matrix form.

95     45     92   41     13                                                       95   19   92   41   13

73     89     20   74     52                                                       73   27   20   74  52

05     19     44   20     73                           →                        05   35   44   20   73

40     35     60   93     67                                                       40   45   60   93   67

81     27   46   83     93                                                         81     89   46   83   93

Third step:

Sort the column 3 elements in the matrix form.

95     45     92   41     13                                                      95   45   20   41   13

73     89     20   74     52                                                      73   89   44   74   52

05     19     44   20     73                           →                        05   19   46   20   73

40     35     60   93     67                                                      40   35   60   93   67

81     27   46   83     93                                                        81     27   92    83   93

Fourth step:

Sort the column 4 elements in the matrix form.

95     45     92   41     13                                                       95   45   92   20   13

73     89     20   74     52                                                       73   89   20   41   52

05     19     44   20     73                           →                         05   19   44   74   73

40     35     60   93     67                                                        40   35   60   83   67

81     27   46   83     93                                                          81     27   46   93   93

Fifth step:

Sort the column 5 elements in the matrix form.

95     45     92   41     13                                                       95   45   92   41   13

73     89     20   74     52                                                      73   89   20   74   52

05     19     44   20     73                           →                       05   19   44   20   67

40     35     60   93     67                                                     40   35   60   93   73

81     27   46   83     93                                                       81     27   46   83   93

Finally sorted columns in the matrix is shown below:

95     45     92   41     13                                                       05   19   20   20   13

73     89     20   74     52                                                       40   27   44   41   52

05     19     44   20     73                           →                        73   35   46   74   67

40     35     60   93     67                                                       81   45   60   83   73

81     27   46   83     93                                                         95   89   92   93   93

Column Sort Output Table are given bellow:

 Selection 3X3 5X5 7X7 9X9 Noise Density psnr Time Psnr Time psnr time psnr time 10% 32.05 3.15 32.02 3.35 32.34 3.32 31.98 3.42 20% 28.40 3.18 28.52 3.12 28.10 3.21 28.38 3.13 30% 25.99 3.05 26.17 3.01 26.17 3.19 25.92 3.11 40% 23.83 2.95 24.08 2.93 24.06 2.99 23.98 3.06 50% 22.14 2.79 22.03 2.97 22.20 2.88 22.12 2.93 60% 20.41 2.67 20.58 2.74 20.36 2.80 20.29 2.72 70% 18.65 2.54 18.54 2.54 18.71 2.62 18.58 2.62 80% 16.58 2.41 16.71 2.40 16.78 2.52 16.76 2.49 90% 13.94 2.33 14.05 2.49 14.24 2.54 13.98 2.36

//

# Row Sorting in MATLAB With Best Efficiency-M. Ganesh Sai

Row sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted through the rows in the matrix swapping each row if they are in the wrong order. The pass through the matrix is repeated until no swaps are needed, which indicates that the matrix is sorted. The algorithm gets its name from the way that the each row is sorted in the matrix.

Illustrative Example

Let us take the matrix form , and sort the each row step by step from lowest number to greatest number using row sort. In each step, elements written in bold are being sorted. Five steps will be required.

95       45       92       41       13

73       89       20       74       52

05       19       44       20       73

40       35       60       93       67

81       27       46       83       93

First step:

First sort the row 1 elements in the matrix form.

95     45     92   41     13                                                       13   41   45   92   95

73     89     20   74     52                                                       73   89   20   74   52

05     19     44    20     73                           →                        05   19   44   20   73

40     35     60   93     67                                                       40   35   60   93   67

81     27   46   83     93                                                         81     27   46   83   93

Second step:

Sort the row 2 elements in the matrix form.

95     45     92   41     13                                                       95   45   92   41   13

73     89     20   74     52                                                       20   52   73   74   89

05     19     44   20     73                           →                         05   19   44   20   73

40     35     60   93     67                                                       40   35   60   93   67

81     27   46   83     93                                                         81     27   46   83   93

Third step:

Sort the row 3 elements in the matrix form.

95     45     92   41     13                                                      95   45   92   41   13

73     89     20   74    52                                                       73   89   20   74   52

05     19     44   20     73                           →                       05   19   20   44   73

40     35     60   93     67                                                      40   35   60   93   67

81     27   46   83     93                                                        81     27   46   83   93

Fourth step:

Sort the row 4 elements in the matrix form.

95     45     92   41     13                                                      95   45   92   41   13

73     89     20   74     52                                                      73   89   20   74   52

05     19     44   20     73                           →                       05   19   44   20   73

40     35     60   93     67                                                      35   40   60   67   93

81     27   46   83     93                                                        81     27   46   83   93

Fifth step:

Sort the row 5 elements in the matrix form.

95     45     92   41     13                                                        95   45   92   41   13

73     89     20   74     52                                                        73   89   20   74   52

05     19     44   20     73                           →                          05   19   44   20   73

40     35     60   93     67                                                        40   35   60   93   67

81     27   46   83     93                                                          27     46   81   83   93

Finally sorted rows in the matrix is shown below:

95     45     92   41     13                                                      13   41   45   92   95

73     89     20   74     52                                                      20   52   73   74   89

05     19     44   20     73                           →                       05   19   20   44   73

40     35     60   93     67                                                      35   40   60   67   93

81     27   46   83     93                                                        27     46   81   83   93

Output Table of Row Sorting in Matlab with better efficiency is given below.

 Row 3X3 5X5 7X7 9X9 Noise Density psnr Time Psnr Time Psnr time psnr Time 10% 31.82 3.04 32.03 3.00 31.85 2.97 32.07 3.03 20% 28.58 3.03 28.15 2.93 28.67 3.07 28.58 2.92 30% 26.20 3.00 25.95 2.89 25.93 2.92 26.21 2.85 40% 24.12 2.84 23.91 2.81 24.21 2.92 24.14 2.85 50% 22.11 2.75 22.09 2.67 22.15 2.85 22.13 2.79 60% 20.66 2.72 20.53 2.70 20.40 2.73 20.43 2.67 70% 18.74 2.69 18.84 2.58 18.76 2.58 18.41 2.66 80% 16.67 2.52 16.53 2.52 16.77 2.47 16.51 2.54 90% 14.02 2.35 13.99 2.35 14.09 2.39 14.23 2.34

//

# Counting Sort(U-Sort) Algorithm in MATLAB

Step 1) A 2-D window “Sxy ” of size 3*3 is selected. Assume the pixel to be processed is P(X, Y).

Step 2) The pixel values inside the window are sorted, and Pmin, Pmax, and Pmed are determined as follows.

1. The entire matrix of an window is are arranged in ascending order with the u-sort.
2. The total elements are been considered in Pmin, Pmax, and Pmed with U-sort.

Step 3)

Case 1) The P(X,Y) is an uncorrupted pixel if Pmin<P(X,Y)<Pmax,Pmin>0, and Pmax<255; the pixel being processed is left unchanged. Otherwise, P(X,Y) is a corrupted pixel.

Case 2) If P(X,Y) is a corrupted pixel, it is replaced by its median value if and .

Case 3) If is not satisfied or , then is a noisy pixel. In this case, the P(X,Y) is replaced by the value of neighborhood pixel value.

Step 4) Steps 1 to 3 are repeated until the processing is completed for the entire image.

Step 5) Here we count the number of times that each number repeats in the given input value.

//