[Closed] Help with Histogram
Hello everybody, could anyone tell what i am doing wrong ? I get a error in line 24, the error that i get says something about the (–unknown property: “r” in undefined). Please, I will be very grateful, if anyone could give me a hand.
Thank in advance
ting
1 fn getHistogram b =
2 (
3 icount = #()
4 rcount = #()
5 gcount = #()
6 bcount = #()
7
8 for t=1 to 256 do
9 (
10 icount[t] = 0
11 rcount[t] = 0
12 gcount[t] = 0
13 bcount[t] = 0
14 )
15
16 for i=0 to b.width do
17 (
18 for j=0 to b.height do
19 (
20 /* Get a pixel */
21 p = getPixels b [i,j] 1
22
23 /* Count the ill. */
24 icount[(p[1].r + p[1].g + p[1].b)/3] = icount[(p[1].r + p[1].g + p[1].b)/3] + 1
25
26 /* Count the red component */
27 if (p[1].r as integer) < 1 then itemp = 1
28 else itemp = p[1].r as integer
29 rcount[itemp] = rcount[itemp] + 1
30
31 /* Count the green component */
32 if (p[1].g as integer) < 1 then itemp = 1
33 else itemp = p[1].g as integer
34 gcount[itemp] = gcount[itemp] + 1
35
36 /* Count the blue component */
37 if (p[1].b as integer) < 1 then itemp = 1
38 else itemp = p[1].b as integer
39 bcount[itemp] = bcount[itemp] + 1
40 )
41 )
42 )
Without running your code, I would say you are counting incorrectly – a bitmap of width someWidth and height someHeight has pixels from 0 to (someWidth-1) and height from 0 to (someHeight-1). If you count from 0 to someHeight and get someWidth pixels, the last pixel in each line will be undefined, and the last line will be completely undefined.
Oh, and if you read the “How To Make It Faster” tips I gave in the Frequently Asked Questions section of the MAXScript Reference, you will see that it is a very bad idea to get a single pixel in a getPixels() call, when you can get the whole line and operate on the resulting array. It is ORDERS OF MAGNITUDE faster, because getPixels() is really slow. If your image is 800×600 pixels, you are calling the function 480,000 times. When getting a full line, you are calling the function just 600 times, or 800 times less!!!