Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sobel edge detection code in c++

2,308 views
Skip to first unread message

Mehwish

unread,
Dec 27, 2006, 5:26:04 PM12/27/06
to
Dear all

Iam doing my final year project on license plate regonsition and
needed some help on
sobel edge detection code in c++ to apply on a license plate.
Is there any one that can provide me with the sobel code.

Mike Smith

unread,
Dec 28, 2006, 6:25:32 AM12/28/06
to

Just look for a library that provides a 3x3 kernel operator, then feed
it the Sobel kernel. This link might be useful:

http://www.codeproject.com/bitmap/ImageConvolution.asp

--
Mike Smith

Mehwish

unread,
Dec 28, 2006, 9:41:51 AM12/28/06
to
thank you for your reply,
But i found the code hard to understand.
I need a code that will just apply sobel on a image.

4N

unread,
Dec 28, 2006, 1:23:58 PM12/28/06
to
> But i found the code hard to understand.
> I need a code that will just apply sobel on a image.

If I recall well the horizontal and vertical sobel operators are

1 |1 0 -1|
-- |2 0 -2|
8 |1 0 -1|

1 |1 2 1|
-- |0 0 0|
8 |-1 -2 -1|

eg in the first (horizontal) case if you want to compute the edge of the
current pixel you multiply the NW pixel by 1, NE by -1, W by 2, E by -2, SW
by 1 and SE by -1 then you sum up everything and divide by 8.
The same goes for the vertical operator


anthony m king

unread,
Dec 29, 2006, 4:56:39 PM12/29/06
to
The following code will output a very good approximation of the
magnitude of the sobel operator. Keep in mind that the magnitude takes
into account gradients in both the x and the y directions, so if you're
looking only for vertical or horizontal edges then this function will
not be useful. However, I believe it will be adequate for your
application since I'm guessing you want to outline license plate
characters and that should require finding edges in both directions.

If your pixels are unsigned chars, you will need to promote them to
ints before passing to the function because the operator requires more
bit space than unsigned chars can provide.

Also the output image will have dimensions (sx-2, sy-2) where sx and sy
are the number of pixels in the x and y directions of your original
image. If you require the output image to have the same size as the
original image then you will need to add the functionality to handle
padding outside the original image.

Hope this helps,
Tony

void sobel( int *a, int sx, int sy, int *out)
{
// (In) a: The image (row major) containing elements of int.
// (In) sx: size of image along x-dimension.
// (In) sy: size of image along y-dimension.
// (Out) out: The output image with dimensions (sx-2, sy-2).

int i, j, ctr = 0;
int *p1, *p2, *p3;

p1 = a; p2 = p1+sx; p3 = p2+sx;

for( j = 0; j < sy-2; ++j )
{
for( i = 0; i < sx-2; ++i )
{
out[ctr++] = ( abs((p1[0] + 2*p1[1] + p1[2]) - (p3[0] + 2*p3[1]+
p3[2])) + abs((p1[2] + 2*p2[2] + p3[2]) - (p1[0] + 2*p2[0] + p3[0])) )
/ 6;
++p1; ++p2; ++p3;
}
p1 += 2; p2 += 2; p3 +=2;

Mehwish

unread,
Dec 31, 2006, 7:25:54 AM12/31/06
to
hi,
thank you for your reply, the code is very clear and easy to
understand.
but iam just having problems with the image as the pixels are unasigned
chars.
i just need some help on how to upload the image in its and pass the
function.
thanks

jg.camp...@gmail.com

unread,
Dec 31, 2006, 10:14:42 AM12/31/06
to
Mehwish wrote:
> hi,
> thank you for your reply, the code is very clear and easy to
> understand.
> but iam just having problems with the image as the pixels are unasigned
> chars.
> i just need some help on how to upload the image in its and pass the
> function.

Stop this nonesense right now.

If you use unsigned char ([0, 255]) you will spend most of your time
(and code) handling underflow and overflows. Take for example the top
line of your Sobel kernel, what is -1*100 + 0*50 + 1*50 (in unsigned
char)? It should be a falling edge.

Or even simpler, maybe you are smoothing, what is (100 + 50 + 50 + ...)
/9?

Are you getting no supervision at all? What department are you in?
Engineering? I hope not CS?

You appear to be writing both a rudimentary image proecessing system,
*and* and complex application (number plate recognition and decoding).
If you attempt both together you are only going to learn bad habits and
suffer great disappointment.

Can you please at least develop a simple data structure to hold an
image (nRows, nCols + data buffer is the minimum) and write software to
load, display (writing numbers to a screen will do for a start) and do
simple processing on that. Much better if you can develop a proper C++
class.

Use float or double for the image data --- unless you like making life
extra difficult for yourself.

What image processing books are you reading? None, it would appear.

For a very quick fix, there is a little book by Myler and Weeks, The
Pocket Handbook of Image Processing Algorithms in C, Prentice Hall,
1993. That will give you code + a very simple Image data structure.

Best regards,

Jon C.

4N

unread,
Jan 1, 2007, 12:06:58 PM1/1/07
to
> but iam just having problems with the image as the pixels are unsigned
> chars.

erm... why don't you simply replace int* with BYTE * as parameter of the
function then you cast the pixels values to int ?


Moon2

unread,
Jan 4, 2007, 9:15:59 AM1/4/07
to
Would Laplacian edge detection not be more benefical for your project?
Moon2
"Mehwish" <0504...@brookes.ac.uk> wrote in message
news:1167258364.7...@42g2000cwt.googlegroups.com...

ELK

unread,
Feb 8, 2015, 5:48:36 PM2/8/15
to
Hi I am doing, I am working in similar project than yours a couple years ago.
I need the Sobel for C++. were you able to find that code?

Stephen Wolstenholme

unread,
Feb 9, 2015, 8:00:14 AM2/9/15
to
On Sun, 8 Feb 2015 14:48:35 -0800 (PST), ELK <rka...@gmail.com>
wrote:
I did some development on edge detection hardware when I worked in TV
a long time ago. It was to remove interference but it would also work
to enhance edge lines. Any rapid change in the luminance resulted in a
search of adjacent pixels for changes in chrominance. If both changed
it was a short line and the search continued in that direction. If the
line continued an edge was assumed.

Steve

--
Neural Network Software for Windows http://www.npsnn.com


0 new messages