Thursday, June 30, 2005

Good Link for Digital Image Processing

http://www.yov408.com/html/tutorials.php?s=82

Tuesday, June 28, 2005

Remove Duplicate Files using Perl Script

#!/usr/local/bin/perl
use File::stat;

my @files=glob("*.jpg");
for $fi (@files) {
$siz= stat($fi)->size;
$sizArr{$fi}=$siz;
# unshift(@sizArr, $siz);
# print "$siz\n";
}

#@sortSiz=sort(@sizArr);
#print @sortSiz;

$prevKey=0;
foreach $key (sort by_value keys %sizArr) {
$xx=$sizArr{$key};
if ($prevKey == $xx) {
print $key, '*', $xx, "\n";
#system("mv $key dups/");
} else {
print $key, '=', $xx, "\n";
}
$prevKey=$xx;
}

# sub by_value { $ary{$a} cmp $ary{$b}; }

sub by_value { $sizArr{$b} <=> $sizArr{$a}; }

Monday, June 27, 2005

One More Perl Script

#!/usr/local/bin/perl
my @dlist=glob("Im*");
$kk=1;

for $dd (@dlist) {
print "$dd\n";
chdir $dd or die "Cannot change dir to $dd: $!\n";;
my @files=glob("*.jpg");
print "@files\n";
for $fi (@files) {
print "$fi\n";
system("mv $fi ../testImg/$kk-$fi");
}
chdir ".." or die "Cannot change dir to ..: $!\n";
$kk++;
}

Thursday, June 23, 2005

Creating Stereo Images Easily

http://www.photostuff.co.uk/stereo.htm
A cool link about creating Stereo Images

int red, green, blue;
int[] StereoPixels = (int[])anaglyphIP.getPixels();
int[] LeftPixels = (int[])LeftIP.getPixels(); // left eye image
int[] RightPixels = (int[])RightIP.getPixels(); // right eye image

for(int i=0;i< LWidth*LHeight;i++){
red = (LeftPixels[i] & 0xff0000)>> 16; //Left eye consists of red channel
green = (RightPixels[i] & 0xff00ff00)>>8; // Right eye image consists of green and blue channel
blue = (RightPixels[i] & 0x0000ff);

StereoPixels[i] = ((red & 0xff)<<16)+((green & 0xff)<<8) + (blue & 0xff);
}
anaglyphIP.setPixels(StereoPixels);

Friday, June 17, 2005

Another Bit Manipulation Function

count=0;//count number of bits
k=2;//number of bits to be removed from R
unsigned char a1;
num=8;//bits empty in a1


int given(unsigned char R)
{
//right now a1 has num bits empty
//hence copy min(num,8-k) bits from R to a1
b_count=min(num,8-k);
if(num<8-k)
{
a=R>>(8-num);//num bits of R left in a
a1 = a1 | a;//fill a1 and write to disk
////write a1 to disk
if(num== (8-k))
{
//we have copied entire R after removing k bits hence no fight
num=8;//since written to disk consider a1 empty
}
else //some portion of R is left and hence we must copy that into a1
{
a=R>>k;// remove ending k bits of R
a=R<<(num+k);//remove starting num bits of R

a1=a;//copy all the bits to a1
num=k+num; // k + num bits of a1 are empty as of now
}
}
else
{
a=R>>k;//remove last k bits of R
a=a< a=a>>(8-num);//align with a1
a1=a1|a;//take or of a1 and a
num=num+8-k;
}

}

Get Unique Elements

#! /usr/local/bin/perl -w
open(FILEREAD, "finale");
open(FILEWR, ">fin");
@in=;

undef %saw;
@out = grep(!$saw{$_}++, @in);
print @out;
print FILEWR @out;

Wednesday, June 15, 2005

Wavelets Haar Daubechies

Wavelets here ... wavelets there ... but for computer vision and graphics I recommend these tutorials

http://www.bearcave.com/misl/misl_tech/wavelets/haar.html

