Ananth's Blog - page 11

Posts

Gray Level Transformations

Following is the matlab codes for Grey Level or Intensity transformation functions...

1. Linear(Negative Transformation)

A=imread('401566.jpg'); %Change 401566.jpg with your image file
A=rgb2gray(A);
imshow(255-A); %(L-1)-r;L=256(8 bit gray scale)
2. Log Transform

%% Log Transform
% Equation s=c*log(1+r)
A=imread('401566.jpg');
A=rgb2gray(A);
imshow(log(double(A)+1));
view raw log_transform.m hosted with ❤ by GitHub

3.Power-Law Transformations

%% Power-Law Transformation
% Equation s=cr^a
A=imread('401566.jpg');
A=rgb2gray(A);
c=1;
a=2.2;
imshow(uint8(c*(double(A).^a)));


Creating and Deleting Folders/Directories using C

This program will create 100 folders from where it is executing. Then after the first half of the program it will delete those folders from the directory.
#include<stdio.h>
#include<io.h>
void create_folder(int n)
{
int i;
char name[256];
for(i=0;i<n;i++)
{
sprintf(name,"%d",i);
mkdir(name);
}
}
void delete_folder(int n)
{
int i;
char name[256];
for(i=0;i<n;i++)
{
sprintf(name,"%d",i);
rmdir(name);
}
}
void main()
{
create_folder(100);
printf("\n\n100 Folders are created..\n======================\n\n");
system("dir/p");
system("pause");
delete_folder(100);
printf("\n\nFolders are deleted..\n====================\n\n");
system("dir/p");
}


Flooding RAM ( C )

This program will dynamically allocate memory and shows the amount of memory it uses. When heap is empty it will show Memory Full. The memory will be freed once you close the program (No harm by the way!). The rate at which memory is allocated is small (1 Kb per each iteration) so you can see at which point heap becomes empty.
#include<stdio.h>
#include<stdlib.h>
/*Struct flood require 256*4=1Kb */
struct flood{
long int a[256];
}*temp;
void main()
{
long int k=sizeof(struct flood);
while(1)
{
temp=(struct flood *)malloc(sizeof(struct flood));
printf("%0.2f Mb\n",(float)k/(1024*1024));
k=k+1024;
if(temp==NULL)
printf("Memory Full\n");
}
}
view raw Flooding_RAM.c hosted with ❤ by GitHub

Simple DIP threshold animation using Matlab


%Dialog Box to browse and select image
[name path]=uigetfile({'*.jpg','IMAGE Files (*.jpg)'},'Choose Image for Thresholding');
A=imread(strcat(path,name));
%converting rgb to gray
A=rgb2gray(A);
for i=1:255
figure(1),imshow(A,[i 255-i]);
pause(0.01);
end

Automating scripts using expect

Expect is one way to automate your scripts by expecting what the output may be. For example you want to create a script that helps you to login to a telnet session and enter your login and password for you. Here is the simple example which helps you to do it.

#! /usr/bin/expect -f
spawn telnet 192.168.1.1
expect "Login:"
send "username\r"
expect "Password:"
send "password\r"
sleep 1
send "?\r"
interact

Above example will login to your router and enter your username and password and list all the possible commands using ? and then let you to interact with using interact command. You can automate anything for example applications, ssh etc..