Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In a code to extract text from image (Stroke Width Transform), I am using gradients at specific pixels. In the following code: gradientX, gradientY are Mat images. G_x and G_y are gradients at (row, col) pixel. I am getting error because G_x and G_y are becoming out of bounds (1.#QNAN 1.INF) in many cases. In some cases, both become such that mag becomes INF or 1.#QNAN .

Please suggest some change in code such that this problem can be fixed. I would like to normalize them to 1 in case of INF rather than going out of bounds.


C#
float G_x = gradientX.ptr<float>(row)[col];
        float G_y = gradientY.ptr<float>(row)[col];
        // normalize gradient
        float mag =  sqrt( (G_x * G_x) + (G_y * G_y) );
        if (dark_on_light){
            G_x = -G_x/mag;
            G_y = -G_y/mag;
        } else {
            G_x = G_x/mag;
            G_y = G_y/mag;

        }
Posted
Updated 31-May-15 19:22pm
v2
Comments
Jochen Arndt 1-Jun-15 3:22am    
You can use isfinite() to check if a floating point value is neither NaN nor INF.
See http://www.cplusplus.com/reference/cmath/isfinite/.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900