aboutsummaryrefslogtreecommitdiff
path: root/fly-tools/misc/FlyTrackingFilter.cpp
blob: 9020522ebba2b076dba5cb5c17f7e7849cc407f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <vector>
#include <list>
#include <fstream>
#include <cassert>
#include <cstdlib>

#include <ImageMagick/Magick++.h>

#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_eigen.h>

#include "FrameInfo.h"

using namespace Magick;
using namespace std;
void findObj(Image* img, int x, int y, vector<pair<int,int> > & shape ,bool eightCon=true, bool colorLookingFor=true);
void eightConnObj(Image* img, int x, int y, vector<pair<int, int> > & obj, bool color=true);
void fourConnObj(Image* img, int x, int y, vector<pair<int, int> > & obj, bool color=true);
vector<double> covariantDecomposition(vector<pair<int,int> > & points);
pair<int,int> getCentroid(vector<pair<int,int> > & points);
bool isInterface(Image* orig, unsigned int x, unsigned int y);
void writeFrameImage(int fn, string imS);
int roundT(double v) {return int(v+0.5);}


const double PI = atan(1.0)*4.0;
const double FACTOR_EIGEN = 100;

Image* residual;

ostream &operator<<(ostream &out, FlyObject & fO) {
	fO.output(out);
	return out;
}

ostream &operator<<(ostream &out, FrameInfo & fI) {
	fI.output(out);
	return out;
}

vector<vector<pair<int, int> > > shapeVectors;
vector<pair<int,int> > shape;
vector<pair<int, int> > sizeNIndexVector;

void bubbleSort() {
	
	for(int i=1; i<sizeNIndexVector.size(); i++) {
		for(int j=0; j<sizeNIndexVector.size()-i; j++) {
			pair<int, int> a = sizeNIndexVector[j];
			pair<int, int> b = sizeNIndexVector[j+1];
			
			if (a.first < b.first) {
				pair<int, int> c = sizeNIndexVector[j];
				sizeNIndexVector[j] = sizeNIndexVector[j+1];
				sizeNIndexVector[j+1] = c;
			}
		}		
	}

}

void fillResidualWithObj(vector<pair<int, int> > & obj, ColorRGB c)
{
	for (unsigned int i = 0; i<obj.size(); i++)
		residual->pixelColor(obj[i].first, obj[i].second, c);
}

void writeHist(const char* filename, map<unsigned int, unsigned int> & len)
{
	map<unsigned int,unsigned int>::iterator front = len.begin(),
	back = len.end();
	back--;
	
	
	unsigned int first = front->first, last = back->first;
	/*if (cutoff != -1 && cutoff < int(last))
	 last = cutoff;
	 */
	cout << "Min: " << first << endl
	<< "Max: " << last << endl
	<< "Count: " << last-first << endl;
	//vector<unsigned int> hist(last-first, 0);
	vector<unsigned int> hist(last+1, 0);
	
	cout << "hist size: " << hist.size() << endl;
	try{
		for(unsigned int j = 0; j<first; j++) {
			hist[j] = 0;
		}
		for (unsigned int j = first; j<=last; j++)
		{
			/*if ( roundT(j-first) >= int(hist.size()) )
			 hist.resize(j-first,0);
			 hist[roundT(j-first)] = len[j];
			 */
			
			/*if ( roundT(j) >= int(hist.size()) )
			 hist.resize(j,0);
			 hist[roundT(j)] = len[j];
			 */
			hist[j] = len[j];
		}
	}
	catch (...)
	{ cerr << "Bad histogram bucketing" << endl; }
	
	/*if ( (cutoff >= 0) && (cutoff<int(hist.size())) )
	 hist.resize(cutoff);
	 */
	len.clear();
	try
	{
		ofstream fout(filename);
		for (unsigned int i = 0; i<hist.size(); i++) {
			fout << hist[i] << endl;
		}
		fout << first << " " << last << " " << hist.size() << endl;
		fout.close();
	}
	catch (...)
	{ cerr << "Bad memory loc for opening file" << endl; }
}

