The first thing to do is convert the RGB image to grayscale:
gr = rgb2gray(imread('gelk.png'));
Then look at the histogram of the image intensity using imhist . Notice anything funny? Use imcontrast(imshow(gr)) to pull out the contrast adjustment tool. I found that eliminating strange material after the main peak of intensity was useful.
The task of image processing can be divided into several stages.
- Split each lane
- Define ("segment") the range in each strip
- Calculate lane location
Step 1 can be performed βmanuallyβ if the track bandwidth is guaranteed. Otherwise, the line detection proposed by Hough Transformation is probably the way out. The documentation for Image Processing Toolbox has a really good tutorial on this topic. My code repeats this lesson with the best options for your image. I spent only a few minutes with them, I am sure that you can improve the results by adjusting the parameters further.
Step 2 can be performed in several ways. The easiest way to use it is the Otsu method for creating grayscale images. This method works by defining a threshold that minimizes variance within the -class or, equivalently, maximizes the variance of the interclass. The Otsu method is present in MATLAB as a function of graythresh . If the Otsu method does not work, you can try the multi-level Otsu or several other methods for determining threshold values ββbased on a histogram .
Step 3 can be performed, as you think, by calculating the average y of the segmented pixels of the strip. This is what my code does, although I limited the validation to only the center column of each strip if there was no separation. I am concerned that the result may not be as good as calculating the strip centroid and using its location.
Here is my solution:
function [locations, lanesBW, lanes, cols] = segmentGel(gr) %%
I suggest that you carefully study not only each output value, but also the results of each step of the function, before using it for real research. To get really good results, you need to read a little about the Hough transforms, Canny edge detection and the Otsu method, and then adjust the parameters. You may also have to change the way lanes are separated; this code assumes that lines will be detected on both sides of the image.
reve_etrange
source share