How to find the area lying between the two region
MATLAB: Find the area between 2 plotted regions in a graph
image processing
Related Solutions
trapz(x,y)
See full demo below:
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; endend%===============================================================================
% Read in gray scale demo image.
folder = fileparts(which('eight.tif')); % Determine where demo folder is (works with all versions).
baseFileName = 'eight.tif';% 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; endendrgbImage = imread(fullFileName);% Display the image.
subplot(2, 2, 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 red channel.
else grayImage = rgbImage; % It's already gray scale.
end% Display the image.subplot(2, 2, 1);imshow(grayImage, []);title('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') subplot(2, 2, 2);histogram(grayImage);grid on;title('Histogram', 'FontSize', fontSize, 'Interpreter', 'None');% Create a binary image
binaryImage = grayImage < 200;% Fill holes and take 2 largest blobs.
binaryImage = bwareafilt(imfill(binaryImage, 'holes'), 2);% Display the image.subplot(2, 2, 3);imshow(binaryImage, []);title('Binary Image with 2 Regions', 'FontSize', fontSize, 'Interpreter', 'None');axis on;%=============================================================================================
% Now we have our two regions and we can begin!
%=============================================================================================% Get the boundaries.
boundaries = bwboundaries(binaryImage);b1 = boundaries{1};b2 = boundaries{2};% Get the x and y
x1 = b1(:, 2);y1 = b1(:, 1);x2 = b2(:, 2);y2 = b2(:, 1);% Get distances between each boundary pixel and each one in the other blob.
% We just check the outer boundaries to reduce the number of distances we need to compute.
distances = pdist2([x1,y1], [x2,y2]);% Find the min distance
minDistance = min(distances(:))[index1, index2] = find(distances == minDistance)% Get the locations
x1Min = x1(index1);y1Min = y1(index1);x2Min = x2(index2);y2Min = y2(index2);% Draw a line between them
hold on;plot([x1Min, x2Min], [y1Min, y2Min], 'r-', 'LineWidth', 2);title('Binary Image with 2 Regions with Closest Points Indicated', 'FontSize', fontSize, 'Interpreter', 'None');
Best Answer