# histogram equalization for medical images

Discussion in 'C' started by metamofia, Sep 28, 2009.

1. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
0
Trophy Points:
0
Hmm, i tried my best. Oh, is it 0-3? Or perhaps it's 100-200? Thanks for replying

2. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
0
Trophy Points:
0
Hi! I think i finally got what you meant actually. Okay for image[] it has 262144 values ranging from 0-255. For image256[] it has 65536 values ranging from 0-255. I hope i answered rightly this time. Hope to hear frm u soon.

3. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
Yes, I think you had a mental block before where you assumed that an array with N elements could only contain the values from 0-N.

OK, so this shows that array1 is correctly defined as having 256 elements and that the crash you reported earlier isn't caused by writing past the end of array1.

Looking at the code:
Code:
if(no_of_rows == 512 && no_of_cols == 512)
{
{
pDC->MoveTo(600+i, 398);
pDC->LineTo(600+i, array1[i]/262144);
}
}

(not sure why the double braces), does this display correctly? If so then I'd suggest the red code should be changed to array1/65536.

4. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
0
Trophy Points:
0
hi, i tried the code u given me and i just got a flat horizontal line like the one attached below. well right now, my supervisor wanted me me to do a for loop for finding which intensity value has the most number of pixel
ie:
array1[40] = 5409
array1[300] = 492

so the intensity of 300 has the most pixel value. to do this i have to do the 'for' loop. any idea how about do i start from?

5. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
0
Trophy Points:
0
hi this is the image sorry for mising out on the prev post.

File size:
29.8 KB
Views:
1
6. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
How can array1[300] exist when it is only defined to have 256 elements? I thought we'd settled this question, remember the whole "range of image256[]" discussion?

If you really do have array1[300] then as it is defined unsigned short array1[256] then you have undefined behaviour due to the very common bug of writing past the end of an array.

I don't understand the question. This for loop isn't difficult to write. How far have you got with it and what have you tried? Can you think of an algorithm for how to determine the highest number in an array?

Also you didn't answer a rather important question (note: ALL questions I ask are important, so if you don't understand why I asked something, feel free to ask why I asked and I'll explain): does the "array1/262144" loop display correctly?

7. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
0
Trophy Points:
0
oh sorry it was a typo. i was in a rush bit today. im still trying to forge an algorithm to determine which intensity value has the most number of pixels, so with this i can use proportion to display the histogram within the scale. well i dont really understand what u meant by does the "array1/262144" loop display correctly? do u mean by if there's anything showing up when i put it as array1/262144? as the matter of fact im just trying to play around with the code in red below. thanks for ur help

{
pDC->MoveTo(600+i, 398);
pDC->LineTo(600+i, array/262144);
}

8. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
0
Trophy Points:
0
but as for now, i have been given the task to find the highest value in array1 ie
array1[200]=5000
array1[3]=4003
array[49]=592

so in this case i want my code to identify that array1[200] has the highest number of pixel corresponding to intensity 200 in the image. i never wrote this kind of codes before so im guessing a for loop with a if statement? help me thanks alot.

9. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
What you're asking is spectacularly easy, just think about it for a few minutes. You can do it in two lines of code. This is the problem with the internet, it's too easy to post a question, so you don't bother trying to figure it out for yourself.

> im guessing a for loop with a if statement?

Well, rather than posting your guesses, why not write some code around that guess and see what happens? If it solves the immediate problem then you're sorted. If not, note down what happens so that when you want some code that does that, you've already worked it out.

Don't really know how to express "does it display correctly" in simpler terms. If you don't understand the question then I recommend you invest in a dictionary.

10. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
0
Trophy Points:
0
oh, okay well i was thinking of this:

Code:
int highest=0;
for (int i=0; i<256; i++)
{
if (array1[i] > array1[highest])
highest = i;
}

what do you think? i cant check it if it was right or wrong in my programme cos this dont display anything. and if u were asking about the display of histogram, nope. its distorted when i run it when i input array1/262144.

Last edited: Oct 5, 2009
11. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
You can try it in a testbed. Create a small program that just sets up a test array and runs the above code on it, then displays the value of highest. If that corresponds to the maximum value setup then you can be confident it works.

In what way is the histogram distorted when you use array1/262144? Can you post a screenshot?

12. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
0
Trophy Points:
0
okay, i will post it below pls have a look.

