- Online Community Forums » Programming Questions » interpreting images produced by a spectroscope

This thread is locked; no one can reply to it. rss feed Print
interpreting images produced by a spectroscope
Member #9,488
February 2008

for my school physics essay i have produced a number of images with a spectroscope
now i need a program to interpret them by extracting data from each pixel about the intensity of light at that point. to illustrate my question:
is there any software that would do it for me?
if not i could try to write the program myself but I am not sure how to go about it.
any advice is welcome :)

Thomas Harte
Member #33
April 2000

I'm a bit confused, are you asking how to take an image like this:


And read the values off the graph into your own program? Do you know the size of the scale in advance? If not, is it too much effort for you to enter scales and have the program do the rest? And are your graphs coloured the same as the one shown here?

If so, it should be a relatively easy task of scanning around using getpixel, looking for the first line with a red pixel in it, then on that and each subsequent column scanning from the top to find the first red pixel, then down to the first black pixel after that to work out the height.

Then you know the height at that point (i.e. distance along y) and the number of columns across (i.e. distance along x), so if you know the scale then you can get the information you want.

Member #5,313
December 2004

I assume he wants to interpret these:


With the intention of producing such a graph.

Thomas Harte
Member #33
April 2000

I'm a science thicky — is the graph just the intensity of each column of that?

If so, then try this to convert an (r,g,b) triplet to an intensity:

/* GetIntensity returns a value between 0 (black) and 1 (brightest) */
float GetIntensity(int r, int g, int b)
  return 0.299f*((float)r / 255.0f) + 0.587f*((float)g / 255.0f) + 0.114f*((float)b / 255.0f);

EDIT: that's the CCIR 601 standard conversion from R, G, B to Y, U, V but with the U and V missing. It's the way that colour television is encoded — brightness is one channel, colour information is in two other channels. That's why colour TV transmissions can be picked up and displayed by black and white televisions that were invented before colour television.

Member #9,752
May 2008


I assume he wants to interpret these:
With the intention of producing such a graph.

I would assume that as well. Your best bet is to write the program yourself, just convert whatever formulas you have into code. It's definitely possible to create a graph like that from pixel information alone, seeing as the person who made that website did just that.
As an added bonus, you would be able to check the output graph of your program against the ones on that website to see if they're right!

[unTied Games]

Matthew Leverton
Supreme Loser
January 1999

I ran one test, and TH's code provides fairly similar graphs. The graphs on the website are a bit more exaggerated than what Open Office gave me, but that could be due to it trying to smooth out the lines.

Member #9,488
February 2008

thanks everyone, I'll try that as soon as possible

Thomas: what is the algorithm you are using to convert the colours? why are the constants the way they are? and why exactly R,G and B are in the range from 0 to 1 not from 0 to 256? i tried to look for CCIR 601 but all i found was some cumbersome theoretical information and this

Y  =      (0.257 * R) + (0.504 * G) + (0.098 * B) + 16

which has different constants

Member #2,030
March 2002


why are the constants the way they are?

Because of the frequency response of the average human eye.

Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Member #9,488
February 2008

hmmm... i tried it on two pics. one went fine but the other returned 1 for every pixel.
what could be wrong?

sorry for being such a noob... but what is wrong with this code? i changed something and now all i get is a black non responsive window and i cant get back :D
apparently its too late for programming

1#include <allegro.h>
2#include <fstream>
3#include <iostream>
4using namespace std;
6void init();
7void deinit();
9float getIntensity(int r, int g, int b)
11 return 0.299f*((float)r / 255.0f) + 0.587f*((float)g / 255.0f) + 0.114f*((float)b / 255.0f);
14int main() {
15 init();
17 ofstream output_file;
18 ("output.txt");
20 int r, g, b, color_value;
21 float intensity;
22 BITMAP* spectrum;
23 //char filename[30];
24 //cin>>filename;
25 //cin.get();
26 spectrum=load_bitmap("spectrum.bmp",0);
27 int j;
28 cin>>j;
29 cin.get();
30// for(int j=0;spectrum->h; j++) {
31 for(int i=0;spectrum->w; i++) {
33 color_value = getpixel(spectrum, i, j);
34 r = getr(color_value);
35 g = getg(color_value);
36 b = getb(color_value);
38 intensity=getIntensity(r,g,b);
40 output_file<<intensity;
41 output_file<<"\n";
42 }
44 output_file<<"\n";
46// }
47 output_file.close();
48 deinit();
49 return 0;
53void init() {
54 int depth, res;
55 allegro_init();
56 depth = desktop_color_depth();
57 if (depth == 0) depth = 32;
58 set_color_depth(depth);
59 res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
60 if (res != 0) {
61 allegro_message(allegro_error);
62 exit(-1);
63 }
65 install_timer();
66 install_keyboard();
70void deinit() {
71 clear_keybuf();

apparently its too late for programming... good night

Thomas Harte
Member #33
April 2000

Sorry, I'm a C++ thickie, shouldn't:


cause your program to wait for the user to enter something? Can anyone else confirm or deny that it is likely to work with Allegro/Windows progerams?

Member #2,028
March 2002

White space isn't read in by >>, so it leaves the '\n' in the stream, which you need to remove. :)

"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

Member #9,488
February 2008

the problem is that even the terminal/command line doesnt show up so i cant enter anything... all i get is a black window that can only be killed by killing the process

EDIT: never mind, I got it to work

Go to: