You have no items in your shopping cart.

Subtotal: 0.00

Linear and Morphological Image Filtering using Spartan3 FPGA Image Processing Kit

Abstract

Field Programmable Gate Array (FPGA) technology has become a viable target for the implementation of real time algorithms suited to image processing applications. The unique architecture of the FPGA has allowed the technology to be used in many applications encompassing all aspects of video image processing. Among those algorithms, linear filtering based on a 2D convolution, and non-linear 2D morphological filters, represent a basic set of image operations for a number of applications. In this work, an implementation of linear and morphological image filtering using a Spartan3 FPGA Image Processing Kit, with educational purposes, is presented. The system is connected to a serial port of a personal computer, which in that way form a powerful and low-cost design station.

Block Diagram

Implementation Process:

 Implementation Process:

Tool required:

Software:

1. Xilinx ISE 11.1i or above
2. Xilinx EDK 11.1i or above
3. VB GUI to display output

Language: VHDL, System C

Hardware:

1. Spartan3 FPGA Image Processing Kit
2. JTAG Cable
3. PC 
4. Serial Cable

Introduction

The Morphology is a broad set of image processing operations that process images based on shapes. Morphological operations apply a structuring element to an input image, creating an output image of the same size. In a morphological operation, the value of each pixel in the output image is based on a comparison of the corresponding pixel in the input image with its neighbors. By choosing the size and shape of the neighborhood, you can construct a morphological operation that is sensitive to specific shapes in the input image.


The most basic morphological operations are dilation and erosion. Dilation adds pixels to the boundaries of objects in an image, while erosion removes pixels on object boundaries. The number of pixels added or removed from the objects in an image depends on the size and shape of the structuring element used to process the image. In the morphological dilation and erosion operations, the state of any given pixel in the output image is determined by applying a rule to the corresponding pixel and its neighbors in the input image. The rule used to process the pixels defines the operation as dilation or erosion. This table lists the rules for both dilation and erosion.

Rules for Dilation and Erosion

Operation

Rule

Dilation

The value of the output pixel is the maximum value of all the pixels in the input pixel's neighborhood. In a binary image, if any of the pixels is set to the value 1, the output pixel is set to 1.

Erosion

The value of the output pixel is the minimum value of all the pixels in the input pixel's neighborhood. In a binary image, if any of the pixels is set to 0, the output pixel is set to 0.

 Erosion

Erosion is one of two fundamental operations (the other being dilation) in Morphological image processing from which all other morphological operations are based.

The erosion of a binary image f by a structuring element s (denoted   s) produces a new binary image g =   s with ones in all locations (x,y) of a structuring element's origin at which that structuring element s fits the input image f, i.e. g(x,y) = 1 is s fits f and 0 otherwise, repeating for all pixel coordinates (x,y).

Morphological Erosion

 Morphological Erosion

Dilation

The dilation of an image f by a structuring element s (denoted   s) produces a new binary image g =   s with ones in all locations (x,y) of a structuring element's origin at which that structuring element shits the the input image f, i.e. g(x,y) = 1 if s hits f and 0 otherwise, repeating for all pixel coordinates (x,y). Dilation has the opposite effect to erosion -- it adds a layer of pixels to both the inner and outer boundaries of regions.

Morphological Dilation

      Morphological Dilation     

Linear Filtering

Linear filters in the time domain process time-varying input signals to produce output signals, subject to the constraint of linearity.

Most filters implemented in analog electronics, in digital signal processing, or in mechanical systems are classified as causal, time invariant, and linear. However the general concept of linear filtering is broader, also used in statistics, data analysis, and mechanical engineering among other fields and technologies. This includes non-causal filters and filters in more than one dimension such as would be used in image processing; those filters are subject to different constraints leading to different design methods, which are discussed elsewhere.

Output Images

Output Images

C Code for Morphological Dilation, Erosion and Linear Filtering using Spartan3 FPGA Image Processing Kit

 Linear and Morphological Image Filtering using Spartan3 Output Image

Linear and Morphological Image Filtering using Spartan3

 

 Linear and Morphological Image Filtering using Spartan3

 

 Linear and Morphological Image Filtering using Spartan3

 

 Linear and Morphological Image Filtering using Spartan3

Conclusion

#include 
#include 
#include 
#define R 128
#define C 128
#define ROW 128
#define COL 128
#define ROW1 128
#define COL1 128

int Sliding_window[3][3];
int kernel[3][3];
int INPUT[R][C];
int OUTPUT1[ROW1][COL1];
int OUTPUT2[ROW1][COL1];
int OUTPUT3[ROW1][COL1];


