how to solve otsu code problem
Show older comments
Hi i am new to matlab and modifing the Exiting code for the Project of my friend can anybudby solve this problem i am getting the error of
Undefined function or variable 'otsu'.
Error in run1 (line 35)
seg_f = otsu(dimg);
In Below Code
function main
%% input image
clc;
close all;
vid = videoinput('winvideo',2);
preview(vid);
for i = 1:5
snapshot = getsnapshot(vid);
imagesc(snapshot);
end
imwrite(snapshot,'E:\Gear_Fault_Detection.jpg');
inp = imread('E:\Gear_Fault_Detection.jpg');
if size(inp,3) > 1
inp = rgb2gray(inp);
end
figure,
subplot(1,2,1);
imshow(inp,[]);
title('Input Image');
%% preprocessing
dimg = medianfilter(inp);
subplot(1,2,2);
imshow(dimg,[]);
title('denoised image');
%% segmentation
seg_f = otsu(dimg);
figure,
subplot(1,2,1);
imshow(seg_f);
title('Otsu image');
[imgcnt] = bwlabel(double(seg_f));
stats = regionprops(img,'all');
for i = 1:cnt
ar = stats(i).Area;
if ar<= 500
img(img==i)=0;
cnt = cnt-1;
end
end
subplot(1,2,2);
imshow(img);
title('segmented image');
%% cropping
[img, cnt] = bwlabel(double(img));
for i = 1:cnt
s = regionprops(img, 'BoundingBox');
rectangle('Position', s(1).BoundingBox,'EdgeColor','r');
end
xmin = round(s(1).BoundingBox(1,1));
xmax = round(s(1).BoundingBox(1,1)+s(1).BoundingBox(1,3));
ymin = round(s(1).BoundingBox(1,2));
ymax = round(s(1).BoundingBox(1,2)+s(1).BoundingBox(1,4));
out = img(ymin:ymax,xmin:xmax);
figure,
imshow(out,[]);
holdon
stats = regionprops(out,'all');
cen = round(stats.Centroid);
plot(cen(2),cen(1),'r*');
title('cropped image with centroid');
cnt = 0;
for i = cen(2):size(out,1);
if out(i,cen(1))==0
cnt = cnt+1;
end
end
A = out;
% determine convex hull
B = regionprops(double(A), 'ConvexImage', 'BoundingBox');
% generate mask image
C = zeros(size(A));
C(floor(B.BoundingBox(2))+(1:size(B.ConvexImage,1)),floor(B .BoundingBox(1))+(1:size(B.ConvexImage,2)))=B.ConvexImage;
C = bwmorph(C, 'erode', 2);
% generate teeth image
D = A;
D(C) = 0;
% count teeth
D = bwfill(D,'holes');
[D, cnt] = bwlabel(D);
stats = regionprops(D,'all');
for i = 1:cnt
ar = stats(i).Area;
if ar<= 7
D(D==i)=0;
cnt = cnt-1;
end
end
[imgNumTeeth] = bwlabel(D);
%disp(sprintf('Inner Diameter value: %d',cnt));
cnt1=5*cnt;
%out11=2*(size(out,2));
%out11=50+out11;
fprintf('InnerDiameter value:%d mm\n',(2*(2*cnt1)/10));
%disp(sprintf('Outer Diameter value: %d',out11));
fprintf('Outer Diameter value: %d mm\n',floor(round((3.2*size(out,2)/10))));
fprintf('No of Teeth: %d\n',NumTeeth);
fprintf('Tooth Height: %d mm\n',floor(round((3.2*size(out,2))-(2*cnt1))/100));
cnt2=((6*NumTeeth)/3.1415);
fprintf('PCD: %d mm\n',cnt2);
cnt3=(cnt2/NumTeeth);
fprintf('Module: %d mm\n',cnt3);
%if NumTeeth>= 12 &&floor(round(3.2*size(out,2)))>= 200
if NumTeeth>= 12 && 2*(2*cnt1) >= 200
s = serial('COM10');
s.baudrate=9600;
fopen(s);
fprintf(s,'K');
fclose(s);
else
s = serial('COM10');
s.baudrate=9600;
fopen(s);
fprintf(s,'F');
fclose(s);
end
end
function dimg = medianfilter(inp)
% Padarray
c=padarray(inp,[1,1],0,'both');
[R1, C1] = size(c);
%Median filter
for i=2:R1-1
for j=2:C1-1
temp = [c(i-1,j-1) c(i-1,j) c(i-1,j+1);c(i,j-1) c(i,j) c(i,j+1);c(i+1,j-1) c(i+1,j) c(i+1,j+1)];
d = temp(:);
e = sort(d);
med = e(5,1);
dimg(i-1,j-1) = med;
end
end
end
1 Comment
Saurav Desai
on 12 Nov 2020
Use function graythresh instead of otsu.Refer the link below for the same https://in.mathworks.com/help/images/ref/graythresh.html#d122e82100
Answers (0)
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!