CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2009
    Posts
    17

    Coming up with the histrogram of medical images

    Hi, im doing histogram equalization as my final year project. as for now i need to come up with just the histogram for the image i will be opening. im using mfc and have done a few codes to my project. the recent update was me writing the codes to calculate every pixel's value within the image. so what i want to know now is how do i draw the histogram, ie the values of the intensity corresponding to the total pixel numbers. this is my code below:

    Code:
    //**************************************************************************************//
    
    void CLEO_MedivisionView::OnUpdateToolsHistogram(CCmdUI *pCmdUI)
    {
    	// TODO: Add your command update UI handler code here
    			
    	if (fileopen) {
    		pCmdUI->Enable(true);
    	}
    	else {
    		pCmdUI->Enable(false);
    	}
    }
    void CLEO_MedivisionView::OnToolsHistogram()
    {
    	// TODO: Add your command handler code here
    	int x_tick, y_tick;
    	int j;
    	int a_loop, b_loop;
    	int k, t, f, i;
    	int x, g;
    	int q;
    	unsigned int intMFC;
    	unsigned int intMFC1;
    	unsigned int intMFC2;
    	unsigned short array1[256];
    	char c[10];
    	char p[10];
    	char l[10];
    	CString MFCString;
    										//unsigned short array1[65535];
    	pDC = GetDC();				    	//OnDraw member function
    	hdc = pDC->GetSafeHdc();
    	CPen aPen;
    	aPen.CreatePen(PS_SOLID, 2, RGB(255, 225, 225));
    	CPen* pOldPen = pDC->SelectObject(&aPen);
    
    
        pDC->MoveTo(600,400);  //drawing of x-axis
    	pDC->LineTo(920,400);
    	pDC->MoveTo(920,400);
    	pDC->LineTo(915,405);
    	pDC->MoveTo(920,400);
    	pDC->LineTo(915,395);  //drawing of x-axis
    	
    	x_tick = 600;		   //declaring constants
    	y_tick = 400;
    
    	for(j=1; j<=10; j++)  //control the loop so that it draws ticks every 30 pixels
    	{
    		pDC->MoveTo(x_tick+(j*30),y_tick-5); 
    		pDC->LineTo(x_tick+(j*30),y_tick+5);	
    	}
    	pDC->MoveTo(600,400);    //drawing of y-axis
    	pDC->LineTo(600,80);
    	pDC->MoveTo(600,80);
    	pDC->LineTo(605,85);
    	pDC->MoveTo(600,80);
    	pDC->LineTo(595,85);	 //drawing of y-axis
    
    	for(t=1; t<=10; t++)	 //control the loop so that it draws ticks every 30 pixels
    	{
    		pDC->MoveTo(x_tick-5,y_tick-(t*30)); 
    		pDC->LineTo(x_tick+5,y_tick-(t*30));
    	}
    
    	intensity= "Intensity";	   //labelling of x-axis
    	SetBkColor(hdc, RGB(0, 0, 0));
    	SetTextColor(hdc, RGB(255, 255 ,255 ));
    	TextOut(hdc, 930, 390, LPCTSTR(intensity), 9);
    
    	pixels = "Pixels";	    //labelling of y-axis
    	SetTextColor(hdc, RGB(255, 255 ,255 ));
    	TextOut(hdc, 580, 55, LPCTSTR(pixels), 6); 
    
    	numeric = "0";         //number zero (universal)
    	SetTextColor(hdc, RGB(95, 158, 160));
    	TextOut(hdc, 585, 400, LPCTSTR(numeric),1);
    
        intMFC = 0;			 //declaring constants
    	a_loop = 595;
    
    	for(k=1; k<=5; k++)	 //for every two ticks, draw legend of +52 each time it loops
    	{	
    		intMFC = intMFC+52;
    	    itoa(intMFC,c,10);
    	    CString MFCString;
    	    MFCString = c;
    		if(intMFC<100)	     //if number is XX then just show 2 digits
    		{
    			test = c;
    			SetTextColor(hdc, RGB(95, 158 ,160));
    			TextOut(hdc, a_loop+(k*60), 405, LPCTSTR(test), 2);
    		}
    		else if(intMFC>=100)  //if number is XXX then show 3 digits
    		{
    			test = c;
    			SetTextColor(hdc, RGB(95, 158, 160));
    			TextOut(hdc, a_loop+(k*60), 405, LPCTSTR(test), 3);
    		}
    
    	}
    
    	intMFC1 = 0;			//declaring of constants
    	intMFC2 = 0;
    	b_loop = 392;
    	
    	if (no_of_rows == 256 && no_of_cols == 256)	 // initiate if image its by 256*256
    	{
    		for(f=1; f<=5; f++)		      // for 5-ticks axis
    		{ 
    		intMFC1 = (((256 * 256)/5)+0.3) * f;  // calculate each of the 5 marking is +13107
    	    itoa(intMFC1,p,10);				  
    	    CString MFCString;
    	    MFCString = p;
    
    		test1 = p;
    		SetTextColor(hdc, RGB(95, 158 ,160));
    		TextOut(hdc, 540, b_loop-(f*60), LPCTSTR(test1), 5);	 // type casting
    		}   
    	}
    
    	else if (no_of_rows == 512 && no_of_cols == 512)  // if image is 512*512 run this
    	{
    		for(q=1; q<=5; q++)    // for 5-ticks axis		                   
    		{   
    			intMFC2 = (((512 * 512)/5)+0.8) * q; // to calculate each marking is +52428
    			itoa(intMFC2,l,10);			   
    		
    			MFCString = l;					 
    			if(intMFC2<100000)	//'if' statement used because if digit is less
    				                //than 100000, it shows crap at the last digit position
    			{
    				test2 = l;
    				SetTextColor(hdc, RGB(95, 158 ,160));
    				TextOut(hdc, 540, b_loop-(q*60), LPCTSTR(test2), 5);	
    			}
    			else if(intMFC2>=100000)	//if digit is more or equal to 100000, do this
    			{
    				test2 = l;
    				SetTextColor(hdc, RGB(95, 158, 160));
    				TextOut(hdc, 540, b_loop-(q*60), LPCTSTR(test2),6);
    			}
    		}
    	}
    
    
    	//count pixel intensity
    	if (no_of_rows == 512 && no_of_cols == 512)
    	{
    	//'for' loop couting from 0 to 255 which simply zeros out each element in array1 to prepare the array to do the count of the bytes in the image
    		for (i=0;i<255;i++) 
    		{
    			array1[i]=0;
    		}
    	//'for' loop to loop through the bytes of the image
    		for (i=0; i<262143; i++)
    		{ 
    		array1[image[i]]++;
    		}
    	}
    
    	else if (no_of_rows == 256 && no_of_cols == 256)
    	{
    		for (g=0;g<255;g++)
    		{
    			array1[g]=0;
    		}
    
    		for (g=0;g<65535; g++)
    		{
    			array1[image256[g]]++;
    		}
    	}
    
    }
    image[i] is a variable for 512*512 image and image256[i] is for 256*256 image. pls do help me in this thanks! and i will upload the jpeg of what my project looks like as of now so u wont get lost in what im doing.
    Attached Images Attached Images

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Coming up with the histrogram of medical images

    You're already drawing the X/Y axis, so adding the histogram itself should be obvious enough, just add the appropriate drawing commands.

    If you're askign how to actually calculate the histogram data, you may be asking the wrong forum, we can help with programming issues, but I'm not sure anyone knows how to actually interpret the data you're handling and how to calculate the stuff you need.

    Maybe we can help with developping it if you can give more detailed information about what exactly you're trying to do.

    I have the vague impression you want to draw a curve from left/right (X-axis) displaying the count of each pixel value ? If yes, that should be something like:

    Code:
    pDC->MoveTo( 600 + 0, 400 - array1[0] );
    for (int x=1; x<_countof(array1); ++x)
    {
        pDC->LineTo( 600 + x, 400 - array1[x] );
    }
    you may need to set pen and color beforehand. and may need to adjust coordinates, but you get the general idea.
    You will likely also need to 'scale' the Y axis down to have it display withing reasonable values. (change 'array1[x]' to be divided by the scaling factor)
    Last edited by OReubens; September 28th, 2009 at 05:17 AM.

  3. #3
    Join Date
    May 2002
    Posts
    1,435

    Re: Coming up with the histrogram of medical images

    Quote Originally Posted by metamofia View Post
    Hi, im doing histogram equalization as my final year project. as for now i need to come up with just the histogram for the image i will be opening. im using mfc and have done a few codes to my project. the recent update was me writing the codes to calculate every pixel's value within the image. so what i want to know now is how do i draw the histogram, ie the values of the intensity corresponding to the total pixel numbers.
    Calculating every pixel's intensity is only the first step. Next you have to generate the actual histogram table and I believe you were already shown how to do that in another thread.

    The histogram table should have 256 elements according to your x axis - and assuming 8-bit greyscale. Those 256 values will represent the x-array values and each value contained in the 256 elements (bins in histogram terminology) will represet the y values.

    You will use the histogram table to generate a BAR CHART. For each of the 256 elements in the array you start at the x-axis and draw a vertical line up to the point that represents the pixel count in the bin. Of course there are going to be scaling issues to deal with - especially after you have done the equalization - so your y-axis range will need to be changed.

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Coming up with the histrogram of medical images

    In addition to the good advice offered by OReubens and 0xC0000005, I'd like to add that, unless there's some unusual circumstance that I'm missing, you probably should not be doing your drawing within the command handler, but rather, you should set a flag in the command handler and then call Invalidate() or InvalidateRect() and handle all the drawing in your OnPaint() (or OnDraw() in a CView derived class) handler.
    Last edited by hoxsiew; September 28th, 2009 at 07:45 AM. Reason: typo

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Coming up with the histrogram of medical images

    As hoxview pointed out
    While not necessarily 'wrong' any painting code typically shouldn't be anywhere other than the OnPaint handler. There's exceptions to the rule of course, but I see no real reason why it shouldn't be the case here.


    One of the parts of testing your program if you do any kind of manual drawing is seeing how your program responds to:
    - Minimize and a following restore
    - Maximize and restore
    - Changing window size manually
    - Move another program over your program and see if everything paints properly.
    - Move your program window so part of it goes outside of the destktop area then drag it back.
    - Always debug with "show window contents while dragging". You can pick up a lot of the bad issues just by doing that.

    Failure to properly update, excessive flickering, or sluggish response are all keys that something is either wrong or very suboptimally implemented.

    Note that both flicker and sluggish response are personal flavouring, some people don't mind, and other are very sensitive to it. Check with your customers if behaviour is acceptable. But depending on the type of problem the type of PC and videocard used, it is possible flicker and/or sluggish behaviour is unavoidable to an extent.

  6. #6
    Join Date
    Sep 2009
    Posts
    19

    Re: Coming up with the histrogram of medical images

    Can you give me the program please ?

    I can make a vow at the time I recieve the program that I must use the program for humanitarian and "right" image processing purposes.

  7. #7
    Join Date
    Sep 2009
    Posts
    17

    Re: Coming up with the histrogram of medical images

    hi, first of all thanks to all of u for replying!

    to OReubens: I have tried inputting the code you have given to me in your reply and what is observed will be attached to my reply. I asked my supervisor an he said it should not be the way. he mentioned about proprtioning which i supposed, lets say if there are 500 pixels with an intensity value of 20, the code should be display array1[20] = (500/262144)*600. what he tried to do here is taking the number of pixels with intensity value 20 divided by the total number of pixels in the image and multiply the range of pixels for my y-axis which is 600 pixels. so i should create a for loop in doing this. but im not too sure how do i declare the variables in referrence to this. and he told me the 2nd step would be the drawing of lines. i have got no idea what he said about that but im really confused. help is much appreciated PS: do view the attachment in this reply in .jpg format thanks


    to 0xC0000005: Yeah that is what im trying to do. I have done with the calculating of each pixel's intensity. and the only way i could see if my codes written for the calculation is right, is to actually set up the vertical lines ie bars. but he said my calculation was correct. so for the case of MoveTo(x,y) and LineTo(x,y) im am confused on what the x and y values will have to be. should it be raw numbers or variables i have used so far. help is needed thanks


    to hoxsiew: Hmm i have not touched all that. I was just introduced to MFC and its functions of VISUAL C++ about a month ago. I have only learnt about C++ and the raw codes. so if u could explain a few stuffs it will be great! thanks in adv
    Attached Images Attached Images
    Last edited by metamofia; September 28th, 2009 at 08:54 PM. Reason: spelling errors

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured