Matlab Pixel Measurement Question

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 件のコメント

Ryan
Ryan 2012 年 8 月 31 日
Do you have access to the image processing toolbox? Can you post an example image?
Coulton
Coulton 2012 年 8 月 31 日
編集済み: Walter Roberson 2012 年 8 月 31 日
Yes I have access to the image processing toolbox and I will attach an image example. The image is originally an uncompressed tif file. http://i630.photobucket.com/albums/uu30/Coulton/Image1.jpg
The line at the -40 mm x-axis and 20 mm on the y-axis is what I am trying to measure. I want to find the center and the plates orientation with the horizontal.
Image Analyst
Image Analyst 2012 年 8 月 31 日
Is it in the same place every time (so we can crop out most of the clutter)? Will you allow manual method such as imdistline() or improfile()? You need to upload another image so we can see what kind of variation in the images could be encountered. Otherwise I'll simply go from one known pixel to another known pixel with improfile and threshold the profile to find the distance, but I doubt this approach will work for other images.
Walter Roberson
Walter Roberson 2012 年 8 月 31 日
Coulton
Coulton 2012 年 9 月 1 日
It will not be in the same place each time. Essentially I will be taking a video (5000 fps) and it will be of the strip of metal (the "streak") falling in water. Therefore the location will almost never be the same. I will work on getting another image, but it will look almost the same except the streak will be in a different location.
Image Analyst
Image Analyst 2012 年 9 月 2 日
See my new code below.
Coulton
Coulton 2012 年 9 月 6 日
Image Analyst,
After looking through the code, here are some of my comments/questions:
I believe that finding all pixels > 100 should be just fine as an algorithm since all of my pictures will be very similar, just the strip will be in a different location.
Quick question: What does the allAreas line (line 74) represent and the corresponding line for bounding.box? I am not familiar with this syntax.
And lastly, the "maxDistance" is that number in number of pixels? I am asking because I am trying to get measurement in mm.
Thanks for the help!

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2012 年 8 月 31 日
編集済み: Image Analyst 2012 年 9 月 2 日

0 投票

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);

1 件のコメント

Coulton
Coulton 2012 年 9 月 3 日
Image Analyst,
This is absolutely perfect. I am still going through the code so don't think I have neglected to comment back. I am going through to try and understand everything you did. I hope you had most of this code laying around or it only took you a few minutes to make, but this saved me many hours of figuring out the image toolbox syntax. Thank you so much for the help, this code is phenomenal I really appreciate it. I will comment back once I go through the code some more. Thanks!
Coulton

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by