Ask Your Question

Revision history [back]

How to change contrast of image?

Hello! I have a problem . I decided to write a function to change the contrast of the image. Original image : RGB 8-bit 1920x1080. Processor of computer , which I use for processing is AMD Phenom II 810 2.6 GHz. To compile in Visual Studio (c + +) I use configuration Release x64. If the algorithm is used as here : http://docs.opencv.org/doc/tutorials/core/basic_linear_transform/basic_linear_transform.html, then the image processing takes 340ms , that's a lot . Next, I decided to write the code in the likeness http://habrahabr.ru/post/139428/ ( taking remarks in the comments ) , the processing time became 240ms , it is lot. Then I tried to use to calculate the "buf ( see article from http://habrahabr.ru/post/139428/) using a thumbnail image of the original ( somewhere in the 10 times ) , the processing time was reduced to 100 ms , but still a lot.

Google doesn't give results about image contrast changing fast algorithm.

How to change contrast of image?

Hello! I have a problem . I decided to write a function to change the contrast of the image. Original image : RGB 8-bit 1920x1080. Processor of computer , which I use for processing is AMD Phenom II 810 2.6 GHz. To compile in Visual Studio (c + +) I use configuration Release x64. If the algorithm is used as here : http://docs.opencv.org/doc/tutorials/core/basic_linear_transform/basic_linear_transform.html, then the image processing takes 340ms , that's a lot . Next, I decided to write the code in the likeness http://habrahabr.ru/post/139428/ ( taking remarks in the comments ) , the processing time became 240ms , it is lot. Then I tried to use to calculate the "buf ( see article from http://habrahabr.ru/post/139428/) using a thumbnail image of the original ( somewhere in the 10 times ) , the processing time was reduced to 100 ms , but still a lot.

Google doesn't give results about image contrast changing fast algorithm.

Stupid rules of this forum doesn't permit to me to publish answer for my question and I put this here:

I found next solution, this takes 35-45ms in my conditions(see above):

Mat ConBright(Mat img, int contrast, int bright){
CV_Assert(img.depth() != sizeof(uchar));

Mat timg;
resize(img, timg, Size(), 0.1, 0.1);
int channels = timg.channels();
int nRows = timg.rows;
int nCols = timg.cols * channels;
unsigned char buf[256];
unsigned int midBright = 0;
uchar* p;
for (int i = 0; i < nRows; ++i)
{
    p = timg.ptr<uchar>(i);
    for (int j = 0; j < nCols; ++j)
    {
        midBright+=p[j];
    }
}
midBright /= (256 * timg.cols*timg.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()));
channels = img.channels();
nRows = img.rows*img.cols * channels;
uchar* po=oimg.data;
p = img.data;
for (int i = 0; i < nRows; ++i)
    *(po++) = buf[*(p++)];
return oimg;

}

How to change contrast of image?

Hello! I have a problem . I decided to write a function to change the contrast of the image. Original image : RGB 8-bit 1920x1080. Processor of computer , which I use for processing is AMD Phenom II 810 2.6 GHz. To compile in Visual Studio (c + +) I use configuration Release x64. If the algorithm is used as here : http://docs.opencv.org/doc/tutorials/core/basic_linear_transform/basic_linear_transform.html, then the image processing takes 340ms , that's a lot . Next, I decided to write the code in the likeness http://habrahabr.ru/post/139428/ ( taking remarks in the comments ) , the processing time became 240ms , it is lot. Then I tried to use to calculate the "buf ( see article from http://habrahabr.ru/post/139428/) using a thumbnail image of the original ( somewhere in the 10 times ) , the processing time was reduced to 100 ms , but still a lot.

Google doesn't give results about image contrast changing fast algorithm.

Stupid rules of this forum doesn't permit to me to publish answer for my question and I put this here:

I found next solution, this takes 35-45ms 35-38ms in my conditions(see above):

Mat ConBright(Mat img, int contrast, int bright){
bright, int dStep=100){
CV_Assert(img.depth() != sizeof(uchar));

Mat timg;
resize(img, timg, Size(), 0.1, 0.1);
int channels = timg.channels();
int nRows = timg.rows;
int nCols = timg.cols * channels;
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;
p=img.data;
for (int i = 0; i < nRows; ++i)
nSize; i+=nStep)
{
    p = timg.ptr<uchar>(i);
    for (int j = 0; j < nCols; ++j)
    {
        midBright+=p[j];
    }
midBright+=*(p++);
}
midBright /= (256 * timg.cols*timg.rows 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()));
channels = img.channels();
nRows = img.rows*img.cols * channels;
uchar* po=oimg.data;
p = img.data;
for (int i = 0; i < nRows; nSize; ++i)
    *(po++) = buf[*(p++)];
return oimg;
}

}

How to change contrast of image?

Hello! I have a problem . I decided to write a function to change the contrast of the image. Original image : RGB 8-bit 1920x1080. Processor of computer , which I use for processing is AMD Phenom II 810 2.6 GHz. To compile in Visual Studio (c + +) I use configuration Release x64. If the algorithm is used as here : http://docs.opencv.org/doc/tutorials/core/basic_linear_transform/basic_linear_transform.html, then the image processing takes 340ms , that's a lot . Next, I decided to write the code in the likeness http://habrahabr.ru/post/139428/ ( taking remarks in the comments ) , the processing time became 240ms , it is lot. Then I tried to use to calculate the "buf ( see article from http://habrahabr.ru/post/139428/) using a thumbnail image of the original ( somewhere in the 10 times ) , the processing time was reduced to 100 ms , but still a lot.

Google doesn't give results about image contrast changing fast algorithm.

Stupid rules of this forum doesn't permit to me to publish answer for my question and I put this here:

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;
}