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');

Tuesday, August 3, 2010

Morphological opening in IDL

pro Mopening
; Handle TrueColor displays:
DEVICE, DECOMPOSED=0

;Read the image
path=FILEPATH('pollens.jpg',SUBDIR=['examples','demo','demodata'])
READ_JPEG, path, img

; Create window:
WINDOW, 0, XSIZE=700, YSIZE=540

;Show original image
XYOUTS, 180, 525, 'Original Image', ALIGNMENT=.5, /DEVICE
TV, img, 20, 280

;Apply the threshold
thresh = img GE 140B

;Load a simple color table
TEK_COLOR

;Display edges
XYOUTS, 520, 525, 'Edges', ALIGNMENT=.5, /DEVICE
TV, thresh, 360, 280

;Apply opening operator
open = MORPH_OPEN(thresh, REPLICATE(1,3,3))

;Show the result
XYOUTS, 180, 265, 'Opening Operator', ALIGNMENT=.5, /DEVICE
TV, open, 20, 20

;Show pixels that have been removed in white
XYOUTS, 520, 265, 'Removed Pixels in White', ALIGNMENT=.5, /DEVICE
TV, open + thresh, 360, 20
end

Hit or Miss operation in IDL

pro Mhotmiss
;Handle TrueColor displays:
DEVICE, DECOMPOSED=0

;Read the image
path=FILEPATH('pollens.jpg',SUBDIR=['examples','demo','demodata'])
READ_JPEG, path, img

WINDOW, 0, XSIZE=700, YSIZE=540

; Display the original image
XYOUTS, 180, 525, 'Original Image', ALIGNMENT=.5, /DEVICE
TV, img, 20, 280

rh = 2 ;Radius of hit disc
rm = 4 ;Radius of miss disc

;Create a binary disc of given radius.
hit = SHIFT(DIST(2*rh+1), rh, rh) LE rh

;Complement of disc for miss
miss = SHIFT(DIST(2*rm+1), rm, rm) GT rm

;Load discrete color table
TEK_COLOR

;Apply the threshold
thresh = img GE 140B

; Display the thresholded image
XYOUTS, 520, 525, 'Thresholded Image', ALIGNMENT=.5, /DEVICE
TV, thresh, 360, 280

;Compute matches
matches = MORPH_HITORMISS(thresh, hit, miss)

;Expand matches to size of hit disc
matches = DILATE(matches, hit)

;Show matches.
XYOUTS, 180, 265, 'Matches', ALIGNMENT=.5, /DEVICE
TV, matches, 20, 20

;Superimpose, showing hit regions in blue.
;(Blue = color index 4 for tek_color.)
XYOUTS, 520, 265, 'Superimposed, hit regions in blue',$
ALIGNMENT=.5, /DEVICE
TV, thresh + 3*matches, 360, 20
end

Image Resizing in IDL

Pro Image_Resizing
device,decompose=0
loadct,0
READ_JPEG,'C:\ITT\IDL64\examples\data\md1107g8a.JPG',im
sz=SIZE(im,/DIMENSIONS)
WINDOW,0,XSIZE=sz[0],YSIZE=sz[1],TITLE='IMAGE'
TV,im
sz1=sz*1.5 ; IMAGE SIZE
PRINT,SZ1
im1 = CONGRID(im,sz1[0],sz1[1],/interp) ; RESIZE THE IMAGE
WINDOW,1,XSIZE=sz1[0],YSIZE=sz1[1],TITLE='RESIZED_IMAGE'
TV,im1

end

RGB to NTSC Color Conversion in IDL

PRO rgbcolorconversion
file = FILEPATH('rose.jpg',SUBDIRECTORY = ['examples', 'data'])
queryStatus = QUERY_IMAGE(file, imageInfo)
PRINT, 'Query Status = ', queryStatus
HELP, imageInfo, /STRUCTURE
imageSize = imageInfo.dimensions
READ_JPEG,file,image
imageDims = SIZE(image, /DIMENSIONS)
; rgb to gray conversion
gimage=fltarr(imagedims[0],imagedims[1],imagedims[2])
CM=[[0.299, 0.587, 0.144],[0.596, -0.274, -0.322],[0.211, -0.523,0.312]]
for i=0,imagedims[1]-1 do begin
for j=0,imagedims[2]-1 do begin
V=image[*,i,j]
gimage[*,i,j]=CM * TRANSPOSE(V)
endfor
endfor

interleaving = WHERE((imageDims NE imageSize[0]) AND $
(imageDims NE imageSize[1])) + 1
PRINT, 'Type of Interleaving = ', interleaving
DEVICE, DECOMPOSED = 1
WINDOW, 0, XSIZE = imageSize[0], YSIZE = imageSize[1], $
TITLE = 'An RGB Image'
TV, image, TRUE = interleaving[0]
WINDOW, 1, XSIZE = imageSize[0], YSIZE = imageSize[1], $
TITLE = 'An NTSC Image'
TV, gimage, TRUE = interleaving[0]
end

Wavelet Transformation in IDL

PRO WAVELETTRANSFORM


READ_GIF,'d:\Image\bird.gif',f
sz=size(f,/dimensions)
DEVICE,DECOMPOSED = 0
LOADCT, 0
WINDOW,0,XSIZE=SZ[0],YSIZE=SZ[1],TITLE='Original Image'
tvscl, congrid(f,sz[0],sz[1])
f1 = wtn(f,20)
f2=abs(f1)^2
f2 = alog10(f2)
WINDOW,1,XSIZE=SZ[0],YSIZE=SZ[1],TITLE='Transformed Image'
tvscl, congrid(f2,sz[0],sz[1])
f3 = wtn(f1,20,/inverse)
WINDOW,2,XSIZE=SZ[0],YSIZE=SZ[1],TITLE='inverse Transformed Image'
tvscl, congrid(f3,sz[0],sz[1])

