Repairing images with Resynthesizer and G'MIC

Frequently an image that otherwise looks good has a small flaw. The flaw can be dust on the lens, a car that drives unexpectedly into the frame, or a traffic sign that is inconveniently located. Inconspicuous removal of areas in images is one of the most frequent issues that arise. A good result depends on knowing the right tricks, having suitable plugins, and not needing to rely so heavily on the more archaic Eraser tool.

Because these problems occur often, you have an entire palette of options for removing troublesome areas as inconspicuously and seamlessly as possible. Gimp offers a series of tools, and special plugins expand these capabilities. The G'MIC plugin collection contains filters for editing out these types of problems.

The basic solution always involves the same approach: The editor responds by trying more or less to cover the problem part of the image automatically to create the impression of a homogeneous area. The simplest way to do this is with a Clone tool. Gimp has three of these, and they all work pretty much alike.

It is a good idea to observe the procedure one time in detail, because it serves as the basis for all additional procedures I describe here. In the future, the process will change fundamentally. Light field cameras [1], for instance, can provide data sets that make it possible to replace unwanted areas completely.

Problems When Cloning

Figure 1 shows the problems that come with cloning. To the left is the original image. To the right is the result after cloning. You should first select a source area by clicking while pressing the Ctrl key; then, use the Clone tool like a standard paint tool and edit the selected area.

Figure 1: Cloning an area of an image to cover a different area of the same image is not a good solution for all situations.

Although a very soft brush point was used in the example, the result does not look realistic for several reasons: The material suitable for cloning is very limited in scope, and the tool point is so large that it covers the middle of the area to be corrected.

The possibility of working with a small point does exist, but this would require a level of precision usually only possessed by experienced users. The application of color has to match the current pattern exactly. With the tool parameter Alignment: None , Gimp uses the same starting position for removing the colors and often makes the work easier.

The second problem is unfortunately more of an issue. The structure of the checkered flower is a real challenge to repair because even a small mistake can create clearly visible artifacts on the edges. The argument could be made that this is a special case that arises only infrequently and therefore does not necessarily call for a filter. However, in reality, these kinds of problems occur fairly frequently. The fur on the donkey in Figure 2 likewise has a regular structure, as do things like fields and ripples on waves in nature. In all of these motifs, a small mistake stands out like a sore thumb.

Figure 2: Small mistakes can be repaired manually in structured environments so that they are barely detectable. However this kind of repair can take a lot of work.

To edit out the barbed wire in Figure 2, you would not ordinarily use the standard Clone tool. Instead, the better choice would be the so-called Heal tool. It is very similar to the Clone tool, but it takes the value for the brightness of the environment into account when applying color.

As a rule, this gives better results with fewer visible aberrations. This rule holds as long as two conditions are fulfilled: You should choose a soft brush point that is larger than the area to be corrected, and you should move the tool with small, short motions or clicking or daubing strokes.

Removing brightness at the edges of the area that is to be corrected works better when the image contains a greater number of gradients of brightness. If you are working with RAW images, these corrections are already easier in the RAW converter.

Resynthesizer

Gimp has extensions for almost every area of application you can think of. These extensions come primarily in the form of scripts and plugins. Resynthesizer is an incredibly powerful add-on that is used in cloning. In his dissertation, the developer of Resynthesizer Paul Harrison describes it as a "texture synthesis" algorithm [2]. Additional information on this and similar procedures is found at Logarithmic.net [3].

In Ubuntu you can install it with

sudo apt install gimp-plugin-registry

Many other distributions offer the Resynthesizer plugin as a package in their repositories. Often these packages already contain scripts that make the expanded functions accessible. Alternatively, you can download the almost 70KB tarball from Resynthesizer at the Gimp Plugin Registry [4]. You unpack the compiled program, copy it either to the ~/.gimp-2.8/plug-ins/ or /usr/lib/gimp/2.0/plug-ins/ directory, and then you restart the graphics program.

Resynthesizer comes as a binary plugin and it displays some astounding properties. It lets you remove a selection (Filters | Enhance | Heal selection ) and also repair transparent areas (Filters | Enhance | Heal transparency ).

