MATLAB Answers

Info

This question is closed.

0

How can I find the largest Euclidean distance to the given location on an image?

Asked by engineer on 18 May 2018
Latest activity Closed by engineer on 8 Jun 2018

Hello everybody

I have implemented a couple of things in my image. After canny edge detection, I applied hough transform. Between the hough lines, I plotted the longest hough line. The position of the starting point of the longest hough line is H(529,279). I would like to find the location of the tip of the attached shape. I am thinking that the largest Euclidean distance to the given starting point of the longest hough line will give the tip of the attached shape. Please help me, if you have any other idea, your opinions are valuable to me.

  1 Comment

Sign in to comment.

Tags

No tags entered yet.

2 Answers

Answer by KALYAN ACHARJYA on 18 May 2018
Edited by KALYAN ACHARJYA on 19 May 2018
 Accepted Answer

%
clc;
clear all;
close all;
im=imread('01_dr.jpg'); %Give the image file name with proper format
input_image=im2bw(rgb2gray(im));
pos_1=[529,279]; %GIVEN
[rows colm]=size(input_image);
for i=1:rows
for j=1:colm
 if (input_image(i,j)==1)
    pos_2=[i,j];
    distance=sqrt((i-529)^2+(j-279)^2);
    end
  end
end 
d=max(distance);
fprintf('#The Max Distance is %i\n',d);
for i=1:rows
for j=1:colm
  if (input_image(i,j)==1)
     pos_2=[i,j];
     distance=sqrt((i-529)^2+(j-279)^2);
         if (distance==d)
           fprintf('#The Row Position %i\n',i);
           fprintf('#The Column Position %i\n',j);
           break;
         end
     end
   end
end 

  1 Comment

Hey, you want to find the distance between A and Tip, Am I right? And Tip Having white pixels, clarify?

Sign in to comment.


Answer by Image Analyst
on 19 May 2018

A simpler more direct way is to just background correct, threshold, and find the left most point and corresponding row number.

Try this (using attached images):

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 long g;
format compact;
fontSize = 20;
% Check that user has the specified Toolbox installed and licensed.
hasLicenseForToolbox = license('test', 'image_toolbox');   % license('test','Statistics_toolbox'), license('test','Signal_toolbox')
if ~hasLicenseForToolbox
	% User does not have the toolbox installed, or if it is, there is no available license for it.
	% For example, there is a pool of 10 licenses and all 10 have been checked out by other people already.
	ver % List what toolboxes the user has licenses available for.
	message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
	reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
	if strcmpi(reply, 'No')
		% User said No, so exit.
		return;
	end
end
%===============================================================================
% Read in gray scale demo image.
folder = pwd; % Determine where demo folder is (works with all versions).
baseFileName = 'example_pipette.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
	% The file doesn't exist -- didn't find it there in that folder.  
	% Check the entire search path (other folders) for the file by stripping off the folder.
	fullFileNameOnSearchPath = baseFileName; % No path this time.
	if ~exist(fullFileNameOnSearchPath, '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
rgbImage = imread(fullFileName);
% Display the image.
subplot(2, 3, 1);
imshow(rgbImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
hp = impixelinfo();
% 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(rgbImage);
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(rgbImage); 
	% 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 = rgbImage(:, :, 1); % Take blue channel.
else
	grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.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;
%------------------------------------------------------------------------------
% Read in background image.
[folder, baseFileNameNoExt, ext] = fileparts(fullFileName);
fullFileName = fullfile(folder, [baseFileNameNoExt, '_background.png']);
backgroundImage = imread(fullFileName);
% Display the image.
subplot(2, 2, 2);
imshow(backgroundImage, []);
title('Background Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
% Divide them
bgCorrectedImage = double(grayImage) ./ double(backgroundImage);
% Display the image.
subplot(2, 2, 3);
imshow(bgCorrectedImage, []);
title('Background Corrected Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
hp = impixelinfo();
% Threshold at 0.4 to find dark edges.
binaryImage = bgCorrectedImage < 0.9;
% Display the image.
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
hp = impixelinfo();
% Find the left most columns
[rows, columns] = find(binaryImage);
[leftColumnX, indexOfLeft] = min(columns)
% Find the corresponding line/row/Y point
leftRowY = rows(indexOfLeft)
caption = sprintf('Binary Image. The tip is at column %d, row %d.', leftColumnX, leftRowY);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Place crosshairs there
hold on;
plot(leftColumnX, leftRowY, 'r+','MarkerSize', 200);
message = sprintf('The tip is at column %d, row %d.', leftColumnX, leftRowY);
uiwait(helpdlg(message));

  4 Comments

Show 1 older comment

Why is the background so dramatically different than with the pipette in place? Also, this pipette is a very different shape - why?

I think you just need to do

% Find the corresponding line/row/Y point
[rightColumnX, indexOfRight] = max(columns)
rightRowY = rows(indexOfRight)

And of course you need to see if you can get better control over your background. It's easier to process a good image than to try to fix up a bad image. If you can't you can try to use adapthisteq() on the original image, and not use a separate background image. adapthisteq() does a decent job of flattening the background.

Sign in to comment.