I have tried several ways (see code below) but I am not getting any good results.
close all
% Read in image and the image info
I = imread('Img000000.tif');
I = I(:,:,1);
option=2; %option 1 sobel
%option 2 segmentation
if option==1
%Sobel Edge filter
BW = edge(I,'Sobel',0,'both');
imtool(BW)
[H,theta,rho] = hough(BW);
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x = theta(P(:,2));
y = rho(P(:,1));
% plot(x,y,'s','color','black');
lines = houghlines(BW,theta,rho,P,'FillGap',5,'MinLength',7);
imshow(BW)
figure, imshow(I), hold on
%Extract endpoints from individual hough lines
points = [];
for k = 1:length(lines)
points = [points; lines(k).point1; lines(k).point2];
end
% Fit overall line from individual lines from hough
x1 = 1:size(I,2);
p = polyfit(points(:,1),points(:,2),1);
y1 = polyval(p,x1);
plot(x1,y1,'r','LineWidth',3)
end
if option==2
% option 2.1
% level = graythresh(I);
%
% BW = imbinarize(I,level);
% imshowpair(I,BW,'montage')
% option 2.2
% level = multithresh(I);
% seg_I = imquantize(I,level);
% figure
% imshow(seg_I,[])
% option 2.3
% [counts,x] = imhist(I,16);
% stem(x,counts)
% % figure(1), plot(x,counts)
% T = otsuthresh(counts);
% BW = imbinarize(I,T);
% figure
% imshow(BW)
% option 2.4 - best
% T = adaptthresh(I, 0.75);
% BW = imbinarize(I,T);
% figure
% imshowpair(I, BW, 'montage')
% option 2.5
%
% J = grayconnected(I,184,30);%choose the x,y values of the seed intesity
% imshow(labeloverlay(I,J))
% option 2.6
%
% mask = zeros(size(I));
% %mask(25:end-25,25:end-25) = 1;
% mask(:,60:end-850) = 1;
% %imshow(mask)
% %title('Initial Contour Location')
% bw = activecontour(I,mask,50);
% imshow(bw)
% title('Segmented Image, 300 Iterations')
% option 2.7
%
% RGB=I;
% L = superpixels(RGB,500);
%
% figure(1)
% imshow(I)
%
% %roi = drawrectangle %provides the coord info of the rectangular draw using
% %the mouse
% f = drawrectangle(gca,'Position',[65 0 120 1020],'Color','g');
%
% foreground = createMask(f,RGB);
% b1 = drawrectangle(gca,'Position',[217.1442 8.7460 203.9004 1.0158e+03],'Color','r');
% b2 = drawrectangle(gca,'Position',[0.5000 2.7489 75.7130 1.0218e+03],'Color','r');
% background = createMask(b1,RGB) + createMask(b2,RGB);
%
% BW = lazysnapping(RGB,L,foreground,background);
% imshow(labeloverlay(RGB,BW,'Colormap',[0 1 0]))
%option 2.8
%mask(:,60:end-850) = 1
% mask = false(size(I));
% mask(30,170) = true;
%
%
% W = graydiffweight(I, mask, 'GrayDifferenceCutoff',1000);
%
% thresh = 0.009;
% [BW, D] = imsegfmm(W, mask, thresh);
% figure
% imshow(BW)
% title('Segmented Image')
%
%
%
%
%
% imshow(I)
% title('Original Image')
% seedpointR = 72;
% seedpointC = 100;
% W = graydiffweight(I, seedpointC, seedpointR,'GrayDifferenceCutoff',250);
% figure (23), imshow(log(W))
% thresh = 0.0001;
% [BW, D] = imsegfmm(W, mask, thresh);
% figure (55)
% imshow(BW)
% title('Segmented Image')
% Ifil=medfilt2(BW, [5 5]);
% imtool(Ifil)
end