TransWikia.com

Replace a color in image with hatchfilling

Mathematica Asked by skyfire on June 12, 2021

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.

2 Answers

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

Correct answer by kglr on June 12, 2021

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

Answered by C. E. on June 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP