Board index » cppbuilder » Sobel Edge Detection...

Sobel Edge Detection...


2004-03-04 11:41:27 PM
cppbuilder40
Dear all,
I am new to graphics programming, so I apologise in advance if
my questions seem stupid.
I have managed to write a simple edge detection routine in C++
builder but it has problems I am unsure of how to solve... Code
is as follows:
// start of code --------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
/* SOBEL filtering */
// input array
unsigned char tempBitArray[512][512];
//output array
unsigned char tempBitOut[512][512];
int r, c; // row and column counters
int pixel; // temp value of pixel
// Load the data from the canvas into input array
// after thresholding function so data is binary!
for (int i=0;i<512;i++) {
for(int j=0; j<512;j++) {
tempBitArray[i][j] = Image1->Canvas->Pixels[i][j];
}
}
delete tempBitArray;
// filter the image & store result in output array
for (r=0; r < 512; r++) {
for (c=0; c < 512; c++) {
tempBitOut[r][c] = -1*tempBitArray[r-1][c-1]
+ -2*tempBitArray[r-1][c+1]
+ -1*tempBitArray[r+1][c-1]
+ 1*tempBitArray[r+1][c+1]
+ 2*tempBitArray[r+1][c]
+ 1*tempBitArray[r+1][c-1];
}
}
for (int i=0;i<512;i++) {
for(int j=0; j<512;j++) {
Image1->Canvas->Pixels[i][j] = tempBitOut[i][j];
}
}
}
// end of code ---------------------------------------
This routine works, but does not give a "clean" edge detection.
I assume this is because it is working only in the horizontal
direction.
I tried to declare two unsigned char arrays (1 to hold the
horizontal edges, 1 to hold the vertical) and a new calculation
to work out the edges in both planes. My plan then was to add
the two arrays together to get a better edge detection. However,
I kept getting a stack overflow error when declaring four
unsigned char arrays. Can anyone advise as to why?
Also, the output pixels are red in colour. Can someone explain
why this is please.. or rather how can I make them white.
Any suggestions / advice much appreciated,
Thanks
Cmsamo
 
 

Re:Sobel Edge Detection...

I found a simple solution to my first problem of poor
performance. I forgot that I needed to look for the absolute
value of the pixel when calculating the filter. That solves
my first problem:
Quote
tempBitOut[r][c] = abs(-1*tempBitArray[r-1][c-1]
+ -2*tempBitArray[r-1][c+1]
+ -1*tempBitArray[r+1][c-1]
+ 1*tempBitArray[r+1][c+1]
+ 2*tempBitArray[r+1][c]
+ 1*tempBitArray[r+1][c-1]);
so now I just need my other stupid mistake pointed out... how
can I control the colour of the output pixel,
Thanks
cmsamo
 

Re:Sobel Edge Detection...

I have now sorted out my second problem - after a bit of digging
around in this group. I replaced my last loop:
Quote

for (int i=0;i<512;i++) {
for(int j=0; j<512;j++) {
Image1->Canvas->Pixels[i][j] = tempBitOut[i][j];
}
}

with this
for (int i=0;i<512;i++) {
for(int j=0; j<512;j++) {
if(tempBitOut[i][j] !=0)
Image1->Canvas->Pixels[i][j] = clBlack;
else
Image1->Canvas->Pixels[i][j] = clWhite;
}
}
and it works fine now...
 

{smallsort}

Re:Sobel Edge Detection...

I have two comments. (See text below.)
cmsamo wrote:
Quote
Dear all,

I am new to graphics programming, so I apologise in advance if
my questions seem stupid.

I have managed to write a simple edge detection routine in C++
builder but it has problems I am unsure of how to solve... Code
is as follows:

// start of code --------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{

/* SOBEL filtering */

// input array
unsigned char tempBitArray[512][512];

//output array
unsigned char tempBitOut[512][512];

int r, c; // row and column counters
int pixel; // temp value of pixel

// Load the data from the canvas into input array
// after thresholding function so data is binary!

for (int i=0;i<512;i++) {
for(int j=0; j<512;j++) {
tempBitArray[i][j] = Image1->Canvas->Pixels[i][j];
}
}
COMMENT #1: Make sure that 'Pixels[][]' is actually returning a
single unsigned character.
Quote
delete tempBitArray;
COMMENT #2: 'tempBitArray' was not allocated using 'new', so you
should not 'delete' it.
Quote
// filter the image & store result in output array
[snip]