The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Sunday, May 12, 2013

Applying Blob Detection Technique to the Original Image 2



clc;
clear all;
originalImage = imread('coins.png');
subplot(3, 3, 1);
imshow(originalImage);
% Maximize the figure window.
set(gcf, 'Position', get(0, 'ScreenSize'));
[pixelCount grayLevels] = imhist(originalImage);
subplot(3, 3, 2);
bar(pixelCount); title('Histogram of original image');
xlim([0 grayLevels(end)]); % Scale x axis manually.
  thresholdValue = 100;
  binaryImage = originalImage > thresholdValue;



binaryImage = imfill(binaryImage, 'holes');
% Display the binary image.
subplot(3, 3, 3); imagesc(binaryImage); colormap(gray(256)); title('Binary Image, obtained by thresholding'); axis square;
labeledImage = bwlabel(binaryImage, 8);    
% Label each blob so we can make measurements of it
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% pseudo random color labels
subplot(3, 3, 4); imshow(labeledImage, []);
title('Labeled Image, from bwlabel()');
axis square; subplot(3, 3, 5); imagesc(coloredLabels);
axis square;
caption = sprintf('Pseudo colored labels, from label2rgb().\nBlobs are numbered from top to bottom, then from left to right.');
title(caption);
blobMeasurements = regionprops(labeledImage, originalImage, 'all');  
numberOfBlobs = size(blobMeasurements, 1);
subplot(3, 3, 6); imagesc(originalImage);
title('Outlines, from bwboundaries()'); axis square; hold on;
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries);

for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
hold off;


Applying Blob Detection Technique to the Original Image



clc;
clear all;
originalImage = imread('coins.png');
subplot(3, 3, 1);
imshow(originalImage);
% Maximize the figure window.
set(gcf, 'Position', get(0, 'ScreenSize'));
[pixelCount grayLevels] = imhist(originalImage);
subplot(3, 3, 2);
bar(pixelCount); title('Histogram of original image');
xlim([0 grayLevels(end)]); % Scale x axis manually.
thresholdValue = 100;
binaryImage = originalImage > thresholdValue;

binaryImage = imfill(binaryImage, 'holes');
% Display the binary image.
subplot(3, 3, 3); imagesc(binaryImage); colormap(gray(256)); title('Binary Image, obtained by thresholding'); axis square;
labeledImage = bwlabel(binaryImage, 8); 
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
subplot(3, 3, 4);              imshow(labeledImage, []);          
title('Labeled Image, from bwlabel()');
axis square;      subplot(3, 3, 5);                                
imagesc(coloredLabels);
axis square;
caption = sprintf('Pseudo colored labels, from label2rgb().\nBlobs are numbered from top to bottom, then from left to right.');
title(caption);
blobMeasurements = regionprops(labeledImage, originalImage, 'all');  
numberOfBlobs = size(blobMeasurements, 1);
subplot(3, 3, 6); imagesc(originalImage);
title('Outlines, from bwboundaries()'); axis square;       hold on;
boundaries = bwboundaries(binaryImage);      
numberOfBoundaries = size(boundaries);

for k = 1 : numberOfBoundaries
              thisBoundary = boundaries{k};
              plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
hold off;

Applying Corner Detection Technique to the Original Image



I = checkerboard(50,2,2);
C = corner(I);
imshow(I);
hold on
plot(C(:,1), C(:,2), 'r*');

Applying Watershed Segmentation to the Original Image



I = imread('image.jpg');
subplot(1,2,1); imshow(I); title('Original Image');
se = strel('diamond',7);
Itop = imtophat(I,se);
Ibot = imbothat(I,se);
Ienhance = imsubtract(imadd(Itop,30), Ibot);
Iec = imcomplement(Ienhance);
Iemin = imextendedmin(Iec,22);
Iimpose = imimposemin(Iec,Iemin);
Iwat = watershed(Iimpose);
Iseg = label2rgb(Iwat);
subplot(1,2,2); imshow(Iseg); title('Watershed Segmented Image');

Applying Dilation and Erosion to the Original Image



Lenna = imread('lenna2.jpg');
SE3 = strel('square',3);
SE5 = strel('square',5);                                   
SE7 = strel('square',7);