AND

Wavelets for Computer Graphics: A Primer Part 1 and Part 2
in computer graphics with some intuition for what wavelets are, as ... Wavelets for Computer Graphics: Theory and Applications. ...
http://grail.cs.washington.edu/pub/stoll/wavelet1.pdf
http://grail.cs.washington.edu/pub/stoll/wavelet2.pdf

Html Text over an Image

The Code That Does It





<TABLE BORDER="0" cellpadding="0" CELLSPACING="0">

<TR>


<TD WIDTH="221" HEIGHT="300" BACKGROUND="newjoe01.jpg" VALIGN="bottom">

<FONT SIZE="+1" COLOR="yellow">Joe Burns at Work</FONT></TD>


</TR>

</TABLE>






OR
USE DIV AND PLACEMENT
OR
go to http://www.htmlgoodies.com/tutorials/web_graphics/article.php/3480021

Perl Script: directory file manip

To perform some actions on all files in a directory
#!/usr/bin/perl

$input_directory = ".";
chomp $input_directory;

until (-d $input_directory) { # the -d bit means check if there is a directory called ...
print "Sorry, that path / directory does not appear to exist, please try again.\n";
$input_directory = ;
chomp $input_directory;
}


opendir (INPUTDIR, $input_directory) || die "sorry, system cannot open $input_directory $input_directory";

while ($infile = readdir(INPUTDIR)) {
chomp $infile;
if ($infile !~ /^\./) {
if ($infile =~ /.*pgm/) {
$outfile = substr($infile,0,length($infile)-4);
print "\n convert $infile $outfile.jpg";
system("\n convert $infile $outfile.jpg");
}
}
}
closedir (INPUTDIR);

Tuesday, June 14, 2005

Store Images using Yahoo! API


#! /usr/local/bin/perl -w

######program takes in keywords file and puts images in dir images/ and creates file Im
ageData with all the data for the images.
use strict;
use warnings 'all';
use Getopt::Long;
use LWP::Simple;
use Yahoo::Search AppId => "shubh_iitd";

my $i=0;
open(FILEREAD, "keywords");

open(FILEWRITE, ">ImageData");


while (){
print " word is: $_";
my $query=$_;
my @results = Yahoo::Search->Results(
Image => $query,
Count => 10
);

foreach (@results) {
my $url = $_->Url();
print "\n $i - $url";
if (getstore($url, "images/$i.jpg") eq 200){
my $im = 0;
open(IN, "images/$i.jpg");
binmode(IN);
read(IN,$b,4);
my $s = 2*length($b);
my $ch = unpack("H$s", $b);
print "\n |$ch| ";
if($ch eq "ffd8ffe0") {print " JPEG ";$im=1;}
if($ch eq "47494638") {print " GIF ";$im=1;}
if($ch eq "89504e47") {print " PNG ";$im=1;}
if($ch eq "4d4d002a") {print " TIFF Big Endian ";$im=1;}
if($ch eq "49492a00") {print " TIFF Little Endian ";$im=1;}
if($ch =~ /^424d/) {print " BMP ";$im=1;}
close(IN);
if($im eq 1){
$i++;
print FILEWRITE "$i.jpg - $url - $query\n";
}
}
}

}
close FILEREAD;
close FILEWRITE;

Tuesday, June 7, 2005

Read till EOF using FILE*

To Read upto end of file using FILE* in c


while (fgets(fname,100,f))
{
//do anything
}

Monday, June 6, 2005

Bit Holder

Class for storing bits as unsigned char for compression,
public:
IntC index;
IntC nbits;
IntC size;
IntC csize;
vector bits;//storing the bits, 8 bits in each unsigned char
unsigned char temp[8];//temprory for storing data
IntC tempsize;

void BitHolderInit(IntC dimension, IntC ind)
{
index=ind;
nbits=dimension;
IntC i; if(dimension%8) i=dimension/8; else i=dimension/8 + 1;
size = i; tempsize = 0; csize = 0;
}

