Need help with code detecting slope of multiple lines. beginner to MATLAB

1 view (last 30 days)
The following code works for 1.png but not for 2.png. What are the changes to be made?
1.png 2.png
clc;
close all;
clearvars;
workspace;
format long g;
format compact;
fontSize = 16;
folder = [];
baseFileName = '1.png';
fullFileName = fullfile(folder, baseFileName);
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
hp = impixelinfo();
caption = sprintf('Original', baseFileName, rows, columns);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
hFig1.Name = 'Pattern';
[binaryImage,maskedRGBImage] = createMask(rgbImage);
subplot(2, 2, 2);
imshow(binaryImage);
binaryImage = imdilate(binaryImage, true(1, 10));
binaryImage = bwconvhull(binaryImage, 'objects');
imshow(binaryImage);
props = regionprops(binaryImage, 'Area');
allAreas = sort([props.Area])
binaryImage = bwareafilt(binaryImage, [1000, inf]);
[labeledImage, numberOfBlobs] = bwlabel(binaryImage, 8);
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
imshow(coloredLabelsImage);
hp = impixelinfo();
axis('on', 'image');
title('Final Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
props = regionprops(labeledImage, 'Centroid', 'Area', 'Orientation');
numberOfBlobs = size(props, 1);
allAreas = [props.Area]
allAngles = [props.Orientation]
xy = vertcat(props.Centroid)
hold on;
for k = 1 : numberOfBlobs
plot(xy(k, 1), xy(k, 2), 'w+', 'LineWidth', 2);
end
aveAngle = mean(allAngles);
numStripes = length(allAngles);
subplot(2, 2, 3);
bar(allAngles, 1);
caption = sprintf('%d Individual Bar Angles. The average angle is %.3f degrees', numStripes, aveAngle);
title(caption, 'FontSize', fontSize);
xlabel('Bar Number', 'FontSize', fontSize);
ylabel('Angle in Degrees', 'FontSize', fontSize);
grid on;
yline(aveAngle, 'Color', 'r', 'LineWidth', 2);
subplot(2, 2, 4);
histogram(allAngles);
xlabel('Angle in Degrees', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
grid on;
caption = sprintf('Histogram of %d Angles. The average angle is %.3f degrees', numStripes, aveAngle);
title(caption, 'FontSize', fontSize);
xline(aveAngle, 'Color', 'r', 'LineWidth', 2);
function [BW,maskedRGBImage] = createMask(RGB)
I = rgb2hsv(RGB);
channel1Min = 0.425;
channel1Max = 0.567;
channel2Min = 0.308;
channel2Max = 1.000;
channel3Min = 0.000;
channel3Max = 0.785;
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
maskedRGBImage = RGB;
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end

Accepted Answer

Image Analyst
Image Analyst on 27 Sep 2021
Edited: Image Analyst on 27 Sep 2021
When I gave you that code, I assumed all your stripes had that color. Since the background and stripes are now of different colors, you need different color segmentation values
channel1Min = 0.425;
channel1Max = 0.567;
channel2Min = 0.308;
channel2Max = 1.000;
channel3Min = 0.000;
channel3Max = 0.785;
Try the Color Thresholder App on the Apps tab of the tool ribbon.
If all your stripes are fairly high contrast, but just of different colors, you might be able to try using rgb2gray() to make a gray scale image and then using imbinarize() to segment it into stripes and background.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!