Solved

Convert a 24bit video in a 8bit video ?

Posted on 1998-07-16
3
770 Views
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?
0
Comment
Question by:jceline
  • 2
3 Comments
 
LVL 2

Expert Comment

by:Slarti
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.
0
 

Author Comment

by:jceline
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...

0
 
LVL 2

Accepted Solution

by:
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.

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

776 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