|
| 1 | +""" |
| 2 | +Morphological Image Processing |
| 3 | +""" |
| 4 | + |
| 5 | +from mathics.builtin.base import Builtin |
| 6 | +from mathics.builtin.image.base import Image, _SkimageBuiltin |
| 7 | +from mathics.core.convert.python import from_python |
| 8 | +from mathics.core.evaluation import Evaluation |
| 9 | +from mathics.eval.image import matrix_to_numpy, pixels_as_float, pixels_as_ubyte |
| 10 | + |
| 11 | + |
| 12 | +class _MorphologyFilter(_SkimageBuiltin, Builtin): |
| 13 | + """ |
| 14 | + Base class for many Morphological Image Processing filters. |
| 15 | + This requires scikit-mage to be installed. |
| 16 | + """ |
| 17 | + |
| 18 | + messages = { |
| 19 | + "grayscale": "Your image has been converted to grayscale as color images are not supported yet." |
| 20 | + } |
| 21 | + |
| 22 | + rules = {"%(name)s[i_Image, r_?RealNumberQ]": "%(name)s[i, BoxMatrix[r]]"} |
| 23 | + |
| 24 | + def eval(self, image, k, evaluation: Evaluation): |
| 25 | + "%(name)s[image_Image, k_?MatrixQ]" |
| 26 | + if image.color_space != "Grayscale": |
| 27 | + image = image.grayscale() |
| 28 | + evaluation.message(self.get_name(), "grayscale") |
| 29 | + import skimage.morphology |
| 30 | + |
| 31 | + f = getattr(skimage.morphology, self.get_name(True).lower()) |
| 32 | + shape = image.pixels.shape[:2] |
| 33 | + img = f(image.pixels.reshape(shape), matrix_to_numpy(k)) |
| 34 | + return Image(img, "Grayscale") |
| 35 | + |
| 36 | + |
| 37 | +class Closing(_MorphologyFilter): |
| 38 | + """ |
| 39 | + <url> |
| 40 | + :WMA link |
| 41 | + :https://reference.wolfram.com/language/ref/Closing.html</url> |
| 42 | +
|
| 43 | + <dl> |
| 44 | + <dt>'Closing[$image$, $ker$]' |
| 45 | + <dd>Gives the morphological closing of $image$ with respect to structuring element $ker$. |
| 46 | + </dl> |
| 47 | +
|
| 48 | + >> ein = Import["ExampleData/Einstein.jpg"]; |
| 49 | + >> Closing[ein, 2.5] |
| 50 | + = -Image- |
| 51 | + """ |
| 52 | + |
| 53 | + summary_text = "morphological closing regarding a kernel" |
| 54 | + |
| 55 | + |
| 56 | +class Dilation(_MorphologyFilter): |
| 57 | + """ |
| 58 | + <url> |
| 59 | + :WMA link: |
| 60 | + https://reference.wolfram.com/language/ref/Dilation.html</url> |
| 61 | +
|
| 62 | + <dl> |
| 63 | + <dt>'Dilation[$image$, $ker$]' |
| 64 | + <dd>Gives the morphological dilation of $image$ with respect to structuring element $ker$. |
| 65 | + </dl> |
| 66 | +
|
| 67 | + >> ein = Import["ExampleData/Einstein.jpg"]; |
| 68 | + >> Dilation[ein, 2.5] |
| 69 | + = -Image- |
| 70 | + """ |
| 71 | + |
| 72 | + summary_text = "give the dilation with respect to a range-r square" |
| 73 | + |
| 74 | + |
| 75 | +class Erosion(_MorphologyFilter): |
| 76 | + """ |
| 77 | + <url> |
| 78 | + :WMA link: |
| 79 | + https://reference.wolfram.com/language/ref/Erosion.html</url> |
| 80 | +
|
| 81 | + <dl> |
| 82 | + <dt>'Erosion[$image$, $ker$]' |
| 83 | + <dd>Gives the morphological erosion of $image$ with respect to structuring element $ker$. |
| 84 | + </dl> |
| 85 | +
|
| 86 | + >> ein = Import["ExampleData/Einstein.jpg"]; |
| 87 | + >> Erosion[ein, 2.5] |
| 88 | + = -Image- |
| 89 | + """ |
| 90 | + |
| 91 | + summary_text = "give the erotion with respect to a range-r square" |
| 92 | + |
| 93 | + |
| 94 | +class MorphologicalComponents(_SkimageBuiltin): |
| 95 | + """ |
| 96 | + <url> |
| 97 | + :WMA link: |
| 98 | + https://reference.wolfram.com/language/ref/MorphologicalComponents.html</url> |
| 99 | +
|
| 100 | + <dl> |
| 101 | + <dt>'MorphologicalComponents[$image$]' |
| 102 | + <dd> Builds a 2-D array in which each pixel of $image$ is replaced \ |
| 103 | + by an integer index representing the connected foreground image \ |
| 104 | + component in which the pixel lies. |
| 105 | +
|
| 106 | + <dt>'MorphologicalComponents[$image$, $threshold$]' |
| 107 | + <dd> consider any pixel with a value above $threshold$ as the foreground. |
| 108 | + </dl> |
| 109 | + """ |
| 110 | + |
| 111 | + summary_text = "tag connected regions of similar colors" |
| 112 | + |
| 113 | + rules = {"MorphologicalComponents[i_Image]": "MorphologicalComponents[i, 0]"} |
| 114 | + |
| 115 | + def eval(self, image, t, evaluation: Evaluation): |
| 116 | + "MorphologicalComponents[image_Image, t_?RealNumberQ]" |
| 117 | + pixels = pixels_as_ubyte( |
| 118 | + pixels_as_float(image.grayscale().pixels) > t.round_to_float() |
| 119 | + ) |
| 120 | + import skimage.measure |
| 121 | + |
| 122 | + return from_python( |
| 123 | + skimage.measure.label(pixels, background=0, connectivity=2).tolist() |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | +class Opening(_MorphologyFilter): |
| 128 | + """ |
| 129 | + <url> |
| 130 | + :WMA link: |
| 131 | + https://reference.wolfram.com/language/ref/Opening.html</url> |
| 132 | +
|
| 133 | + <dl> |
| 134 | + <dt>'Opening[$image$, $ker$]' |
| 135 | + <dd>Gives the morphological opening of $image$ with respect to structuring element $ker$. |
| 136 | + </dl> |
| 137 | +
|
| 138 | + >> ein = Import["ExampleData/Einstein.jpg"]; |
| 139 | + >> Opening[ein, 2.5] |
| 140 | + = -Image- |
| 141 | + """ |
| 142 | + |
| 143 | + summary_text = "get morphological opening regarding a kernel" |
| 144 | + |
| 145 | + |
| 146 | +# TODO DistanceTransform, Thinning, Pruning, |
| 147 | +# and lots of others under "Morophological Transforms |
0 commit comments