Gradient magnitude gives us how steep the change in intensity across pixels are. So the higher the gradient magnitude, the more likely it lies on a real-life border, such as delineating the outline of an object. So if we identify regions with large such changes and filter out ones based on a threshold, they are more likely to be on edges of some shape. We'll try using this technique on the cameraman image.
Let's try to compute the gradient magnitude image. First, we have to get the partial derivative in x and y. This can be done by convolving with the finite difference operators, which are D_x = [1, -1] and D_y [[1], [-1]] for the x and y directions respectively.
|
|
As we expected, the x-gradient reveals all vertical edges. This makes sense because if we detect change across the x-axis direction, it means the dividing line is a vertical line. Vice versa for the y-gradient.
Now to combine these two into a gradient magnitude. We can compute as the norm of each pixel in the x- and y-gradients. This is sqrt(x^2 + y^2) (arithmetic done entry-wise). We can binarize this by setting a threshold (i.e. alpha = 0.10), where all pixels above the value are rounded up to 1, or else are rounded down to 0.
|
|
Now it's time to get rid of the noise with the DoG filter. First, we can try convolving the image with the Gaussian kernel and computing the gradient magnitude. We can convolve the finite difference filter with the Gaussian and see we get the same result.
|
|
These are a lot less noisy. You can also see that the outline of the cameraman is a lot more pronounced and recognizable, not to mention that it appears to be a set of continuous lines, instead of choppy discrete lines. It also captures all edges that have "weaker" edges, such as the cameraman's back leg. We can confirm that the results are identical and that convolution is associative. Namely: (img * gaussian) * (D_x) = (img) * (gaussian * D_x)
Let's try sharpening an image. We'll put it through a low pass filter and subtract the result to strengthen high frequencies. For example:
|
|
It works on the Taj Mahal, but as a sanity check let's try a photo of Berkeley. (Ignore the small grey bubble at the bottom left. This is a screenshot of some image that I didn't want to pay for.)
|
|
This looks good. Let's try it on the golden gate bridge. We'll first blur the original image and try to sharpen it.
|
|
|
Now note that the re-sharpened image isn't identical to the original image. From the blurring, we actually lost information. We cannot retrieve the original information by emphasizing high frequencies. One detail is that the "lines" that are almost overlapping but clearly distinguishable in the original image lose significant detail in the re-sharpened image. Particularly on the right side, they seem to be completely overlapping at times. If you zoom in far enough, you will also see that the details of the passing cars are far less noticeable. Finally, the movement of the water beneath seem unnatural, suggesting a rather rough texture rather than a smooth one.
|
|
|
Here's the log magnitude of the FFT.
|
|
|
Let's try this on a couple more images.
|
|
|
|
|
|
However, combining President Biden and VP Harris doesn't work as well:
|
|
|
Their facial features overlap a lot, so the high frequency and the low frequency in the image blend together. But if you move far away enough, you can catch a glimpse of VP Harris.
Here's a recreation of the Laplacian stacks. The layers represent the 0th, 2nd, and 4th levels in this order.
Now finally, the Orapple.
But I think we can make something far more interesting. We know there are bicolor cats. But can we create one with computers?
We can indeed. The alignment is a little off due to the nature of the tilt on the black cat's head, but this looks good.
We can actually generalize this masking process. We can have any 2D array of 1s and 0s, corresponding to portions of the images that we want to mask out.
Now let's make a mask for the face.
The final result:
This beautiful scene from the movie Titanic has been enhanced by former president George W. Bush. Technology is great.