int BitHolderWrite()
{
if (tempsize != 8) return -1;
//cerr<<"\n Csize : "<< csize <<" "<< size<<" "<< nbits<<" "<< tempsize;

bits.push_back( ((temp[0] & 0x1)<<7) | ((temp[1] & 0x1)<<6) | ((temp[2] & 0x1)
<< 5) | ((temp[3] & 0x1)<< 4) | ((temp[4] & 0x1)<< 3) | ((temp[5] & 0x1)<< 2) | ((temp[6] & 0x1)<< 1) | (temp[7] & 0x1) );
// bits.push_back(((temp[0] & 0x1)<<7) | ((temp[1] & 0x1)<<6 & 0x) | ((temp[
2] & 0x1)<<5) | ((temp[3] & 0x1)<<4) | ((temp[4] & 0x1)<<3) | ((temp[5] & 0x1)<<2)
| ((temp[6] & 0x1)<<1) | ((temp[7] & 0x1)));

tempsize=0; csize++;
temp[0]=temp[1]=temp[2]=temp[3]=temp[4]=temp[5]=temp[6]=temp[7]='0';
return 1;
}

int AddBit(IntC b)
{
//cerr<<" ADDING "<< b<<" "<< tempsize;
if(b==0) temp[tempsize]='0'; else temp[tempsize]='1';
tempsize++;
if(tempsize==8) BitHolderWrite();
}

int GetBit(IntC pos)
{
//cerr<<" Position : "<< pos<<" "<< pos/8<<" "<< pos%8<<" "<< bits.size();;

if(pos>nbits) {cerr<<" ERROR ";return -1;}

if(((pos-1)/8)< bits.size())
{
if (((bits[(pos-1)/8] >> (7-(pos-1)%8)) & 0x1) == 0x1)
return 1;
else
return 0;
}

pos=(pos)%8;
pos--;
//cerr<<" "<< pos<<" "<< tempsize<<" "<< temp[pos];
if(temp[pos]=='1') return 1; else return 0;
}

int PrintBits()
{
cerr<<" Printing Bits Index:"<< index<<" nBits:"<< nbits<<" ";
for (IntC i = 0; i < bits.size(); i++)
{
cerr<<(bits[i]>>7 & 0x1)<<(bits[i]>>6 & 0x1)<<(bits[i]>>5 & 0x1)<<(bits[i]
>>4 & 0x1)<<(bits[i]>>3 & 0x1)<<(bits[i]>>2 & 0x1)<<(bits[i]>>1 & 0x1)<<(bits[i] &
0x1);
}

for (IntC i = 0; i < tempsize; i++)
cerr<< temp[i];
return 1;
}

} BitHolder,*PBitHolder;

bool operator<(const BitHolder &a, const BitHolder &b)
{
//cerr<<"\n SORTING "<< a.index<<" "<< b.index;
IntC i,j;
for(i=0;i< a.bits.size();i++)
{
for(j=7;j>=0;j--)
{
//cerr<<"| i:"<< i<<" j:"<< j<<" "<<(a.bits[i] >> j & 0x1)<<" "<< (b.bits[
i] >> j & 0x1);
if(((a.bits[i] >> j & 0x1)==0x1)&&((b.bits[i] >> j & 0x1)==0x0))
return false;
if(((a.bits[i] >> j & 0x1)==0x0)&&((b.bits[i] >> j & 0x1)==0x1))
return true;
}
}

//cerr<<" Tempsize : "<< a.tempsize;
for(i=0;i< a.tempsize;i++)
{
if((a.temp[i]=='1')&&(b.temp[i]=='0'))
return false;
if((a.temp[i]=='0')&&(b.temp[i]=='1'))
return true;
}

return false;
}

bool operator==(const BitHolder &a, const BitHolder &b)
{if(((a< b)==false)&&((b< a)==false)) return true; else false;}

bool operator>(const BitHolder &a, const BitHolder &b)
{if (b < a) return true; else false;}

