Showing posts with label edges. Show all posts
Showing posts with label edges. Show all posts

Thursday, December 1, 2011

Boo, the sharpest dog in the world !

I do not like Matlab's unsharp filter. Aside from the fact that the name is stupid, this filter overshoots the edges.

Here is today's fun assignment: design a filter using linear combination of derivatives of the image that does not overshoot.

Here is the output where I sharpen Boo, without overshooting him.

Boo, the sharpest dog in the world




Compare the above image with the original below. There is a subtle change in the image, which I personally believe makes it pleasing to look at.

Original Boo




Monday, November 28, 2011

The edge of darkness is upon us!

I assume that now we know how to find edges in a given image u. It's simple, just take the derivative!

Today, I want to find the smooth regions. Well, it's kind of silly, as now that we have edges, the rest of the image is kind of edgeless. Right... but I would like to assign a number from 0 to 1 to it ... where 1 indicates a flat region and zero indicates an edge, in other words I want to make the edges appear dark.

There are many ways to do it. One of the ways to do it is to look at the function $$g(x, y):=\frac{1}{\sqrt{1+|K*\nabla u(x, y)|}}.$$Where, K is your favourite smoothing kernel. (I will leave it to you to code this as an exercise. It is a four liner.)

Staring at this function is a refreshing activity, something that I love to do in my spare time.


Boo in the edge of darkness

Lenna in the edge of darkness

Barbara in the edge of darkness






Tuesday, November 22, 2011

Our eyes are most sensitive to gradients

Our eyes, and our sense in general, are most sensitive to gradients. May be that is the reason when we are asked to draw a picture from memory, we tend to draw the sketch involving primarily the edges. It is also the reason why it is difficult for a person from California to get adjusted to the winter in Canada.

Anyway we are ready to talk about gradient (u_x, u_y) of an image u. We already have the code Dfx_plus for computing the partial derivative in the x-direction.


Image gradient 

Now let's compute the gradient and store it in matrices Gx and Gy. Here is the trivial code to do that.

%==========================================================================

function [Gx, Gy]=gradient_plus(u)

% Author: Prashant Athavale
% Date: 1122/2011
% Please acknowledge my name if you use this code, thank you.

% This function computes the gradient f the image u.
% It uses forward approximation of the derivatives and thus the name
% gradient_plus

%==========================================================================

if strcmp(class(u),'uint8')
    u=double(u);
end
Gx=Dfx_plus(u);
Gy=Dfy_plus(u);
end

%==========================================================================

Magnitude of the image gradient


Let us now write another code for computing the magnitude of the gradient.

%==========================================================================

function Gm=grad_plus_magnitude(u)

% Author: Prashant Athavale
% Date: 11/22/2011
% Please acknowledge my name if you use this code, thank you.

% This function takes the image stored in u and gives the magnitude of the
% gradient

%==========================================================================

if strcmp(class(u),'uint8')
    u=double(u);
end
[Gx, Gy]=gradient_plus(u);
Gm=sqrt(Gx.*Gx+Gy.*Gy);
end

%==========================================================================


Remark 1: Pointwise operation

Note in the above code you see the line Gx.*Gx. Here the .* gives point wise multiplication of the entries in the matrix Gx. This is useful as one more time we could avoid a for loop, making things faster.

Let's implement the following in the command window.

>> u=imread('barbara.png');
>> Gm=grad_plus_magnitude(u);
>> figure; imshow(Gm/255+0.5);
>> edges=(Gm>15);
>> figure; imshow(edges);

Remark 2: Logical assignments

Note the line edges=(Gm>15). Here is another instance we avoided a for loop. What it does is, it creates a matrix edges of class logical. The value edges(i, j)=1 if Gm(i, j) > 15. Write a code using a nested for loop to do this and see which one is faster.


Results

Following are the images I got for Gm and edges as a result of the above code. This is how a gradient looks like. I don't know about you, but I was very happy when I saw a gradient of an image for the first time.

Magnitude of the gradient in the image Barbara

Edges, defined as the points where gradient is more than 15

Partial derivatives with backward difference scheme


The partial derivatives with backward difference scheme are given by

u_x:=(u(x, y)-u(x-h, y))/h  and u_y:=(u(x, y)-u(x, y-h))/h .

I recommend that you code these as a practice.