unclear use of an array more elements then pre defined

oded

i have to following code taken from the complete reference

it about the following syntax:- " hist[y]++; " which appears there its about creating histogram of brightness values of img.

i wonder what it means in the code since hist[] is defined as int[256] , while the itteration here is much larger then 256.

int hist[] = new int[256];   <<<<<<<<defined here 256 elements

...

for (int i=0; i<iw*ih; i++) {
    int p = pixels[i];
    int r = 0xff & (p >> 16);
    int g = 0xff & (p >> 8);
    int b = 0xff & (p);
    int y = (int) (.33 * r + .56 * g + .11 * b);
    hist[y]++;     <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<here many more elements
}

etc. etc..

aioobe

hist[y]++; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< here many more elements

You're perhaps mixing up the expression hist[y]++ with hist[y++] or confusing y with the loop index i.

The expression hist[y]++ simply increments the value at index y by one. And the index y is set in the line above to

int y = (int) (.33 * r + .56 * g + .11 * b);

Since r, g and b are all <= 255 the maximum value of y is

  .33*255 + .56*255 + .11*255
= (.33 + .56 + .11) * 255
= 1 * 255

so an array of 256 elements will suffice.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is it possible to have number of elements in an array more than array's size which is defined at compile time?

Why does Cython needs more Python calls if elements of NumPy array are defined during array creation?

Sort an array with a pre-defined sort order

Use defined variable in array

How to pre type array of React Elements in typescript

use of double brackets unclear

Unclear use of operator double()

Filter for one or more elements in an array

Highcharter - Having one or more of the grouping elements pre-selected

How to create certain elemental array from a pre-defined array

Insert dynamic numbers (in order) into multiple pre-defined spots in array

F# Create an empty Array of a pre-defined size

How to split string based on pre-defined values from Array

Appending value to empty list or allocating value to pre-defined array?

Populate a javascript array randomly with pre-defined values

Is there an easier/better way to use a pre-defined dictionary?

Want to use <pre> without and css which have defined in css file

Use variables as keywords for functions with pre-defined keywords (Python)

How to use pre-defined destroy method in laravel

Can we use a pre-defined column for CV (resampling) in mlr?

Unable to use pre - launch task more than once in VS Code

Javascript not defined error, when adding elements to array

Checking string defined array elements with a for loop

Is there a user defined typeguard for requiring elements in an array?

Aggregate the elements of an array to not be larger than a defined value

Unclear use of (void) with register value

Unclear how to use takewhile python

Array allocates space for more elements than requested

Check an array if there are two or more of same elements in a row