1

I'm using MESH2D in Matlab in order to mesh ROI (Region Of Interest) from images. Now I would like to make binary masks from these triangular meshes. The outputs from [p,t] = mesh2d(node) are:

  • p = Nx2 array of nodal XY co-ordinates.
  • t = Mx3 array of triangles as indicies into P, defined with a counter-clockwise node ordering.

Example of an initial code (feel free to improve it!):

    mask= logical([0 0 0 0 0; 0 1 1 0 0; 0 1 1 1 1; 0 1 1 0 0]) %let's say this is my ROI
    figure, imagesc(mask)
    lol=regionprops(mask,'all')
    [p,t] = mesh2d(lol.ConvexHull); %it should mesh the ROI

How to make masks from this triangular mesh? Thank you in advance!

This is p:

    1,50000000000000    2
    1,50000000000000    2,50000000000000
    1,50000000000000    3
    1,50000000000000    3,50000000000000
    1,50000000000000    4
    1,93703949778653    2,56171771423604
    1,96936200278303    3,98632617574682
    2   1,50000000000000
    2   4,50000000000000
    2,00975325040940    3,53647067507122
    2,01137717786904    2,05700769275495
    2,05400996239344    3,03376821385856
    2,41193753423879    2,49774899749798
            2,45957145752038    3,46313210038859
    2,50000000000000    1,50000000000000
    2,50000000000000    4,50000000000000
    2,51246316199066    3,99053096338726
    2,56500321259084    1,97186739050944
    2,64423955240966    2,98576823004855
    3   1,50000000000000
    3   4,50000000000000
            3,00248771086621    2,47385860181019
    3,01650848812758    3,52665319517610
    3,08981230082503    3,98949609178151
    3,12731558449295    2,02370031640169
    3,36937385842331    2,99811446160210
    3,50000000000000    1,75000000000000
    3,50000000000000    4,25000000000000
    3,85193739480358    3,46578962137238
    3,85353024582881    2,53499308989903
    4   2
    4   4
    4,42246720814684    3,00037409439956
    4,50000000000000    2,25000000000000
    4,50000000000000    3,75000000000000
    4,97304775909580    2,99999314296989
    5   2,50000000000000
    5   3,50000000000000
    5,50000000000000    3

and t:

    9   5   7
    20  18  15
    1   8   11
    8   15  11
    11  15  18
    11  2   1
    6   2   11
    20  27  25
    25  18  20
    27  30  25
    17  10  14
    7   10  17
    24  21  17
    9   7   17
    29  35  32
    26  30  29
    23  19  26
    14  19  23
    26  29  23
    23  29  24
    23  17  14
    24  17  23
    6   11  13
    13  11  18
    34  30  31
    31  30  27
    3   2   6
    12  19  14
    14  10  12
    6   13  12
    12  13  19
    12  3   6
    28  21  24
    28  29  32
    24  29  28
    9   17  16
    16  17  21
    38  35  33
    35  29  33
    33  29  30
    34  37  33
    33  30  34
    19  13  22
    26  19  22
    18  25  22
    22  13  18
    22  30  26
    22  25  30
    4   7   5
    4   10  7
    4   12  10
    3   12  4
    38  33  36
    36  33  37
    39  38  36
    36  37  39        
Kevin
  • 569
  • 1
  • 5
  • 12
  • There is a function `poly2mask` to do this. – Daniel Mar 14 '15 at 18:40
  • poly2mask requires as input a ROI polygon, represented by the vectors x and y. The issue is very simple, I don't know how to make polygons from p and t (mesh2d output)... – Kevin Mar 14 '15 at 20:25
  • I am unable to run the code because `tsearch` is missing, `mesh2d` seems to work only with old matlab releases. Could you add `p` and `t` to your question? – Daniel Mar 15 '15 at 16:11
  • Since Mathworks abandoned tsearch, you have to use tsearchn and change these lines: mytesearch.m Line 68: i(j)=tsearchn([x y],t,[xi(j) yi(j)]); meshfaces.m Line 199: i = tsearchn(ph,th,p); Thank you for your help! – Kevin Mar 15 '15 at 18:40
  • Could you put `p` and `t` into your question? There is no need that I get this part of the code running on my pc, having `p` and `t` is enough to answer this question. – Daniel Mar 15 '15 at 19:19
  • I don't really understand what happened here, you got 56 triangles to mesh that very small mask made of 8 pixels, which means the average triangle has a size of 1/7 pixel. – Daniel Mar 15 '15 at 20:08

1 Answers1

1

To get the mask for the ix-th triangle, use:

poly2mask(p(t(ix,:),1),p(t(ix,:),2),width,height)

t is used to index n to get the data for one triangle.

Daniel
  • 36,610
  • 3
  • 36
  • 69
  • I'm sorry, could you please elaborate your answer? What's your variable 'n'? What should be in this case the width and the height variables? Should I do a loop? Thank you so much! – Kevin Mar 14 '15 at 22:15
  • mixed up some variable names, `n` is `p` – Daniel Mar 14 '15 at 22:33
  • I haven't figured out yet your answer... I have added an example code in my question. Could you please refer to it? – Kevin Mar 14 '15 at 22:45