Showing posts with label Matlab. Show all posts
Showing posts with label Matlab. Show all posts

Monday, September 13, 2010

Black and white Image Water marking in spatial domain


Base Image After Watermarking


In this program the base image is a gray scale image and the water mark image is a black&white image. The water mark image will insert on any bit plain position that will be chosen by the chaotic random sequence. Before inserting the image the image will scrambled and inserted to the base image.

clear all; close all; clc;

tic;
f1 = imread('le.tif');
%f1 = imresize(f1,[64,64]);
%f1 = rgb2gray(f1);
[m n] = size(f1);
f=zeros(m,n);
for i=1:m
for j=1:n
if f1(i,j)>=128
f(i,j)=1;
end
end
end

f2=f;
figure,imshow(f1),title('Water Mark Image');
si1=m*n;
con =10;
rimg = f;
if m == n
while (con > 0)
for i =1:m
for j = 1:n
xva = mod((i+j),m);
if xva == 0
xva=m;
end
%XA(i,j) = xva;
yva = mod((i+(2*j)),m);
if yva == 0
yva=m;
end
%YA(i,j) = yva;
rimg(xva,yva) = f(i,j);
end
end
con = con-1;
f = rimg;
end
end
figure,imshow(f);
imwrite(f,'wat1.tif');
img = imread('cameraman.tif');
img = imresize(img,[256,256]);
[m1,n1]=size(img);
si=m1*n1;
Z=zeros(1,si1);
Z1=zeros(1,si1);
Zn=0.3; mu=3.5699456;
for i=1:(si1)
Z(i)=mu*Zn*(1-Zn);
Zn=Z(i);
if Zn >= 0.75
Z1(i)=7;
elseif Zn >= 0.5
Z1(i)=6;
elseif Zn >= 0.25
Z1(i)=5;
else
Z1(i)=4;
end
end
img1=img;
img2=img(:)';
f1=f(:)';
co=0;
%co1=1;co2=0;
for i=1:si1
S1=dec2bin(img2(i),8);
S3 = Z1(i);
S1(S3)=num2str(f(i));
img2(i)=bin2dec(S1);
end
res=reshape(img2,m1,n1);
figure,imshow(res);
imwrite(res,'water.tif');

Monday, August 2, 2010

Morphology Edge Detection

clear all; close all; clc;
a = imread('circles.png');
a = imresize(a,[256,256]);
%a = imcrop(a);
[m n]=size(a);
b = zeros([m n]);
figure,imshow(a);
w = [1 1 1 1 1]; % structuring element
for i=1:m-1
for j=3:n-2
con=0;
for p=j-2:j+2
con=con+1;
s(con)= a(i,p)*w(con);
end
s1 = 1;
for r=1:con
s1 = s1*s(r);
end
if s1 > 0
b(i,j)=1;
end
end
end
figure,imshow(b);
f1 = a-b;
figure,imshow(f1);

Chain Coding

