MATLAB Answers

Lines for the region of interest.

4 views (last 30 days)

Answers (1)

Vinai Datta Thatiparthi
Vinai Datta Thatiparthi on 15 Sep 2020
Suppose, the image dimensions are [w h] and the image center is at (p,q) [with general considerations}.
"How do I plot lines in an image according to the angles..."
Using a little bit of trigonometry and MATLAB's column-major notations, the X-coordinate of the third vertex of the shape made by the coordinate axes and the blue line that makes an angle of alpha with the Y-axis would be:
The Y-coordinate would be 1.
Using these coordinates, use the line function to draw the line on the image as follows:
im = imread('img.png');
hold on;
line([x, p], [y, q]); % Draws a line from (p,q) to (x,y)
For the next line i.e. with angle beta, replace "alpha" in the aforementioned equation with "alpha + beta" angles. You'll probably have to write a code to do this process repeatedly if you want a programmatic solution to this problem.
"extract region between these lines for analysis..."
Use the Polygon functionality.
Region created by vertices (p,q), (p,1) & (x,y) [which we just calculated] can be inputs to this. Examples on this Documentation page describe how to do this non-interactively using images.roi.Polygon.
Similarly, for the line that makes an angle beta, you'd have to add an additional vertex - the edge/corner of the image i.e. [w 1].
Hope this helps!
  1 Comment
Vinai Datta Thatiparthi
Vinai Datta Thatiparthi on 18 Sep 2020
Hey Thulyo,
Have a look at this code:
close all
%% Setup
im = rgb2gray(imread('peppers.png'));
[heightIm, widthIm] = size(im); %
% Center of the image (p,q)
p = widthIm/2;
q = heightIm/2;
%% Red lines
% Horizontal Red Line
rHorzX1 = 1;
rHorzY1 = q;
rHorzX2 = widthIm;
rHorzY2 = q;
% Vertical Red Line
rVertX1 = p;
rVertY1 = 1;
rVertX2 = p;
rVertY2 = heightIm;
%% Blue Lines
alpha = 15; % In degrees
bX2 = p + q*tan(alpha*pi/180);
bY2 = 1;
beta = 30;
bX3 = p + q*tan((alpha+beta)*pi/180);
bY3 = 1;
%% Display everything
hold on;
%% Extract Region
% Region of Interest
h = images.roi.Polygon(gca,'Position',[p, q; rVertX1, rVertY1; bX2, bY2]);
% Create mask for ROI
maskRegion = createMask(h);
% Create new image
dispImg = im;
% Logical Indexing
dispImg(maskRegion == 0) = 0;
% Display only the ROI image
Hope this helps!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!