When you choose Filters | Enhance | Uncrop , the plugin enlarges images and fills in the transparent edges. Additionally, you can create larger textures out of small pieces. With Filters | Map | Style you can transfer the style applied in one image to another. Filters | Enhance | Enlarge & sharpen makes it possible to enlarge and sharpen an image simultaneously.

Figure 3 shows the plugin options, which you can bring up by selecting Filters | Map | Resynthesize . It provides access to all functions and is therefore complex.

Figure 3: The Resynthesizer interface offers every function; as a result, it ends up being complex.

First Steps

Once installed, the tool appears in Filters | Enhance | Heal selection , as well as under Filters | Map | Resynthesize . The tool is very easy to access thanks to multiple menu entries.

To remove areas from an image, make a selection and then call Heal selection . Depending on the distribution and installation, you see one of the two dialogs in Figure 4. The parameters are largely self-explanatory. If you see the second version of the dialog, you can experiment with Filling order , which refers to the order of filling, and Sample from , which you use to define the origin of the material.

Figure 4: Heal selection has two versions of the dialog with different parameters. Both dialogs are found in the Enhance menu.

If you are not getting the results you want, you can do two things: either recall the function with Ctrl+F and check to see what has changed or change the selection. Sometimes it helps to move the selection somewhat or modify its size to achieve better results.

The Heal selection filter can also restore corners of images that are often be lost when "stitching" a panorama together. It occurs because information is missing in the relevant areas. Figure 5 illustrates the problem. The panorama does not have individual images that can provide information about the corners. Even so, Resynthesizer can compute these relatively well.

Figure 5: The Resynthesizer filter makes it possible to create parts of images that originally had no information.

G'MIC Inpaint

You can also use G'MIC plugins [5] to tackle problems associated with the removal of image areas. However, the corresponding filters are still not well documented.

Because G'MIC is pre-compiled, you can install it in a jiffy. The software comes in two versions: a standalone program that runs on the command line, even without Gimp, and a Gimp plugin, which you save to the plugin directory, as you did the Resynthesizer tool. Numerous distributions also offer suitable packages with names like gmic for the standalone program and gimp-gmic for the plugin version.

Ubuntu users can install the plugin with:

sudo apt install gimp-gmic

"Inpainting" is largely a restoration procedure that seamlessly covers selected areas with other areas [6]. In certain respects, the methods applied are similar to those of the Resysnthesizer tool. However, in practice, the use and the results are different for each tool. G'MIC currently offers no fewer than five versions of the procedure under Filters | G'MIC | Repair :

  1. Inpaint [holes] closes transparent areas.
  2. Inpaint [morphological] uses a mask to designate the area.
  3. Inpaint [patch-base]> uses small stitches for the procedure.
  4. Inpaint [multi-scale] combines procedures 2 and 3.
  5. Inpaint [transport-diffusion] , the most recent version, diffuses material in the masked region.

Each of these methods has distinct advantages and disadvantages. The example shown in Figure 6 illustrates the performance capability of Inpaint [holes] . This filter closes the gap in the checkered flower completely after a few repetitions. The result is so good that the outcome is very difficult to distinguish from the original.

Figure 6: The result achieved with Inpaint [holes] looks almost as good as the original in spite of the complex image structure.

At times, you need to get creative. In this example, the filter was applied experimentally with default settings. It did not completely cover the hole. The newly created layer was blended into the original layer via Layer | Merge Down and the layer was used again. Increased values for Tolerance and Maximal area then closed the rest of the hole.

Another method that has barely been documented but that achieves surprisingly good results (Figure 7) uses Inpaint [patch-based] . This method works directly on the image to be restored and lets you mark the areas that need editing with color (Mask color ). The filter replaces the marked areas immediately with small insert pieces from the environment that the plugin selects in such a way that the structure is maintained.

Figure 7: G'MIC's patch-based inpainting works with masks and achieves surprisingly good results with complex textures.

Conclusion

Many of the methods discussed let you remove pieces of almost any image without creating visible artifacts. However, your level of experience and significant manual work can sometimes determine outcomes. In most cases, however, you can expect very good results from simple procedures. The plugins expand the reach of Gimp with a large variety of features, making it comparable to commercial programs.