END

POWER SPECTRUM of IMAGES in IDL

PRO POWER_SPECTRUM_EX
im = [64,64]
file = FILEPATH('abnorm.dat',SUBDIRECTORY =['examples','data'])
f = READ_BINARY(file,DATA_DIM=im)
sz = 2*im
DEVICE, DECOMPOSED = 0
loadct, 0
f1 = FFT(f)
center = im/2 + 1
fs = SHIFT(F1,center)
ps = ABS(fs)^2
sps = ALOG10(ps)
WINDOW,0,XSIZE = SZ[0],YSIZE = SZ[1],TITLE = 'POWER SPECTRUM'
TVSCL,CONGRID(sps,sz[0],sz[1])
END

Creating Object boundaries in IDL

pro OBJECTBOUNDARY_SMOOTHING

;Initializing the display
DEVICE, DECOMPOSED = 0, RETAIN = 2
LOADCT, 0

;Reading a jpeg file
file = FILEPATH('meteor_crater.jpg', SUBDIRECTORY = ['examples','data'])
READ_JPEG,file, f,/GRAYSCALE
sz = SIZE(f, /DIMENSIONS)
print, sz

;creating a window
WINDOW, 0, XSIZE = (2*SZ[0]),YSIZE=(SZ[1])

TV,f,0

;writing a text object on image
XYOUTS, 50, 220, 'Original Image', ALIGNMENT = 0.5, /DEVICE, COLOR = 255

;Image smoothing
f1 = smooth(f,7,/edge_truncate)
tvscl, f1,1
XYOUTS, (50+SZ[0]), 220, 'SMOOTH IMAGE', ALIGNMENT = 0.5, /DEVICE, COLOR = 255

end

HIGH_PASS filter in IDL

PRO HIGH_PASS_FILTER


READ_GIF,'d:\Image\bird.gif',im
sz=SIZE(im,/DIMENSIONS)
im1=float(im)/255;
nim=im1
KER = REPLICATE(-1.0,3,3)
KER[1,1]=8.0
kER=FLOAT(KER)
PRINT,KER

nim=CONVOL(im1,ker)
nim=byte(nim*255)
WINDOW,0,XSIZE=sz[0],YSIZE=sz[1],TITLE='INPUT IMAGE'
TV,im
WINDOW,1,XSIZE=sz[0],YSIZE=sz[1],TITLE='MEAN filtered'
TV,nim
PRINT,NIM[0:9,0:9]
END

MEDIAN filter in IDL

PRO MEDIAN_FILTER
READ_GIF,'d:\Image\bird.gif',im
im1=im
sz=SIZE(im,/DIMENSIONS)
s=[0,0,0,0,0,0,0,0,0]
FOR i=1,sz[0]-2 DO BEGIN
FOR j=1,sz[1]-2 DO BEGIN
CO=0;
FOR k = i-1,i+1 DO BEGIN
FOR l = j-1,j+1 DO BEGIN
s[CO]=im[k,l]
CO=CO+1
ENDFOR
ENDFOR
im1[i,j]=MEDIAN(s)
ENDFOR
ENDFOR
WINDOW,0,XSIZE=sz[0],YSIZE=sz[0],TITLE='ORIGINAL IMAGE'
TV,im
WINDOW,1,XSIZE=sz[0],YSIZE=sz[0],TITLE='MEDIAN IMAGE'
TV,im1
END

REGION GROWING in IDL

pro REGION_GROWING

; initialize the device
DEVICE, DECOMPOSED = 0, RETAIN = 2
LOADCT, 0
;read the image

file = FILEPATH('md1107g8a.jpg',SUBDIRECTORY = ['examples','data'])
READ_JPEG, file, f,/GRAYSCALE
sz = SIZE(f,/DIMENSIONS)

;RESIZE THE IMAGE
f = REBIN(BYTSCL(f),sz[0]*2,sz[1]*2)
sz=2*sz

WINDOW,0,XSIZE = SZ[0], YSIZE=SZ[1],TITLE = 'SELECT A POINT ON THE IMAGE'
TVSCL, F

;SELECT A POINT  FROM THE IMAGE
CURSOR, xi,yi, /DEVICE
x = LINDGEN(10*10) MOD 10 + xi
y = LINDGEN(10*10) / 10 + yi
roiPixels = x + y * sz[0]

WDELETE, 0
TOPCLR = !D.TABLE_SIZE - 1
TVLCT, 255,0,0, TOPCLR
regionPts = BYTSCL(F, TOP = (topClr - 1))
regionPts[roiPixels] = topClr

WINDOW, 0, XSIZE = sz[0], YSIZE = sz[1], TITLE = 'ORIGINAL REGION'
TV, regionPts

;REGION GROWING FUNCTION

ROIPIXELS = REGION_GROW(F,roiPixels, THRESHOLD = [185,235])

F1 = BYTSCL(F, TOP = (TOPCLR-1))
F1[ROIPIXELS] = TOPCLR

WINDOW,2, XSIZE=SZ[0],YSIZE=SZ[1],TITLE = 'THRESHOLD GROWN REGION'
TV, F1

end

Monday, August 2, 2010

Auto Draft

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