MATLAB Image Data Extraction Issue
2 views (last 30 days)
Show older comments
Hello MATLAB users,
I have a question regarding MATLAB and would like to understand how to implement image recognition and extract data. Specifically, I am interested in recognizing "lines" within specific experimental data.
Due to my limited experience in image recognition, I have attempted to find code from others and tried to emulate it, but the results have not been satisfactory.
Taking this image as an example, I would like to know how to proceed in order to selectively extract only the "lines" from the data. Any relevant code or suggestions would be greatly appreciated.
Answers (1)
Aastha
on 3 Dec 2024
To detect lines in an image, you can use the "hough" functions, which performs the Hough Transform operation. Here’s how to do it:
1. Read the image and convert it to grayscale as illustrated in the MATLAB code below:
img = imread('example.jpg');
grayImg = rgb2gray(img); % Convert to grayscale
2. Perform edge detection using the "edge" function and apply the Hough Transform to detect lines by identifying peaks in the Hough space, which correspond to potential lines. This can be done in MATLAB as follows:
edges = edge(grayImg, 'Canny');
[H, theta, rho] = hough(edges);
peaks = houghpeaks(H, 5, 'threshold', ceil(0.3 * max(H(:))));
3. Extract the lines using the peaks detected using the following code snippet:
lines = houghlines(edges, theta, rho, peaks, 'FillGap', 10, 'MinLength', 20);
4. Overlay the detected lines on the original image.
imshow(img);
hold on;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1), xy(:,2), 'LineWidth', 2, 'Color', 'green');
plot(xy(1,1), xy(1,2), 'x', 'LineWidth', 2, 'Color', 'yellow');
plot(xy(2,1), xy(2,2), 'x', 'LineWidth', 2, 'Color', 'red');
end
hold off;
Refer to the following documentation links for more information on:
- hough: https://www.mathworks.com/help/images/ref/hough.html
- houghlines: https://www.mathworks.com/help/images/ref/houghlines.html
I hope this is helpful!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!