clear all; close all; clc;
a = imread('circles.png');
a = imresize(a,[256,256]);
[m n]=size(a);
b = zeros([m n]);
figure,imshow(a);
w = [0 1 0 1 1 1 0 1 0]; % structuring element
for i=2:m-1
for j=2:n-1
con=0;
for k=i-1:i+1
for p=j-1:j+1
con=con+1;
s(con)= a(k,p)*w(con);
end
end
if sum(s) > 0
b(i,j)=1;
end
end
end
f2 = xor(a,b);
figure,imshow(f2),title('Object Edge');
Ipo = a(19,35);
co=0; Cont = 0;Cva = 0;
i=19; j=35;
while (co==0)
if a(i,j)==Ipo
if a(i,j+1) == a(i,j)
Cont=Cont+1;
CVa(Cont)=0;
i=i;j=j+1;
if (i==19) && (j == 35)
co=1;
end
elseif a(i-1,j+1) ==  a(i,j)
Cont=Cont+1;
CVa(Cont)=1;
i=i-1;j=j+1;
if (i==19) && (j == 35)
co=1;
end
elseif a(i-1,j) ==  a(i,j)
Cont=Cont+1;
CVa(Cont)=2;
i=i-1; j=j;
if (i==19) && (j == 35)
co=1;
end
elseif a(i-1,j-1) ==  a(i,j)
Cont=Cont+1;
CVa(Cont)=3;
i=i-1; j=j-1;
if (i==19) && (j == 35)
co=1;
end
elseif a(i-1,j) ==  a(i,j)
Cont=Cont+1;
CVa(Cont)=4;
i=i-1; j=j;
if (i==19) && (j == 35)
co=1;
end
elseif a(i+1,j-1) ==  a(i,j)
Cont=Cont+1;
CVa(Cont)=5;
i=i+1; j=j-1;
if (i==19) && (j == 35)
co=1;
end
elseif a(i+1,j) ==  a(i,j)
Cont=Cont+1;
CVa(Cont)=6;
i=i+1; j=j-1;
if (i==19) && (j == 35)
co=1;
end
elseif a(i+1,j+1) ==  a(i,j)
Cont=Cont+1;
CVa(Cont)=7;
i=i+1; j=j+1;
if (i==19) && (j == 35)
co=1;
end
else
co=1;
end
else
co=1;
end
end

Morphology Dilation

clear all; close all; clc;
a = imread('Bimage1.bmp');
a = imresize(a,[256,256]);
[m n]=size(a);
b = zeros([m n]);
figure,imshow(a);
w = [0 1 0 1 1 1 0 1 0]; % structuring element
for i=2:m-1
for j=2:n-1
con=0;
for k=i-1:i+1
for p=j-1:j+1
con=con+1;
s(con)= a(k,p)*w(con);
end
end

if sum(s) > 0
b(i,j)=1;
end
end
end
figure,imshow(b);

Morphology Erosion

clear all; close all; clc;
a = imread('Bimage1.bmp');
a = imresize(a,[256,256]);
%a = imcrop(a);
[m n]=size(a);
b = zeros([m n]);
figure,imshow(a);
w = [1 1 1 1 1]; % structuring element
for i=1:m-1
for j=3:n-2
con=0;
for p=j-2:j+2
con=con+1;
s(con)= a(i,p)*w(con);
end
s1 = 1;
for r=1:con
s1 = s1*s(r);
end
if s1 > 0
b(i,j)=1;
end
end
end
figure,imshow(b);

Image segmantation using Region Splitting and merging

clear all; close all; clc;
f = imread('coins.png');
g = splitmerge(f,2,@predicate);
figure,imshow(f);
figure,imshow(g);

function g = splitmerge(f, mindim, fun)
% splitmerge Segment an image using a split-and merge algorithm.
% split regions down to size 1-by-1.
q = 2^nextpow2(max(size(f)));
[m n]=size(f);
f = padarray(f, [q-m, q-n], 'post');
s = qtdecomp(f, @split_test, mindim, fun);
lmax = full(max(s(:)));
g=zeros(size(f));
marker = zeros(size(f));
for k = 1:lmax
[vals, r, c] = qtgetblk(f, s,k);
if ~isempty(vals)
for i= 1:length(r)
xlow = r(i);
ylow = c(i);
xhigh = xlow + k -1;
yhigh = ylow + k -1;
region = f(xlow:xhigh, ylow:yhigh);
flag = feval(fun, region);
if flag
g(xlow:xhigh, ylow:yhigh) = 1;
marker(xlow, ylow) = 1;
end
end
end
end
g = bwlabel(imreconstruct(marker,g));
g = g(1:m,1:n);

Image segmantation using region growing

clear all; close all; clc;
f = imread('coins.png');
t=70; % Thres Hold value
s = 60; % Seed Value
[g, nr,si,ti] = regiongrow(f,s,t);
figure,imshow(f),title('Input Image');
figure,imshow(ti),title('After Segmentation');

