Convert a 24bit video in a 8bit video ?

Posted on 1998-07-16
Last Modified: 2012-06-27
I need to convert the format of the frames given by a digital camera, from 24 bits to 8 bits. So I can use them with
a Java applet. I tried the capGetVideoFormat and the capSetVideoFormat macros given by Visual C++ 5.0, but it doesn't work. How can I do?
Question by:jceline
  • 2

Expert Comment

ID: 1167989
This might not totally answer you question since I am not familiar with video-capture functions, but I can tell you the algorithm for converting 24-bit to palletized (8-bit) color. Seeing as nobody has even left a comment yet although this question is getting old, this might be as good as you can get. However there may be a function which does that, and saves you the trouble of coding the algorithm. The algorithm is rather standard, but I don't think there is any Windows API which does it (I may be wrong).

In short, if you would accept such an answer, leave a comment to that effect. It is not a simple algorithm and I would hate to submit it and then have you fail it.

Author Comment

ID: 1167990
Thank you to answer me. I think I need to code it because I didn't find such a function and I'm in hurry...


Accepted Solution

Slarti earned 200 total points
ID: 1167991
Let me state the problem more clearly: We have a 24-bit RGB picture, and we want to convert it to an 8-bit palettized picture. This means that, of the 16.7 million colors, we need to select 256 colors which are used most often, and use only those colors. Again, this is _not_ a simple process. You can get a feel for the complexity of the algorithm just by looking at the amount of time it takes a program like Photoshop to convert a single 24-bit picture to optimized 8-bit. This process has two stages:

1. Select the palette, i.e. the 256 colors which you will use. There are several ways of doing this, but a relatively simple method which gives pretty good results is as follows.
Look at the 24-bit color set as a three-dimensional space where the coordinates are R, G, B. Every pixel that is used has a certain value, represented as a point in this space. If, for example, this is a picture of the sky, the most of the points will be in the area of high B's and low R's and G's, so we want most of the palette to contain values with high B's and low R's and G's.
The way we do this is divide the 3-space into two separate sets, both of which have roughly the same number of points. We then say that the two sets shall each have 128 colors applied to them, and continue recursively into each of these sets, dividing them into four 64-color sets, and so on. Eventually we reach a very small set to which we need to assign just one color. This set might be, for example, the set R:23-25,G:10-14,B:11-12, in which case we can choose an average value of R:24,G:12,B:11 for the color which will be assigned to this set. So now we have 256 colors and we've assigned them in a fairly intelligent way, because X percent of the pixels in the picture will use up almost exactly X percent of the palette.

This is the most time-consumin part of the calculation. _IF_ this is too complicated for you, you could consider using a "fixed" palette: that is, choose the palette in advance, and go immediately to step 2. However, results will be much poorer. Most pictures with optimized palettes can be distinguished from true 24-bit color pictures only when looking closely at the details, whereas when you see a fixed-palette picture you will immediately recognize that it is lower-quality.

2. Convert all pixels to the nearest color in the palette. The "distance" between two colors can be defined pretty well as abs(r1-r2)+abs(g1-g2)+abs(b1-b2), where abs() is the absolute value function, and (r1,g1,b1) and (r2,g2,b2) are the two colors. What you do is, for a given pixel find the distance between that pixel and all colors in the palette. The color in the palette with minimum distance is the one you want for your pixel.


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Interoperability issue between VC++ (ocx) & VB.NET applications 7 73
c++ syntax question 9 43
Safe conversion? 4 54
gdb doesn't stop on breakpoint 2 45
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now