9
$\begingroup$

Is it possible to identify a color (say grey in a 3 color Black, Gray and White image) and replace the color with HatchFilling?

I've tried to convert an image using ImageGraphics and then combine the result together inside of a Graphics with Hatchfill but I have had no luck. Is the result of ImageGraphics considered a Graphics expression?

labels = ClusteringComponents[img]
Image[Replace[labels, 
   ComponentMeasurements[{img, labels}, "MeanIntensity"], 2]];
MedianFilter[%, 1];
candidate1 = 
 CurvatureFlowFilter[MeanShiftFilter[%, 8, 1/16], 2]


(* Also have tried *)
ColorQuantize[img, 4, Dithering -> False];
ImageApply[Max, ColorSeparate[%]] // ImageAdjust;
smooth = CurvatureFlowFilter[MeanShiftFilter[%, 8, 1/14], 2];
MedianFilter[%, 1];
Blur[%, 2]

These are the current settings I am using to transform a picture into a smoothed out greyscaled image but trying Image

ImageGraphics[#, 3, Method -> {"LinearSeparable", 70 \[Degree]}, 
   MinColorDistance -> .1, ImageSize -> 600] &@%

and then

graphics[{%,Hatchfilling}]

Does nothing. I know I need to throw in a colordetect for the color but I do not think that is the issue.

$\endgroup$
1
  • 1
    $\begingroup$ Most likely the answer is yes. Add some code to your question and there's a better chance for an answer that's useful to you. $\endgroup$ Jan 23, 2021 at 19:00

2 Answers 2

10
$\begingroup$

Using an input example from the documentation page for ImageGraphics:

hilbert = Import["https://i.stack.imgur.com/F9aZB.png"]

enter image description here

smoothHilbert = CurvatureFlowFilter[MeanShiftFilter[hilbert, 8, 1/16], 2];

ig = ImageGraphics[smoothHilbert, 3]

enter image description here

colors = Cases[ig, {a_, _FilledCurve} :> a, All]

enter image description here

ig /. colors[[1]] -> HatchFilling[]

enter image description here

ig /. {colors[[1]] -> HatchFilling[], 
  colors[[2]] -> PatternFilling["Checkerboard", {10, 10}], 
  colors[[3]] -> HatchFilling[-45 Degree, 5, 10]} 

enter image description here

$\endgroup$
4
  • $\begingroup$ awesome thank you so much for the help! $\endgroup$
    – skyfire
    Jan 23, 2021 at 19:35
  • $\begingroup$ @skyfire, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Jan 23, 2021 at 19:37
  • 1
    $\begingroup$ colors =Union[ Cases[ig, _?ColorQ, {1, -1}]] works fine too. $\endgroup$
    – andre314
    Jan 23, 2021 at 19:39
  • $\begingroup$ @andre314, thank you. It is much cleaner and seems more general. $\endgroup$
    – kglr
    Jan 23, 2021 at 19:48
4
$\begingroup$

Here's a way to do it without converting the image into vectorized graphics.

hilbert = Import["https://i.stack.imgur.com/F9aZB.png"];
smoothHilbert = CurvatureFlowFilter[MeanShiftFilter[hilbert, 12, 1/16], 2];
quantizedHilbert = ColorQuantize[smoothHilbert, {Black, White, Gray}, Dithering -> False];

Mathematica graphics

whiteMask = ColorReplace[quantizedHilbert, Gray -> Black]

Mathematica graphics

{width, height} = ImageDimensions[whiteMask];
filling = Rasterize@Graphics[{
     HatchFilling[],
     Rectangle[{0, 0}, {width, height}]
     }, ImageSize -> {width, height}, PlotRangePadding -> None];

filling whiteMask + ColorNegate[whiteMask] quantizedHilbert

Mathematica graphics

$\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.