function [g,nr,si,ti]=regiongrow(f,s,t)
% regiongrow perform segmentation by region growing.
f = double(f);
if numel(s) ==1
si =f == s;
s1 = s;
else
si = bwmorph(s, 'shrink', Inf);
j = find(si);
s1 = f(j);
end
ti = false(size(f));
for k=1:length(s1)
seedvalue = s1(k);
s = abs(f - seedvalue) <= t;
ti = ti | s;
end
[g, nr] = bwlabel(imreconstruct(si, ti));

Point Detection on Image

clear all ; close all; clc;
I = imread('House24.png');
I = rgb2gray(I);
I = im2double(I);
h = [-1 -1 -1;-1 8 -1;-1 -1 -1];
f = imfilter(I,h);
figure,imshow(I),title('Original Image');
figure,imshow(f),title('After filtering');

Hough Edge detection

clear all; close all; clc;
f = imread('House24.png');
f = rgb2gray(f);
f = imcrop(f,[3 3 255 255]);
%f = im2double(f);
%f = imrotate(f,33,'crop');
BW = edge(f,'canny');
[H, theta, rho] = hough(BW);
imshow(theta, rho, H, [], 'notruesize');
axis on, axis normal
xlabel('\theta'), ylabel('\rho');
p = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
c = theta(p(:,2));
r = rho(p(:,1));
hold on
plot(c,r, 'linestyle', 'none', 'marker', 'S', 'color', 'w');
lines = houghlines(BW,theta,rho,p,'FillGap',5,'MinLength',7);
figure,imshow(f), hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
end

Roberts Edge Detection

clear all; close all; clc;
I = imread('lena.gif');
%I = rgb2gray(I);
I = im2double(I);
h = [-1 0;0 1]; % 45 degree Mask
h1 = [0 -1;1 0]; % -45 degree Mask
f1 = imfilter(I,h,'replicate');
f2 = imfilter(I,h1,'replicate');
figure(1),imshow(I),title('Original Image');
figure(2);
subplot(1,2,1),imshow(f1),title('45 Degree Edge');
subplot(1,2,2),imshow(f2),title('-45 Degree Edge');

Sobel Edge Detection

clear all; close all; clc;
I = imread('lena.gif');
%I = rgb2gray(I);
I = im2double(I);
h = [-1 -2 -1;0 0 0;1 2 1]; % Horizontal Mask
h1 = [-1 0 1;-2 0 2;-1 0 1]; % Vertical Mask
h2 = [0 1 2;-1 0 1;-2 -1 0]; % -45 Degree
h3 = [-2 -1 0;-1 0 1;0 1 2]; % 45 Degree
f1 = imfilter(I,h,'replicate');
f2 = imfilter(I,h1,'replicate');
f3 = imfilter(I,h2,'replicate');
f4 = imfilter(I,h3,'replicate');
figure(1),imshow(I),title('Original Image');
figure(2);
subplot(2,2,1),imshow(f1),title('Horizontal Edge');
subplot(2,2,2),imshow(f2),title('Vertical Edge');
subplot(2,2,3),imshow(f3),title('-45 Degree Edge');
subplot(2,2,4),imshow(f4),title('45 Degree Edge');

Image Rotation

clear all; close all; clc;
f = imread('cameraman.tif');
[m n]=size(f);
f1 = padarray(f,[128 128]);
[m1 n1]=size(f1);
f2 = zeros(m1,n1);
figure,imshow(f1);
t=30; % Rotation angle
t1 = t*pi/180;
% rotate = maketform('affine',[ cos(t1)  sin(t1)  0; ...
%                                -sin(t1)  cos(t1)  0; ...
%                                     0       0       1 ]);

for i=1+128:m+128
for j=1+128:n+128

xd = fix((i*cos(t))+(i*sin(t)));
yd = fix((j*-sin(t))+(j*cos(t)));
a(i,j)=xd;
a1(i,j)=yd;

%f2(xd,yd)=f1(i,j);
end
end
ma1 = max(max(a));
mi1 = min(min(a));
mi1 = abs(mi1);
for i=1+128:m+128
for j=1+128:n+128
s1 = (mi1+a(i,j))+1;
s2 = a1(i,j);
f2(s1,s2)=f1(i,j);
end
end
figure,imshow(f2);

