The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Set up a float array and populate
Discuss Set up a float array and populate in the C Programming forum on Dev Shed. Set up a float array and populate C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 4th, 2008, 11:45 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Location: UK
Posts: 116
Time spent in forums: 11 h 10 m 52 sec
Reputation Power: 10
|
|
Set up a float array and populate
can someone advise on the putting this array togther.
basically i created a float array like so
Code:
float Hist_r[2000] = {0.0}; (initialise all bins with 0)
PHP Code:
/* Sets variables. */
data = image->data;
w = image->width;
h = image->height;
// normalisation factor
float norm_f = 1 / (w * h);
float x,y,z, a,b,c;
for ( j = 0; j < h; j++ )
{
for ( i= 0 ; i< w; i++)
{
/* RED */
// dirac unit impulse function
a = (((data[(i + w*(j))])) - ((data[((i-1) + w*(j-1))])));
//normalise a < 1, 1/w*h
a = (norm_f * a);
Hist_r[a]++;
}
}
I would like to create an array or histogram of floating values. is there an optimised way to do this
|

February 4th, 2008, 12:55 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
|
First of all, I don't know how your "image" is implemented, but it sure looks like you're walking off the end of your array in that for loop. (Do you know what's in data[(-1) + w*(-1)]?)
Secondly, if your array Hist_r is counting occurrences of things, then its type should probably be int or unsigned int rather than float (unless you have a good reason for it to be float).
Thirdly, you need to normalize your index to the range [0, 2000), not [0, 1), since your array has 2000 elements. You should also make sure that the index you're using is not less than 0 or greater than or equal to 2000.
|

February 4th, 2008, 04:04 PM
|
|
Contributing User
|
|
Join Date: Apr 2004
Location: UK
Posts: 116
Time spent in forums: 11 h 10 m 52 sec
Reputation Power: 10
|
|
Quote: | Originally Posted by Lux Perpetua First of all, I don't know how your "image" is implemented, but it sure looks like you're walking off the end of your array in that for loop. (Do you know what's in data[(-1) + w*(-1)]?)
Secondly, if your array Hist_r is counting occurrences of things, then its type should probably be int or unsigned int rather than float (unless you have a good reason for it to be float).
Thirdly, you need to normalize your index to the range [0, 2000), not [0, 1), since your array has 2000 elements. You should also make sure that the index you're using is not less than 0 or greater than or equal to 2000. |
firstly : Ok, well spotted. no i don't know what's beyond data[(-1) + w*(-1)]?) its a bad attempt at trying to decipher an equation (1) for histogram function in the pdf (see attachment). except you can advise on what dirac or unit impulse function for the pixel is (eqn 1)? and what the code would look like
secondly: Yes... i am counting occurances... and its not a simple histogram as each bin value or element will be normalised by number of counts for both loops... in this case (resolution = width * height). its more of a probality density function
thirdly: 2000 equates to number of bins for the histogram.
i appreciate your response, but i just don't understand how to translate this equations (1,2 and 3) into c /c++ code
|

February 4th, 2008, 05:35 PM
|
|
|
|
Pardon me for saying so, but you should ask the question you want an answer to. Seems far-fetched, I know.
|

February 5th, 2008, 03:52 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Location: UK
Posts: 116
Time spent in forums: 11 h 10 m 52 sec
Reputation Power: 10
|
|
Quote: | Pardon me for saying so, but you should ask the question you want an answer to. Seems far-fetched, I know. |
ok, here's my question ...
how do you code a dirac impulse function of a pixel colour value in C.
Normal way i'm used to creating a histogram or occurances of red i.e. c = f(i,j) where i,j are vector or set of pixel in an image
image loaded and data is the container, so i have histogram of r,g,b values ..
expressed as
H(i,j)rgb = sum(i).sum(j) [f(i,j)]
Code:
unsigned char * data = (unsigned char*) malloc (3*w*h..blah)
w = 100
h = 75
hist_r[256] = {0};
hist_g[256] = {0};
hist_b[256] = {0};
for (int j = 0; j < h; j++){
for (int i = 0; i < w; i++){
unsigned int r= data[ ((j)*w) + i)];
unsigned int g= data[ ((j)*w) + i)+1];
unsigned int b= data[ ((j)*w) + i)+2];
hist_r [r]++;
hist_g[g]++;
hist_b[b]++;
}
}
now the difference is that i need to get the probablity density function for the r,g,b vlaues of the image such that
the bin values sum to 1.
H(i,j)rgb = (1/w*h) sum(i).sum(j) dirac[f(i,j)]
where d(x) = u(x) - u(x-1). (d(n) = unit impulse function). hence the previous code...with array of 2000 elements containing floats.
Code:
/* Sets variables. */
float hist_r[2000] = {0.0};
w = 100;
h = 75;
// normalisation factor
float norm_f = 1 / (w * h);
float r,g,b;
for ( j = 0; j < h; j++ )
{
for ( i= 0 ; i< w; i++)
{
/* RED */
// dirac unit impulse function
r = abs (((data[(i + w*(j))])) - ((data[((i-1) + w*(j-1))])));
//normalise a < 1, 1/w*h
a = (norm_f * a);
Hist_r[a]++; <--- i can't do this because the element needs to be an integer but i need to create unit impulses of floats.. probability the colour occurrs
}
}
The Dirac delta function d(x) takes the value zero everywhere except when x=0. so i need to populate the histogram with the occurrences of each colour c in the image.
and the finally redefine each bin value according to its ratio with the largest bin value.
|

