MATLAB CODE - for Image Segmentation Based on Threshold Processing



clc;
close all;
clear all;

a = imread('grayflower256.jpg');
a = rgb2gray(a);
subplot(3,3,1);
imshow(a); title('Original Image');

% Simple thresholding at 0.3
% This is equivalent to saying threshoding at pixel value
% 0.3 x 255 = 76.5 , approximatesly 77
% imlabel() is a Matlab command that can threshold any matrix


level = 0.3;
%% Display the threshold image
subplot(3,3,2);
segimage1 = im2bw(a,level);
imshow(segimage1); title('Simple Thresholding at 0.3');


% Simple thresholding at 0.6
% So threshold value is 0.6 x 255 = 153
% Single Thresholding can be done like this also


%% Display the threshold image
subplot(3,3,3);
imshow(a > 153); title('Simple Thresholding at 0.6');


% Multiple thresholding Algorithm
% Let us assume that the output should be zero if pixel value is
% if <= 0.1 x 255 = 25.5 = 26, pixel output 204 if pixel value is <= 0.9 x
% 255 = 230 and 0 if pixel value is above 230.

%% Create a temporary matrix g

tmp = a;

[m n]= find(a<26);
for j = 1: length(m)
    tmp(m(j),n(j))=0;
end

[m n]= find(a>26 & a <= 230);
for j = 1: length(m)
    tmp(m(j),n(j))=0.8;
end

[m n]= find(a>230);
for j = 1: length(m)
    tmp(m(j),n(j))=0;
end
subplot(3,3,4);
segimage2 = im2bw(tmp,0);
imshow(segimage2); title('Multiple threshoding(Between 27-230)');

%% Find the threshold Value using Otsu Algorithm

level = graythresh(a);

%% Display the threshold image

subplot(3,3,5);
segimage = im2bw(a,level);
imshow(segimage); title('Otsu - Optimal Segmented Image');

%% Display Blured Image

b = imread('bluredtxt.jpg');
subplot(3,3,6);
imshow(b); title('Badly illuminated Image');

level = graythresh(b);
subplot(3,3,7);
segimage = im2bw(b,level);
imshow(segimage); title('Otsu - Segmentation for bad illuminated Image');


b = imread('bluredtxt.jpg');
b = rgb2gray(b);

%Create an average Image

avgfilt = ones(13,13);
adaptfiltmask = avgfilt/sum(avgfilt);
im = imfilter(b,adaptfiltmask,'replicate');

%Create an median image
im1 = medfilt2(b,[20 20]);

%Adaptive threshold algorithm use
% threshold = mean + constant (Here 18)

thresh = im+18;
adaptthreshimg = b - thresh;
subplot(3,3,8);
imshow(adaptthreshimg > 0);


%Adaptive threshold algorithm used threshold = mean + constant (Here 2)
thresh1 = im1 + 2;
adaptthreshimg = b - thresh1;
subplot(3,3,9);
imshow(adaptthreshimg > 0);

MATLAB CODE - for Image Segmentation Based on Threshold Processing MATLAB CODE - for Image Segmentation Based on Threshold Processing Reviewed by Suresh Bojja on 10/22/2015 01:24:00 AM Rating: 5

Subjects & Labs

AC (6) ADS (2) ATMEL (6) AWP (5) C- Language (3) CAO (5) CDAC (1) Cloud Computing (1) CMC (6) CN (10) CONSERVATION & MANAGEMENT Complete Notes (1) Control Sys (2) CVSM (2) Data Analytics (1) DC (5) Digital System Design (3) DIP (17) DLD (10) DLD Lab Manual (1) DS (4) DSD&DICA (9) DSP (6) EDC (45) Electonic Components (2) Embedded (7) EMWTL (6) ENERGY AUDIT (1) Engg. Mechanics (2) Engg.Chemistry (11) Faculty Files (6) Faculty Labs (1) Fuzzy (3) II MID (1) Job Exam Papers (4) LICA (1) Linux OS (1) Materials (1) MATLAB (27) MCU IDE 8051 (1) MEMS (4) MPMC (57) MPMC LAB Manual (3) MTECH (6) MWE (2) OC (1) PDC (9) Projects (8) RADAR (1) Research (9) RVSP (3) SC (1) Softwares (1) SS (1) STLD (18) TCSS (1) TSSN (1) TV Engg (1) VHDL (4) VLSI (8) VLSI Lab manual (1) WCN (1) Wireless Commu. (1) WSN (2)

Contact Form :

Name

Email *

Message *

Theme images by enjoynz. Powered by Blogger.