Hello.
Today, I’ve implemented and added two new commands in the G’MIC standard library
(namely img2patches
and patches2img
), which I find very interesting.
These two commands work hand in hand. They are designed to manipulate image patches as separate entities.
With this post, I’d like to give some information about them. So, let’s start!
- First, the command
img2patches
:
When calling img2patches
, you end up with a volumetric image with size patch\_size \times patch\_size \times nb\_patchs \times nb\_channels that contains all the (possibly overlapping) patches from your input image.
Like :
$ gmic sp colorful,256 +img2patches 32
gives this:
(default arguments make the patch non-overlapped).
You get the idea: it returns a volumetric image where each slice is one image patch. A kind of “patch representation” of your initial image.
- Command
patches2img
: it simply reconstructs the image from its set of patches.
So that:
$ gmic sp colorful,256 +img2patches 32 +patches2img. 256,256
will reconstruct the original image from its patch representation:
Think of it as a (reversible) patch-transform of an image (I think it’s also called patch embedding in the ML literature).
I won’t enter in all the option details, but basically you can extract overlapping patches from images, and reconstruct images from them, even with a choice for the local reconstruction weights in case of overlapping patches (weights can be uniform or gaussian, for the moment).
When possible, the extraction / reconstruction steps even use multi-threading to speed up the whole process.
- Now the interesting part: of course, if you manipulate the patch representation before the reconstruction of the full image, you can easily get funny/interesting effects.
Let me show you some some examples:
- Mirror the patches:
$ gmic sp colorful,256 w,h:=w,h img2patches 16 mirror xy patches2img $w,$h
(here you see that no overlapping is used).
- Shuffling the patches, but with a smooth image reconstruction, using overlapping patches and gaussian weights:
$ gmic sp colorful,256 img2patches 32,16 s z sort_list +,u a z patches2img $w,$h,16,30%
I guess this could be interesting to do this on more regular textures.
- Randomly rotate the patches: Here again, with a smooth reconstruction.
$ gmic sp colorful,256 w,h:=w,h img2patches 16,6 s z foreach { rotate {90*round(u(3))} } a z patches2img $w,$h,6,5
- Normalize patch values: A very simple way to apply local normalization to lightness/colors to create vivid colors.
$ gmic sp colorful,256 w,h:=w,h +img2patches 16,5 l. { s z foreach { n 0,255 } a z patches2img $w,$h,5,1 }
- Local patch quantization:
$ gmic sp colorful,256 w,h:=w,h img2patches 32,16 s z quantize 2 n 0,255 a z patches2img $w,$h,16,10
Here, I’ve quantized each patch with 2 colors only. But these two colors are of course chosen differently for each patch. As a result, the reconstructed image (done with patch overlapping) is quite smooth and locally adapts to reproduces the colors of the original image.
Now, if I try with more quantization levels (e.g. 5 instead of 2), I get this:
I think this kind of local quantization was something @Reptorian has been interested in, a few weeks ago. With these new commands, it becomes really simple to deal with it.
That’s all for now. As you see, these two commands already allow to quickly build different image filters. If you have more ideas to test, do not hesitate to share them with us.
These commands have been pushed to the G’MIC stdlib, so they are already available after a $ gmic update
.
Happy patching!