int main(int argc, char* argv[])
{
	if (argc < 5)
	{
		cerr << "Usage: executablename <inputFile.txt> <ratio_largest_to_second_largest> <InputLocationOfMaskImage> <outputFolderName>" << endl; // input file contains name of the
		// input image files
		return -1;
	}
	
	MagickCore::SetMagickResourceLimit(MagickCore::MemoryResource, 1536);
	MagickCore::SetMagickResourceLimit(MagickCore::MapResource, 2048);
	
	string fileName;
	ifstream inputFile(argv[1]);
	if (inputFile.fail() ) {
		cout << "cannot open the input file that contains name of the input images\n";
		exit(1);
	}

	// get the output file name along with the location from argv[4]
	string outputFileLocation(argv[4]);
	string outputFileName = outputFileLocation + "final/outputFile.txt";
	ofstream outputFile(outputFileName.c_str());
	
	// get the location of the input mask file
	string inputMaskFileLocation(argv[3]);
	
	int frameCounter = 0;
	// use 15, 20 or 10
	// 15 found to be working correct
	double ratioSecondLargestToLargest = atof(argv[2]);
	
	cout << "Ratio is 1/"<<ratioSecondLargestToLargest<< " = "<<(1/ratioSecondLargestToLargest)<<endl;
	
	outputFile<<"Ratio given 1/"<<ratioSecondLargestToLargest<<" = "<<(1/ratioSecondLargestToLargest)<<endl;
	
	ratioSecondLargestToLargest = 1/ratioSecondLargestToLargest;
	// to find the largest object avg size
	
	long totalSize = 0;
	
	char buffer[100];
	
	while (inputFile>>fileName) {
		
		string savedFileName = fileName;
	
		// get the input mask file
//		fileName = "input/"+fileName;
		fileName =  inputMaskFileLocation + fileName;

		Image* img = new Image(fileName.c_str());
		int width = img->columns(),height = img->rows();
		Image* imgForFilter;
		sprintf(buffer,"%ix%i",width,height);
		
		// residual image is initialized with black representing not visited.
		residual = new Image(buffer, "black");
		
		imgForFilter = new Image(buffer, "white");
		cout << "Reading "<<savedFileName<<endl;
		cout << "Filter wxh "<<width<<","<<height<<endl;
		shape.clear();
		// find the black background from location (0,0)
		findObj(img, 0, 0, shape, false, false);
		int s = shape.size();
		if (s > 0)
			cout << "black object size is "<<s;
		for (int i=0; i<s; i++) {
			imgForFilter->pixelColor(shape[i].first, shape[i].second, "black");
		}
		
		// store the intermediate file in temp folder under the output location
//		string oFilteredFileName = "output/filtered/temp/"+outputFile;
		string oFilteredFileName =  outputFileLocation +"temp/"+savedFileName;
		
		cout << "Saving the filtered image "<< oFilteredFileName<<endl;
		imgForFilter->write(oFilteredFileName.c_str());

		delete residual;
		/*
		residual = new Image(buffer, "black");
		
		shapeVectors.clear();
		sizeNIndexVector.clear();
		
		// find the two largest object
		int objectCounter = 0;
		for (int x=0; x<width; x++) {
			for (int y=0; y<height; y++) {
				//find the white object now using eight connected neighbours
				shape.clear();
				findObj(imgForFilter, x, y, shape, true, true);
				int s = shape.size();
				if (s>0) {
					shapeVectors.push_back(shape);
					cout << "new object pushed back at position "<<x<<","<<y<<" of size "<<s<<endl;
					pair<int, int> si(s, objectCounter);
					sizeNIndexVector.push_back(si);
					objectCounter++;
				}

			}
		}
		
		// sort the sizes and take the largest to find average largest
		cout<<"shapeVectors size = "<<shapeVectors.size()<<endl;
		
		bubbleSort();
		
		cout <<"Largest object size is "<<sizeNIndexVector[0].first<<endl;
		
		// add the largest size to sum
		totalSize += static_cast<long>(sizeNIndexVector[0].first);
		
		cout << "Current total size is "<<totalSize<<" for object "<<(frameCounter+1)<<endl;
		
		cout << "-----------------------------------------------------------"<<endl;
		*/
		
		frameCounter++;
		
		delete imgForFilter;

//		delete residual;
		
		delete img;
	

	}
	
	inputFile.close();
	
	/*
	avgLargestSize = static_cast<long> (totalSize/frameCounter);
	cout << "Average largest size is "<<avgLargestSize<<endl;
	*/
	
	// previous loop calculates the average largest size
	 
	inputFile.open(argv[1]);
	
	if (inputFile.fail() == true) {
		cout << "Cannot open the input file again"<<endl;
		exit(1);
	}
	
	
	while (inputFile>>fileName) {
		
		//string inputFileName = "output/filtered/temp/"+fileName;
		string inputFileName =  outputFileLocation + "temp/"+fileName;
		
		Image* img = new Image(inputFileName.c_str());
		int width = img->columns();
		int height = img->rows();
		
		outputFile<<"File name is "<<inputFileName<<endl;
		outputFile<<"----------------------------------------------\n";
		
		sprintf(buffer,"%ix%i",width,height);
		
		// residual image is initialized with black representing not visited.
		residual = new Image(buffer, "black");
		
		cout << "Reading "<<inputFileName<<endl;
		cout << "Filter wxh "<<width<<","<<height<<endl;
		
		shapeVectors.clear();
		sizeNIndexVector.clear();

		// find the objects and sort according to size
		int objectCounter = 0;
		for (int x=0; x<width; x++) {
			for (int y=0; y<height; y++) {
				// find the white object using eight connected
				shape.clear();
				findObj(img, x, y, shape, true, true);
				int s = shape.size();
				
				if (s > 0) {
					
					shapeVectors.push_back(shape);
					cout << "New object found at position ("<<x<<","<<y<<") of size "<<s<<endl;
					pair<int, int> si(s, objectCounter);
					sizeNIndexVector.push_back(si);
					objectCounter++;
				
				}
					
			}
		}
		
		cout << "Total object found "<<sizeNIndexVector.size()<<endl;
		
		outputFile<<"Total objects found "<<sizeNIndexVector.size()<<endl;
		
		bubbleSort();
		
		// take the largest object
		double currentLargestSize = static_cast<double>(sizeNIndexVector[0].first);
		
		double secondLargest = 0;
		double ratio = 0;
		if (sizeNIndexVector.size() > 1) {
			
			secondLargest = static_cast<double>(sizeNIndexVector[1].first);
			ratio = secondLargest/currentLargestSize;
			cout << "Ratio is "<<secondLargest<<"/"<<currentLargestSize<<" = "<<ratio<<endl;
			outputFile<<"secondLargest = "<<secondLargest<<"\ncurrentLargest = "<<currentLargestSize<<"\nRatio = "<<ratio<<endl;
			
		}
		
		// find the largest to second largest ratio if it is less than the defined ratio then
		// the objects are single object
				
		int numberOfObjects = 0;
		
		if (sizeNIndexVector.size() == 1) {
			cout << "Frame contains one object\n";
			outputFile << "Frame contains one object\n";
			numberOfObjects = 1;
		}
		else if (ratio <= ratioSecondLargestToLargest ) {
			cout << "Single object calculated in the frame because current ratio = "<<ratio<<" is less than defined ratio = "<<ratioSecondLargestToLargest<<endl;
			outputFile<< "Single object calculated in the frame because current ratio = "<<ratio<<" is less than defined ratio = "<<ratioSecondLargestToLargest<<endl;
			numberOfObjects = 1;
		} else { 
			cout << "Two objects in the frame\n";
			outputFile<<"Two objects in the frame\n";
			numberOfObjects = 2;
		}
		
		cout << "Total object found "<<numberOfObjects<<endl;
		outputFile << "Total object found "<<numberOfObjects<<endl;
		
		Image* imgFinal = new Image(buffer, "black");
				
		for (int n=0; n<numberOfObjects; n++) {
			
			int totalPoints = sizeNIndexVector[n].first;
			
			for (int i=0; i<totalPoints; i++) {
				
				imgFinal->pixelColor(shapeVectors[ sizeNIndexVector[n].second ][i].first, shapeVectors[ sizeNIndexVector[n].second ][i].second, "white");
			}
			

		}
		
		//string finalImageName = "output/filtered/final/"+fileName;
		
		string finalImageName = outputFileLocation + "final/"+fileName;
		
		imgFinal->write( finalImageName.c_str() );
		
		
		
		
		// writing the single in red
		if (numberOfObjects == 1) {
			
			Image* singleObjectFinal = new Image(buffer, "black");
		
			int totalPoints = sizeNIndexVector[0].first;
			
			cout << "Output the single object of size = "<<totalPoints<<endl;
			
			for (int i=0; i<totalPoints; i++) {
				
				singleObjectFinal->pixelColor(shapeVectors[ sizeNIndexVector[0].second ][i].first, shapeVectors[ sizeNIndexVector[0].second ][i].second, "red");
			}
			
			//string singleImageName = "output/filtered/single/"+fileName;
			string singleImageName = outputFileLocation + "single/"+fileName;
			
			singleObjectFinal->write(singleImageName.c_str());
			
			delete singleObjectFinal;
			
		}
		
		outputFile<<"----------------------------------------------------\n";
		
			
		delete img;
		
		delete residual;
		
		delete imgFinal;
		
		
	}
	
	inputFile.close();
	outputFile.close();
	
	return 0;
}


