Thursday, 19 June 2014

Circular hough Transfrom matlab code


%E-Edge image which is binary image
%distMax=diagonal
[iHeight,iWidth]=size(E);
%max possible distance from origin
distMax=round(sqrt(iHeight^2+iWidth^2);
%range of theta values
theta=-90:1:89
rho=-distMax:1:distMax;
%alocate accumulator array
H=zeros(length(rho),length(theta));

for ix=1:iWidth
   for iy=1:iHeight
      if E(iy,ix)==0

      % fill accumulator
       for iTheta=1:length(theta)
           t=theta(iTheta)*pi/180;  %get angle in radius

            %calculate distance from origin, given angle
             dist=ix*cost(t)+iy*sint(t);

             % find rho value closest to this
             [d,iRho]=min(abs(rho-dist));
             if d<=1
               %inc accumulator array
               H(iRho,iTheta)=H(iRho,ITheta)+1;
             end
           end
        end
     end
 end

Wednesday, 18 June 2014

Morphology in matlab

Morphology

strel constructs structuring elements with a variety of shapes and sizes. Its basic syntax is
se=strel(shape,parameters)

se1 = strel('square',11)      % 11-by-11 square
se2 = strel('line',10,45)     % length 10, angle 45 degrees
se3 = strel('disk',15)        % disk, radius 15
se4 = strel('ball',15,5)      % ball, radius 15, height 5



strel(’diamond’,R)
Creates a flat, diamond-shaped structuring element,where R specifies the distance from the structuring element origin to the extreme points of the diamond
strel(’disk’,R)
Creates a flat, disk-shaped structuring element with radius R.
strel(’line’,LEN,DEG)
Creates a flat, linear structuring element, where LEN specifies the length, and DEG specifies the angle (in degrees) of the line, as measured in a counterclock-wise direction from the horizontal axes
strel(’octagon’,R)
Creates a flat, octagonal structuring element, where R specifies the distance from the structuring element origin to the sides of the octagon, as measured along the horizontal and vertical axes. R must be a nonnegative multiple of 3
strel(’pair’,OFFSET)
Creates a flat structuring element containing two members. One member is located at the origin. The second member’s location is specified by the vector OFFSET, which must be a two-element vector of integers.
strel(’periodicline’,P,V)
Creates a flat structuring element containing 2*P+1members.Vis a two-element vector con-taining integer-valued row and column offsets.One structuring element member is located atthe origin. The other members are located at1*V,-1*V,2*V,-2*V,V.
strel(’rectangle’,MN)
Creates a flat, rectangle-shaped structuring element, where MN specifies the size. MN must be a two- element vector of nonnegative integers. The first ele- ment of MN is the number of rows in the structuring el-ement; the second element is the number of columns
strel(’square’,W)
Creates a square structuring element whose width is W pixels. W must be a nonnegative integer scalar
strel(NHOOD)
Creates a structuring element of arbitrary shape. NHOOD is a matrix of 0s and 1s that specifies the shape

a=imread(‘image.jpg’);
% invert the array
a=255-a;
%create structuring element
se=strel(‘disk’,7);
a_erode=imerode(a,se);
a_dilate=imdilate(a,se);
a_open=imopen(a,se);
a_closed=imclose(a,se);


or

a=imread(‘image.jpg’);
B=[0 1 0;1 1 1;0 1 0];
A2=imdilate(a,B);

convert frames to video

ImagesFolder=uigetdir;
jpegFiles = dir(strcat(ImagesFolder,'\*.jpg'));
S = [jpegFiles(:).datenum];
[S,S] = sort(S);
jpegFilesS = jpegFiles(S);
% create a video file object called MyVideo
VideoFile=strcat(ImagesFolder,'\MyVideo');
writerObj = VideoWriter(VideoFile);
%Define the video frames per second
fps= 10;
writerObj.FrameRate = fps;
%Open file for writing video data
open(writerObj);
%convert them to movie frames using im2frame and writing the video data to file using writeVideo
for t= 1:length(jpegFilesS)
    Frame=imread(strcat(ImagesFolder,'\',jpegFilesS(t).name));
    writeVideo(writerObj,im2frame(Frame));
end
close(writerObj);

Monday, 16 June 2014

Circular Hough Transform


                   First we find all the edge in the image .This step has nothing to do with Hough transform and any edge detection technique can be used .It could be canny or sobel or prewitt. Here we used sobel edge detection technique. At each edge point we draw a circle with centre in the point with the desired radius .This circle is drawn in the parameter space, such that our x!axis is the a! value and y!axis in the b!value and z! axis is the radii. At the coordinates which belongs to the parameter of the drawn circle .We increment the value in our accumulator matrix which essentially has same size as parameter space .In this way we sweep over energy edge point in the input image drawing circle with the desired circle with desired radii and incrementing the value in our accumulator

When every edge point and every desired radius is used ,we can turn our attention to accumulator will now contain numbers corresponding to the number of circles passing through the individual coordinate .

The parameter space for CHT







One important difference between the Hough Transform and other approaches is resistance of the former to noise in the image and its tolerance towards holes in the boundary line.

Robert cross operator

The Robert cross operator performs a simple, quick to compute, 2-D spatial gradient measurement on image. The output pixels values at each point represent the absolute magnitude of the spatial gradient of the input at that point.

The operator consists of a pair of 2×2 convolution masks  kernels as shown. One kernel is simply rotated by the other by 90 degree. It is very similiar to the Sobel operator

Gx=
Gy
These masks are designed to respond maximally to edges running at 45° o the pixel grid
Then these are applied to the input image to produce separate measurements of gradient component in each orientation. Gx and Gy
The gradient magnitude is given by
The angle of orientation of the edge giving rise to the spatial gradient

Saturday, 14 June 2014

Convert Video to frames in matlab

The videoReader takes the input video. It creates a folder called snaps. If the directory is not there, it creates using mkdir
The frames are read now and stored in Frame. Later the file is taken and written into the output folder. The frame is then incremented.


clc;
close all;
clear all;
filename = 'rhinos.avi';
mov = VideoReader(filename);
opFolder = fullfile(cd, 'snaps');
if ~exist(opFolder, 'dir')
      mkdir(opFolder);
end
numFrames = mov.NumberOfFrames;
numFramesWritten = 0;
for t = 1 : numFrames
    Frame = read(mov, t);
    opBaseFileName = sprintf('%3.3d.png', t);
     opFullFileName = fullfile(opFolder, opBaseFileName);
     imwrite(Frame, opFullFileName, 'png');
     Indicate = sprintf('Wrote frame %4d of %d.', t, numFrames);
     disp(Indicate);
     numFramesWritten = numFramesWritten + 1;
end
Indicate = sprintf('Wrote %d frames to folder "%s"',numFramesWritten, opFolder);
disp(Indicate);

Extract frames from video



The following code takes input as a video reader and read the number of frames and writes to a directory.
and the output is a video of the frames which are sorted in certain time intervals.

workingDir = tempname;
mkdir(workingDir);
mkdir(workingDir,'images');
shuttleVideo = VideoReader('shuttle.avi');
for ii = 1:shuttleVideo.NumberOfFrames
    img = read(shuttleVideo,ii);
    imwrite(img,fullfile(workingDir,'images',sprintf('img%d.jpg',ii)));
end
imageNames = dir(fullfile(workingDir,'images','*.jpg'));
imageNames = {imageNames.name}';
disp(imageNames(1:10));
imageStrings = regexp([imageNames{:}],'(\d*)','match');
imageNumbers = str2double(imageStrings);
[~,sortedIndices] = sort(imageNumbers);
sortedImageNames = imageNames(sortedIndices);
disp(sortedImageNames(1:10));
outputVideo = VideoWriter(fullfile(workingDir,'shuttle_out.avi'));
outputVideo.FrameRate = shuttleVideo.FrameRate;
open(outputVideo);
for ii = 1:length(sortedImageNames)
    img = imread(fullfile(workingDir,'images',sortedImageNames{ii}));
    writeVideo(outputVideo,img);
end
close(outputVideo);
shuttleAvi = VideoReader(fullfile(workingDir,'shuttle_out.avi'));
mov(shuttleAvi.NumberOfFrames) = struct('cdata',[],'colormap',[]);
for ii = 1:shuttleAvi.NumberOfFrames
    mov(ii) = im2frame(read(shuttleAvi,ii));
end
set(gcf,'position', [150 150 shuttleAvi.Width shuttleAvi.Height])
set(gca,'units','pixels');
set(gca,'position',[0 0 shuttleAvi.Width shuttleAvi.Height])
image(mov(1).cdata,'Parent',gca);
axis off;
movie(mov,1,shuttleAvi.FrameRate);
displayEndOfDemoMessage(mfilename)







How to acquire video from matlab in real time and convert to frames

To get the information about devices

info=imaqhwinfo
info.InstalledAdaptors
info = imaqhwinfo('winvideo')
dev_info = imaqhwinfo('winvideo', 1)
info.DeviceInfo
info.DeviceInfo.SupportedFormats
%celldisp(dev_info.SupportedFormats);
imaqtool
How to acquire video from matlab in real time

vid=videoinput('winvideo',1);
preview(vid);
start(vid);
% foto=getsnapshot(vid);
%image(foto);
im=getdata(vid,1);figure,imshow(im);
imwrite(im,'testimage.jpg');


  • imaqhwinfo gives information about the existing adaptors for your webcam device
  • winvideo is one of the adaptors.
  • Further information pertaining to the device can be obtained by imaqhwinfo('winvideo',1) where 1 is the Device ID you saw earlier
  • The resolution (800×600, 1024×768, 1600×1200, etc.), format (RGB, YUV, etc.) which needs to be selected when creating a video object.
  • create a videoinput object
    ->vidobj = videoinput('winvideo',1,'RGB_1024x768');
  • to start your video object start(vidobj)
  • You can obtain snapshots of capture by using the frame = getsnapshot(vidobj)
  • view the continuous stream of frames by saying preview(vidobj);
  • A stop(vidobj) followed by delete(vidobj) is the best way to follow.
  • All the options can be seen by imaqhelp(videoinput).

Friday, 13 June 2014

Matlab Commands

Image Representation

Intensity:


Each pixel value in the dynamic range [minP, maxP]

Can represent a grayscale image, results of a 2d function etc.

Useful commands:

imshow () ,imagesc(), colormap()

binary masks

Can represent areas of interest in image, morphological structuring elements and more

Useful commands:

bwconncomp(),labelmatrix(), bwmorph(), bwdist(), im2bw(), bwperim()

image and video I/O

• imread() – read image

• imwrite() – write image

• im2frame() – convert image to movie frame

• movie2avi() – write avi file

• aviread() – read avi file

• mmreader()/VideoReader() – read video (better)

• VideoWriter() – create video file (2011b+)

• movie() – show movie

• implay() – show video interactively

Matrix Access

Useful Commands:


• sub2ind() – convert subscript (e.g. (r,c,clr)) to index (n).

• ind2sub() – convert index (n) to subscipt (e.g. (r,c,clr)).

• meshgrid() – generate X,Y grids.

Image Manipulation

Useful Functions:


• imcrop() – Useful for interactive cropping.

• imrotate() – Rotate image.

• imfilter() – Use kernal to convolve/correlation.

• nlfilter() – Sliding neighborhood operation.

• blockproc() – Perform function on distinct blocks.

• fspecial() – Create common image filter kernels.

• imresize() – scale up/down image using defined interpolation.

• padarray() – Pad image.

• colfilt() – Column-stack filtering (faster)

• imfreehand() – Select region with mouse

how to read an image and remove noise using median filter


This program reads the image of cameraman.
imnoise- adds salts and pepper noise to the read image
This can be removed using median filter.

x=imread('cameraman.tif');
y=imnoise(x,'salt & pepper',0.2);
I=find(x==0|x==255);
x1=medfilt2(x);
x2=x;
x2(I)=x1(I);
imshow([y x1 x2],[]);



Thursday, 12 June 2014

image processing video tutorials link


 It is a video collection on Image processing of IIT kharagpur. It deals with image acquistion, image restoration and many other.

http://freevideolectures.com/Course/2316/Digital-Image-Processing-IIT-Kharagpur

Monday, 2 June 2014

Hough Transform


Edges are among the earliest primitives that have been studied in computer vision. There are many other primitives that can be defined as Corner, line, circle, ellipse, square, triangle


The ultimate vision tasks involve the detection of general objects Face, pedestrian, vehicle, mouth, eyes, door

Two Paradigms

1. Model-based approaches : built a model
2. Data-driven (machine learning) approaches : here training set is provided to find objects for which complex models can't do it.


Generalizations of Hough Transform


 [H,theta,rho]=hough(f);
figure, imshow(theta,rho,H, [ ], 'notruesize')
axis on, axis normal;
xlabel('\theta'),ylabel('\rho'),
colorbar
colormap(jet)
[r,c]=houghpeaks(H,5);
hold on
plot(theta(c), rho(r), 'linestyle', 'none','marker' , ...
's', 'color', 'w')

Image Enhancement


Two types of techniques are done
1.Spatial domain techniques
  • Point operations
  • Histogram equalization and matching
  • Applications of histogram-based enhancement

2. Frequency domain techniques

  • Unsharp masking
  • Homomorphic filtering

MATLAB function >imadjust

How can we achieve adjustment
1. histogram equalization
2. histogram specification

Histogram of an image represents the relative frequency of occurrence of various gray levels in the image
MATLAB function >imhist(x)

function y=hist_eq(x)

%to calculate histogram
[M,N]=size(x);
for i=1:256    
h(i)=sum(sum(x= =i-1));
End

% histogram equalization
y=x;s=sum(h);
for i=1:256    
I=find(x= =i-1);    
y(I)=sum(h(1:i))/s*255;
end

Unsharp masking
% Implementation of Unsharp masking

function y=unsharp_masking(x,lambda)

% Laplacian operation
h=[0 -1 0;-1 4 -1;0 -1 0]/4;
dx=filter2(h,x);
y=x+lambda*dx;


Geometrical transformation of images


Geometrical transformation of images


close all
clear all
a=imread('cameraman.tif');
b=imrotate(a,-30,'bilinear','crop');
imshow(a);
figure,imshow(b);

Image Scaling

b=imrotate(a,-3,'bilinear','crop');
>> imshow(a);
>> figure,imshow(b);
>> [m,n]=size(a);
>> i=0.5;
>> c=imresize(a,[m*i n/i]);
>> figure,imshow(c);


Interpolation

a=imread('cameraman.tif');
[x y]=meshgrid(1:256,1:256);
t=pi/4;
for i=1:256
    for j=1:256
x1(i,j)=cos(t)*x(i,j)+sin(t)*y(i,j);
y1(i,j)=sin(t)*x(i,j)+cos(t)*y(i,j);
end;
end
z=interp2(x,y,a,x1,y1,'cubic');


Sampling rate:

f=ind2gray(a,gray(256));
>> f1=imresize(f,0.2);
>> figure,imshow(f1);
>>  f2=imresize(f1,5);
>> figure,imshow(f2);
imhist(f);


Add noise to an image and median filter is used to remove the noise

f=ind2gray(a,gray(256));
f1=imnoise(f,'salt & pepper',0.05);
f2=medfilt2(f1,[3 3]);
figure; imshow(f);
figure; imshow(f1);
figure; imshow(f2);

Sunday, 1 June 2014

Image Proceesing main techniques


to gray scale image

A gray scale image consists of each pixel and its value ranges from 0 (black) to 255 (white). The transformation is based on the equation
gray value = 0.299*r + 0.587*g + 0.114*b
where r, g, b are the red, green, blue values of a pixel in the color image.


negative image
It mainly works for gray scale image
It replaces each color (gray scale value) with its opposite value
This can be accomplished by the simple formula: new value = 255 - old value.

Brighten

This  brightens the image

Darken

This darkens the image

Thresholding


We use this method only for gray scale images. Select a value T.and any pixel having a value not less than T will be changed to white(Foreground), otherwise to black(background)


Histogram equalization

Histogram equalization means all pixels values are spread uniformly in the image.The steps necessary to do are:
  • Generate histogram. The histogram is an array h[0..255] of integer, in which h[i] is the occurrence frequency of pixels having value i in a given image.
  • Generate Cumulatice Distribution Function (CDF). The CDF function is defined as follows:
  • CDF[i] = h[0] + h[1] + ... + h[i] for i = 0, 1, .., 255.
  • After scaling, map into a uniform histogram.

Median filter

Median filter replaces each pixel with its median neighborhood. The middle pixel is called the median neighbor

At first select  a neighbourhood mask such as 3x3 etc

1.Arrange the pixels in the ascending order
2. Select the median of those values
3. Replace the value at the center of the neighbourhood mask

Erosion

Erosion  is mainly applied to only black and white images. Erosion removes  a layer from around the periphery of all regions.

Dilation

Dilation adds any background pixel that touches a foreground pixel. This will add a layer around the periphery of each region.

Counting objects
It is done by using region growing technique.
  1. Initialize the counter to 0 and unmark all pixels.
  2. For each unmarked pixel not on the background, do the following:
    1. Increment the counter by one.
    2. From this pixel, grow to all neighbors not on the background, and mark them with an identification number. This process is performed repeatedly to create an area.