void ColorEnhance::Quantize(Mat &img, int bits_per_channel)//减少每个通道的位深;
{
if (bits_per_channel == 0)
{
bits_per_channel = 6;
}
try
{
if (img.depth()!= 0)
{
throw - 1;
}
int shift = 8 - bits_per_channel;
int halfbin = (1 << shift) >> 1;
int num_rows = img.rows;
int num_channels = img.cols*img.channels();
for (int i = 0;i < num_rows;i++)
{
uchar *data = img.ptr<uchar>(i);
for (int j = 0;j < num_channels;j++)
{
//data[j] = (ceil((float)data[j] / (float)shift))* shift + halfbin;
data[j] = data[j] /shift* shift + halfbin;
}
}
}
catch (int e)
{
if(e==-1)
cout << "Depth of image is not unsinged 8 bit!" << endl;
}
}
代码为本人原创,转载请注明。