How to remove scales and the black border from the segmented image
2 views (last 30 days)
Show older comments
I need a matlab code to remove the scale and the black part from the segmented image. The segmentation was done using Otsus. Here is the code below for Otsus.
%% Otsu Thresholding
close all;
clear all;
clc
%%================================================================================================
location = 'demo.jpg'; % folder in which your images exists
ds = imageDatastore(location) % Creates a datastore for all images in your folder
while hasdata(ds)
img = read(ds) ; % read image from datastore
figure, imshow(img); % creates a new window for each image
%%=================================================================================================
n=imhist(img); % Compute the histogram
N=sum(n); % sum the values of all the histogram values
max=0; %initialize maximum to zero
%%================================================================================================
for i=1:256
P(i)=n(i)/N; %Computing the probability of each intensity level
end
%%================================================================================================
for T=2:255 % step through all thresholds from 2 to 255
w0=sum(P(1:T)); % Probability of class 1 (separated by threshold)
w1=sum(P(T+1:256)); %probability of class2 (separated by threshold)
u0=dot([0:T-1],P(1:T))/w0; % class mean u0
u1=dot([T:255],P(T+1:256))/w1; % class mean u1
sigma=w0*w1*((u1-u0)^2); % compute sigma i.e variance(between class)
if sigma>max % compare sigma with maximum
max=sigma; % update the value of max i.e max=sigma
threshold=T-1; % desired threshold corresponds to maximum variance of between class
end
end
%%====================================================================================================
bw=im2bw(img,threshold/255); % Convert to Binary Image
figure ,imshow(bw); % Display the Binary Image
%%====================================================================================================
end
0 Comments
Answers (1)
yanqi liu
on 21 Mar 2022
I = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/933929/image.jpeg');
I = imcrop(I, [138 47 1061 799]);
bw = im2bw(I);
bw = bwareafilt(imfill(bw, 'holes'), 1);
figure; imshow(bw);
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!