Here's a partial answer which might lead you in the right direction. If we convert the binarized image into image data, we can establish a condition suitable for RegionPlot
:
rp = With[{idata = ImageData[binapple],
xmax = First@ImageDimensions[binapple],
ymax = Last@ImageDimensions[binapple]},
RegionPlot[
idata[[IntegerPart@(ymax - y), IntegerPart@x]] == 1, {x, 1,
xmax}, {y, 1, ymax}]]
The problem with this approach is that one should be able to create an implicit region in a similar manner, however I am getting a part specification error message when I try:
With[{idata = ImageData[binapple],
xmax = First@ImageDimensions[binapple],
ymax = Last@ImageDimensions[binapple]},
ImplicitRegion[
idata[[IntegerPart@(ymax - IntegerPart@y), IntegerPart@x]] == 1 &&
1 <= IntegerPart@x <= xmax && 1 <= IntegerPart@y <= ymax, {x, y}]]
I'll certainly update this half-answer once I find my error.
Note: One hackish way to get the region itself is to run BoundaryDiscretizeGraphics@rp[[1]]
. We can wrap this all up in a function:
binaryImageToRegion[bimg_] :=
With[{idata = ImageData[bimg], xmax = First@ImageDimensions[bimg],
ymax = Last@ImageDimensions[bimg]},
BoundaryDiscretizeGraphics@
First@RegionPlot[
idata[[IntegerPart@(ymax - y), IntegerPart@x]] == 1, {x, 1,
xmax}, {y, 1, ymax}]]
So that binaryImageToRegion[binapple]
gives:
The RegionPlot
does add quite a bit of overhead, but I don't see a significant performance issue with your test case.