13
$\begingroup$

I have a black-white image, but it is rather blurry so I was thinking of converting it into vector graphics so I could do some coloring. This is the image:

img = Import["https://i.stack.imgur.com/cyHjC.jpg"];

Source image

I used Binarize with ImageGraphics, but the result is not so good:

img2 = Binarize[ImageApply[If[Mean@# < 0.9, #, {1, 1, 1}] &, img], 0.8]
ImageGraphics[Erosion[img2, 1], 2, Method -> {"LinearSeparable", 10}]

enter image description here

As you can see, the lines are pretty rough with weird curves here and there.

ImageGraphics tends to deal with much more complicated stuff, so I thought maybe it is possible to optimize this with the knowledge that this is a black and white image with only the lines required. But how?

$\endgroup$
  • $\begingroup$ Could you post the image here directly? $\endgroup$ – Carl Lange Oct 5 '18 at 11:53
  • $\begingroup$ @CarlLange edited, thanks. $\endgroup$ – t-smart Oct 5 '18 at 12:08
  • $\begingroup$ It doesn't look very blurry to me. What kind of coloring is it that you want to do? $\endgroup$ – C. E. Oct 5 '18 at 12:53
  • 1
    $\begingroup$ Hm. I usually don't suggest to use external programs but I've just tried it with Inkscape (import the image, go to "Path" -> "Trace bitmap", and use "Brightness cutoff" 0.8). It was easy as cake. $\endgroup$ – Henrik Schumacher Oct 5 '18 at 14:10
  • 2
    $\begingroup$ @VitaliyKaurov eye of agamotto, from doctor strange $\endgroup$ – t-smart Oct 6 '18 at 1:15
18
$\begingroup$

Your image is too small, so post-processing algorithms run into a lot of errors as initial information is tiny. Right form the start significantly resize image (upsample) trying to "guess" what info in the image is missing. As you are using original image

i = Import["https://i.stack.imgur.com/cyHjC.jpg"]

to do this before post-processing ImageResize will do a great job as it will have all available info. The lesson here is: do not upsample after some post-processing stages as some info is getting lost during these operations. BTW ImageResize generally upsamples nicely in my experience. Now both Binarize and ImageGraphics will have much more "room" to work and you get a very high quality vector:

ImageGraphics[Binarize[ImageResize[i, 2000], .8],2,Method -> "Exact"]

You can export it into a vector format

Export["vector.SVG", vector]

that you can see below. Now you can play further various improvement routes found in Image Restoration and other guides, but the key point is to increase the image resolution from the start.

$\endgroup$
  • 2
    $\begingroup$ Thanks a lot, that's totally what I was expecting $\endgroup$ – t-smart Oct 6 '18 at 1:17
  • 4
    $\begingroup$ You can show svgs on Stack Exchange, try to put e.g. <img src="https://svgshare.com/i/8b_.svg" /> in your post. $\endgroup$ – C. E. Oct 6 '18 at 8:13
  • $\begingroup$ @C.E. thank you, done! $\endgroup$ – Vitaliy Kaurov Oct 6 '18 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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