Random Distribution: Gaussian

Some random number genertion codes for some distributions


// The state vector for generation of random numbers.
char rngState[256];

// Initialize the random number generator.
void initRandom(){
initstate(2, rngState, 256);
}

// Generate a random integer in the range [rangeStart,
// rangeEnd]. Inputs must satisfy: rangeStart <= rangeEnd.
IntC genRandomInt(IntC rangeStart, IntC rangeEnd){
if(!(rangeStart <= rangeEnd)) return -1;
IntC r;
r = rangeStart + (IntC)((rangeEnd - rangeStart + 1.0) * random() / (RAND_MAX + 1
.0));
return r;
}

// Generate a random 32-bits unsigned (Uns32C) in the range
// [rangeStart, rangeEnd]. Inputs must satisfy: rangeStart <=
// rangeEnd.
Uns32C genRandomUns32(Uns32C rangeStart, Uns32C rangeEnd){
Uns32C r;
if (RAND_MAX >= rangeEnd - rangeStart) {
r = rangeStart + (Uns32C)((rangeEnd - rangeStart + 1.0) * random() / (RAND_MAX
+ 1.0));
} else {
r = rangeStart + (Uns32C)((rangeEnd - rangeStart + 1.0) * ((LongUns64C)random(
) * ((LongUns64C)RAND_MAX + 1) + (LongUns64C)random()) / ((LongUns64C)RAND_MAX * (
(LongUns64C)RAND_MAX + 1) + (LongUns64C)RAND_MAX + 1.0));
}
return r;
}

// Generate a random real distributed uniformly in [rangeStart,
// rangeEnd]. Input must satisfy: rangeStart <= rangeEnd. The
// granularity of generated random reals is given by RAND_MAX.
RealC genUniformRandom(RealC rangeStart, RealC rangeEnd){
RealC r;
r = rangeStart + ((rangeEnd - rangeStart) * (RealC)random() / (RealC)RAND_MAX);
return r;
}

// Generate a random real from normal distribution N(0,1).
RealC genGaussianRandom(){
// Use Box-Muller transform to generate a point from normal
// distribution.
RealC x1, x2;
do{ x1 = genUniformRandom(0.0, 1.0); } while (x1 == 0); // cannot take log o
f 0.
x2 = genUniformRandom(0.0, 1.0);
RealC z;
z = SQRT(-2.0 * log(x1)) * COS(2.0 * M_PI * x2);
return z;
}

GetStore and Url Extraction

Things to Note:
perl program can take out URL out of xml files,
getstore() can store the file at the given url


@lines= split(/ /,$line);

@url="";
@filenames="";

foreach $item (@lines)
{
if($item =~ /^URL*/)
{
$i=index($item,"\"");
$j=rindex($item,"\"");
push(@url,substr($item,$i+1,$j-$i-1));
}
}

foreach $item (@url)
{
print "\n ";
$i=rindex($item,"/");
$filename= substr($item,$i+1,length($item)-2-$i);
push(@filenames, $filename);
if( getstore($item,"$dir/$numg-$filename") < 400)
{$numg++;}
}

Magic Number in Perl

A way to check for various image formats, the code is in perl. It checks the magic number for the image format

($in) = @ARGV;
die "Missing input file name.\n" unless $in;

$im = 0;
open(IN, "< $in");
binmode(IN);
read(IN,$b,4);
$s = 2*length($b);
$ch = unpack("H$s", $b);
print "\n |$ch| ";
if($ch eq "ffd8ffe0") {print " JPEG ";$im=1;}
if($ch eq "47494638") {print " GIF ";$im=1;}
if($ch eq "89504e47") {print " PNG ";$im=1;}
if($ch eq "4d4d002a") {print " TIFF Big Endian ";$im=1;}
if($ch eq "49492a00") {print " TIFF Little Endian ";$im=1;}
if($ch =~ /^424d/) {print " BMP ";$im=1;}
close(IN);

exit;