www.pudn.com > GaitRsystem.rar > tifall.cpp


#include  
#include  
#include  
 
#include  
#include "tiffio.h" 
 
 
bool readtif(char *fname, unsigned char *buffer, int &width, int &height); 
 
bool readtif(char *fname, unsigned char *buffer, int &width, int &height) 
{ 
	 TIFF *image; 
 
    int stripSize, stripMax, imageOffset, bufferSize, result, stripCount; 
	 
// Open the TIFF image 
    if ((image = TIFFOpen(fname, "r")) == NULL) 
    { 
		 
      fprintf(stderr, "Could not open incoming image\n"); 
      exit(42); 
    } 
 
 
    // Check that it is of a type that we support 
   
 
     
  if (TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width) == 0) 
    { 
      fprintf(stderr, "Image does not define its width\n"); 
      exit(42); 
    } 
     
 
    // Read in the possibly multiple strips 
    stripSize = TIFFStripSize(image); 
    stripMax = TIFFNumberOfStrips(image); 
    imageOffset = 0; 
 
    bufferSize = TIFFNumberOfStrips(image) *stripSize; 
 
//	HANDLE buf = GlobalAlloc(GHND, bufferSize); 
 
//	buffer = (char *)GlobalLock(buf); 
    if ((buffer = (unsigned char*)malloc(bufferSize)) == NULL) 
    { 
      fprintf(stderr, 
        "Could not allocate enough memory for the uncompressed image\n"); 
      exit(42); 
    } 
 
 
 
    for (stripCount = 0; stripCount < stripMax; stripCount++) 
    { 
      if ((result = TIFFReadEncodedStrip(image, stripCount, buffer + 
        imageOffset, stripSize)) ==  - 1) 
      { 
        fprintf(stderr, "Read error on input strip number %d\n", stripCount); 
        exit(42); 
      } 
      imageOffset += result; 
    } 
        
	height = 160; 
 
 
return true; 
 
} 
int main() 
{ 
 
 
 
	char *fname, *segname; 
  char *buffer, *bkbuffer; 
	int width = 0, height = 0; 
	  
	int j = 0, i = 0, m = 0; 
	TIFF *image; 
 
    int stripSize, stripMax, imageOffset, bufferSize, result, stripCount; 
	 
		TIFF *output; 
 
	int a =0, b = 0; 
	float tmp1 = 1.0, tmp2 = 1.0, fun = 1.0; 
	buffer = ( char *)malloc(160 * 320); 
	bkbuffer = (char *)malloc(160 * 320); 
 
	fname = (char *)malloc(6); 
	segname = (char *)malloc(6); 
 
 
// Open the TIFF image 
    if ((image = TIFFOpen("bk.tif", "r")) == NULL) 
    { 
		 
      fprintf(stderr, "Could not open incoming image\n"); 
      exit(42); 
    } 
 
 
    // Check that it is of a type that we support 
   
 
     
  if (TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width) == 0) 
    { 
      fprintf(stderr, "Image does not define its width\n"); 
      exit(42); 
    } 
     
 
    // Read in the possibly multiple strips 
    stripSize = TIFFStripSize(image); 
    stripMax = TIFFNumberOfStrips(image); 
    imageOffset = 0; 
 
    bufferSize = TIFFNumberOfStrips(image) *stripSize; 
 
//	HANDLE buf = GlobalAlloc(GHND, bufferSize); 
 