void findObj(Image* img, int x, int y, vector<pair<int,int> > & shape ,bool eightCon, bool colorLookingFor)
{
	assert(residual != NULL);

	if (eightCon == true)
		eightConnObj(img, x, y, shape, colorLookingFor);
	else {
		fourConnObj(img, x, y, shape, colorLookingFor);
	}
}

int barrier = 1000;
void fourConnObj(Image* img, int x, int y, vector<pair<int, int> > & obj, bool color)
{
	int width = img->columns(),height = img->rows();
	
	// boundary violation check
	if ( (x >= (width)) || (x < 0) || (y >= (height) ) || (y < 0) )
		return;
	
	// residualpixel.mono() == true implies it is visited. Otherwise not visited.
	ColorMono residualpixel = ColorMono(residual->pixelColor(x,y));
	// originalpixel.mono() == true implies it is an object pixel. Otherwise it is blank region pixel.
	ColorMono originalpixel = ColorMono(img->pixelColor(x,y));
	
	// If the current pixel is already visited then return
	if (residualpixel.mono() == true)
		return;
	
	// Else if current pixel is not visited and it is black, which means it is not an object pixel; so return
	else if (residualpixel.mono() == false && originalpixel.mono() != color)
		return;
	// If current pixel is not visited and its value is white, which means a new object is found.
	else if (residualpixel.mono() == false && originalpixel.mono() == color) {
		// Save the coordinates of the current pixel into the vector and make the pixel visited in the residual image
		pair<int,int> p;
		p.first = x;
		p.second = y;
		obj.push_back(p);
		
//		if (obj.size() > barrier) {
//			cout<<obj.size()<<endl;
//			barrier = barrier + 1000;
//		}
		// setting the residual image at pixel(x,y) to white.
		residual->pixelColor(x,y, ColorMono(true));
		
		// Recursively call all of it's eight neighbours.
		fourConnObj(img, x+1, y, obj, color);
		fourConnObj(img, x, y-1, obj, color);
		
		fourConnObj(img, x-1, y, obj, color);
		fourConnObj(img, x, y+1, obj, color);
	}
	
}