void main()
{
int i,j,m;
int len=127;
int b=0,a=255;

////////////////////// INPUT IMAGE ////////////////////
for( i=0;i<R;i++)
{
for( j=0;j<C;j++)
{
INPUT[i][j]=hai[i][j];
xil_printf("%d\n",INPUT[i][j]);
}
}                                                          

////////////// BLACK AND WHITE IMAGE //////////////
for( i=0;i<128;i++)
{
for( j=0;j<128;j++)
{
INPUT[i][j]=hai[i][j];
if(INPUT[i][j] > 100)
{
xil_printf("%d\n",a);
}
else
{
xil_printf("%d\n",b);
}
												  
}
}
/////////////// MORPHOLOGICAL EROSION IMAGE /////////////
for(i=0;i<ROW;i++)
{
for (j=0;j<COL;j++)
{
if (i==0 & j==0 & INPUT[i][j] < 100 )
{                                                          
	OUTPUT1[i][j]= 0 ;
	OUTPUT1[i][j+1]= 0 ;
	OUTPUT1[i+1][j]= 0 ;
	OUTPUT1[i+1][j+1]= 0 ;
}

else if (i==0 & j==len & INPUT[i][j] < 100 )
{
	OUTPUT1[i][j-1]= 0 ;
	OUTPUT1[i][j]= 0 ;
	OUTPUT1[i+1][j-1]= 0 ;
	OUTPUT1[i+1][j]= 0 ;
}
else if (i==len & j==0 & INPUT[i][j] < 100 )
{
	OUTPUT1[i-1][j]= 0 ;
	OUTPUT1[i-1][j+1]= 0 ;
	OUTPUT1[i][j]= 0 ;
	OUTPUT1[i][j+1]= 0 ;
}
else if (j==len & i==len & INPUT[i][j] < 100 )
{
	OUTPUT1[i-1][j-1] = 0 ;
	OUTPUT1[i-1][j]= 0 ;
	OUTPUT1[i][j-1]= 0 ;
	OUTPUT1[i][j]= 0 ;
  
}
else if (i==0 & INPUT[i][j] < 100 )
{          
	OUTPUT1[i][j-1]= 0 ;
	OUTPUT1[i][j]= 0 ;
	OUTPUT1[i][j+1]= 0 ;
	OUTPUT1[i+1][j-1]= 0 ;
	OUTPUT1[i+1][j]= 0 ;
	OUTPUT1[i+1][j+1]= 0 ;
}
else if (i==len & INPUT[i][j] < 100 )
{
	OUTPUT1[i-1][j-1] = 0 ;
	OUTPUT1[i-1][j]= 0 ;
	OUTPUT1[i-1][j+1]= 0 ;
	OUTPUT1[i][j-1]= 0 ;
	OUTPUT1[i][j]= 0 ;
	OUTPUT1[i][j+1]= 0 ;
}
else if (j==len & INPUT[i][j] < 100 )
{
	OUTPUT1[i-1][j-1] = 0 ;
	OUTPUT1[i-1][j]= 0 ;
	OUTPUT1[i][j-1]= 0 ;
	OUTPUT1[i][j]= 0 ;
	OUTPUT1[i+1][j-1]= 0 ;
	OUTPUT1[i+1][j]= 0 ;
}
else if (j==0 & INPUT[i][j] < 100 )
{
	OUTPUT1[i-1][j]= 0 ;
	OUTPUT1[i-1][j+1]= 0 ;
	OUTPUT1[i][j]= 0 ;
	OUTPUT1[i][j+1]= 0 ;
	OUTPUT1[i+1][j]= 0 ;
	OUTPUT1[i+1][j+1]= 0 ;
}
else
{          
if(INPUT[i][j] < 100 )
{
	OUTPUT1[i-1][j-1] = 0 ;
	OUTPUT1[i-1][j]= 0 ;
	OUTPUT1[i-1][j+1]= 0 ;
	OUTPUT1[i][j-1]= 0 ;
	OUTPUT1[i][j]= 0 ;
	OUTPUT1[i][j+1]= 0 ;
	OUTPUT1[i+1][j-1]= 0 ;
	OUTPUT1[i+1][j]= 0 ;
	OUTPUT1[i+1][j+1]= 0 ;
}
else
{
	OUTPUT1[i][j]= 255;
}
}
}
}

for( i=0;i<ROW1;i++)
{
for( j=0;j<COL1;j++)
{
xil_printf("%d\n",OUTPUT1[i][j]);

}                      
}

/////////////// MORPHOLOGICAL DILATION IMAGE /////////////           
for(i=0;i<ROW;i++)
{
for (j=0;j<COL;j++)
{
if (i==0 & j==0 & INPUT[i][j] > 100 )
{                                                          
	OUTPUT2[i][j]= 255 ;
	OUTPUT2[i][j+1]= 255 ;
	OUTPUT2[i+1][j]= 255 ;
	OUTPUT2[i+1][j+1]= 255 ;
}                                              
else if (i==0 & j==len & INPUT[i][j] > 100 )
{
	OUTPUT2[i][j-1]= 255 ;
	OUTPUT2[i][j]= 255 ;
	OUTPUT2[i+1][j-1]= 255 ;
	OUTPUT2[i+1][j]= 255 ;
}
else if (i==len & j==0 & INPUT[i][j] > 100 )
{
	OUTPUT2[i-1][j]= 255 ;
	OUTPUT2[i-1][j+1]= 255 ;
	OUTPUT2[i][j]= 255 ;
	OUTPUT2[i][j+1]= 255 ;
}
else if (j==len & i==len & INPUT[i][j] > 100 )
{
	OUTPUT2[i-1][j-1] = 255 ;
	OUTPUT2[i-1][j]= 255 ;
	OUTPUT2[i][j-1]= 255 ;
	OUTPUT2[i][j]= 255 ;
  
}
else if (i==0 & INPUT[i][j] > 100 )
{          
	OUTPUT2[i][j-1]= 255 ;
	OUTPUT2[i][j]= 255 ;
	OUTPUT2[i][j+1]= 255 ;
	OUTPUT2[i+1][j-1]= 255 ;
	OUTPUT2[i+1][j]= 255 ;
	OUTPUT2[i+1][j+1]= 255 ;
}
else if (i==len & INPUT[i][j] > 100 )
{
	OUTPUT2[i-1][j-1] = 255 ;
	OUTPUT2[i-1][j]= 255 ;
	OUTPUT2[i-1][j+1]= 255 ;
	OUTPUT2[i][j-1]= 255 ;
	OUTPUT2[i][j]= 255 ;
	OUTPUT2[i][j+1]= 255 ;
}
else if (j==len & INPUT[i][j] > 100 )
{
	OUTPUT2[i-1][j-1] = 255 ;
	OUTPUT2[i-1][j]= 255 ;
	OUTPUT2[i][j-1]= 255 ;
	OUTPUT2[i][j]= 255 ;
	OUTPUT2[i+1][j-1]= 255 ;
	OUTPUT2[i+1][j]= 255 ;
}
else if (j==0 & INPUT[i][j] > 100 )
{
	OUTPUT2[i-1][j]= 255 ;
	OUTPUT2[i-1][j+1]= 255 ;
	OUTPUT2[i][j]= 255 ;
	OUTPUT2[i][j+1]= 255 ;
	OUTPUT2[i+1][j]= 255 ;
	OUTPUT2[i+1][j+1]= 255 ;
}
else
{          
if(INPUT[i][j] > 100 )
{
	OUTPUT2[i-1][j-1] = 255 ;
	OUTPUT2[i-1][j]= 255 ;
	OUTPUT2[i-1][j+1]= 255 ;
	OUTPUT2[i][j-1]= 255 ;
	OUTPUT2[i][j]= 255 ;
	OUTPUT2[i][j+1]= 255 ;
	OUTPUT2[i+1][j-1]= 255 ;
	OUTPUT2[i+1][j]= 255 ;
	OUTPUT2[i+1][j+1]= 255 ;
}
else
{
	OUTPUT2[i][j]= 0;
}
}
}
}

for( i=0;i<ROW1;i++)
{
for( j=0;j<COL1;j++)
{
xil_printf("%d\n",OUTPUT2[i][j]);                      
}

}
////////////////LINEAR FILTERING////////////////
kernel[0][0] =  0; kernel[0][1] = -1; kernel[0][2] =  0;
kernel[1][0] = -1; kernel[1][1] =  5; kernel[1][2] = -1;
kernel[2][0] =  0; kernel[2][1] = -1; kernel[2][2] =  0;

for(i=0;i<128;i++)
{
for (j=0;j<128;j++)
{                      
if (i==0 & j==0)
{

}

else if (i==0 & j==len)
{

}
else if (i==len & j==0)
{
								  
}
else if (j==len & i==len)
{
				  
}
else if (i==1)
{          

}
else if (i==len)
{

}
else if (j==len)
{

}
else if (j==1)
{

}
else
{          


Sliding_window[1][1]=INPUT[i-1][j-1] * kernel[0][0];
Sliding_window[1][2]=INPUT[i-1][j] * kernel[0][1];
Sliding_window[1][3]=INPUT[i-1][j+1] * kernel[0][2];
Sliding_window[2][1]=INPUT[i][j-1] * kernel[1][0];
Sliding_window[2][2]=INPUT[i][j] * kernel[1][1];
Sliding_window[2][3]=INPUT[i][j+1] * kernel[1][2];
Sliding_window[3][1]=INPUT[i+1][j-1] * kernel[2][0];
Sliding_window[3][2]=INPUT[i+1][j] * kernel[2][1];
Sliding_window[3][3]=INPUT[i+1][j+1] * kernel[2][2];

OUTPUT3[i][j]=Sliding_window[1][1]+Sliding_window[1][2]+Sliding_window[1][3]+Sliding_window[2][1]+Sliding_window[2][2]+Sliding_window[2][3]+Sliding_window[3][1]+Sliding_window[3][2]+Sliding_window[3][3];
}
}
}

for( i=0;i<ROW1;i++)
{
for( j=0;j<COL1;j++)
{
xil_printf("%d\n",OUTPUT3[i][j]);

}

}

}

A low-cost image processing system for real time applications with educational purposes has been implemented. The system takes advantages of the available resources in a EDK system based on the Spartan3 FPGA Image Processing Kit