//	buffer = (char *)GlobalLock(buf); 
    if ((bkbuffer = (char *)malloc(bufferSize)) == NULL) 
    { 
      fprintf(stderr, 
        "Could not allocate enough memory for the uncompressed image\n"); 
      exit(42); 
    } 
 
 
 
    for (stripCount = 0; stripCount < stripMax; stripCount++) 
    { 
      if ((result = TIFFReadEncodedStrip(image, stripCount, buffer + 
        imageOffset, stripSize)) ==  - 1) 
      { 
        fprintf(stderr, "Read error on input strip number %d\n", stripCount); 
        exit(42); 
      } 
      imageOffset += result; 
    } 
        
	height = 160; 
 
 
	for(m = 1; m <= 8; m++) 
	{ 
		fname = itoa(m, fname, 10); 
		fname = strcat(fname, ".tif"); 
 
   
	 
// Open the TIFF image 
    if ((image = TIFFOpen(fname, "r")) == NULL) 
    { 
		 
      fprintf(stderr, "Could not open incoming image\n"); 
      exit(42); 
    } 
 
 
    // Check that it is of a type that we support 
   
 
     
  if (TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width) == 0) 
    { 
      fprintf(stderr, "Image does not define its width\n"); 
      exit(42); 
    } 
     
 
    // Read in the possibly multiple strips 
    stripSize = TIFFStripSize(image); 
    stripMax = TIFFNumberOfStrips(image); 
    imageOffset = 0; 
 
    bufferSize = TIFFNumberOfStrips(image) *stripSize; 
 
//	HANDLE buf = GlobalAlloc(GHND, bufferSize); 
 
//	buffer = (char *)GlobalLock(buf); 
    if ((buffer = (char *)malloc(bufferSize)) == NULL) 
    { 
      fprintf(stderr, 
        "Could not allocate enough memory for the uncompressed image\n"); 
      exit(42); 
    } 
 
 
 
    for (stripCount = 0; stripCount < stripMax; stripCount++) 
    { 
      if ((result = TIFFReadEncodedStrip(image, stripCount, buffer + 
        imageOffset, stripSize)) ==  - 1) 
      { 
        fprintf(stderr, "Read error on input strip number %d\n", stripCount); 
        exit(42); 
      } 
      imageOffset += result; 
    } 
        
 
 
 
		for (i = 0; i< height; i ++) 
			for ( j = 0; j < width; j ++) 
			{ 
				 
				a = *(buffer + i * width + j); 
				b = *(bkbuffer + i * width + j); 
				tmp1 = sqrt((a + 1) * (b + 1)) * sqrt((256 - a) *(256 - b)) ; 
				tmp2 = ((a + b + 2) * (512 - a - b)); 
				fun = 1 - tmp1 / tmp2;  
				if(fun < 0.7515) 
					*(buffer + i * width + j) = 0; 
				else 
					*(buffer + i * width + j) = 255; 
			} 
			 
			 
/*	for (j = 0; j < height; j ++) 
		for ( i = 0; i < width; i ++) 
			if(abs(*(buffer + j * width + i) - *(bkbuffer + j * width + i)) < 180) 
			*(buffer + j * width + i) = 0; 
			else  
			*(buffer + j * width + i) = 255;   
 
*/ 
 
		segname = itoa(m, segname, 10); 
		segname = strcat(segname, "seg.tif"); 
  
if ((output = TIFFOpen(segname, "w")) == NULL) 
  { 
    fprintf(stderr, "Could not open outgoing image\n"); 
    exit(42); 
  } 
 
  // Write the tiff tags to the file 
  TIFFSetField(output, TIFFTAG_IMAGEWIDTH, width); 
  TIFFSetField(output, TIFFTAG_IMAGELENGTH, height); 
  TIFFSetField(output, TIFFTAG_COMPRESSION, 1); 
  TIFFSetField(output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); 
  TIFFSetField(output, TIFFTAG_PHOTOMETRIC, 1); 
  TIFFSetField(output, TIFFTAG_BITSPERSAMPLE, 8); 
  TIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, 1); 
 
  // Actually write the image 
  if (TIFFWriteEncodedStrip(output, 0, buffer, width * height) == 0) 
  { 
    fprintf(stderr, "Could not write image\n"); 
    exit(42); 
  } 
 
 
  TIFFClose(output);   
	} 
   
  delete(buffer); 
  delete(bkbuffer); 
 // delete(count); 
 buffer = NULL; 
  bkbuffer = NULL; 
 // count = NULL; 
  return 1; 
} 
 
 
 
 
#include  
#include  
#include  
#include "../tmp/pgm.h" 
#include  
#include "../tmp/tiffio.h" 
#include "../tmp/queue.h" 
 
typedef struct PIXEL{ 
	int x; 
	int y; 
}pixel; 
 
