3x3 sobel edge detection

Joined
Jun 11, 2007
Messages
4
Reaction score
0
I'm working on sobel edge detection on an fpga using a 3x3 mask. To store the pixel values of the input image, I've decided to use an array. I wrote the code for just the array and some test code to see how it's working. The code worked, so I tried adding a "full" bit that would say when to stop loading the memory. For some reason, whenever I use the "full" bit and implement it to prevent the array from loading again once it's full, the array won't load at all and I get no output. The code is pretty simple and it seems like adding the full bit shouldn't be an issue. Does anyone have any suggestions on what I might be doing wrong?

And the code:

Library IEEE;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_misc.all;

entity sobel is
PORT ( input: IN integer:=0;
SW: IN integer:=0;
output: OUT integer:=0;
LEDR: OUT integer:=0;
LEDG: OUT std_logic_vector(8 DOWNTO 0);
CLOCK_50: IN std_logic);
end sobel;


architecture sobelizer of sobel is



constant num_cols: Natural:=640;
constant num_rows: Natural:=480;
constant edge: Natural:=0;
constant foreground: Natural:=255;
constant threshold: Natural:=68;
subtype pixel is integer;
signal full: std_logic:='0';

type memory_array is array (1 to 3, 1 to num_cols) of pixel;
type mask is array (1 to 3, 1 to 3) of pixel;
type next3 is array (1 to 3) of pixel;

begin
process

variable A : mask:=((0,0,0),(0,0,0),(0,0,0));
variable X1 : Natural:=1;
variable Y1 : Natural:=1;
variable Current_X: Integer:=1;
variable Current_Y: Integer:=1;
variable memory: memory_array;

begin

Wait Until CLOCK_50'EVENT and CLOCK_50='1';

if full='0' then

memory(X1,Y1):=input;
X1:=X1+1;
Current_X:=X1;
CUrrent_Y:=Y1

if X1=num_cols+1 then
X1:=1;
Y1:=Y1+1;
current_Y:=Y1;
end if;

if (current_X=num_cols) and (current_Y=num_rows) then
full<='1';
X1:=0;
Y1:=0;
end if;

elsif full='1' then
X1:=0;
Y1:=0;

end if;

end process;
end sobelizer;
 
Last edited:
Joined
Nov 21, 2006
Messages
31
Reaction score
0
You have not defined parameters in sensitivity lists. signal "full" should be in sensitivity list. Or you can modify your process like :

process( CLOCK_50 )

variable A : mask:=((0,0,0),(0,0,0),(0,0,0));
variable X1 : Natural:=1;
variable Y1 : Natural:=1;
variable Current_X: Integer:=1;
variable Current_Y: Integer:=1;
variable memory: memory_array;

begin

if CLOCK_50'EVENT and CLOCK_50='1' then

if full='0' then

memory(X1,Y1):=input;
X1:=X1+1;
Current_X:=X1;
CUrrent_Y:=Y1

if X1=num_cols+1 then
X1:=1;
Y1:=Y1+1;
current_Y:=Y1;
end if;

if (current_X=num_cols) and (current_Y=num_rows) then
full<='1';
X1:=0;
Y1:=0;
end if;

elsif full='1' then
X1:=0;
Y1:=0;

end if;

end if;

end process;
end sobelizer;
 
Joined
Jun 11, 2007
Messages
4
Reaction score
0
Thank you for your help! I ended up changing the code around so the "full" issue could be worked around. I now have a working edge detection algorithm with a few issues that need to be worked out. I keep getting triples of the input image on the output. I'm thinking it's because I set a lower resolution for the output because the FPGA can't hold a full 640x3 array in addition to all the other code. Either that, or I'm having a timing issue.
 
Joined
Sep 26, 2009
Messages
1
Reaction score
0
I have 1 code about 8 mask sobel as follows. Please help me.
Thanks you very much!!!
Code:
#include "stdafx.h"

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