Median filter on Image

clear all;
close all;
clc;
a=imread('rice.png');
b=im2double(a);
b=imnoise(b,'salt & pepper',0.3);
[m n]=size(b);
for i=1:m
for j=1:n
s=0;con=0;
for k= i-1:i+1
for p=j-1:j+1
if (k>0 && p>0) &&(k<m &&p<n)
con=con+1;
s(con)=b(k,p);
end
end
end
c(i,j)=median(s);
end
end
figure,imshow(b);
figure,imshow(c);

Mean Filter on Image

clear all; close all; clc;
a = imread('test_pattern.tif');
a = im2double(a);
a = imnoise(a,'gaussian',0.1);
figure,imshow(a);
[m n o]=size(a);
si=2;
%for r=1:3
for i = 1:m
for j = 1:n
con=0; s1=0;
for k1 = i-si:i+si
for p1 = j-si:j+si
if ((k1>0 && p1 >0) && (k1<m && p1<n))
con = con+1;
s1=s1+a(k1,p1);
end
end
end
b1(i,j)=s1/con;
end
end
%end
figure,imshow(b1),title('After Mean Filtering');

Contraharmonic mean filter

clear all; close all; clc;
f = imread('lena.gif');
f = imnoise(f,'salt & pepper',0.1);
f = im2double(f);
subplot(1,2,1),imshow(f),title('Original Image');
[m n]=size(f);
si=1;Q=0;
for i = 1:m
for j = 1:n
con=0; s1=0; s2=0;
for k1 = i-si:i+si
for p1 = j-si:j+si
if ((k1>0 && p1 >0) && (k1<m && p1<n))
con = con+1;
s1=s1+(f(k1,p1)^Q);
s2=s2+(f(k1,p1)^(Q+1));
end
end
end
b1(i,j)=s2/s1;
end
end
subplot(1,2,2),imshow(b1),title('Cantraharmonic mean filtered');

Adaptive Median filter

%
clear all; close all; clc;
f = imread('cameraman.tif');
f = im2double(f);
f = imnoise(f,'salt & pepper',0.4);
subplot(1,2,1),imshow(f),title('Noise Image');
[m n]=size(f);
for i = 1:m
for j =1:n
si = 1;
cn = 1;
while(cn == 1)
con = 1;
for k = i-si:i+si
for p = j-si:j+si
if(k >0 && p>0)&&(k <m && p <n)
con = con+1;
s(con) = f(k,p);
end
end
end
ma = max(s);
mi = min(s);
me = median(s);
d1 = me - mi;
d2 = me - ma;
if (d1 >0) && (d2 <0)
df1 = f(i,j) - mi;
df2 = f(i,j) - ma;
if (df1 > 0) && (df2 < 0)
f1(i,j) = f(i,j);
else
f1(i,j) = me;
end
cn=0;
else
si = si+1;
if si>m
cn = 0;
f1(i,j)=f(i,j);
end
end
s=0;
end
end
end
imwrite(f1,'cameraout.tif');
subplot(1,2,2),imshow(f1),title('After filtering');

Adaptive localnoisereduction filter

clear all; close all; clc;
f = imread('cameraman.tif');
f = im2double(f);
[m n]=size(f);
f1=zeros(m,n);
f = imnoise(f,'gaussian',0,0.1);
D = std2(f);
M = mean2(f);
for i=3:m-2
for j=3:n-2
con=0; s=0; s1=0;
for k=i-2:i+2
for p=j-2:j+2
con=con+1;
s = s+f(k,p);
s1(con)=f(k,p);
end
end
lm=s/con;
ld = std(s1);
if ld >0
f1(i,j) = f(i,j)-((D*(f(i,j)-lm)/ld));
else
f1(i,j) = f(i,j)-0;
end
end
end
figure,imshow(f);
figure,imshow(f1);

Hadamard transformation on 2D image

