How to create meshes with multiple regions from 2D images?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I would like to import 2D images to Mathematica and to use them to create meshed regions which can then be used for FEM. I create the images in Adobe illustrator so can either import a vector based image or a bitmap/raster image. Importantly I need to provide different materials properties to the different regions meaning that I need to keep track of which boundary is which.



A typical image would be as follows where we have 3 different regions, and we need to track boundaries between light and medium, light and dark and dark and medium grey. To be consistent of course the boundaries need to match.



test image



  • Vector graphics can be imported and visualised, however from illustrator I get spline objects which I cannot seem to mesh correctly.


  • Alternatively I can import bitmap images and then use these together with ImageMesh however this doesn't seem to work for multiple regions? I only get one region and not multiple ones.


At the moment am stuck and would appreciate any hints about how to solve this.










share|improve this question



























    up vote
    1
    down vote

    favorite












    I would like to import 2D images to Mathematica and to use them to create meshed regions which can then be used for FEM. I create the images in Adobe illustrator so can either import a vector based image or a bitmap/raster image. Importantly I need to provide different materials properties to the different regions meaning that I need to keep track of which boundary is which.



    A typical image would be as follows where we have 3 different regions, and we need to track boundaries between light and medium, light and dark and dark and medium grey. To be consistent of course the boundaries need to match.



    test image



    • Vector graphics can be imported and visualised, however from illustrator I get spline objects which I cannot seem to mesh correctly.


    • Alternatively I can import bitmap images and then use these together with ImageMesh however this doesn't seem to work for multiple regions? I only get one region and not multiple ones.


    At the moment am stuck and would appreciate any hints about how to solve this.










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I would like to import 2D images to Mathematica and to use them to create meshed regions which can then be used for FEM. I create the images in Adobe illustrator so can either import a vector based image or a bitmap/raster image. Importantly I need to provide different materials properties to the different regions meaning that I need to keep track of which boundary is which.



      A typical image would be as follows where we have 3 different regions, and we need to track boundaries between light and medium, light and dark and dark and medium grey. To be consistent of course the boundaries need to match.



      test image



      • Vector graphics can be imported and visualised, however from illustrator I get spline objects which I cannot seem to mesh correctly.


      • Alternatively I can import bitmap images and then use these together with ImageMesh however this doesn't seem to work for multiple regions? I only get one region and not multiple ones.


      At the moment am stuck and would appreciate any hints about how to solve this.










      share|improve this question















      I would like to import 2D images to Mathematica and to use them to create meshed regions which can then be used for FEM. I create the images in Adobe illustrator so can either import a vector based image or a bitmap/raster image. Importantly I need to provide different materials properties to the different regions meaning that I need to keep track of which boundary is which.



      A typical image would be as follows where we have 3 different regions, and we need to track boundaries between light and medium, light and dark and dark and medium grey. To be consistent of course the boundaries need to match.



      test image



      • Vector graphics can be imported and visualised, however from illustrator I get spline objects which I cannot seem to mesh correctly.


      • Alternatively I can import bitmap images and then use these together with ImageMesh however this doesn't seem to work for multiple regions? I only get one region and not multiple ones.


      At the moment am stuck and would appreciate any hints about how to solve this.







      image-processing mesh finite-element-method






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago

























      asked 3 hours ago









      Dunlop

      2,02421134




      2,02421134




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          If you don't mind using ImageMesh, you can use simple image processing operations to split the bitmap into separate binary mask images for each shape:



          img = Import["https://i.stack.imgur.com/NRoOd.png"];
          components =
          ComponentMeasurements[ArrayComponents[ImageData@img, 2],
          "Mask", #Area > 10000 &];


          Explanation: ArrayComponents assigns identical labels to connected components with the same color. ComponentMeasurements can then calculate measurements for each label, and the mask measurement simply returns a binary 1/0 mask. Your image is anti-aliased, so I get a lot of small components at the edges of each shape - that's why I need the #Area>10000 & filter.



          This returns a list of labelIndex -> mask elements, which we can then turn into separate meshes:



          meshes = components /. (idx_ -> mask_) :> ImageMesh[Image[mask]]


          enter image description here




          Vector graphics can be imported and visualised, however from illustrator I get spline objects which I cannot seem to mesh correctly.




          Can you post a sample spline? This is taken more or less from the documentation and seems to work fine:



          g = Graphics[BSplineCurve[0, 0, 1, -1, 2, -1, 3, 
          0, 4, -2, 5, 0, SplineClosed -> True], Circle[2, 1]]
          BoundaryDiscretizeGraphics[g]





          share|improve this answer






















          • Do you mean from Adobe Illustrator software?
            – Jose E Calderon
            2 hours ago










          • @JoseECalderon Yes, edited now in the question.
            – Dunlop
            1 hour ago










          • @Niki Estner - I understand now Anti-Aliasing I should turn off when exporting a bitmap (forgot about that). In terms of vector images, I see that it works well now, but not when the lines overlap e.g. `g = Graphics[BSplineCurve[0, 0, 1, -1, 2, -1, 3, 0, 4, -2, 5, 0, SplineClosed -> True], Circle[2, 0]] BoundaryDiscretizeGraphics[g]'. I guess I need to separate the regions. I will have a bit more of a play and see what I can come up with. Thanks for your help!
            – Dunlop
            1 hour ago










          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "387"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185064%2fhow-to-create-meshes-with-multiple-regions-from-2d-images%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote













          If you don't mind using ImageMesh, you can use simple image processing operations to split the bitmap into separate binary mask images for each shape:



          img = Import["https://i.stack.imgur.com/NRoOd.png"];
          components =
          ComponentMeasurements[ArrayComponents[ImageData@img, 2],
          "Mask", #Area > 10000 &];


          Explanation: ArrayComponents assigns identical labels to connected components with the same color. ComponentMeasurements can then calculate measurements for each label, and the mask measurement simply returns a binary 1/0 mask. Your image is anti-aliased, so I get a lot of small components at the edges of each shape - that's why I need the #Area>10000 & filter.



          This returns a list of labelIndex -> mask elements, which we can then turn into separate meshes:



          meshes = components /. (idx_ -> mask_) :> ImageMesh[Image[mask]]


          enter image description here




          Vector graphics can be imported and visualised, however from illustrator I get spline objects which I cannot seem to mesh correctly.




          Can you post a sample spline? This is taken more or less from the documentation and seems to work fine:



          g = Graphics[BSplineCurve[0, 0, 1, -1, 2, -1, 3, 
          0, 4, -2, 5, 0, SplineClosed -> True], Circle[2, 1]]
          BoundaryDiscretizeGraphics[g]





          share|improve this answer






















          • Do you mean from Adobe Illustrator software?
            – Jose E Calderon
            2 hours ago










          • @JoseECalderon Yes, edited now in the question.
            – Dunlop
            1 hour ago










          • @Niki Estner - I understand now Anti-Aliasing I should turn off when exporting a bitmap (forgot about that). In terms of vector images, I see that it works well now, but not when the lines overlap e.g. `g = Graphics[BSplineCurve[0, 0, 1, -1, 2, -1, 3, 0, 4, -2, 5, 0, SplineClosed -> True], Circle[2, 0]] BoundaryDiscretizeGraphics[g]'. I guess I need to separate the regions. I will have a bit more of a play and see what I can come up with. Thanks for your help!
            – Dunlop
            1 hour ago














          up vote
          3
          down vote













          If you don't mind using ImageMesh, you can use simple image processing operations to split the bitmap into separate binary mask images for each shape:



          img = Import["https://i.stack.imgur.com/NRoOd.png"];
          components =
          ComponentMeasurements[ArrayComponents[ImageData@img, 2],
          "Mask", #Area > 10000 &];


          Explanation: ArrayComponents assigns identical labels to connected components with the same color. ComponentMeasurements can then calculate measurements for each label, and the mask measurement simply returns a binary 1/0 mask. Your image is anti-aliased, so I get a lot of small components at the edges of each shape - that's why I need the #Area>10000 & filter.



          This returns a list of labelIndex -> mask elements, which we can then turn into separate meshes:



          meshes = components /. (idx_ -> mask_) :> ImageMesh[Image[mask]]


          enter image description here




          Vector graphics can be imported and visualised, however from illustrator I get spline objects which I cannot seem to mesh correctly.




          Can you post a sample spline? This is taken more or less from the documentation and seems to work fine:



          g = Graphics[BSplineCurve[0, 0, 1, -1, 2, -1, 3, 
          0, 4, -2, 5, 0, SplineClosed -> True], Circle[2, 1]]
          BoundaryDiscretizeGraphics[g]





          share|improve this answer






















          • Do you mean from Adobe Illustrator software?
            – Jose E Calderon
            2 hours ago










          • @JoseECalderon Yes, edited now in the question.
            – Dunlop
            1 hour ago










          • @Niki Estner - I understand now Anti-Aliasing I should turn off when exporting a bitmap (forgot about that). In terms of vector images, I see that it works well now, but not when the lines overlap e.g. `g = Graphics[BSplineCurve[0, 0, 1, -1, 2, -1, 3, 0, 4, -2, 5, 0, SplineClosed -> True], Circle[2, 0]] BoundaryDiscretizeGraphics[g]'. I guess I need to separate the regions. I will have a bit more of a play and see what I can come up with. Thanks for your help!
            – Dunlop
            1 hour ago












          up vote
          3
          down vote










          up vote
          3
          down vote









          If you don't mind using ImageMesh, you can use simple image processing operations to split the bitmap into separate binary mask images for each shape:



          img = Import["https://i.stack.imgur.com/NRoOd.png"];
          components =
          ComponentMeasurements[ArrayComponents[ImageData@img, 2],
          "Mask", #Area > 10000 &];


          Explanation: ArrayComponents assigns identical labels to connected components with the same color. ComponentMeasurements can then calculate measurements for each label, and the mask measurement simply returns a binary 1/0 mask. Your image is anti-aliased, so I get a lot of small components at the edges of each shape - that's why I need the #Area>10000 & filter.



          This returns a list of labelIndex -> mask elements, which we can then turn into separate meshes:



          meshes = components /. (idx_ -> mask_) :> ImageMesh[Image[mask]]


          enter image description here




          Vector graphics can be imported and visualised, however from illustrator I get spline objects which I cannot seem to mesh correctly.




          Can you post a sample spline? This is taken more or less from the documentation and seems to work fine:



          g = Graphics[BSplineCurve[0, 0, 1, -1, 2, -1, 3, 
          0, 4, -2, 5, 0, SplineClosed -> True], Circle[2, 1]]
          BoundaryDiscretizeGraphics[g]





          share|improve this answer














          If you don't mind using ImageMesh, you can use simple image processing operations to split the bitmap into separate binary mask images for each shape:



          img = Import["https://i.stack.imgur.com/NRoOd.png"];
          components =
          ComponentMeasurements[ArrayComponents[ImageData@img, 2],
          "Mask", #Area > 10000 &];


          Explanation: ArrayComponents assigns identical labels to connected components with the same color. ComponentMeasurements can then calculate measurements for each label, and the mask measurement simply returns a binary 1/0 mask. Your image is anti-aliased, so I get a lot of small components at the edges of each shape - that's why I need the #Area>10000 & filter.



          This returns a list of labelIndex -> mask elements, which we can then turn into separate meshes:



          meshes = components /. (idx_ -> mask_) :> ImageMesh[Image[mask]]


          enter image description here




          Vector graphics can be imported and visualised, however from illustrator I get spline objects which I cannot seem to mesh correctly.




          Can you post a sample spline? This is taken more or less from the documentation and seems to work fine:



          g = Graphics[BSplineCurve[0, 0, 1, -1, 2, -1, 3, 
          0, 4, -2, 5, 0, SplineClosed -> True], Circle[2, 1]]
          BoundaryDiscretizeGraphics[g]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 hours ago

























          answered 3 hours ago









          Niki Estner

          30k373130




          30k373130











          • Do you mean from Adobe Illustrator software?
            – Jose E Calderon
            2 hours ago










          • @JoseECalderon Yes, edited now in the question.
            – Dunlop
            1 hour ago










          • @Niki Estner - I understand now Anti-Aliasing I should turn off when exporting a bitmap (forgot about that). In terms of vector images, I see that it works well now, but not when the lines overlap e.g. `g = Graphics[BSplineCurve[0, 0, 1, -1, 2, -1, 3, 0, 4, -2, 5, 0, SplineClosed -> True], Circle[2, 0]] BoundaryDiscretizeGraphics[g]'. I guess I need to separate the regions. I will have a bit more of a play and see what I can come up with. Thanks for your help!
            – Dunlop
            1 hour ago
















          • Do you mean from Adobe Illustrator software?
            – Jose E Calderon
            2 hours ago










          • @JoseECalderon Yes, edited now in the question.
            – Dunlop
            1 hour ago










          • @Niki Estner - I understand now Anti-Aliasing I should turn off when exporting a bitmap (forgot about that). In terms of vector images, I see that it works well now, but not when the lines overlap e.g. `g = Graphics[BSplineCurve[0, 0, 1, -1, 2, -1, 3, 0, 4, -2, 5, 0, SplineClosed -> True], Circle[2, 0]] BoundaryDiscretizeGraphics[g]'. I guess I need to separate the regions. I will have a bit more of a play and see what I can come up with. Thanks for your help!
            – Dunlop
            1 hour ago















          Do you mean from Adobe Illustrator software?
          – Jose E Calderon
          2 hours ago




          Do you mean from Adobe Illustrator software?
          – Jose E Calderon
          2 hours ago












          @JoseECalderon Yes, edited now in the question.
          – Dunlop
          1 hour ago




          @JoseECalderon Yes, edited now in the question.
          – Dunlop
          1 hour ago












          @Niki Estner - I understand now Anti-Aliasing I should turn off when exporting a bitmap (forgot about that). In terms of vector images, I see that it works well now, but not when the lines overlap e.g. `g = Graphics[BSplineCurve[0, 0, 1, -1, 2, -1, 3, 0, 4, -2, 5, 0, SplineClosed -> True], Circle[2, 0]] BoundaryDiscretizeGraphics[g]'. I guess I need to separate the regions. I will have a bit more of a play and see what I can come up with. Thanks for your help!
          – Dunlop
          1 hour ago




          @Niki Estner - I understand now Anti-Aliasing I should turn off when exporting a bitmap (forgot about that). In terms of vector images, I see that it works well now, but not when the lines overlap e.g. `g = Graphics[BSplineCurve[0, 0, 1, -1, 2, -1, 3, 0, 4, -2, 5, 0, SplineClosed -> True], Circle[2, 0]] BoundaryDiscretizeGraphics[g]'. I guess I need to separate the regions. I will have a bit more of a play and see what I can come up with. Thanks for your help!
          – Dunlop
          1 hour ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185064%2fhow-to-create-meshes-with-multiple-regions-from-2d-images%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          Confectionery