Monday, March 26, 2012

Searching Arrays in Assembly Language


TITLE Program Template (ArraySerach.asm)

; Program Description: This program searches for an element in an array
; Author: Akhtar Jamil
; Date Created: 21/3/2012
; Last Modification Date:

INCLUDE Irvine32.inc

; (insert symbol definitions here)

.data
arrElements BYTE 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
msgOptions BYTE "Please select an option:",0dh,0ah
msgOption1 BYTE "Press 1 for searching",0dh,0ah
msgOption2 BYTE "Press 2 for exiting",0
msgNumberEntry BYTE "Please enter a number to search: ",0
msgError BYTE "Error: Please press either 1 or 2",0
msgFound BYTE "Entered element is found, ie: ",0
msgNotFound BYTE "Entered element is NOT found in array",0
sprtLine BYTE "--------------------------------------",0
.code
main PROC
globalLoop:

 MOV EDX,OFFSET msgOptions
 CALL WRITESTRING
 CALL CRLF
 CALL READINT
 CMP AL,1
 JE searchLabel
 CMP AL,2
 JE exitLabel
 CALL CLRSCR
 MOV EDX,OFFSET msgError  ; Tell user that wrong option selected
 CALL WRITESTRING
 CALL CRLF
 JMP globalLoop ; jump back to main options
 
searchLabel:
  MOV EDX,OFFSET msgNumberEntry
  CALL WRITESTRING
  CALL READINT
  MOV ESI, OFFSET arrElements
  MOV ECX,15
loop1:
CMP AL, [ESI]
JE found
INC ESI
LOOP  loop1     ; Loop through all elements of array to search for element

  MOV EDX,OFFSET msgNotFound
  CALL CLRSCR
  CALL WRITESTRING
  CALL CRLF
  JMP globalLoop
 
  found:
  CALL CLRSCR
  MOV EDX,OFFSET msgFound
  CALL WRITESTRING
  CALL WRITEINT
  CALL CRLF
  MOV EDX,OFFSET sprtLine
  CALL WRITESTRING
  CALL CRLF
 
JMP globalLoop

exitLabel:
 EXIT ; exit to operating system
main ENDP

; (insert additional procedures here)

END main

Sunday, April 17, 2011

How to calculate vertical and Horizontal Gradients, binarize and find vertical and horizontal projections of an Image in Matlab


The following code snippet shows the few steps for calculating horizontal and vertical gradients,  binarize and find vertical and horizontal projections of an Image of an image and show the corresponding results in the respective figures:
%Function starts here

function calcGradients()
close all;%Closes all opened figures
img=imread(PATH); % provide the path from where u want to load your image
gimg=rgb2gray(img); %To show how to convert color image into grayscale image
 figure, imshow(gimg); %Display gray image
 title('Gray Image');
%Define vertical gradient mask
maskV =[-1, 0, 1;
        -2, 0, 2;
        -1, 0, 1];
%Define Horizontal gradient mask
maskH =[-1, -2, -1;
         0,  0,  0;
         1,  2,  1];

grdv=imfilter(gimg,maskV);%Use matlab imfilter function to calculate vertical gradients with mask given above
figure, imshow(grdv,[]);
title('Vertical Edge');

grdh=imfilter(gimg,maskH);%Use matlab imfilter function to calculate Horizonal gradients with mask
figure, imshow(grdh,[]);
title('Horizontal Edge');

%Binarize the images

level = graythresh(grdv);
BWv = im2bw(grdv,level);
figure,imshow(BWv,[]);
title('Binarized image Vertical');

level = graythresh(grdh);
BWh = im2bw(grdh,level);
figure,imshow(BWh,[]);
title('Binarized image Horizontal');


%Dilation

dv =ones(3,4);
dilateBWv = imdilate (BWv, dv);
figure, imshow(dilateBWv,[]);
title('Dilated vertical image ');

dh=ones(3,6);
dilateBWh = imdilate (BWh, dh);
figure, imshow(dilateBWh,[]);
title('Dilated Horizontal Image');

%Combine vertical and horizontal images into one image by AND operation
finalImg=dilateBWh & dilateBWv;

figure, imshow(finalImg,[]);
title('finalImg Image');

pH=sum(finalImg,1); %Projection Horizontal
pV=sum(finalImg,2); %Projection Vertical
figure, plot(pH);
title('horitozntal projection');
figure, plot(pV);
title('Vertical projection');
end

Wednesday, September 15, 2010