clear all; close all; clc;
%%%% HADAMARD MATRIX
H = [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 -1 -1 -1 -1 1 1;
1 -1 -1 1 -1 1 1 -1];
Input_Image = imread('cameraman.tif');
[m n] = size(Input_Image);
for i = 1:8:m
for j = 1:8:n
i1 = 0;
for k = i:i+7
i1 = i1 +1; j1 = 0;
for p = j:j+7
j1 = j1 + 1;
b(i1,j1) = double(Input_Image(k,p));
end
end
b = b.*H;

i1 = 0;
for k = i:i+7
i1 = i1 +1; j1 = 0;
for p = j:j+7
j1 = j1 + 1;
B1(k,p)=b(i1,j1);
end
end
end
end
figure,imshow(im2double(B1)),title('HADAMARD TRANSFORMED IMAGE');

Simple Image Scrambling

clear all;
close all;
clc;
a = imread('cameraman.tif');
a = im2double(a);
[m n]=size(a);
% for i=1:m
%     for j=1:n
%         a(i,j)=a(i,j).*255;
%     end
% end
s = input('Enter the size to split :');
ps = input('Enter Password:','s');
cnt = length(ps);
%%%%$$$$$$$$$$$$$$FINDING THE KEY $$$$$$$$$$$$$$$$$$$$$$$$$$
T=0;
for i=1:cnt
T=T+double(ps(i));
end
%%%%$$$$$$$$$$$$$$$$$$$$$$$$$$$$ SPLITING THE IMAGE $$$$$$$$$$$$$$$
s1=m/s;
s2=n/s;
le = s1*s2;
c = cell(s1,s2);
c1 = cell(s1,s2);
c2 = cell(s1,s2);
c3 = cell(s1,s2);
i1=0;
for i=1:s:m
i1=i1+1;j1=0;
for j=1:s:n
j1=j1+1;
k1=0;
for k=i:((i+s)-1)
k1=k1+1;p1=0;
for p=j:((j+s)-1)
p1=p1+1;
if k<=m&p<=n
a2(k1,p1)=a(k,p);
end
end
end
c{i1,j1}=a2;
end
end
%%%%$$$$$$$$$$$$$$$$$$$$$$$ FINDING THE BLOCK DIFFERENCE $$$$$$$$$$$
i2=1;
for i=1:s1
for j=1:s2
e=c{i,j};
j2=j+1;
if (i2<= s1 & j2 <= s2)
e1=c{i2,j2};
elseif (i2<=s1 & j2 > s2)
i2=i2+1;
j2=1;
if (i2 <= s1)&(j2 <=s2)
e1=c{i2,j2};
end
if (i2>s1)&(j2 <=s2)
i2=1;
e1=c{i2,j2};
end
end
[m1 n1]=size(e);
for k=1:m1
for p=1:n1
blodiff(k,p)=abs(e(k,p)-e1(k,p));
end
end
c1{i,j}=blodiff;
end
end
%%%%%$$$$$$4 FIND THE BLOCK TOTAL $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
for i = 1:s1
for j=1:s2
btot = 0;
for k=1:m1
for p=1:n1
btot = btot+c1{i,j}(k,p);
end
end
btot1(i,j)=btot;
clear btot;
end
end
%%%%%$$$$$$$$$$$$$ SUBTRACT BLOCK TOTAL WITH BLOCK PIXEL VALUE $$$$$$$$$
for i=1:s1
for j=1:s2
bt=btot1(i,j);
for k=1:m1
for p=1:n1
c2{i,j}(k,p)=bt - c{i,j}(k,p);
end
end
end
end
%%%%$$$$$$$$$$$$$$$$$ GENERATE RANDOM NUMBER $$$$$$$$$$$$$$$
rand('state',T);
[h g]=sort(rand(1,le));
re = g;
co=0;
for i=1:s1
for j=1:s2
co =co+1;
sm(i,j)=re(co);
end
end
%%%$$$$$$$$$$$$ SCRAMBLE THE IMAGE $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[s1 s2] = size(c2);
for i=1:s1
for j=1:s2
rv = sm(i,j);
co=0;
for k=1:s1
for p=1:s2
co=co+1;
if co == rv;
c3{k,p}=c2{i,j};
end
end
end
end
end
%%%%%%$$$$$$$$$$$$$$ MERGE INTO SINGLE IMAGE $$$$$$$$$$$$$$$$$$$$$$$$$$
ii=1;jj=1;
for i=1:s1
for j=1:s2
im12 = c3{i,j};
for k=1:m1
for p=1:n1
oimag(((k+ii)-1),((p+jj)-1))=im12(k,p);
end
end
jj=jj+s;
end
ii=ii+s;
jj=1;
end
figure,imshow(a),title('ORIGINAL IMAGE');
figure,imshow(oimag),title('AFTER SCRAMBLE');
% imwrite(oimag,'scrcameraman.tif');
B25 = max(max(oimag));
M25 = min(min(oimag));
opimag = scramb_rev(oimag);
% for i=1:m
%     for j=1:n
%         re45im(i,j)=oimag(i,j)/255;
%     end
% end
% figure,imshow(re45im);

