How to draw a best fit line based on pixels, on the image itself?

26 views (last 30 days)
I have picked certain pixel points on an image and I wanted to draw a best fit line based on the distribution of those points right on the image itself.
Edit:
My input is the white pixels you see on the image. They all deviate to a certain degree horizontally from the center of the image, being zero. So for my data set I have values that are column numbers in reference to the center. This is done for each row.
For example, first row the white pixel may be at +3, that is 3 columns to the right of the center Second row the pixel may be at -27, 27 columns to the left of the center.
This continues all the way down to the image going row by row. I want to now draw a best fit line of these points on the image itself.

Accepted Answer

Image Analyst
Image Analyst on 16 Nov 2015
Edited: Image Analyst on 16 Nov 2015
Please post your image and annotate it to indicate which pixels the line should be drawn through and indicate if you already know their (x,y) or (row,column) locations. In the meantime, see my attached polyfit demo.
And explain what "the distribution" means to you. Is it the histogram (probability density function), or is it the spatial arrangement/layout of where the pixels are?
  5 Comments
Image Analyst
Image Analyst on 16 Nov 2015
Well somehow you got the x,y values of the white dots. Perhaps x is just the row number and y is the intensity, like, say, from column 214:
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;
%===============================================================================
% Read in a standard MATLAB gray scale demo image.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
baseFileName = 'cameraman.tif';
% 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.
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
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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')
% Put a red line down through column 42.
lineNumber = 214; % Whatever...
line([lineNumber, lineNumber], [1, columns], 'Color', 'r', 'LineWidth', 2);
% Plot the line all by itself
subplot(2, 2, 2);
y = double(grayImage(:, lineNumber)); % Need to cast to double to use polyfit().
x = [1 : size(grayImage, 1)]';
plot(x, y, 'b-');
grid on;
% Now plot the line over the image.
% Display the image.
subplot(2, 2, 3);
imshow(grayImage, []);
axis on;
title('Image with plot', 'FontSize', fontSize, 'Interpreter', 'None');
hold on;
plot(y, x, 'y-', 'LineWidth', 2);
% Do the regression with polyfit
linearCoefficients = polyfit(x, y, 1)
% The x coefficient, slope, is coefficients(1).
% The constant, the intercept, is coefficients(2).
% Make fit. It does NOT need to have the same
% number of elements as your training set,
% or the same range, though it could if you want.
% Make 15 fitted samples going from the top to the bottom of the image.
xFit = linspace(1, rows, 15);
% Get the estimated values with polyval()
yFit = polyval(linearCoefficients, xFit);
% Plot the fit over the plot
subplot(2, 2, 2);
hold on;
plot(xFit, yFit, 'g.-', 'MarkerSize', 15, 'LineWidth', 1);
legend('Training Set', 'Fit', 'Location', 'Northeast');
% Plot the fit over the image
subplot(2, 2, 3);
hold on;
plot(yFit, xFit, 'g.-', 'MarkerSize', 15, 'LineWidth', 1);
Hassan Aleem
Hassan Aleem on 16 Nov 2015
thanks so much for your help, I appreciate the picture example!

Sign in to comment.

More Answers (3)

Walter Roberson
Walter Roberson on 16 Nov 2015
  5 Comments
Walter Roberson
Walter Roberson on 16 Nov 2015
fit_coeff = polyfit(list_of_rows, list_of_columns, 1);
fit_columns = polyval(fit_coeff, list_of_rows);
plot(list_of_rows, fit_columns)

Sign in to comment.


ACHAMMA JACOB
ACHAMMA JACOB on 7 Feb 2019
lpd1.JPG

ACHAMMA JACOB
ACHAMMA JACOB on 7 Feb 2019
I have to apply RANSAC algorithm for the above image of car vehicle plate. how could it be possible? the line should pass through the character candidates.
  1 Comment
Image Analyst
Image Analyst on 7 Feb 2019
These are not Answers to ACHAMMA's 3 year old question. You should start your own question.

Sign in to comment.

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!