void eightConnObj(Image* img, int x, int y, vector<pair<int, int> > & obj, bool color)
{
	int width = img->columns(),height = img->rows();
	
	// boundary violation check
	if ( (x >= (width)) || (x < 0) || (y >= (height) ) || (y < 0) )
		return;
	
	// residualpixel.mono() == true implies it is visited. Otherwise not visited.
	ColorMono residualpixel = ColorMono(residual->pixelColor(x,y));
	// originalpixel.mono() == true implies it is an object pixel. Otherwise it is blank region pixel.
	ColorMono originalpixel = ColorMono(img->pixelColor(x,y));
	
	// If the current pixel is already visited then return
	if (residualpixel.mono() == true)
		return;
	
	// Else if current pixel is not visited and it is black, which means it is not an object pixel; so return
	else if (residualpixel.mono() == false && originalpixel.mono() != color)
		return;
	// If current pixel is not visited and its value is white, which means a new object is found.
	else if (residualpixel.mono() == false && originalpixel.mono() == color) {
		// Save the coordinates of the current pixel into the vector and make the pixel visited in the residual image
		pair<int,int> p;
		p.first = x;
		p.second = y;
		obj.push_back(p);

//		if (obj.size() > barrier) {
//			//cout<<obj.size()<<endl;
//			barrier = barrier + 1000;
//		}
		// setting the residual image at pixel(x,y) to white.
		residual->pixelColor(x,y, ColorMono(true));
		
		// Recursively call all of it's eight neighbours.
		eightConnObj(img, x+1, y, obj, color);
		eightConnObj(img, x+1, y-1, obj, color);
		eightConnObj(img, x, y-1, obj, color);
		eightConnObj(img, x-1, y-1, obj, color);
		
		eightConnObj(img, x-1, y, obj, color);
		eightConnObj(img, x-1, y+1, obj, color);
		eightConnObj(img, x, y+1, obj, color);
		eightConnObj(img, x+1, y+1, obj, color);		

	}
	
}

