from 0 to combat opencv N (3) - an image operation, and the operation of the pixel contrast conversion

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

from 0 to combat opencv N (3) - an image operation, and the operation of the pixel contrast conversion

1, OpenCV below provides some of the functions used to perform addition, subtraction images.

void add(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray(), int dtype=-1);

void subtract(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray(), int dtype=-1);

void multiply(InputArray src1, InputArray src2, OutputArray dst, double scale=1, int dtype=-1);

void divide(InputArray src1, InputArray src2, OutputArray dst, double scale=1, int dtype=-1);

void divide(double scale, InputArray src2, OutputArray dst, int dtype=-1);

void scaleAdd(InputArray src1, double alpha, InputArray src2, OutputArray dst);

void addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1);

Similarly there are bit operation functions:

void bitwise_and(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray());

void bitwise_or(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray());

void bitwise_xor(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray());

void bitwise_not(InputArray src, OutputArray dst, InputArray mask=noArray());

Commonly used differencing operation is the absolute value of the pixels of the two images.

void absdiff(InputArray src1, InputArray src2, OutputArray dst);

Some of the functions are operating a single image, for example, squared value of each pixel, the square root logarithm.

void sqrt(InputArray src, OutputArray dst);

void pow(InputArray src, double power, OutputArray dst);

void exp(InputArray src, OutputArray dst);

void log(InputArray src, OutputArray dst);

2, operation of image pixels:


int height = src.rows; // 图像高度

int width = src.cols; // 图像宽度(像素为单位)

int step = src.step; // 相邻行的同列点之间的字节数

int channels = src.channels(); // 颜色通道数目 (1,2,3,4)

uchar *data = (uchar *)src.data;



for (int i = 0; i != height; ++i)

    {

        for (int j = 0; j != width; ++j)

        {
    
            for (int k = 0; k != channels; ++k)

            {


                data[i*step + j*channels + k] = 255;


            }

        }

}

or:

for (int i = 0;i < src.rows;i++)

{

    uchar* current_src = src.ptr< uchar>(i);

    uchar* current_dst = dst.ptr< uchar>(i);

    for (int j = 0;j < src.cols;j++)

    {


        if (current_src[j]==0)

        {

        current_dst[j] = 255;

        }


    }

}





 

3, changing the brightness of the image, the contrast ratio is commonly used: histogram equalization, exponential transformation, logarithmic transformation.

Examples of transformations are given below Index:


int gammm(Mat& src,double g, Mat& dst)

{

    Mat table,d;

    for (int i = 0;i < 256;i++)

    {

        table.push_back((int)(255*pow(i / 255.0, g)));

        //cout << (int)(255 * pow(i / 255.0, g)) << " ,";

     }

        LUT(src,table,d); //查表

        d.convertTo(dst,CV_8U);

    return 0;

}

gammm(img, 0.5, dst);

More attention to micro-channel public number: ML_Study

Guess you like

Origin blog.csdn.net/qq_34106574/article/details/93769633