int main() 
{ 
	PGM pgm; 
	 
	 
	 
	pgm.Load("bk.pgm"); 
	 
	unsigned char *data; 
	unsigned char *dataseg; 
	int width, height; 
	width = pgm.width; 
	height = pgm.height; 
	data = ( unsigned char *)malloc(width * height); 
	dataseg = (unsigned char *)malloc(width * height); 
	int j, i; 
	 
	for (j = 0; j < height; j ++) 
		for ( i = 0; i < width; i ++) 
			*(data + j * width + i) = pgm.data[j][i]; 
		 
		 
			/*		 
			PGM pgm0; 
			pgm0.Load("g11.0054.pgm"); 
			for (j = 0; j < height; j ++) 
			for ( i = 0; i < width; i ++) 
			if(abs( pgm0.data[j][i]- *(data + j * width + i)) < 8) 
			*(dataseg + j * width + i) = 0; 
			else  
			*(dataseg + j * width + i) = 255;   
		*/ 
		 
		PGM pgm0[10]; 
		int a, b; 
		float fun; 
		float tmp1, tmp2; 
		char *fname, *segname; 
		 
		TIFF *output; 
		 
		fname = (char *)malloc(6); 
		segname = (char *)malloc(6); 
		for(int m = 1; m <=10; m ++) 
		{ 
			fname = itoa(m, fname, 10); 
			fname = strcat(fname, ".pgm"); 
			 
			pgm0[m - 1].Load(fname); 
			for (i = 0; i< height; i ++) 
				for ( j = 0; j < width; j ++) 
				{ 
					 
					a = pgm0[m -1].data[i][j]; 
					b = *(data + i * width + j); 
					tmp1 = sqrt((a + 1) * (b + 1)) * sqrt((256 - a) *(256 - b)) ; 
					tmp2 = ((a + b + 2) * (512 - a - b)); 
					fun = 1 - tmp1 / tmp2;  
					if(fun < 0.75029) 
						*(dataseg + i * width + j) = 0; 
					else 
						*(dataseg + i * width + j) = 255; 
				} 
				 
				pgm0[m - 1].~PGM(); 
				 
	///Á¬½ÓÈ¥³ýСÔëÉù			 
			    int *count; 
				int label[160][320]; 
				int index = 0; 
				count = (int *)malloc(100); 
				for( i = 0; i < 160; i ++) 
					for(j = 0; j < 320; j ++) 
						label[ i ][ j ] = 0; 
					 
				 
							 
							 
						Queue queue(1000); 
						PIXEL tmp, dtmp; 
						//Label connecting  
						for( i = 1; i < height - 1; i ++) 
							for(j = 1; j < width - 1; j ++) 
							{ 
								if(*(dataseg + i * width + j) == 255 && label[ i ][ j ] ==0) //it is foreground 
								{ 
									 
									index ++; 
									//add to queue 
									if(!queue.IsFull()) 
									{ 
										tmp.x = i; 
										tmp.y = j; 
										queue.Add(tmp); 
										label[tmp.x][tmp.y] = index; 
									} 
									while(!queue.IsEmpty()) 
									{ 
										queue.Delete(dtmp); 
										 
										// eight direction 
										 
										if(dtmp.x >=1 && dtmp.y >=1){ 
											 
											if(*(dataseg + (dtmp.x - 1)  * width + dtmp.y - 1) == 255 && label[ dtmp.x - 1 ][ dtmp.y - 1] ==0) 
											{ 
												tmp.x = dtmp.x - 1; 
												tmp.y = dtmp.y - 1; 
												queue.Add(tmp); 
												label[tmp.x][tmp.y] = index; 
											} 
											if(*(dataseg + (dtmp.x - 1) * width + dtmp.y) == 255 && label[ dtmp.x - 1 ][ dtmp.y ] ==0) 
											{ 
												tmp.x = dtmp.x - 1; 
												tmp.y = dtmp.y; 
												queue.Add(tmp); 
												label[tmp.x][tmp.y] = index ; 
											} 
											 
											if(*(dataseg + (dtmp.x - 1 )  * width + dtmp.y + 1) == 255 && label[ dtmp.x - 1 ][ dtmp.y + 1] ==0) 
											{ 
												tmp.x = dtmp.x - 1; 
												tmp.y = dtmp.y + 1; 
												queue.Add(tmp); 
												label[tmp.x][tmp.y] = index; 
											} 
											if(*(dataseg + dtmp.x  *  width + dtmp.y - 1) == 255 && label[ dtmp.x ][ dtmp.y - 1 ] ==0) 
											{ 
												tmp.x = dtmp.x; 
												tmp.y = dtmp.y - 1; 
												queue.Add(tmp); 
												label[tmp.x][tmp.y] = index; 
											}	 
											if(*(dataseg + dtmp.x  * width + dtmp.y + 1) == 255 && label[ dtmp.x ][ dtmp.y + 1] ==0) 
											{ 
												tmp.x = dtmp.x; 
												tmp.y = dtmp.y + 1; 
												queue.Add(tmp); 
												label[tmp.x][tmp.y] = index; 
											} 
											if(*(dataseg + (dtmp.x + 1) * width + dtmp.y) == 255 && label[ dtmp.x + 1 ][ dtmp.y ] ==0) 
											{ 
												tmp.x = dtmp.x + 1; 
												tmp.y = dtmp.y; 
												queue.Add(tmp); 
												label[tmp.x][tmp.y] = index; 
											} 
											if(*(dataseg + (dtmp.x + 1 )  * width + dtmp.y - 1) == 255 && label[ dtmp.x + 1 ][ dtmp.y - 1] ==0) 
											{ 
												tmp.x = dtmp.x + 1; 
												tmp.y = dtmp.y - 1; 
												queue.Add(tmp); 
												label[tmp.x][tmp.y] = index; 
											} 
											if(*(dataseg + (dtmp.x + 1 )  * width + dtmp.y + 1) == 255 && label[ dtmp.x + 1 ][ dtmp.y + 1] ==0) 
											{ 
												tmp.x = dtmp.x + 1; 
												tmp.y = dtmp.y + 1; 
												queue.Add(tmp); 
												label[tmp.x][tmp.y] = index; 
											} 
										} 
									}//end while 
								}//end if  
							}//end for  
							 
							for(i = 0; i <= index; i++) 
								count[ i ] = 0; 
							 
							for(i = 0; i < height; i ++) 
								for(j = 0; j < width; j ++) 
								{ 
									if(label[ i ][ j ] != 0) 
										count[label[ i ][ j ]] = count[label[ i ][ j ]] + 1; 
								} 
								 
								 
								int max = 0, maxindex = 0; 
								for(i = 0; i <= index; i++) 
								{ 
									 
									if(count[ i ] > max) 
									{ 
										max = count[i]; 
										maxindex = i; 
									} 
									 
								} 
								 
								for(i = 0; i < height; i ++) 
									for(j = 0; j < width; j ++) 
									{ 
										if(label[ i ][ j ] == maxindex) 
											*(dataseg + i * width + j) = 255; 
										else 
											*(dataseg + i * width + j) = 0; 
									} 
								 
									 
									segname = itoa(m, segname, 10); 
									segname = strcat(segname, "con.tif"); 
									 
									 
									 
									if ((output = TIFFOpen(segname, "w")) == NULL) 
									{ 
										fprintf(stderr, "Could not open outgoing image\n"); 
										exit(42); 
									} 
									 
									TIFFSetField(output, TIFFTAG_IMAGEWIDTH, width); 
									TIFFSetField(output, TIFFTAG_IMAGELENGTH, height); 
									TIFFSetField(output, TIFFTAG_COMPRESSION, 1); 
									TIFFSetField(output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); 
									TIFFSetField(output, TIFFTAG_PHOTOMETRIC, 1); 
									TIFFSetField(output, TIFFTAG_BITSPERSAMPLE, 8); 
									TIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, 1); 
									 
									// Actually write the image 
									if (TIFFWriteEncodedStrip(output, 0, dataseg, width * height) == 0) 
									{ 
										fprintf(stderr, "Could not write image\n"); 
										exit(42); 
									} 
									 
									 
									TIFFClose(output); 
		} 
		delete(data); 
		delete(dataseg); 
		// delete(count); 
		data = NULL; 
		dataseg = NULL; 
		// count = NULL; 
		return 1; 
		}