There is a curve with say 3000 data points. I need to plot it first and divide it into two sections and fit straight lines separately. Then get the intersection of those straight lines and get the co-ordinate points.
MATLAB: How to divide a curve into two sections and fit straight lines for two sections in a curve separately and then find an intersection point of the lines. Kindly help
curve fittinggraphinterpolationpiecewise linear fitregression
Related Solutions
You need to draw a line across the bottom to close off the arm before you fill it.
% Make tempoprary line.
binaryImage(end, :) = true;% arm is sealed off. Now we can fill.
binaryImage = imfill(binaryImage, 'holes');% Erase temporary line.
binaryImage(end,:) = false;
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 = 20;% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');if ~hasIPT % User does not have the toolbox installed.
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; endend%===============================================================================
% Read in a standard MATLAB gray scale demo image.
folder = pwd;baseFileName = 'oneline.png';% 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; endendgrayImage = imread(fullFileName);% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);if numberOfColorBands > 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 original gray scale 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')% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);subplot(2, 2, 2);bar(grayLevels, pixelCount); % Plot it as a bar chart.
grid on;title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');xlabel('Gray Level', 'FontSize', fontSize);ylabel('Pixel Count', 'FontSize', fontSize);xlim([0 grayLevels(end)]); % Scale x axis manually.
% Get a horizontal profile
horizontalProfile = sum(grayImage, 1) / rows;% Threshold the image and create a binary image
subplot(2, 2, 3);x = 1:length(horizontalProfile);plot(x, horizontalProfile, 'b-', 'LineWidth', 2); % Plot it as a line.
grid on;title('Profile of original image', 'FontSize', fontSize, 'Interpreter', 'None');xlabel('Column Number', 'FontSize', fontSize);ylabel('Gray Level', 'FontSize', fontSize);% Threshold at 240
theThreshold = 240;inAWord = find(horizontalProfile < theThreshold)% Draw a red line over the plot
hold on;plot([x(1), x(end)], [theThreshold, theThreshold], 'r-', 'LineWidth', 2); % Plot it as a line.% Determine the left and right-most columns:
leftColumn = inAWord(1);rightColumn = inAWord(end);% Display the original gray scale image.subplot(2, 2, 4);imshow(grayImage, []);axis on;title('Image with detected words', 'FontSize', fontSize, 'Interpreter', 'None');% Put up shaded areas over words
hold on;for k = 1 : length(inAWord) col = inAWord(k); fill([col, col], [1, rows], 'y', 'FaceAlpha', 0.1, 'EdgeColor', 'y');endmessage = sprintf('The left column = %d\nThe right column = %d', leftColumn, rightColumn);uiwait(helpdlg(message));
Best Answer