Matlab Pixel Measurement Question
7 views (last 30 days)
Show older comments
I have images that are only black and white. The white pixels are essentially a streak that can have any orientation (it can look like \ or / or | etc). My question is can Matlab measure the overall length of each streak no matter what the orientation?
Might I add that I hope to do this with an algorithm (i.e. automatically)
7 Comments
Accepted Answer
Image Analyst
on 31 Aug 2012
Edited: Image Analyst
on 2 Sep 2012
Yes.
measurements = regionprops(binaryImage, 'area');
allLengths = [measurements.Area];
Note: the area is the number of pixels in the line or curve so that is essentially the length in pixels. There are different definitions of length that you may want to use instead. This code works for the one particular image that you uploaded.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Users\Mark\Documents\Temporary';
baseFileName = 'image1.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Crop away tick marks, colorbar, etc..
grayImage = rgb2gray(imcrop(grayImage, [1 1 725 725]));
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
% Suppress the 0 bin:
pixelCount(1) = 0;
subplot(2, 2, 2);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Get the binary image
% Use an algorithm to find the threshold.
% Since I have only one image I can't develop a robust algorithm
% So I will just pick 100, which I got from experimentation.
binaryImage = grayImage > 100;
% Display the original gray scale image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Smooth it out some and clean it up.
binaryImage = bwareaopen(binaryImage, 80)
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Cleaned Binary Image', 'FontSize', fontSize);
figure;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'all');
allAreas = [measurements.Area]
% Crop it to the bounding box.
bb = measurements.BoundingBox
% Crop the image.
binaryImage = imcrop(binaryImage, bb);
subplot(2, 2, 1);
imshow(binaryImage, []);
title('Cropped Binary Image', 'FontSize', fontSize);
% Skeletonize the image.
skeleton = bwmorph(binaryImage, 'skel', 'inf');
subplot(2, 2, 2);
imshow(skeleton, []);
title('Skeleton Image', 'FontSize', fontSize);
% Find the endpoints of the skeleton.
skeleton = bwmorph(skeleton, 'endpoints');
subplot(2, 2, 3);
imshow(skeleton, []);
title('EndPoints Image', 'FontSize', fontSize);
% Get the coordinates of all endpoints
[rows cols] = find(skeleton)
% Find the two that are farthest apart
maxRow = 0;
maxCol = 0;
maxDistance = -1;
for k1 = 1 : length(rows)
row1 = rows(k1);
col1 = cols(k1);
for k2 = 1 : length(rows)
row2 = rows(k2);
col2 = cols(k2);
distance = sqrt((row1-row2)^2 + (col1-col2)^2);
if distance > maxDistance
bestIndex1 = k1;
bestIndex2 = k2;
maxDistance = distance;
end
end
end
% Draw a line between the
line([cols(bestIndex1) cols(bestIndex2)], [rows(bestIndex1) rows(bestIndex2)], ...
'Color', 'y');
message = sprintf('The max distance is %f', maxDistance);
msgbox(message);
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!