ErodeLenna = imerode(Lenna,SE3);
subplot(2,3,1); imshow(ErodeLenna);  ylabel('Eroded Image'); title('5x5');
ErodeLenna = imerode(Lenna,SE5);
subplot(2,3,2); imshow(ErodeLenna); title('5x5');
ErodeLenna = imerode(Lenna,SE7);
subplot(2,3,3); imshow(ErodeLenna); title('7x7');

Converting Grayscale Image into Binary Image (Black-White)


Lenna = imread('lenna.png');
GrayLenna = rgb2gray(Lenna);
bwLenna = im2bw(GrayLenna);
subplot(1,2,1); imshow(bwLenna);                          
subplot(1,2,2); imhist(bwLenna)

Creating a Histogram of an Original Image



Lenna = imread('lenna.png');                                                     
GrayLenna = rgb2gray(Lenna);
imhist(GrayLenna)

Creating the Negative of an Original Image



negLenna = double(Lenna);
negLennaScale = 1.0/max(negLenna(:));
negLenna = 1 - negLenna*negLennaScale;
figure;
imshow(negLenna)

Implementing Edge Detection Techniques to an Original Image



Lenna = imread('Lenna2.jpg');

figure;   SobLenna = edge(Lenna, 'sobel', 0.055, 'horizontal');
subplot(1,3,1);  imshow(SobLenna); xlabel('Horizontal');
SobLenna = edge(Lenna, 'sobel', 0.055, 'vertical'); 
subplot(1,3,2);  imshow(SobLenna); xlabel('Vertical'); title('Sobel Edge Detection');
SobLenna = edge(Lenna, 'sobel', 0.055, 'both');
subplot(1,3,3);  imshow(SobLenna); xlabel('Both');

figure;   PreLenna = edge(Lenna, 'prewitt', 0.055, 'horizontal');
subplot(1,3,1);  imshow(PreLenna); xlabel('Horizontal');
PreLenna = edge(Lenna, 'prewitt', 0.055, 'vertical'); 
subplot(1,3,2);  imshow(PreLenna); xlabel('Vertical'); title('Prewitt Edge Detection');
PreLenna = edge(Lenna, 'prewitt', 0.055, 'both');
subplot(1,3,3);  imshow(PreLenna); xlabel('Both');

figure;   RobLenna = edge(Lenna, 'roberts');
subplot(1,3,1);  imshow(RobLenna); xlabel('With Default Threshold');
RobLenna = edge(Lenna, 'roberts', 0.055);
subplot(1,3,2);  imshow(RobLenna); xlabel('Threshold = 0.055'); title('Roberts Edge Detection');
RobLenna = edge(Lenna, 'roberts', 0.1);
subplot(1,3,3);  imshow(RobLenna); xlabel('Threshold = 0.1');

figure;   LogLenna = edge(Lenna, 'log');
subplot(1,3,1);  imshow(LogLenna); xlabel('With Default Threshold & Sigma');
LogLenna = edge(Lenna, 'log', 0.0055);
subplot(1,3,2);  imshow(LogLenna); xlabel('Threshold = 0.0055 & Default Sigma');
title('Laplacian of Gaussian Edge Detection');
LogLenna = edge(Lenna, 'log', 0.0055, 1.5);
subplot(1,3,3);  imshow(LogLenna); xlabel('Threshold = 0.0055 & Sigma = 1.5');

figure;   CanLenna = edge(Lenna, 'canny');
subplot(1,3,1);  imshow(CanLenna); xlabel('With Default Threshold & Sigma');
CanLenna = edge(Lenna, 'canny', 0.155);
subplot(1,3,2);  imshow(CanLenna); xlabel('Threshold = 0.155 & Default Sigma');
title('Canny Edge Detection');
CanLenna = edge(Lenna, 'canny', 0.155, 1.25);
subplot(1,3,3);  imshow(CanLenna); xlabel('Threshold = 0.155 & Sigma = 1.25');

Removing Noise from a Noisy Corrupted Image using various Filtering Techniques



Lenna = imread('lenna2.jpg');
NoiseLenna = imnoise(Lenna,'salt & pepper',0.02);

