Only 1 image is being displayed. Rest images are not being displayed.
2 次查看(过去 30 天)
显示 更早的评论
I have few images which was captured under microscope. From those only 1 image I could get the binary edge image whereas for other images its displaying blank output for the same code. I have attched the image which I am able to get the output (1.jpg) and the image I couldn't get the output (10028MODIFIED.jpg).
I have to further continue with this algorithm, and I am unable to continue with this algorithm:
1. Convert the image to Binary image.
2. Detect the edges using Canny edge detection.
3. Perform hough transformation and get the straight lines.
4. Find the coordinates and angle of each line wrt x or y axis from the edge image and display them.
5. If any 2 lines have a gap in between then remove those lines and add a new line from starting point of 1st line to ending point of 2nd line.
5. Iterate the 4th step until all the lines are completed.
6. Display the image with connected lines
I have tried it using this code and if anyone can help me out for this algorithm and the image output as well.
clc;
close all;
clear all;
% Step 1: Convert the image to Binary
image = imread('1.jpg');
subplot(2,2,1);
imshow(image);
binaryImage = imbinarize(rgb2gray(image));
subplot(2,2,2);
imshow(binaryImage);
% Step 2: Detect edges using Canny edge detection
edgeImage = edge(binaryImage, 'Canny');
subplot(2,2,3);
imshow(edgeImage);
% Step 3: Perform Hough transform and get the straight lines
[H,theta,rho] = hough(edgeImage);
peaks = houghpeaks(H, 10);
lines = houghlines(edgeImage, theta, rho, peaks);
% Step 4: Find coordinates and angles of each line
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
angle = lines(k).theta;
% Display coordinates and angle
disp(['Line ', num2str(k), ' - Coordinates: (', num2str(xy(1,1)), ', ', num2str(xy(1,2)), ...
') - (', num2str(xy(2,1)), ', ', num2str(xy(2,2)), ') - Angle: ', num2str(angle)]);
end
% Step 5: Check and remove gaps between lines
numLines = length(lines);
k = 1;
while k < numLines
line1 = lines(k);
line2 = lines(k+1);
gapThreshold = 10; % Adjust the gap threshold as needed
% Calculate the distance between the end point of line1 and the start point of line2
distance = sqrt((line1.point2(1)-line2.point1(1))^2 + (line1.point2(2)-line2.point1(2))^2);
if distance <= gapThreshold
% Remove line2 and create a new line from the start point of line1 to the end point of line2
newLine = struct('point1', line1.point1, 'point2', line2.point2, 'theta', line1.theta);
lines(k+1) = newLine;
lines(k) = [];
numLines = numLines - 1;
else
k = k + 1;
end
end
% Step 6: Display the image with lines
imshow(image);
hold on;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1), xy(:,2), 'LineWidth', 2, 'Color', 'r');
end
hold off;
1 个评论
Image Analyst
2023-6-13
image is the name of a built-in variable. Do NOT use it as the name of your variable.
采纳的回答
Image Analyst
2023-6-13
Your segmentation is no good. Just look at your binary image! And don't use image as the name of your variable since it's the name of an important built-in function.
clc;
close all;
clear all;
% Step 1: Convert the image to Binary
rgbImage = imread('10028MODIFIED.jpg');
subplot(2,2,1);
imshow(rgbImage);
title('Original Image')
binaryImage = imbinarize(rgb2gray(rgbImage));
subplot(2,2,2);
imshow(binaryImage);
title('Binary Image')
% Step 2: Detect edges using Canny edge detection
edgeImage = edge(binaryImage, 'Canny');
subplot(2,2,3);
imshow(edgeImage);
title('Edge Image')
% Step 3: Perform Hough transform and get the straight lines
[H,theta,rho] = hough(edgeImage);
peaks = houghpeaks(H, 10);
lines = houghlines(edgeImage, theta, rho, peaks);
% Step 4: Find coordinates and angles of each line
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
angle = lines(k).theta;
% Display coordinates and angle
disp(['Line ', num2str(k), ' - Coordinates: (', num2str(xy(1,1)), ', ', num2str(xy(1,2)), ...
') - (', num2str(xy(2,1)), ', ', num2str(xy(2,2)), ') - Angle: ', num2str(angle)]);
end
% Step 5: Check and remove gaps between lines
numLines = length(lines);
k = 1;
while k < numLines
line1 = lines(k);
line2 = lines(k+1);
gapThreshold = 10; % Adjust the gap threshold as needed
% Calculate the distance between the end point of line1 and the start point of line2
distance = sqrt((line1.point2(1)-line2.point1(1))^2 + (line1.point2(2)-line2.point1(2))^2);
if distance <= gapThreshold
% Remove line2 and create a new line from the start point of line1 to the end point of line2
newLine = struct('point1', line1.point1, 'point2', line2.point2, 'theta', line1.theta);
lines(k+1) = newLine;
lines(k) = [];
numLines = numLines - 1;
else
k = k + 1;
end
end
% Step 6: Display the image with lines
subplot(2,2,4);
imshow(rgbImage);
hold on;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1), xy(:,2), 'r.-', 'LineWidth', 2, 'MarkerSize', 20);
end
caption = sprintf('Image with %d lines', length(lines));
title(caption)
hold off;
2 个评论
Image Analyst
2023-6-13
Why do you think:
binaryImage = imbinarize(rgb2gray(rgbImage));
will give a good segmentation? Sometimes it does but I guess for this image it did not. Try a different segmentation. For example try the Color Thresholder on the Apps tab of the tool ribbon. Or you might try cropping off the part of the image that has the scale bar. Or tell your image capture app not to burn a scale bar into the image.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!