0% found this document useful (0 votes)
51 views

Assignment7 Digital Image Processing

This MATLAB code implements Otsu's thresholding method and manual thresholding to perform image segmentation on an image. It first reads in an image, resizes it, and applies Otsu's thresholding method to obtain a threshold value. It then manually implements thresholding by calculating the histogram, finding the threshold that minimizes intra-class variance, and binarizes the image. It displays the output of both methods for comparison. Key steps include resizing the image, calculating the histogram, finding the optimal threshold, and binarizing the image.

Uploaded by

Naruto Uzumaki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Assignment7 Digital Image Processing

This MATLAB code implements Otsu's thresholding method and manual thresholding to perform image segmentation on an image. It first reads in an image, resizes it, and applies Otsu's thresholding method to obtain a threshold value. It then manually implements thresholding by calculating the histogram, finding the threshold that minimizes intra-class variance, and binarizes the image. It displays the output of both methods for comparison. Key steps include resizing the image, calculating the histogram, finding the optimal threshold, and binarizing the image.

Uploaded by

Naruto Uzumaki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DIGITAL IMAGE PROCESSING

Assignment 7
Name: Prathamesh
Anwekar

BITS ID: 2020A3PS1039P

MATLAB Code:
function assignment7
img=imread('img.tif');
img_rs = imresize(img, [512 512]);

[c,~] = imhist(img_rs,16);
threshold = otsuthresh(c);
op = imbinarize(img_rs,threshold);
imshow(op);
title("Inbuilt Otsu's Method's output");

%manual implementation
lin_img=reshape(img_rs,[],1);
histogram=hist(lin_img,0:255);
H=reshape(histogram,[],1);
index_0=0:255;
index_1=reshape(index_0,[],1);
out=zeros(size([1 256]));

for i=0:255
[w1,var1]=calculate(1,i);
[w2,var2]=calculate(i+1,255);
out(i+1)=(w1*var1)+(w2*var2);
end

%Min in the array.


[~,val]=min(out);
temp_val=(val-1)/256
binary_img=imbinarize(img_rs,temp_val);
figure,imshow(binary_img);
title("Manual implementation");

function [weight,var]=calculate(m,n)
%weight calculation
weight=sum(H(m:n))/sum(H);

%Mean Calculation
val=H(m:n).*index_1(m:n);
tot_val=sum(val);
mean=tot_val/sum(H(m:n));
if(isnan(mean)==1)
mean=0;
end

%Variance calculation
val2=(index_1(m:n)-mean).^2;
numerator=sum(val2.*H(m:n));
var=numerator/sum(H(m:n));
if(isnan(var)==1)
var=0;
end
end
end

Threshold value obtained after the Otsu method = 0.347


IMAGE OBTAINED AFTER SEGMENTATION:

You might also like