How to calculate the distance between two black point?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have two black points. I need to measure maximum distance between these two points in every pics. I think the edge of upper and lower of black points would help us.But I can not find out how to explain these method in Matlab.I should have to find these distance for 1200 pics.Please help me.
1 comentario
Respuestas (2)
Akira Agata
el 15 de Oct. de 2017
How about the following script??
% Read the image and binarize
I = imread('DCB57 0346.jpg');
BW = imbinarize(rgb2gray(I));
% Remove noise
se = strel('disk',3);
BW = imclose(BW, se);
% Detect edge pixels for each area
BW2 = bwperim(~BW);
stats = regionprops(BW2,'PixelList');
% Measure distance and extract maximum
d = pdist2(stats(1).PixelList, stats(2).PixelList);
[maxDist,idx] = max(d(:));
[r,c] = ind2sub(size(d), idx);
% Show result
imshow(I)
hold on
plot(stats(1).PixelList(r,1), stats(1).PixelList(r,2), 'ro')
plot(stats(2).PixelList(c,1), stats(2).PixelList(c,2), 'ro')
The result is as follows.
>> maxDist
maxDist =
203.3839
0 comentarios
Image Analyst
el 15 de Oct. de 2017
What I would do is
- Binarize and extract the two largest blobs
- Scan across columns getting the top edge of the top blob and bottom edge of the bottom blob
- Scan across columns getting the min distance from the top edge pixel to the bottom edge pixel
- Average those minimum distances
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 25;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'DCB57 0346.jpg';
% Get the full filename, with path prepended.
folder = pwd
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
errorMessage = sprintf('This image does not exist:\n%s', fullFileName);
errordlg(errorMessage);
return;
end
%===============================================================================
% Read in a first image.
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
axis image;
caption = sprintf('Original Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, .96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Binarize the image.
threshold = 128;
binaryImage = grayImage < threshold; % Determine number from histogram.
% binaryImage = imbinarize(grayImage);
% Take only the largest 2 blobs
binaryImage = bwareafilt(binaryImage, 2);
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
axis on;
axis image;
caption = sprintf('Binary Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
% Scan the top and bottom edge
topEdge = zeros(1, columns); % Initialize
bottomEdge = rows * ones(1, columns); % Initialize
for col = 1 : columns
thisColumn = binaryImage(:, col);
% Find top edge
edgeLocation = find(thisColumn, 1, 'first');
if isempty(edgeLocation)
% No edge found.
continue; % Skip to next columns
end
if edgeLocation < rows/2
% It's in the top half, so log it.
topEdge(col) = edgeLocation;
end
% Find bottom edge
edgeLocation = find(thisColumn, 1, 'last');
if edgeLocation > rows/2
% It's in the top half, so log it.
bottomEdge(col) = edgeLocation;
end
end
% Show the edges in red
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
axis image;
caption = sprintf('Binary Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;
plot(topEdge, 'r.-', 'MarkerSize', 20);
plot(bottomEdge, 'r.-', 'MarkerSize', 20);
% Find the distance from each top pixel to the closest bottom pixel
x2 = 1:columns;
minDistances = zeros(1, columns);
for col = 1 : columns
x1 = col;
y1 = topEdge(col);
if y1 == 0
% Skip those columns not touching a blob.
continue;
end
distances = sqrt((x1 - x2) .^ 2 + (y1 - bottomEdge) .^ 2);
minDistances(col) = min(distances);
end
subplot(2, 2, 4);
area(minDistances);
grid on;
% Find the average min distance.
averageMinDistance = mean(minDistances(minDistances> 0))
xlabel('Column', 'FontSize', fontSize);
ylabel('Distance', 'FontSize', fontSize);
message = sprintf('Average Min Distance from top of top blob\nto bottom of bottom blob = %.2f', averageMinDistance);
title(message, 'FontSize', fontSize);
0 comentarios
Ver también
Categorías
Más información sobre Image Processing and Computer Vision en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!