1 | initial version |
I found next solution, this takes 35-38ms in my conditions(see above):
Mat ConBright(Mat img, int contrast, int bright, int dStep=100){
CV_Assert(img.depth() != sizeof(uchar));
int nSize = img.cols*img.rows*img.channels();
int nStep = img.cols*img.rows / (dStep*dStep)+1;
unsigned char buf[256];
unsigned int midBright = 0;
uchar* p=img.data;
for (int i = 0; i < nSize; i+=nStep)
{
midBright+=*(p++);
}
midBright /= (256 * img.cols*img.rows / 3);
for (size_t i = 0; i < 256; i++)
{
int a = (((i - midBright) * contrast) / 256) + midBright + bright;
if (a < 0) buf[i] = 0;
else if (a > 255) buf[i] = 255;
else buf[i] = a;
}
Mat oimg(img.rows, img.cols, CV_MAKETYPE(img.depth(), img.channels()));
uchar* po=oimg.data;
p = img.data;
for (int i = 0; i < nSize; ++i)
*(po++) = buf[*(p++)];
return oimg;
}
2 | No.2 Revision |
I found next solution, this takes 35-38ms 30-33ms in my conditions(see above):
Mat ConBright(Mat img, int contrast, int bright, int dStep=100){
dStep=200){
CV_Assert(img.depth() != sizeof(uchar));
unsigned int nSize = img.cols*img.rows*img.channels();
unsigned int nStep = img.cols*img.rows / (dStep*dStep)+1;
unsigned char buf[256];
unsigned int midBright = 0;
uchar* p=img.data;
int is = 0;
for (int i = 0; i; i < nSize; i+=nStep)
{
i+=nStep,is++)
midBright+=*(p++);
}
midBright /= (256 * img.cols*img.rows is / 3);
for (size_t i = 0; i < 256; i++)
{
int a = (((i - midBright) * contrast) / 256) + midBright + bright;
if (a < 0) buf[i] = 0;
else if (a > 255) buf[i] = 255;
else buf[i] = a;
}
Mat oimg(img.rows, img.cols, CV_MAKETYPE(img.depth(), img.channels()));
uchar* po=oimg.data;
p = img.data;
for (int i = 0; i < nSize; ++i)
*(po++) = buf[*(p++)];
return oimg;
}