I want to read the image of a line and then to find the slope of the line.
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
Cedric
2015-8-10
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?
采纳的回答
Image Analyst
2015-8-10
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 个评论
Walter Roberson
2015-8-10
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
2015-8-10
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!