Monday, June 14, 2010

Multiplier VHDL example -2

The following code compute the square of any input with 10 bits

-- VHDL CODE for MULTIPLIER

-- ----------------------------------

library ieee;
use ieee.numeric_bit ;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;



entity global_mul is
port( num1: in std_logic_vector(9 downto 0);
product: out std_logic_vector(19 downto 0)
);
end global_mul;

architecture behv of global_mul is

begin
process(num1)

variable num1_reg: std_logic_vector(9 downto 0);
variable product_reg: std_logic_vector(19 downto 0);
variable num2:std_logic_vector(19 downto 0);
begin

num1_reg := num1;
product_reg := "00000000000000000000";
num2:= "0000000000" & num1 ;

for i in 0 to 9 loop
if num1_reg(i)='1' then
product_reg := product_reg + num2;
end if;
num2:= num2(18 downto 0)&'0' ;
end loop;

product <= product_reg;

end process;

end behv;

Tuesday, June 8, 2010

Comparator Design With VHDL [Example -1]


Very high speed integrated circuit Hardware Description Language (VHDL) is a hardware description language widely used to describe FPGA (Field Programmable Logic Gate ),IC (Integrated Circuit) and others digital or mixed signal devices.The sense of hardware description is mainly define the functionality, its behavior. So for every VHDL file there should be atleast one entity and for entity there must be atleast one architecture. Entity is system entry point for a specific device. Its describe the Hardware architecture in top level, i.e number of input / output interfaces. The device input/output roughly speaking the input/output pins are defined here with the name of port. Architecture define the hardware functionality. Like other High level programming language the VHDL give the opprtunity to block code, Functions and data types. below the example is a simple intro to design a comparator with VHDL.
---------------------------------------------------
-- comparator for substration in SOBEL EDGE DETECTION block
---------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;

---------------------------------------------------

entity global_com is


port( COMA: in std_logic_vector(20 downto 0);
OPNum: out std_logic_vector(7 downto 0)
);
end global_com;

---------------------------------------------------

architecture behv of global_com is

begin

process(COMA)
begin
if (COMA<"00000010011100010000") then
OPNum <= "00000000";
else
OPNum <="11111111";
end if;
end process;

end behv;

Here the standrad library is used which is defiened by IEEE. The device has only one input and one output is defined by the Entity. Entity is a reserved keyword for VHDL and global_com is the name of the entity. COMA is the input 21 bit data Bus with standrad type (O |1). Output is 8 bit data bus .The input value is checked with predefined threshold value to defiend the output value. This functionality of this block is defined by architecture section where behv is the name of the architecture, Process can be imagine for the software devloper as a microblock of code which will run sequentially (virtual! Remember: Hardware application always run parallel this is the vital difference between software design and Hardware design. This idea will be used in simulation process through testbench ,[later] ).
I used this code while designing a FPGA to perform Sobel and prewitt Edge Detection Algorithm.
Enjoy the code ...

Monday, February 8, 2010

Video Processing With MatLab


In my previous post I describe some way to detect Edge of an Image by MatLab .
In this post I will describe one way to process a video .
Video are just a series of still images and it changes so fast that we can not detect the single image .
Basically this images are called frames , and a video has constant frames rate measured in Second
basically 15 , 20 or 30 fps (frame per second ).
The idea behind the video processing is to capture the frame , it is now raw image . Before futher processing we have make it as a standard image , then move forward . It is now just image processing . After that finally we make video file with the processed images (images_>frame) .
Here is the following simle code of video Processing :-
it will take an avi (audio Video Interleaved)file as an input , capture the frame into image
then use this image for edge detection and finally make the output Video . Lets Enjoy it ... ...



%% It is for Avi file , that keep in working directory
Vidin = 'sample.avi' ;
Vidout = 'out.avi';
% give all the properties of the video file belongs

fileinfo = aviinfo(Vidin);
nframes = fileinfo.NumFrames;
aviobj = avifile(Vidout, 'compression', 'none', 'fps',20);
for i = 1:nframes
%Read frames from input video
mov_in = aviread(Vidin,i);
% frame to image
im_in = frame2im(mov_in);
% RGB TO Gary Image
imf_out = rgb2gray(im_in);
% Edge detection with canny
canny_im = edge(imf_out,'canny');
figure(2);
imagesc(canny_im);
axis('square');
colormap('gray');

[X, map] = gray2ind(canny_im, 16);
grapmap = gray(256);
frm = im2frame(X , grapmap);
aviobj = addframe(aviobj,frm);

end;
aviobj = close(aviobj);
return;

Wednesday, January 20, 2010

Edge Detection with sobel operator

Edge Detection with sobel operator
shahnewaz ali

the sobel operator is widely used to detect edges of image , in the area of image and video processing , computer vision, robotics etc . here I am going to give a brief description of sobel operator and matlab implementation (source code ) both using matlab built-in function for edge detection and manually sobel edge detection (function).

Basics :
the sobel operator calculates the gradient of the image intensity at each point, giving the direction of the largest possible increase from light to dark and the rate of change in that direction.

Mathematically, the gradient of a two-variable function (here the image intensity function) is at each image point a 2D vector with the components given by the derivatives in the horizontal and vertical directions.


sobel operator :

-1
0
1
-2
0
2
-1
0
1
Sx
and
1
2
1
0
0
0
-1
-2
-1
Sy

Convolution :

The convolution operation is a mathematical operation which takes two functions f(x) and g(x) and produces a third function h(x). In image processing 2D convolution is used .


no more theory or mathematical definition if you want go to the following link :

more about theory : http://en.wikipedia.org/wiki/Sobel_operator


Source code :

I am going to detect the edges of image (flower ) that is placed on the right side of this post , the threshold is determined emperically.

type 1 : matlab built in function :-

im=imread('flower.jpg');
img=rgb2gray(im);

sob_im = edge(img,'sobel');
figure(2);
imagesc(sob_im);
axis('square');
colormap('gray');

imshow(sob_im);

type 2:

edge_sobel is a M-file that contain the function :

the function describe below . in matlab file ->new->M file . a new text editor will be open then copy the following code and paste it , save the file name as edge_sobel.m

function [y]=edge_sobel(imT,i,j)

% sobel kernel
kx=[-1,-2,-1;0,0,0;1,2,1];
ky=[-1,0,1;-2,0,2;-1,0,1];
sx=0;
sy=0;
frac=0;
for xx=-1:1
for yy=-1:1
frac=double (imT(i+xx , j+yy))* kx(xx+2,yy+2);
%convolution
sx = double(sx+frac);

%kx(xx+2,yy+2)
%frac
%sx
end;
end;
frac=0;
for xx=-1:1
for yy=-1:1
frac=double(imT(i+xx , j+yy))* ky(xx+2,yy+2);
%ky(xx+2,yy+2)
sy=double(sy+frac);
%frac
%sy
end;
end;
%sx
%sy
z=uint8(sqrt(sx^2+sy^2))
%here threshold i defined threshold empirecaly . you can choose any method
%adaptive thresholding
if(z>=100)
y=255;
else
y=0;
end;
y;

then copy the following code and paste it in command window and enjoy it

im=imread('flower.jpg');
img=rgb2gray(im);
im3=img ;
[ht wd ch] = size(img);
im_out = imresize(img, [432 432], 'bilinear');
for i=2:431
for j=2:431
im3(i,j)=edge_sobel(im_out,i,j);%edge_sobel.m file
end;
end;
imshow(im3);


hope this post will help you to start learning image processing methodology