int sobel1[3][3]={{1,2,1},{0,0,0},{-1,-2,-1}};
int sobel2[3][3]={{2,1,0},{1,0,-1},{0,-1,-2}};
int sobel3[3][3]={{1,0,-1},{2,0,-2},{1,0,-1}};
int sobel4[3][3]={{0,-1,-2},{1,0,-1},{2,1,0}};
int sobel5[3][3]={{-1,-2,-1},{0,0,0},{1,2,1}};
int sobel6[3][3]={{-2,-1,0},{-1,0,1},{0,1,2}};
int sobel7[3][3]={{-1,0,1},{-2,0,2},{-1,0,1}};
int sobel8[3][3]={{0,1,2},{-1,0,1},{-2,-1,0}};

IplImage *img,*grayImg,*imgSobel1,*imgSobel2,*imgSobel3,*imgSobel4,*imgSobel5,*imgSobel6,*imgSobel7,*imgSobel8;

void mask(IplImage *grayImg,IplImage *resultImg,int mask[3][3]){
	int rows=grayImg->height;
	int cols=grayImg->width;
	int step=grayImg->widthStep;
	int dx[]={-1,-1,-1,0,0,0,1,1,1};
	int dy[]={-1,0,1,-1,0,1,-1,0,1};
	uchar * grayData=(uchar *)grayImg->imageData;
	uchar * resultData=(uchar *)resultImg->imageData;
	int i,j,k;

        [B][COLOR="Blue"]// My question Is why i isn't equal 0 ~> equal 1[/COLOR][/B] 
	for(i=[B][COLOR="green"]1[/COLOR][/B];i<rows-1;i++)
		for(j=[B][COLOR="green"]1[/COLOR][/B];j<cols-1;j++){
			resultData[i*step+j]=0;
			for(k=0;k<9;k++)
                                [B][COLOR="Blue"]// My question: You explain help me significance of line this code[/COLOR][/B]
 			[B][COLOR="green"]resultData[i*step+j]+=mask[1+dx[k]][1+dy[k]]*grayData[(i+dx[k])*step+j+dy[k]];[/COLOR][/B]
		}
}

int _tmain(int argc, _TCHAR* argv[])
{
	img=cvLoadImage("../../test.jpg");
	grayImg=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
	imgSobel1=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
	imgSobel2=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
	imgSobel3=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
	imgSobel4=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
	imgSobel5=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
	imgSobel6=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
	imgSobel7=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
	imgSobel8=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);

	cvCvtColor(img,grayImg,CV_BGR2GRAY);

	mask(grayImg,imgSobel1,sobel1);
	mask(grayImg,imgSobel2,sobel2);
	mask(grayImg,imgSobel3,sobel3);
	mask(grayImg,imgSobel4,sobel4);
	mask(grayImg,imgSobel5,sobel5);
	mask(grayImg,imgSobel6,sobel6);
	mask(grayImg,imgSobel7,sobel7);
	mask(grayImg,imgSobel8,sobel8);

	cvNamedWindow("Orginal Image",CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Gray Image",CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Sobel 1",CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Sobel 2",CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Sobel 3",CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Sobel 4",CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Sobel 5",CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Sobel 6",CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Sobel 7",CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Sobel 8",CV_WINDOW_AUTOSIZE);

	cvShowImage("Orginal Image",img);
	cvShowImage("Gray Image",grayImg);
	cvShowImage("Sobel 1",imgSobel1);
	cvShowImage("Sobel 2",imgSobel2);
	cvShowImage("Sobel 3",imgSobel3);
	cvShowImage("Sobel 4",imgSobel4);
	cvShowImage("Sobel 5",imgSobel5);
	cvShowImage("Sobel 6",imgSobel6);
	cvShowImage("Sobel 7",imgSobel7);
	cvShowImage("Sobel 8",imgSobel8);

	cvWaitKey(0);
	cvDestroyAllWindows();
	cvReleaseImage(&img);
	cvReleaseImage(&grayImg);
	cvReleaseImage(&imgSobel1);
	cvReleaseImage(&imgSobel2);
	cvReleaseImage(&imgSobel3);
	cvReleaseImage(&imgSobel4);
	cvReleaseImage(&imgSobel5);
	cvReleaseImage(&imgSobel6);
	cvReleaseImage(&imgSobel7);
	cvReleaseImage(&imgSobel8);

	return 0;
}
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top