File size:
30.7 KB
Views:
2
13. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
0
Trophy Points:
0
however, i managed to work on that AFTER coming up with:
Code:
[SIZE=2][COLOR=#ffffff][SIZE=2][COLOR=#ffffff]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][COLOR=black]int[/COLOR][/SIZE][/SIZE][SIZE=2][SIZE=2][COLOR=black] highest=0;[/COLOR]
[/SIZE][/SIZE][COLOR=black][SIZE=2][SIZE=2]for[/SIZE][/SIZE][SIZE=2][SIZE=2] ([/SIZE][/SIZE][SIZE=2][SIZE=2]int[/SIZE][/SIZE][/COLOR][SIZE=2][SIZE=2][COLOR=black] i=0; i<256; i++)[/COLOR]
[COLOR=black]{[/COLOR]
[/SIZE][/SIZE][SIZE=2][SIZE=2][COLOR=black]if[/COLOR][/SIZE][/SIZE][SIZE=2][COLOR=#ffffff][SIZE=2][COLOR=#ffffff][COLOR=black] (array1[i] > array1[highest])[/COLOR]
[COLOR=black]highest = i;[/COLOR]
[COLOR=black]}
[/COLOR]

and i managed to edit into this:
Code:
[SIZE=2][COLOR=#ffffff][SIZE=2][COLOR=#ffffff]
[COLOR=black]CPen* gOldPen = pDC->SelectObject(&greenPen);[/COLOR][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][COLOR=black]//green colour pen for histogram lines[/COLOR]
[/SIZE][/SIZE][SIZE=2][SIZE=2][/SIZE][/SIZE][SIZE=2][SIZE=2][COLOR=black]//starting of histogram drawing (intensity vs pixel value)[/COLOR]
[/SIZE][/SIZE][SIZE=2][SIZE=2][COLOR=black]pDC->MoveTo(600, 398);[/COLOR]
[/SIZE][/SIZE][SIZE=2][SIZE=2][COLOR=black]for[/COLOR][/SIZE][/SIZE][SIZE=2][COLOR=#ffffff][SIZE=2][COLOR=#ffffff][COLOR=black] (i=1; i<255; i++)[/COLOR]
[COLOR=black]{ [/COLOR]
[COLOR=black]pDC->MoveTo(600+i, 398);[/COLOR]
[COLOR=black]pDC->LineTo(600+i, array1[i]/262144);[/COLOR]
[COLOR=black]}[/COLOR]
[/COLOR][/SIZE][/COLOR][/SIZE]
and i got a proper histogram but just that its not drawn to scale. any idea why is this so?

[/COLOR][/SIZE][/COLOR][/SIZE]

#### Attached Files:

• ###### clip_image009.jpg
File size:
27.7 KB
Views:
4
Last edited: Oct 6, 2009
14. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
0
Trophy Points:
0
sorry, i mean this code:

Code:

CPen* gOldPen = pDC->SelectObject(&greenPen);
//starting of histogram drawing (intensity vs pixel value)
pDC->MoveTo(600, 398);
for (i=1; i<256; i++)
{
pDC->MoveTo(600+i, 398);
pDC->LineTo[COLOR=red]([/COLOR][COLOR=red]600+i, (398-array1[i]*398/array1[highest]));[/COLOR]
}

that got me the histogram which is not scaled.

Last edited: Oct 6, 2009
15. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
What I do in cases like this, if I can't step through the code in a debugger, is to start writing "debug" information to a log file, showing where in the code it is, and anything relevant (comments, values of variables etc) that might help me find out why the code isn't doing what I expect it to.

So in this case since the code in red presumably isn't working, add an fprintf before that line that writes relevant information out to a file (which you've previously opened with fopen()), perhaps printing i, array, array[highest], the result of the calculation and anything else relevant you can think of. Don't worry that you have to get it all right first time; you won't, and as you go back over the code and the log file you may realise you could use something else from the code, so just add that extra bit in and rerun the program.

As you read the code try to determine what you think the code SHOULD BE doing, and use the log file to determine what the code IS ACTUALLY doing. Then by comparing the two you should be able to work out what is wrong.

16. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
0
Trophy Points:
0
okay where do i have to find to display the debugging information? and where can the log file be found at? thanks

17. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
I don't understand the first question, and the answer to the second is wherever you put the file. It was you that specified the parameters to the fopen() function. Default directory is often the program's current directory, but you can specify an exact location with fopen("c:\\some\\path\\file.txt","w");

18. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
0
Trophy Points:
0
okay thanks