hi, im currently doing image processing for medical imaging under histogram equalization. so right now i have done quite abit and need help on the plotting of histogram. my supervisor told me that i need a for loop[ to identify which intensity value has the most pixel number. im dealing with an 8-bit image ie (0-255 intensity values). my codes is posted below. i will be glad if some help is given. thanks alot in adv.
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;
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;
pDC = GetDC(); //OnDraw member function
hdc = pDC->GetSafeHdc();
CPen aPen, greenPen;
aPen.CreatePen(PS_SOLID, 2, RGB(255, 225, 225));
greenPen.CreatePen(PS_SOLID, 1, RGB(124, 252, 0));
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
//'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;
}
if (no_of_rows == 512 && no_of_cols == 512)
{
//'for' loop to loop through the bytes of the image
for (i=0; i<=262144; i++)
{
array1[image[i]]++;
}
}
else if (no_of_rows == 256 && no_of_cols == 256)
{
for (i=0; i<=65536; i++)
{
array1[image256[i]]++;
}
}
CPen* gOldPen = pDC->SelectObject(&greenPen); //green colour pen for histogram lines
//starting of histogram drawing (intensity vs pixel value)
pDC->MoveTo( 600, 398);
for (i=1; i<255; i++)
// if(no_of_rows == 512 && no_of_cols == 512)
// {
{
pDC->MoveTo(600+i, 398);
pDC->LineTo(600+i, 400-array1[i]/262144);
}
// }
//else if(no_of_rows == 256 && no_of_cols == 256)
//{
// {
// pDC->MoveTo(600+i, 398);
// pDC->LineTo(600+i, array1[i]);
// }
//}
}
the code line in red is which im not sure yet. but nvm that, right now i need to come up with a for loop to identify which intensity value has the most pixel number. thanks
1) Determine the highest value in the array
lets say array[136] = 123456 is the highest one
2) Divide this highest value by the highest value you'd like to get as result (say you want values 0 to 100.
123456 / 100 = 1234
this result is your scaling factor.
Any value in the array divided by this scaling factor (1234) will yield at most 100.
This is for linear scaling.
If you need logarithmic, exponential, ... scaling the basics are the same, though the calculation of the scaling factor could be a lot more complex.
hi, well yeah ur totally right. but i dont know which array1[i] has the highest number of pixels. so i need to write a set of codes to identify which array1[i] has most number of pixels corresponding to the intensity value. any ideas?
hi, yeah i thought so. might have a mental block yesterday. well here it is:
//find the array location containing most value
int highest=0;
for (int i=0; i<255; i++)
{
if (array1[i] > array1[highest])
highest = i;
}
is it supposed to be the way it is?
and my new encountered problem is i got the histogram BUT its very small. so how do i stretch along the axises? i will post the code and image of it below pls review it and help me out thanks.
Code:
CPen* gOldPen = pDC->SelectObject(&greenPen);//green colour pen for histogram lines
//starting of histogram drawing (intensity vs pixel value)
pDC->MoveTo(600, 398);
for (i=1; i<255; i++)
{
pDC->MoveTo(600+i, 398);
pDC->LineTo(600+i, (398-array1[i]*398/array1[highest]));
{
Last edited by metamofia; October 5th, 2009 at 09:23 PM.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.