// Aspect Ratio
pair<int,int> getCentroid(vector<pair<int,int> > & points)
{
	pair<int,int> centroid;
	centroid.first = 0;
	centroid.second = 0;
	
	for (unsigned int i = 0; i<points.size(); i++)
	{
		centroid.first += points[i].first;
		centroid.second += points[i].second;
	}
	
	centroid.first = roundT(double(centroid.first)/points.size());
	centroid.second = roundT(double(centroid.second)/points.size());
	
	return centroid;
}


vector<double> covariantDecomposition(vector<pair<int,int> > & points)
{
	unsigned int i,j,k;
	pair<int,int> centroid = getCentroid(points);
	vector<double> retval;
	
	gsl_matrix* matrice = gsl_matrix_alloc(2, 2);
	
	double sumX2 = 0, sumXY = 0, sumY2 = 0;
	for (k = 0; k<points.size(); k++)
	{
		sumX2 += pow(double(points[k].first - centroid.first),2.0);
		sumY2 += pow(double(points[k].second - centroid.second),2.0);
		// should we take the absolute value of X*Y
		sumXY += (points[k].first - centroid.first) * (points[k].second - centroid.second);
	}
	gsl_matrix_set(matrice, 0, 0, roundT(sumX2/points.size()));
	gsl_matrix_set(matrice, 0, 1, roundT(sumXY/points.size()));
	gsl_matrix_set(matrice, 1, 0, roundT(sumXY/points.size()));
	gsl_matrix_set(matrice, 1, 1, roundT(sumY2/points.size()));
	
	//	outputMatrix("Covariant", matrice);
	
	// This function allocates a workspace for computing eigenvalues of n-by-n
	// real symmetric matrices. The size of the workspace is O(2n).
	gsl_eigen_symmv_workspace* eigenSpace = gsl_eigen_symmv_alloc(2);
	gsl_vector* eigenVal = gsl_vector_alloc(2);
	gsl_matrix* eigenVec = gsl_matrix_alloc(2, 2);
	// This function computes the eigenvalues and eigenvectors of the real
	// symmetric matrix A. Additional workspace of the appropriate size must be
	// provided in w. The diagonal and lower triangular part of A are destroyed
	// during the computation, but the strict upper triangular part is not
	// referenced. The eigenvalues are stored in the vector eval and are unordered.
	// The corresponding eigenvectors are stored in the columns of the matrix evec.
	// For example, the eigenvector in the first column corresponds to the first
	// eigenvalue. The eigenvectors are guaranteed to be mutually orthogonal and
	// normalised to unit magnitude.
	gsl_eigen_symmv (matrice, eigenVal, eigenVec, eigenSpace);
	gsl_eigen_symmv_free (eigenSpace);
	
	gsl_eigen_symmv_sort(eigenVal, eigenVec, GSL_EIGEN_SORT_VAL_ASC);
	
	for (i = 0; i<eigenVal->size; i++)
		retval.push_back(gsl_vector_get(eigenVal, i));
	
	for (j = 0; j<eigenVec->size2; j++)
		for (i = 0; i<eigenVec->size1; i++)
			retval.push_back(gsl_matrix_get(eigenVec, i, j));
	
	retval.push_back(static_cast<double>(centroid.first));
	retval.push_back(static_cast<double> (centroid.second));
	
//	for (i=0; i<2; i++) {
//		gsl_vector_view evec_i = gsl_matrix_column (eigenVec, i);
//		//printf ("eigenvalue = %g\n", eval_i);
//		cout<<"eigenvector = \n";
//		gsl_vector_fprintf (stdout, &evec_i.vector, "%g");
//	}
	
	gsl_vector_free(eigenVal);
	gsl_matrix_free(matrice);
	gsl_matrix_free(eigenVec);
	
	return retval;
}

