I want to read the image of a line and then to find the slope of the line.
7 views (last 30 days)
Show older comments
Navdeep Goel
on 10 Aug 2015
Commented: Image Analyst
on 10 Aug 2015
Dear Sir, Please find enclosed the image of a line. I wish to find the slope of this line by using MATLAB. I have to calculate the data point of this line by reading the image. The correct answer is slope=4. The code is attached. Please suggest me where am i wrong.
2 Comments
Cedric
on 10 Aug 2015
Do you need to fit a line to the points that you get from the image, or can you assume that it is a line, and then you just want a way to estimate a slope?
Accepted Answer
Image Analyst
on 10 Aug 2015
Try this:
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 = 14;
rgbImage = imread('abc.jpg');
% Display the image.
subplot(1, 2, 1);
imshow(rgbImage, []);
title('Original RGB JPEG 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')
grayImage = rgb2gray(rgbImage);
binaryImage = grayImage ~= 255;
% Display the image.
subplot(1, 2, 2);
imshow(binaryImage, []);
title('Binary Image showing horrible JPEG artifacts and fitted line', 'FontSize', fontSize, 'Interpreter', 'None');
% Find locations of pixels
[y, x] = find(binaryImage);
% Do the fit to a line:
coefficients = polyfit(x, y, 1)
slope = coefficients(1);
intercept = coefficients(2);
message = sprintf('The slope of the line is %f.\nThe intercept is %f',...
slope, intercept);
helpdlg(message);
% Make a fitted line
x1 = min(x)
x2 = max(x)
xFit = x1:x2;
yFit = polyval(coefficients, xFit);
hold on;
plot(xFit, yFit, 'r-', 'LineWidth', 2);
axis on;
4 Comments
Walter Roberson
on 10 Aug 2015
The only way that the slope can be positive is if your numbering starts from the bottom left corner. But for array elements, the numbering starts from the top left.
Without explaining why: the easiest fix is
coefficients = polyfit(x, -y, 1)
Image Analyst
on 10 Aug 2015
Walter is correct. Remember that the "origin" for matrices is in the upper left and Y gets larger as it goes down, while for graphs & plots in the cartesian coordinate system the origin is in the lower right and Y increases as you go up.
I just want to add that you should never use lossy JPEG images when you're doing image analysis. Just look how bad it looks. If you have used PNG format instead of JPG, you wouldn't have that problem and your results would be more accurate. The binary image would have been more like your original image. I could have made it a little bit better by comparing to some number less than 255 instead of to exactly 255 but I wanted to illustrate why you should not use JPG images so I chose a number that really makes the problem glaringly obvious.
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox 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!