Matlab Pixel Measurement Question

조회 수: 10 (최근 30일)
Coulton
Coulton 2012년 8월 31일
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
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일
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개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by