// isInterface for binary image
bool isInterface(Image* orig, unsigned int x, unsigned int y)
{
	// Get the current pixel's color
	ColorMono currentpixel = (ColorMono)orig->pixelColor(x,y);
	// If the current pixel is black pixel then it is not boundary pixel
	// error check
	if (currentpixel.mono() == false)
		return false;
	
	// If the current pixel is not black then it is white. So, now we need
	// to check whether any four of its neighbor pixels (left, top, right,
	// bottom ) is black. If any of this neighbor is black then current
	// pixel is a neighbor pixel. Otherwise current pixel is not neighbor
	// pixel.
	
	ColorMono leftneighborpixel = (ColorMono)orig->pixelColor(x-1,y);
	ColorMono topneighborpixel = (ColorMono)orig->pixelColor(x,y-1);
	ColorMono rightneighborpixel = (ColorMono)orig->pixelColor(x+1,y);
	ColorMono bottomneighborpixel = (ColorMono)orig->pixelColor(x,y+1);
	
	// If leftneighborpixel is black and currentpixel is white then it is
	// boundary pixel
	if ( leftneighborpixel.mono() != currentpixel.mono())
		return true;
	// If topneighborpixel is black and currentpixel is white then it is
	// boundary pixel
	else if (topneighborpixel.mono() != currentpixel.mono())
		return true;
	// If rightneighborpixel is black and currentpixel is white then it
	// is boundary pixel
	else if (rightneighborpixel.mono() != currentpixel.mono())
		return true;
	// If bottomneighborpixel is black and currentpixel is white then it
	// is boundary pixel
	else if (bottomneighborpixel.mono() != currentpixel.mono())
		return true;
	// Else all of its neighbor pixels are white so it can not be a
	// boundary pixel
	else
		return false;
	
}