3
$\begingroup$

How to make high resolution DiscretizeRegion from the given image?

enter image description here

ImageDimensions[img]

$\{963, 594\}$

For instance there is a small size (about $10 \times 10$) but high resolution mesh region of Germany

Show[DiscretizeRegion[
Entity["Country", "Germany"]["Polygon"] /. GeoPosition -> Identity],
Graphics[Disk[{35, 10}, 5]]]

enter image description here

I want to make small size and high resolution mesh region from the given img

Show[img // ImageMesh // DiscretizeRegion, Graphics[Disk[{40, 10}, 5]]]

enter image description here

$\endgroup$
2
  • $\begingroup$ could you provide an example image with which to work with? $\endgroup$
    – glS
    Mar 14, 2017 at 11:19
  • $\begingroup$ @glS this is original image, then use ChanVeseBinarize for mask. $\endgroup$
    – vito
    Mar 14, 2017 at 11:43

1 Answer 1

3
$\begingroup$

The option DataRange, from the relevant documentation page, does what you want:

Mind that the aspect ratio is not preserved in this way, so you have to manually use the correct values for x- and y-range to preserve the original one.

A way to automatically extract the aspect ratio of the original image is using ImageDimensions:

originalAspectRatio = #[[2]]/#[[1]] &@ImageDimensions@img;
With[{newSize = 40},
 Show[
  ImageMesh[img, 
   DataRange -> {{0, #}, {0, originalAspectRatio #}} &@newSize],
  Axes -> True, AxesOrigin -> {0, 0}
  ]
 ]

Wrapping it all up in a function:

meshDiscretizeAndResize[img_Image, newSize_Integer] := With[{
   originalAspectRatio = #[[2]]/#[[1]] &@ImageDimensions@img
   },
  DiscretizeRegion@ImageMesh[img,
    DataRange -> {{0, #}, {0, originalAspectRatio #}} &@newSize
    ]
  ]

An example showing it work as intended:

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.