I'm trying to draw a red line on an image, which is a microscope image of a glass slice. I could already find the edge and draw a small line on a part of it, but I couldn't draw it along the whole image.
If I have a group of lines from the Hough Transform, how to make these lines more accurate, because they aren't always on the edge in some images, and then filter them to get the highest and the most horizontal one, and finally draw it along the picture?
rotI = imread('VHX_000006.jpg');
[PIC_X, PIC_Y] = size(rotI);
%% convert it to the gray scale
rotI = rgb2gray(rotI);
%Binarize grayscale the image by thresholding
BW = imbinarize(rotI);
% complement the image (objects of interest must be white)
BW = ~BW;
img = BW;
%% edge detection using canny flter to detect only the horizontal lines with the given threshold
BW = edge(img, 'canny', [0.6 0.8], 'horizontal');
%Hough Transform
[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',10000,'MinLength',70);
highestLine = [lines(1).point1; lines(1).point2];
figure, imshow(rotI), hold on
max_len = 0;
for k = 2:length(lines)
firstLine = [lines(k - 1).point1; lines(k - 1).point2];
plot(firstLine(:,1),firstLine(:,2),'LineWidth',0.0001,'Color','red');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = firstLine;
end
end