0

enter image description hereI'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?

enter image description here

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
masinnlos
  • 117
  • 8
  • 2
    Could you please [edit] your question to contain an image displaying the problem? Also please edit the code to be **minimal** and **reproducible**, i.e. remove any unnecessary comments and make sure that we can run the code (thus provide us the image, or use a build-in one from MATLAB). – Adriaan Apr 14 '20 at 13:28
  • Your question is a bit unclear. Are you aware, that the Hough transformation allows you to find lines in images (in fact, any mathematical function...), but not contours? The difference is that the line has no start and end-points. – max Apr 14 '20 at 13:51
  • please strip your code to the essential. There is no reason why we would need your uncommented code – max Apr 14 '20 at 13:52
  • 2
    Please include the image `VHX_000006.jpg` so that we can reproduce your result. The first issue I see just looking at your code is that you binarize the image first, then apply Canny edge detector. Canny is meant to be applied to a gray-scale image, not a binary image. The premature binarization is likely destroying your results. – Cris Luengo Apr 14 '20 at 14:16
  • No actually I didn't know that much about Hough Transform, but I thought I could use these given red lines(but before the should be more accurate). I'm not sure if my idea is very effecient. What i want is to get the lines and choose the highest red points, draw a line from the and then delete the others and keep the highest line which should be on exactly on the edge. – masinnlos Apr 14 '20 at 14:19
  • Yes I tried before just to convert the image into black and white but with binarizing it I got better results that's why I used it. – masinnlos Apr 14 '20 at 14:24
  • I added the original image – masinnlos Apr 14 '20 at 15:50

0 Answers0