Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Obtain a triangular portion from an image

Subject: Obtain a triangular portion from an image

From: Jose L.

Date: 14 Jul, 2010 18:35:20

Message: 1 of 5

Hi.

I hope anyone help me with the next situation:

I need obtain a triangular portion from an image using three points (x1,y1), (x2,y2) and (x3,y3).

The objective is obtain a image portion similar to "imcrop" Matlab Function, but on triangular form, not a square form.

Thanks for your attention.

Subject: Obtain a triangular portion from an image

From: Walter Roberson

Date: 14 Jul, 2010 18:53:53

Message: 2 of 5

Jose L. wrote:
> I hope anyone help me with the next situation:
>
> I need obtain a triangular portion from an image using three points
> (x1,y1), (x2,y2) and (x3,y3).
>
> The objective is obtain a image portion similar to "imcrop" Matlab
> Function, but on triangular form, not a square form.


Suppose the three points are (0,0), (1,0) and (2,1E6) . What output would you
expect, taking into account that some of the elements are going to be _very_
thin ?

Subject: Obtain a triangular portion from an image

From: Jose L.

Date: 14 Jul, 2010 19:54:30

Message: 3 of 5

Walter Roberson <roberson@hushmail.com> wrote in message <i1l1ad$eu9$1@canopus.cc.umanitoba.ca>...
> Jose L. wrote:
> > I hope anyone help me with the next situation:
> >
> > I need obtain a triangular portion from an image using three points
> > (x1,y1), (x2,y2) and (x3,y3).
> >
> > The objective is obtain a image portion similar to "imcrop" Matlab
> > Function, but on triangular form, not a square form.
>
>
> Suppose the three points are (0,0), (1,0) and (2,1E6) . What output would you
> expect, taking into account that some of the elements are going to be _very_
> thin ?

Hi Walter,

Thanks for attend the request. With respect to the expect, I need a triangular information portion from the original image from three points, which are not aligned and are within into the original image.

Thanks.

Subject: Obtain a triangular portion from an image

From: Alan B

Date: 14 Jul, 2010 20:11:05

Message: 4 of 5

"Jose L." <jgonzalezh1@mathworks.com> wrote in message <i1l4lm$36g$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <i1l1ad$eu9$1@canopus.cc.umanitoba.ca>...
> > Jose L. wrote:
> > > I hope anyone help me with the next situation:
> > >
> > > I need obtain a triangular portion from an image using three points
> > > (x1,y1), (x2,y2) and (x3,y3).
> > >
> > > The objective is obtain a image portion similar to "imcrop" Matlab
> > > Function, but on triangular form, not a square form.
> >
> >
> > Suppose the three points are (0,0), (1,0) and (2,1E6) . What output would you
> > expect, taking into account that some of the elements are going to be _very_
> > thin ?
>
> Hi Walter,
>
> Thanks for attend the request. With respect to the expect, I need a triangular information portion from the original image from three points, which are not aligned and are within into the original image.
>
> Thanks.

One easy way: Use meshgrid() to generate X and Y coordinate arrays, pass those coordinates into inpolygon(), then use reshape() to create a mask image. This could be improved by skipping points outside the bounding box of the triangle.

Walter's point is that you have not precisely defined "cropping" for a triangular region on a rectangular grid of points - should a pixel be kept if any amount of it is covered by the triangular region, or only if the whole thing is covered? Or do you want a grayscale mask, with the value corresponding to the percent of coverage? There are different ways to deal with this issue, which might determine the best approach, and you have not specified how you want to deal with it.

Subject: Obtain a triangular portion from an image

From: Sean

Date: 14 Jul, 2010 20:26:04

Message: 5 of 5

"Jose L." <jgonzalezh1@mathworks.com> wrote in message <i1l4lm$36g$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <i1l1ad$eu9$1@canopus.cc.umanitoba.ca>...
> > Jose L. wrote:
> > > I hope anyone help me with the next situation:
> > >
> > > I need obtain a triangular portion from an image using three points
> > > (x1,y1), (x2,y2) and (x3,y3).
> > >
> > > The objective is obtain a image portion similar to "imcrop" Matlab
> > > Function, but on triangular form, not a square form.
> >
> >
> > Suppose the three points are (0,0), (1,0) and (2,1E6) . What output would you
> > expect, taking into account that some of the elements are going to be _very_
> > thin ?
>
> Hi Walter,
>
> Thanks for attend the request. With respect to the expect, I need a triangular information portion from the original image from three points, which are not aligned and are within into the original image.
>
> Thanks.

One way:

Your border will have 3 lines and thus 3 slopes and 3 intercepts. Calculate each of these slopes using :
m = (y2-y)/(x2-x1) %then
b = y1 - m*x1 %(y = mx+b)

Now create a points vector for each line; let's do line1 as an example:
line1_xpoints = pt1x1:pt1x2;
line1_ypoints = m1*(pt1x1:ptx2)+b; %This is the actual line between your points, no
indices and thus needs rounded/floored/ceiled...
You can decide what to do here but I would try rounding.
line1_ypoints = rounds(line1_ypoints);

Now for the actual masking:
Imask = false(size(your_image)); %create false mask.

For simplicity's sake use a for loop to figure out what's inside the three lines and turn that part of the mask to true.
%create a matrix with for each set of points
Xmat = [line1_xpoints;line2_ypoints;line3_xpoints;%assuming they're column vectors.
Ymat = ...same thing with y
for ii = 1:size(your_image,2)
    idx = find(Xmat == ii);%sorted index to points in x/y
    ymin = min(Ymat(idx));
    ymax = max(Ymat(idx));
    %Now set that part of the max to true
    ys = ymin:ymax; %y points to fill
    xs = repmat(ii,size(ys)); %x points corresponding to those ys for sub2ind()
    Imask(sub2ind(size(your_image),ys,xs)) = true; %Note ys,xs switched for sub2ind since is operates along the vertical dimension first.
end

Good luck!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us