Ask Your Question
0

Barcode recognition, Sobel derivatives and image transformation problem!

asked 2017-11-27 00:42:51 -0500

esconda gravatar image

updated 2017-11-27 00:47:35 -0500

First of all, I am developing a program for barcode reading.I use Sobel derivatives to obtain Gradient representation of image in x and y direction for barcode bars.It works very well in both directions(0 to 270 degree).

But unfortunately Sobel derivative representation can not recognize another angle of the image.I don't know how to explain it, but you can understand it by looking at the two different pictures I've added below.

Correctly recognized barcode bars,

image description

Can not detect from the same angle in reverse view, image description

Here is my code for sobel derivatives , I have added y direction gradient view to x direction using addweighted command.

vector<Sobel_variables> sobel_variables(1);
sobel_variables[0].alpha = 1;

sobel_variables[0].beta = 0.9;

Sobel(gray_image, sobel_variables[0].Gradx, sobel_variables[0].ddepth, 1, 0, 3);
Sobel(gray_image, sobel_variables[0].Grady, sobel_variables[0].ddepth, 0, 1, 3);

subtract(sobel_variables[0].Gradx, sobel_variables[0].Grady, sobel_variables[0].Gradient);

convertScaleAbs(sobel_variables[0].Gradient, sobel_variables[0].Gradient);

subtract(sobel_variables[0].Grady, sobel_variables[0].Gradx, sobel_variables[0].Gradient1);

convertScaleAbs(sobel_variables[0].Gradient1, sobel_variables[0].Gradient1);

addWeighted(sobel_variables[0].Gradient, sobel_variables[0].alpha, sobel_variables[0].Gradient1, sobel_variables[0].beta, 0, sobel_variables[0].Out_Image);

//sobel_variables[0].Out_Image.

imshow("Sobel_operations", sobel_variables[0].Out_Image);
return sobel_variables[0].Out_Image;

It is really strange.Any help will be apreciated to solve this problem.

edit retag flag offensive close merge delete

Comments

if you convert the (subtracted)sobel output to CV_8U, all negative values will get saturated to 0

berak gravatar imageberak ( 2017-11-27 01:05:16 -0500 )edit

Thank you berak,I really apreciate your effort for my questions.But I think "convertscaleabs" function did almost the same thing that you mentioned.Unfortunately it is not effecting the result of the image.There is still the same problem.Also I changed the x and y order of the sobel function,it is affecting the result but when I solved to x order, y order gets fail and vise versea.

esconda gravatar imageesconda ( 2017-11-27 01:47:58 -0500 )edit
1

i mean, you should NOT convert it. remove the convertScaleAbs calls.

if you REALLY need a CV_8U image in the end, use normalize() with NORM_MINMAX to do that.

maybe using sobel filters is a bad idea here, because it will make "outlines" of anything. imho, you'd want "ridge detection", not "edge detection" here

berak gravatar imageberak ( 2017-11-27 01:52:44 -0500 )edit

I removed this convertScaleAbs as you recommended.Also I solved reverse view problem for sobel derivative with using "add" function after substraction.Should I add it with pictures as an answer? or leave it as it is.

esconda gravatar imageesconda ( 2017-11-27 02:39:07 -0500 )edit

oh, if you'd write your answer, that would be great !

berak gravatar imageberak ( 2017-11-27 02:40:14 -0500 )edit

Unfortunately the system does not allow me to write it. "New user" restriction

esconda gravatar imageesconda ( 2017-11-27 03:12:21 -0500 )edit

yea, tomorrow, if you still care ..

berak gravatar imageberak ( 2017-11-27 03:17:12 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-12-11 01:37:25 -0500

esconda gravatar image

I solved it using "add" function to calculate image vertices in x and y axis.Therefore it we are able to combine each thresholded image in x and y direction.even if the area we want to distinguish on the image comes in the x direction, we can get the appropriate photo by combining it with the y direction.

Here is the code :

Mat Morphology::Sobel_operations(Mat& gray_image){//directly change vector with using only struct(future operations)

    vector<Sobel_variables> sobel_variables(1);
    //Sobel_variables sobel_variables;
    sobel_variables[0].alpha = 1;

    sobel_variables[0].beta = 0.9;

    Sobel(gray_image, sobel_variables[0].Gradx, sobel_variables[0].ddepth, 1, 0, 3);
    Sobel(gray_image, sobel_variables[0].Grady, sobel_variables[0].ddepth, 0, 1, 3);

    subtract(sobel_variables[0].Gradx, sobel_variables[0].Grady, sobel_variables[0].Gradient);

    //normalize(sobel_variables[0].Gradient, sobel_variables[0].Gradient, 1, 0, NORM_MINMAX);
    //convertScaleAbs(sobel_variables[0].Gradient, sobel_variables[0].Gradient);


    add(sobel_variables[0].Grady, sobel_variables[0].Gradx, sobel_variables[0].Gradient1);


    //convertScaleAbs(sobel_variables[0].Gradient1, sobel_variables[0].Gradient1);//It can be used but not necessary

    addWeighted(sobel_variables[0].Gradient, sobel_variables[0].alpha, sobel_variables[0].Gradient1, sobel_variables[0].beta, 0, sobel_variables[0].Out_Image);



    imshow("Sobel_operations", sobel_variables[0].Out_Image);

    //------free memory of all unnecessarry images--------------------------
    sobel_variables[0].Gradx.release();
    sobel_variables[0].Grady.release();
    gray_image.release();
    sobel_variables[0].Gradient.release();
    sobel_variables[0].Gradient1.release();

    return sobel_variables[0].Out_Image;

}
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2017-11-27 00:42:51 -0500

Seen: 387 times

Last updated: Dec 11 '17