Ask Your Question

fingerprint orientation map through gradient method - opencv c++

asked 2013-03-17 22:37:38 -0500

ssvenkatesh6666 gravatar image

updated 2016-01-21 10:27:59 -0500

I am trying to develop a fingerprint system using OpenCV 2.4.3 and C++ in Visual Studio 2010. I need to form the orientation map of the input fingerprint in order to use it in Gabor filters. I have computed the gradient x and y values using Sobel filters. But I don't how to form a orientation map from this. Can anyone help me with the steps to do so. Thanks in advance.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-03-18 09:53:04 -0500

updated 2013-03-19 08:34:58 -0500

The following code is some sample code from a project I am doing in C++. It uses as input two gradient containers to define the orientation of the gradient for each point.

Mat calculateOrientations(Mat gradientX, Mat gradientY){
    // Create container element
    Mat orientation = Mat(gradientX.rows, gradientX.cols, CV_32F);

    // Calculate orientations of gradients --> in degrees
    // Loop over all matrix values and calculate the accompagnied orientation
    for(int i = 0; i < gradientX.rows; i++){
        for(int j = 0; j < gradientX.cols; j++){
            // Retrieve a single value
            float valueX =<float>(i,j);
            float valueY =<float>(i,j);
            // Calculate the corresponding single direction, done by applying the arctangens function
            float result = fastAtan2(valueX,valueY);
            // Store in orientation matrix element
  <float>(i,j) = result;

    return orientation;

Using this information, you can decide to calculate the average orientation for like a bin of 5x5 pixels easily. Hope this helps you out!

Addition 1 :

As suggested, these angles can be used to create arrows that point in the angle direction and take the absolute vector size when combining both gradients as length. An example result of those orientation map on a microscopic organism can be seen below, which is done by using matlab, but i get similar images in OpenCV, just havent created one lately.

image description

edit flag offensive delete link more


I asked a question yesterday and I think we are looking for the same thing.. From this question I made the two mat containers of gradients and applied this function on it, but I dont really understand how these thins work :S Should I display the result ? When I try it, i get some errors...

Milanista gravatar imageMilanista ( 2013-03-18 12:00:55 -0500 )edit

Ok first of all, you should basically understand what a gradient orientation is. It means the direction in which the gradient in that pixel is. Using basic giometry, one knows that the arctangens (inverse tangens) can be used to calculate the angle in which the combined gradient of X and Y points.

Link to geometry :

Also, when visualizing this, just think about what a gradient actually is. It can be displayed with an arrow, that has the pixel as starting point, and which points in the direction of the angle searched (orientation here) and as size, the actual length of the combined vector, which is algebra again.

Be aware that when you display this, you display the arrows for lets say, every 5 pixels, else your screen will be filled with arrows and show as a block.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-18 12:56:20 -0500 )edit

What must be the types of the input gradient containers in your function? If I use CV_8U what is returned from Sobel function it gives me errors. When I converted it to 32F it gives me the same result for each pixel about 44 (degrees if I understand good? )

Milanista gravatar imageMilanista ( 2013-03-19 06:00:43 -0500 )edit

Thanks for your kind reply StevenPuttemans. As per your suggestion should i apply to a 5x5 block or to every 5 pixels in the image? and can i use the input fingerprint image rather than orientation matrix from your example to get the arrows along the ridges of the fingerprint.

ssvenkatesh6666 gravatar imagessvenkatesh6666 ( 2013-03-19 07:54:11 -0500 )edit

@Milanista : Input gradient containers are of type CV_32F since this is expected by the algorithms I process later on those data. About the actual same degrees for each point ,that is not possible if your gradient elements are not constant for the complete image. Can you link me to an example input image?

@ssvenkatesh6666 : my code only generates the angle of the orientation for each pixel of your gradient image. You still have to do postprocessing to get the actual arrow displayed as i said. Indeed use 5x5 pixel blocks, for displaying, meaning you take stepsize of 5 pixels in each dimension, each time you visualize an element.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-19 08:30:44 -0500 )edit

thanks for the help!, I will try this and will return to you for further guidance :)

ssvenkatesh6666 gravatar imagessvenkatesh6666 ( 2013-03-19 09:24:14 -0500 )edit

For getting the gradients I used this tutorial: After it I converted "abs_grad_x" like this: abs_grad_x.convertTo(abs2_grad_x, CV_32F, 1./255); I used "abs2_grad_x" (and y) in your function.

Milanista gravatar imageMilanista ( 2013-03-19 11:21:01 -0500 )edit

Hello @ssvenkatesh6666. How did you get the gradients?

Milanista gravatar imageMilanista ( 2013-03-25 05:49:54 -0500 )edit

getting gradients is pretty simple, just use the sobel functionality, and define the x or y direction in which you want to calculate the result. It is also good to first process your image by applying some gaussian blur and converting it to a grayscale input. Link :

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-25 08:05:11 -0500 )edit

Thank you @StevenPuttemans. I think I'm in the finish line, just something misses. I got this picture, but it doesnt seem very good: I made it this way: 1. I applied Gaussian filter on it : GaussianBlur( src, src, Size(15,15), 0, 0, BORDER_DEFAULT ); 2. I computed gradient for x and y like this : Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT ); 3. converted it to CV_64F 4. I computed the average of grad in 3030 blocks (I have bigger picture about 12001100) 5. I found this function for getting angle and magnitude : cv::cartToPolar 6. I averaged the angle and magnitude in blocks

Milanista gravatar imageMilanista ( 2013-03-27 12:13:25 -0500 )edit

answered 2015-06-12 08:51:14 -0500

Hi, i'm a student in master degree and i work on fingerprint recognition using opencv c++ with ms vc++. i read your post and i'm interest on your code. please help me My email Thanks in advance

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2013-03-17 22:37:38 -0500

Seen: 5,280 times

Last updated: Mar 19 '13