Thread: Image Processing Help Required

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    21

    Image Processing Help Required

    Hi guys,

    i am doin a project in image processing using C at col. Now they have given me somethin called as underlaying of images.

    Does anyone know what underlay is abt?

    tried many sources but couldn get any information.


    Thanks

    Regards

    Harsha

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Well, an overlay is a picture that covers another picture. The underlay is the picture that gets covered. A common example fo underlaying is the tv weatherman. The weather guy (or gal) stands in front of a blue-screen ( its actualyl green, but its called blue-screening). A computer then removes the parts that are that shade of green and overlays the rest onto the CG image of the weathermap. The weathermap would be the underlay.
    Last edited by abachler; 12-22-2007 at 12:09 AM.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    21
    thanks a lot for the reply.

    But sorry to say this....i couldn get exactly the meaning of underlay with the example. Now he stands in front of a green screen right? then the image is processed such that green shade is removed...and then the weathermap is put on to the blue screen is it? and shown to us??

    is this what u meant???

    thanks

    regards

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you have some C code that needs further help, please come on back.

    Right now, you need to be talking about this with your teacher, about this project. We can't do that for you, and you RIGHT NOW, have no clue what the basics of the project are, or what you'll need to do for the project, itself.

    Talk to your teacher, assistant, advisor, other students, and look back over your notes and books, also. You have a lot of catching up to do before you can think about coding.

    I'm not trying to chase you off, I'm just telling you that you're wasting your time trying to get a basic understanding of your project, from this forum. That would be an inefficient use of your time.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    9

    image processing

    I'm also working on image processing now with libtiff and libpng. I may be able to help if you have specific questions.

    I don't know about your project and what they mean by "underlay". You may be working with transparency. An image overlay may have pixel values for three channels = RGB and a fourth channel = transparency. You could have an "underlay" image that would show through the image overlay if the overlay has transparency values.

    I'm only guessing about this project, can you provide more information?

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    21
    Hi

    well i figured out that underlay is nothin but coombining two images such tat both are seen together. i.e we are combinin 2 images and both are to be seen. one image pixel values have to be reduced such tat both are seen.

    i am using imagemagick now but not able to figure out how to reduce the pixel values.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    9
    Ok, sounds like you need RGBA type images. Each pixel has a four values for each of R, G, B, A. RGB are the red, green, blue and A is the Alpha channel for transparency (actually opacity). For an 8-bit per channel image, each of RGBA can have values from 0-255 (because 2^8 = 256). For a solid red pixel you would have RGBA = 255, 0, 0, 0. This means red = 255 green = 0, blue = 0 and opacity = 0.

    If you want to see the image underlay at that pixel location then change the A value to 255 which will make that pixel opaque. An A = 128 value will have 50% opacity and you will see a 50% mix of the overlay and underlay at that pixel location.

    Its really simple when you get the hang of it. The complicated part is dealing with reading the image file from disk and putting the pixel values into a memory allocation for processing.


    You will get better Imagemagick help in the help forum over there. I prefer to work with Libtiff directly. There are library functions for reading the image, here is a simple example:

    Code:
    #include "tiffio.h"
    main()
    {
        TIFF* tif = TIFFOpen("myfile.tif", "r");
        if (tif) {
    	uint32 imagelength;
    	tdata_t buf;
    	uint32 row;
    
    	TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength);
    	buf = _TIFFmalloc(TIFFScanlineSize(tif));
    	for (row = 0; row < imagelength; row++){
    	    TIFFReadScanline(tif, buf, row);
                /*insert row processing stuff here*/
            }
    	_TIFFfree(buf);
    	TIFFClose(tif);
        }
    }
    This will put the rows of pixel data in memory. In the area in the code snippet that says /*insert row processing stuff here*/ you could insert something like:

    Code:
    for(x=0; x< image_width; x++)
    {
          buf[x * 4] = 255;
          buf[(x * 4) + 4] = 128;
    }
    This would change the red value to 255 for every pixel and change the transaprency to 50% for every pixel. Then of course you need to write the image back to a file.
    Last edited by JBull; 12-30-2007 at 11:05 PM.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    21
    Hi

    i have this code where in i can offset an image. by dist specified. Now can anyone tell me how can i read another image and put it on the 1st image such tat both are seen. I need to reduce the intensity of the pixels of the 2nd image so tat i can see the 1st image...this is underlay basically....

    can anyone help me in this


    thanks
    Code:
    #include <stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    //Some important definitions & Constants
    #define _BLUE 0
    #define _GREEN 1
    #define _RED 2
    
    //RGB Quad Structure Declaration
    struct RGBQUADDEF
    {
        unsigned char rgbBlue;
        unsigned char rgbGreen;
        unsigned char rgbRed;
    };
    int Row=0,Col=0,width=0,height=0;
    typedef struct RGBQUADDEF RGBQUAD;
    
    
    struct BMP
    {
    
        //BITMAP FILE HEADER
        short BF_TYPE;				//Specifies type of File (0)
        int BF_SIZE;				//Stores Size of File (2)
        int BF_OFFBITS;				//Specifies offset to Image Data (10)
    
        //BITMAP INFO HEADER
        int BI_SIZE;				//Size of Info Header (14)
        int BI_WIDTH;				//Stores Width of Image (pixels) (18)
        int BI_HEIGHT;				//Stores Height of Image (pixels) (22)
        short BI_BITCOUNT;			//Specifies number of bits per pixel
        int BI_SIZEIMAGE;			//Specifies size of Image Data in bytes
        int BI_CLRUSED;				//Specifies number of colors used in Bitmap
    
        							//END OF BITMAP HEADER;
        char *szFileName;			//File name opened
        int **buffer;				//buufer storing the integer value picked from file
        RGBQUAD *COLORS;			//RGBQUAD structure variable
    
    
    	//Not nesessary BMP Header data but can be used in case of some requirement
        //short BF_RESERVED1;	//Always set to zero (6)
        //short BF_RESERVED2;	//Always set to zero (8)
        //short BI_PLANES;	//Stores Number of planes of target device (26)
        //int BI_COMPRESSION;	//Specifies type of compression
        //int BI_XPELSPERMETER;//Specifies Horizontal pixels per meter (target)
        //int BI_YPELSPERMETER;//Specifies Vertical pixels per meter (target)
        //int BI_CLRIMPORTANT;	//Specifies 'important' color for bitmap
    };
    
    typedef struct BMP BMPObj;
    BMPObj bmpFile;
    
    
    /* FUNCTION WriteBuffer()													*/
    /* PARAMETERS	: Source and Destination FileNames (with Path)				*/
    /* RETURNS	: Number of bytes written										*/
    /* CALLS	: No user defined function										*/
    /* PURPOSE	: To write current buffer to Destination using Source header	*/
    
    void Offset(char szSource[],char szDestination[],BMPObj* obj)
    {
        	//Local Variable Declarations
    	unsigned char Header[54];
       	int iIndex,iBytes=0,jIndex,oldw,oldh,zero=255,xtraw=0,xtrah=0,jIndex1=0;
    	FILE *fSource=NULL;
    	FILE *fTarget=NULL;
    
    	//TODO:DONE
    	fSource = fopen(szSource,"r");
    
    
        	if(fSource == NULL)
        	{
    			exit(0);
        	}
    	//TODO:DONE
    		fTarget = fopen(szDestination,"w");
        	if(fTarget==NULL)
        	{
    			exit(0);
        	}
        	//Copy Header Information
    
    	//TODO:
    /*  if(Row==0)
    	Row = obj->WIDTH;
        if(Col==0)
    	Col = obj->HEIGHT;*/
        
    
    
        //BITMAP INFO HEADER
     oldw = obj->BI_WIDTH;				//Stores Width of Image (pixels) (18)
    oldh =     obj->BI_HEIGHT;
        obj->BI_WIDTH += Row;
    xtraw = (obj->BI_WIDTH%4==0)?0:(4-(obj->BI_WIDTH%4));				//Stores Width of Image (pixels) (18)
    obj->BI_WIDTH += xtraw;
        obj->BI_HEIGHT += Col;
    xtrah = (obj->BI_HEIGHT%4==0)?0:(4-(obj->BI_HEIGHT%4));				//Stores Width of Image (pixels) (18)
    obj->BI_HEIGHT += xtrah;
    				        					//Stores Height of Image (pixels) (22)
    
        obj->BI_SIZEIMAGE = obj->BI_WIDTH * obj->BI_HEIGHT *3;			//Specifies size of Image Data in bytes
     	obj->BF_SIZE =obj->BI_SIZEIMAGE + 54;
    
    //   fseek ( f1, obj->BF_OFFBITS, SEEK_SET ) ;
    		fread ( &Header, 54, 1, fSource ) ;
        	fwrite ( &Header, 54, 1, fTarget ) ;
       fseek ( fTarget, 2, SEEK_SET ) ;
    	fwrite ( &obj->BF_SIZE, sizeof(int), 1, fTarget ) ;
       fseek ( fTarget, 18, SEEK_SET ) ;
    	fwrite ( &obj->BI_WIDTH, sizeof(int), 1, fTarget ) ;
       fseek ( fTarget, 22, SEEK_SET ) ;
    	fwrite ( &obj->BI_HEIGHT, sizeof(int), 1, fTarget ) ;
       fseek ( fTarget, 34, SEEK_SET ) ;
    	fwrite ( &obj->BI_SIZEIMAGE, sizeof(int), 1, fTarget ) ;
    fseek ( fTarget, 54, SEEK_SET ) ;
    printf("old height = %d,old width = %d,new height = %d,new width = %d\n",oldh,oldw,obj->BI_HEIGHT,obj->BI_WIDTH);
    //printf("imagesize=%d,filesize=%d\n",obj->BI_SIZEIMAGE,obj->BF_SIZE);
    
        	iBytes=0;
        	//Copy Buffer to Target/Destination File
    
    		for(iIndex=0;iIndex<oldh;iIndex++)
    		{
    		//	if(iIndex%oldw==0)
    for(jIndex=0; jIndex< Row; jIndex++)
    		{
    	    		//To be written 3 bytes a time for 24-bit images
    		 	fwrite ( &zero, sizeof(char), 1, fTarget ) ;
    		 	fwrite ( &zero, sizeof(char), 1, fTarget ) ;
    		 	fwrite ( &zero, sizeof(char), 1, fTarget ) ;
    		}		
    //		if(iIndex%(oldw+Row)==0)
    		for(jIndex=0; jIndex< oldw; jIndex++){
    	    		//To be written 3 bytes a time for 24-bit images
    		 	fwrite ( (char*)&(obj->buffer[_BLUE][jIndex1]), sizeof(char), 1, fTarget ) ;
    		 	fwrite ( (char*)&(obj->buffer[_GREEN][jIndex1]), sizeof(char), 1, fTarget ) ;
    		 	fwrite ( (char*)&(obj->buffer[_RED][jIndex1]), sizeof(char), 1, fTarget ) ;
    jIndex1++;
    }
    		for(jIndex=0; jIndex< xtraw; jIndex++)
    		{
    
    	    		//To be written 3 bytes a time for 24-bit images
    		 	fwrite ( &zero, sizeof(char), 1, fTarget ) ;
    		 	fwrite ( &zero, sizeof(char), 1, fTarget ) ;
    		 	fwrite ( &zero, sizeof(char), 1, fTarget ) ;
    		}
    
    		}
        	
    	for(iIndex=0; iIndex< (Col * (obj->BI_WIDTH)); iIndex++)
    		{
    	    		//To be written 3 bytes a time for 24-bit images
    		 	fwrite ( &zero, sizeof(char), 1, fTarget ) ;
    		 	fwrite ( &zero, sizeof(char), 1, fTarget ) ;
    		 	fwrite ( &zero, sizeof(char), 1, fTarget ) ;
    		}
    		fclose(fSource);
    		fclose(fTarget);
    
    }
    
    /* FUNCTION: ShowProperties()					*/
    /* PARAMETERS	: NONE						*/
    /* RETURNS	: INTEGER DENOTING SUCCESS			*/
    /* CALLS	: NO USER DEFINED FUNCTIONS			*/
    /* PURPOSE	: DISPLAYS SOME IMPORTANT VALUES		*/
    
    int ShowProperties(BMPObj* obj)
    {
        printf("Properties of File:\n");
        printf("---------- -- -----");
        printf("Type\t\t: %d\n",obj->BF_TYPE);
        printf("Size\t\t: %d\n",obj->BF_SIZE);
        printf("Offset\t\t: %d\n",obj->BF_OFFBITS);
        printf("Info Header size: %d\n",obj->BI_SIZE);
        printf("BitCount\t: %d\n",obj->BI_BITCOUNT);
        printf("Width\t\t: %d pixels\n",obj->BI_WIDTH);
        printf("Height\t\t: %d pixels\n",obj->BI_HEIGHT);
        printf("Image Size\t: %d\n",obj->BI_SIZEIMAGE);
        printf("Colors Used\t: %d\n",obj->BI_CLRUSED);
        return 0;
    }
    
    /* FUNCTION: GetProperties(char [])					*/
    /* PARAMETERS	: CHAR ARRAY						*/
    /* RETURNS	: INTEGER DENOTING SUCESS				*/
    /* CALLS	: NO USER DEFINED FUNCTIONS				*/
    /* PURPOSE	: TO GET IMPORTANT PROPERTIES OF FILE 	*/
    
    void GetProperties(char fileName[],BMPObj* obj)
    {
    	int iIndex=0;
    	FILE *bmpFile=NULL;
    
    	bmpFile= fopen(fileName,"rb");
    
        if(bmpFile == NULL)
        {
    		exit(0);
        }
    
        iIndex=0;
        //Copying Argument Value
    
     //   printf("Stored FileName %s",obj->szFileName);
    
    	fseek ( bmpFile, 0, SEEK_SET ) ;
    	fread ( &(obj->BF_TYPE), sizeof(obj->BF_TYPE), 1, bmpFile ) ;
    	fread ( &(obj->BF_SIZE), sizeof(obj->BF_SIZE), 1, bmpFile ) ;
    
    	fseek ( bmpFile, 10, SEEK_SET ) ;
    	fread ( &(obj->BF_OFFBITS), sizeof(obj->BF_OFFBITS), 1, bmpFile ) ;
    
    	fseek ( bmpFile,18,SEEK_SET) ;
    	fread ( &(obj->BI_WIDTH), sizeof(&obj->BI_WIDTH), 1, bmpFile ) ;
    	fread ( &(obj->BI_HEIGHT), sizeof(obj->BI_HEIGHT), 1, bmpFile ) ;
    
    	fseek ( bmpFile, 28, SEEK_SET ) ;
    	fread ( &(obj->BI_BITCOUNT), sizeof(obj->BI_BITCOUNT), 1, bmpFile );
    
    	fseek ( bmpFile, 34, SEEK_SET) ;
    	fread ( &(obj->BI_SIZEIMAGE), sizeof(obj->BI_SIZEIMAGE), 1, bmpFile ) ;
    
    	fseek ( bmpFile, 46, SEEK_SET ) ;
    	fread ( &(obj->BI_CLRUSED), sizeof(obj->BI_CLRUSED), 1, bmpFile ) ;
    
    	fclose(bmpFile);
    
       // ShowProperties(obj);
    
    }
    
    
    
    
    
    /* FUNCTION: ReadBuffer()					*/
    /* PARAMETERS	: CHAR ARRAY					*/
    /* CALLS	: NO USER DEFINED FUNCTIONS			*/
    /* RETURNS	: INTEGER DENOTING SUCCESS			*/
    /* PURPOSE	: TO FILL IMAGE DATA IN A BUFFER		*/
    int ReadBuffer(char fileName[],BMPObj* obj)
    {
        //Local Variable Declarations
        int jIndex,iIndex=0;		//As Indexing Variables
        int iByteCount=3;		//For varying bitCount
        int  iColor=0;			//Unit of Data being read
    	int iLoopCount=0;
    	FILE *bmpFile=NULL;
    
         	bmpFile = fopen(fileName,"r");
    
    	if(bmpFile == NULL)
        {
    	exit(0);
        }
    
     
        	//Allocation of Pointers to buffer
    	obj->buffer= (int**) malloc(iByteCount);
    
    
        if(obj->buffer == NULL)
        {
    	   exit(0);
        }
    
        	//3 For R,G,B
        	//Allocation of Memory to Pointers
        for(jIndex=0;jIndex<iByteCount;jIndex++)
        {
    		obj->buffer[jIndex] = (int*) malloc(sizeof(int)*obj->BI_HEIGHT*obj->BI_WIDTH);
    
    		if(obj->buffer[jIndex]==NULL)
    		{
    
    		    exit(0);
    		}
    	}
    
        jIndex=0;	iIndex=0;
    
    
    	fseek ( bmpFile, obj->BF_OFFBITS, SEEK_SET) ;
        //Note: Image Data copied as it is, without keeping inversion in mind...
    
    		//printf("\n Width : %d Height %d   =  %d",obj->BI_WIDTH,obj->BI_HEIGHT,(obj->BI_WIDTH*obj->BI_HEIGHT));
    		jIndex=0;
    		while(jIndex!=(obj->BI_WIDTH*obj->BI_HEIGHT))
    		{
    			//TODO:
    			fread ( (char*)&iColor, sizeof(char), 1, bmpFile ) ;
    	    	obj->buffer[_BLUE][jIndex] = iColor;	//BLUE COMPONENT
    
    			fread ( (char*)&iColor, sizeof(char), 1, bmpFile ) ;
    	    	obj->buffer[_GREEN][jIndex]=iColor;		//GREEN COMPONENT
    
    			fread ((char*)&iColor, sizeof(char), 1, bmpFile ) ;
    	    	obj->buffer[_RED][jIndex]=iColor;		//RED COMPONENT
    
    	    	jIndex++;
    	    	iLoopCount++;
    		}
    
    
    
    
    
      	//TODO:DONE
    	fclose(bmpFile);
    
        	//bmpFile.close();
    
        return 0;
    }
    void GetRgnArea(int *Area)
    {
    	*Area = width*height;
    //return 0;
    }
    
    void IsPtInRgn(Row, Col)
    {
    	if(Row<=width && Col<=height)
    	printf("\nSpecified pixel is in the associated Bitmap region");
    	else
    	printf("\nSpecified pixel is not in the associated Bitmap region");
    }
    
    void GetRgnBounds(int *Rect)
    {
    	*Rect = bmpFile.BF_OFFBITS;
    }
    
    int main()
    {
    
    	
    
    	int iLength,m,Area = 0,Rect=0;
    
        printf("\n Enter the bmp file name with the extension .bmp:\n");
    	bmpFile.szFileName = (char*) malloc (100);
    	scanf("%s",bmpFile.szFileName);
    	iLength = strlen(bmpFile.szFileName);
    
    	GetProperties(bmpFile.szFileName,&bmpFile);
    	width = bmpFile.BI_WIDTH;
    	height = bmpFile.BI_HEIGHT;
    	ReadBuffer(bmpFile.szFileName,&bmpFile);
    	while(1){	
    	printf("\nEnter 1 for project 1");
    	printf("\nEnter 2 for project 2");
    	printf("\nEnter 3 for project 3");
    	printf("\nEnter 4 for project 4");
    	printf("\nEnter 5 to exit from the project\n");
    	scanf("%d",&m);
    		
    	switch(m)
    	{
    	case 1:
    	printf("Enter the offset for width,height\n");
    	scanf("%d,%d",&Row,&Col);
    	Offset(bmpFile.szFileName,"changed.bmp",&bmpFile);
    	break;
    	case 2: 
    	GetRgnArea(&Area);
    	printf("The pixels in the given area is : %d\n",Area);
    	break;
    	case 3:
    	GetRgnBounds(&Rect);
    	printf("\n The bounded region in the associated Bitmap region starts from %d\n",Rect);
    	break;
    	case 4:
    	printf("\nEnter the row,column\n");
    	scanf("%d,%d",&Row,&Col);
    	IsPtInRgn(Row, Col);
    	break;
    	case 5:
    	exit(0);	
    	default:break;
    	}
    	}	
    return 0;
    	
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  2. Simple Image Processing
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2009, 12:10 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Replies: 4
    Last Post: 03-02-2003, 09:12 AM
  5. Memory Allocation in Intell Image processing liberary
    By nisar in forum Windows Programming
    Replies: 0
    Last Post: 01-12-2003, 07:29 AM