GF1 = fspecial('gaussian',[3 3],0.5);
GF2 = fspecial('gaussian',[5 5],0.5);
GF3 = fspecial('gaussian',[7 7],0.5);

AV1 = [1 1 1;
            1 1 1;
            1 1 1]/9;

AV2 = [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]/25;

AV3 = [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]/49;
  
GaussLenna = imfilter(NoiseLenna,GF1);
subplot(3,4,1);  imshow(GaussLenna);  ylabel('3x3'); title('Gaussian Filter');
GaussLenna = imfilter(NoiseLenna,GF2);
subplot(3,4,5);  imshow(GaussLenna);  ylabel('5x5');
GaussLenna = imfilter(NoiseLenna,GF3);
subplot(3,4,9);  imshow(GaussLenna);  ylabel('7x7');

AvgLenna = imfilter(NoiseLenna,AV1); 
subplot(3,4,2);  imshow(AvgLenna);  title('Averaging Filter');
AvgLenna = imfilter(NoiseLenna,AV2);
subplot(3,4,6);  imshow(AvgLenna);
AvgLenna = imfilter(NoiseLenna,AV3);
subplot(3,4,10);  imshow(AvgLenna);

MedLenna = medfilt2(NoiseLenna,[3 3]); 
subplot(3,4,3);  imshow(MedLenna);  title('Median Filter');
MedLenna = medfilt2(NoiseLenna,[5 5]);
subplot(3,4,7);  imshow(MedLenna);
MedLenna = medfilt2(NoiseLenna,[7 7]);
subplot(3,4,11);  imshow(MedLenna);

OrdLenna = ordfilt2(NoiseLenna,5,ones(3,3)); 
subplot(3,4,4);  imshow(OrdLenna);  title('Ordered Filter');
OrdLenna = ordfilt2(NoiseLenna,5,ones(5,5));
subplot(3,4,8);  imshow(OrdLenna);
OrdLenna = ordfilt2(NoiseLenna,5,ones(7,7));
subplot(3,4,12);  imshow(OrdLenna);

Adding Noise to an Original Image


 Lenna = imread('Lenna2.jpg');
 SPNoiseLenna = imnoise(Lenna,'salt & pepper',0.07);
 GNoiseLenna = imnoise(Lenna,'gaussian',0.01,0.005);
 PNoiseLenna = imnoise(Lenna,'poisson');
 SNoiseLenna = imnoise(Lenna,'speckle',0.06);
 subplot(2,2,1); imshow(SPNoiseLenna);  title('Salt & Pepper Noise');
 subplot(2,2,2); imshow(GNoiseLenna);   title('Gaussian Noise');
 subplot(2,2,3); imshow(PNoiseLenna);   title('Poisson Noise');
 subplot(2,2,4); imshow(SNoiseLenna);   title('Speckle Noise');

Finding Out the Fourier and Inverse Fourier Transform of an Image


A = [ 0 0 0 0 0 0 0 0;
         0 0 0 0 0 0 0 0;
         0 0 0 1 1 0 0 0;
         0 0 0 1 1 0 0 0;
         0 0 0 1 1 0 0 0;
         0 0 0 1 1 0 0 0;
         0 0 0 0 0 0 0 0;
         0 0 0 0 0 0 0 0]

iptsetpref('ImshowTruesize','manual') figure;
subplot(3,2,1); imshow(A); title('Original Image');
subplot(3,2,2); mesh(A); title('Mesh Plot of Original Image');

B = fft2(A);
subplot(3,2,3); imshow(B); title('2-D FFT of Original Image');
subplot(3,2,4); mesh(B); title('Mesh Plot of 2-D FFT');

C = ifft2(B);
subplot(3,2,5); imshow(C); title('Inv 2-D FFT of Above Image');
subplot(3,2,6); mesh(C); title('Mesh Plot of Inv 2-D FFT');

Converting an Image into Grayscale


Lenna = imread('lenna.png');
GrayLenna = rgb2gray(Lenna);                                                  
imshow(GrayLenna)

Loading and Displaying an Image


Lenna = imread('lenna.png');
imshow(Lenna)