From: Jose L. on
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.
From: Walter Roberson on
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 ?
From: Jose L. on
Walter Roberson <roberson(a)hushmail.com> wrote in message <i1l1ad$eu9$1(a)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.
From: Alan B on
"Jose L." <jgonzalezh1(a)mathworks.com> wrote in message <i1l4lm$36g$1(a)fred.mathworks.com>...
> Walter Roberson <roberson(a)hushmail.com> wrote in message <i1l1ad$eu9$1(a)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.
From: Sean on
"Jose L." <jgonzalezh1(a)mathworks.com> wrote in message <i1l4lm$36g$1(a)fred.mathworks.com>...
> Walter Roberson <roberson(a)hushmail.com> wrote in message <i1l1ad$eu9$1(a)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!