metamofia's Avatar
Go4Expert Member
hi, the values in image256[i] is from 0-65535. i tried what u asked me to do. what i got is a distorted histogram with some of it go below negative values. i will post the image on the attachment here. anysuggestions?
Attached Images
File Type: jpg clip_image004.jpg (31.2 KB, 4 views)
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
> the values in image256[i] is from 0-65535

That'll be why you get the crash then. array1 is defined as only having 256 elements, so if, for example, image256[N]=4000, then array1[image256[N]]++ will attempt to update array1[4000], which doesn't exist. If image256[i] can contain values from 0-65535 then array1 must be defined unsigned short array1[65536].

The negative values suggest wraparound, i.e. some values of array1[i] exceed the maximum value for unsigned short. What compiler are you using?
metamofia's Avatar
Go4Expert Member
ouh im lost here. array1[i] has 256 element which will represent the pixel intensity from 0-255 yeah? image[i] and image256[i] are variables to represent 512*512 and 256*256 image. if so, how does this clash? im using microsoft visual studio 2005 if thats what ur asking yeah. anyway if possible let me know what codes i should replace with my current set of codes. thanks alot!
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
> array1[i] has 256 element which will represent the pixel intensity from 0-255 yeah?

Well, you're designing the software so you should know what thhe various arrays and variables are for. But as I understand it, no, array1[i] counts the number of pixels at intensity i, so if the original image contains 250 pixels at intensity 17, then array[17] will equal 250.

OK, sorry, I hadn't noticed you had image[] and image256[] as different arrays. Try using sensible names! So with reference to:
Code:
for (i=0; i<262143; i++)
{ 
array1[image[i]]++;
}
what is the range of values that image[i] can contain? And with reference to:
Code:
for (i=0; i<65535; i++)
{
array1[image256[i]]++;
}
what is the range of values that image256[i] can contain?

With reference to both, why are you updating array1 from both image and image256?

In VS2005 I think unsigned short is 2 bytes but check that with sizeof(), or search the help. So this means the maximum value is 65535, but for values over 32767 it is possible these will be considered negative by the WinAPI functions and so scaling is likely to be needed. If you want to map these values to a line from 0-400 pixels long then first multiply array1[i] by 400 (make sure you have enough headroom in the integer type you're using) then divide by the maximum possible value of array1[i].

So for example if array1[i] can contain values from 0-65535, then you should calculate array1[i]*400/65535 and this will scale the value to the correct range. 65535*400=26214000 so make sure you're using integers that can contain values this high (so probably it's worth using 32-bit integers for the scaling calculation).

Prior to plotting, you could scan array1 checking for the maximum value then scale it accordingly, let's say the maximum value is in mx, then the calculation would be array1[i]*400/mx.
metamofia's Avatar
Go4Expert Member
hi, for image[] it should contain 0-262143 and image256[] should have 0-65535. so at ur advise, which set of codes do i need to change? and change to what? yeah my supervisor told me about scaling too. i came up with the equation too but i think i should at least see a HISTOGRAM before i can do any scaling cos i might never know i might be wrong prior to what i have done so far in the codes.
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Hmm...

> for image[] it should contain 0-262143 and image256[] should have 0-65535
Code:
for (i=0; i<262143; i++) { array1[image[i]]++; }
for (i=0; i<65535; i++) { array1[image256[i]]++; }
Seems a bit of a coincidence. Are you answering the question that I asked?

What you're saying is that image[] contains 262143 values that can each be in the range 0-262143, and image256 contains 65535 values that can each be in the range 0-65535.

Or are you saying that image[] contains 262143 values that can be in the range UNKNOWN1-UNKNOWN2, and image256 contains 65535 values that can each be in the range UNKNOWN3-UNKNOWN4?

If the latter please fill in the four unknowns, because that's actually what I'm trying to determine.
metamofia's Avatar
Go4Expert Member
Hi, when you were saying about range, do you mean the numbr of pixels? or the range of intensity value?
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Neither. I'm asking about the numeric values in the array. So if, say, the smallest possible number that can be found in image256 is 23 and the biggest is 76, then the range of image256 is 23-76.
metamofia's Avatar
Go4Expert Member
oh, okay understood. the range for the array image[] is 0-262143 (512*512=262144 numbers) and for array image256[] is 0-65535 (256*256=65536 numbers). i hope this helps. thanks the code below is the latest updated one so u might want to refer to this one and not the rest. thanks alot

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;
unsignedint intMFC;
unsignedint intMFC1;
unsignedint intMFC2;
unsignedshort 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);
}
elseif(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
} 
}
elseif (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); 
}
elseif(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]]++;
}
}
elseif (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, array1[i]/262144);
}
} 
elseif(no_of_rows == 256 && no_of_cols == 256)
{
{
pDC->MoveTo(600+i, 398);
pDC->LineTo(600+i, array1[i]);
}
}
}

as before, the code in red is the one im still unsure about as my supervisor said it should be changed to something else. tried array1[] but gave me distortions. hope u can help me to find a way out of this. thanks.

Last edited by metamofia; 2Oct2009 at 07:58.. Reason: missing code blocks
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
The wording still suggests to me that you're answering the wrong question. Consider the following code:
Code:
int arr[3];
arr[0]=100;
arr[1]=200;
arr[2]=300;
What is the range of the array arr?