February 5th, 2008, 04:53 AM
|
 |
Paris est magique!
|
|
Join Date: Apr 2004
Location: France!
|
|
Something I don't get:
You store RGB values in an array. I assume that you use 0..255 values for each color. A commonly used way to store RGB values would be to use, say an int, the first byte being red, then blue, then green, the 4th remains open for transparency, luminance, whatever...
That gives you:
Code:
int aRGB[ w * h];
To index a value in your array:
int col = aRGB[ j*w + i];
// Then address the bytes to get a particular color.
char R = *((char *) &col);
char G = *(((char *) &col) + 1);
char B = *(((char *) &col) + 2);
Or (as it seems you are trying to do):
Code:
const int ELEM_LENGTH = sizeof(int);
char aRBG[ w * h * ELEM_LENGTH];
//To index R,G, B values in your array:
aRGB[ (j * ELEM_LENGTH) * w + (i * ELEM_LENGTH)];
aRGB[ (j * ELEM_LENGTH) * w + (i * ELEM_LENGTH) + 1];
aRGB[ (j * ELEM_LENGTH) * w + (i * ELEM_LENGTH) + 2];
Yet, a cleaner way would be to:
Code:
typedef struct
{
char R;
char G;
char B;
} my_color;
my_color aRGB[ w * h];
// index:
aRBG[ j*w + i].R;
aRBG[ j*w + i].G;
aRBG[ j*w + i].B;
The last solution allows you to change the type of R,G,B components, if you want ot give more precision to a 'bigger' type.
__________________
etienne:~ > %blow
fg: %blow: no such job
There are 10 kind of people:
- those who know binary
- those who don't.
|

February 5th, 2008, 05:17 AM
|
 |
Paris est magique!
|
|
Join Date: Apr 2004
Location: France!
|
|
I am not sure I understand what you are want to achieve. Yet, what do you say from the following?
Code:
#include <math.h>
// You want floats? Let's use floats.
typedef struct
{
float R;
float G;
float B;
} my_color;
// Or is one item' color stored over _one_ float? Needs to be precised!
const int w = 100;
const int h = 75;
// the red part of the data to normalise. Right?
float hist_r[ w * h] = {0.0};
float hist_g[ w * h] = {0.0};
float hist_b[ w * h] = {0.0};
my_color data[ w * h];
float r, g, b;
int iR, iG, iB;
// normalisation factor
float norm_f = 1 / (w * h);
for( j = 0; j < h; j++ )
{
for( i= 0 ; i< w; i++)
{
/* RED component*/
// dirac unit impulse function
// don't get what you do here. How is that a dirac? With what parameter?
r = abs (data[(i + w*(j))].R - data[(i-1) + w*(j-1)].R);
g = abs (data[(i + w*(j))].R - data[(i-1) + w*(j-1)].R);
b = abs (data[(i + w*(j))].R - data[(i-1) + w*(j-1)].R);
// normalise a < 1, 1/w*h
// round it to the closest int.
// you better have r, g, b distributed in [0,w*h]
iR = floor( norm_f * r);
iG = floor( norm_f * g);
iB = floor( norm_f * b);
hist_r[ iR]++;
hist_g[ iG]++;
hist_b[ iB]++;
}
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|