Application of Rough Set Theory in Coal Gangue Image Processing

% Application of Rough Set Theory in Coal Gangue Image Processing
% This program will remove the salt & pepper noise from the coal image without much image degradation.
% IEEE 2009
clear all; close all; clc;
f = imread('20090506-anthracite-coal');
%f=rgb2gray(f);
f=im2double(f);
[m,n o]=size(f);
figure,imshow(f),title('Input Image');
if o == 1
f1=imnoise(f,'salt & pepper',0.5);
figure,imshow(f1),title('Noise Image');
Q=0.1;
t(1:9)=0;
S1(1:m,1:n)=0;
S2(1:m,1:n)=0;
for i=2:m-1
for j=2:n-1
co=0;
for k=i-1:i+1
for p=j-1:j+1
co=co+1;
t(co)=f1(k,p);
end
end
fi=t(5);
me=median(t);
C1=[t(2),t(3),t(5),t(6)];
C2=[t(1),t(2),t(4),t(5)];
C3=[t(4),t(5),t(7),t(8)];
C4=[t(5),t(6),t(8),t(9)];
tm(1)=mean(abs(fi-C1));
tm(2)=mean(abs(fi-C2));
tm(3)=mean(abs(fi-C3));
tm(4)=mean(abs(fi-C4));
tR=min(tm);
if tR>Q
S1(i,j)=me;
else
S1(i,j)=f1(i,j);
end
S2(i,j)=me;
end
end
figure,imshow(S1),title('rough set');
figure,imshow(S2),title('median');
elseif o == 3
Ns=0.05;
f1(:,:,1)=imnoise(f(:,:,1),'salt & pepper',Ns);
f1(:,:,2)=imnoise(f(:,:,2),'salt & pepper',Ns);
f1(:,:,3)=imnoise(f(:,:,3),'salt & pepper',Ns);
figure,imshow(f1),title('Noise Image');
Q=0.1;
t(1:9)=0;
S1(1:m,1:n,1:o)=0;
S2(1:m,1:n,1:o)=0;
for q=1:o
for i=2:m-1
for j=2:n-1
co=0;
for k=i-1:i+1
for p=j-1:j+1
co=co+1;
t(co)=f1(k,p,q);
end
end
fi=t(5);
me=median(t);
C1=[t(2),t(3),t(5),t(6)];
C2=[t(1),t(2),t(4),t(5)];
C3=[t(4),t(5),t(7),t(8)];
C4=[t(5),t(6),t(8),t(9)];
tm(1)=mean(abs(fi-C1));
tm(2)=mean(abs(fi-C2));
tm(3)=mean(abs(fi-C3));
tm(4)=mean(abs(fi-C4));
tR=min(tm);
if tR>Q
S1(i,j,q)=me;
else
S1(i,j,q)=f1(i,j,q);
end
S2(i,j,q)=me;
end
end
end
figure,imshow(S1),title('roughset Result');
figure,imshow(S2),title('median filter result');
end