Mask operation of OpenCV learning notes

1, Mask operation

The mask operation of matrix is very simple. The idea is to recalculate the value of each pixel in the image according to the mask matrix (also known as the core). The value in the mask matrix indicates how much the value of the neighboring pixel (including the value of the pixel itself) affects the new pixel value. From the mathematical point of view, we use our own set of weights to make a weighted average of the values in the neighborhood of the pixel.

2, Operation practice

2.1 mask operation based on pixel neighborhood

/*!
 * \brief myMaskFun
 * \param srcImage
 * \return
 */
Mat myMaskFun(Mat srcImage)
{
    const int nChannels = srcImage.channels();
    Mat resultImage(srcImage.size(), srcImage.type());
    for (int j = 1; j < srcImage.rows - 1; j++)
    {
        const uchar* previous = srcImage.ptr<uchar>(j - 1);
        const uchar* current = srcImage.ptr<uchar>(j);
        const uchar* next = srcImage.ptr<uchar>(j + 1);
        uchar * output = resultImage.ptr<uchar>(j);
        for (int i = nChannels; i < nChannels*(srcImage.cols - 1); ++i)
        {
            *output++ = saturate_cast<uchar>(current[i - nChannels] + current[i + nChannels]
                + previous[i] + next[i]) / 4;
        }
    }

    //! do boundary processing
    resultImage.row(0).setTo(Scalar(0));
    resultImage.row(resultImage.rows - 1).setTo(Scalar(0));
    resultImage.col(0).setTo(Scalar(0));
    resultImage.col(resultImage.cols - 1).setTo(Scalar(0));
    return resultImage;
}

2.2 system function mode

/*!
 * \brief systemMaskFun
 * \param srcImage
 * \return
 */
Mat systemMaskFun(Mat srcImage)
{
    Mat resultImage(srcImage.size(), srcImage.type());
    //! construct kernel factor
    Mat kern = (Mat_<float>(3, 3) << 0, 1, 0,
                                1, 0, 1,
                                0, 1, 0) / (float)(4);
    filter2D(srcImage, resultImage, srcImage.depth(), kern);
    return resultImage;
}

See Appendix 1 for the filter2D function.

Code details< OpenCV mask operation exercise>

reference material:

Appendix 1: filter2D function

In OpenCV, the filter2D function is provided to calculate image convolution. First, this function is briefly introduced:

void filter2D( InputArray src, OutputArray dst, int ddepth,InputArray kernel,Point anchor=Point(-1,-1),double delta=0, int borderType=BORDER_DEFAULT );

 

This function is basically used to realize the convolution operation of the image. The first two parameters represent the input image and the output image respectively, and the third parameter ddepth represents the depth of the image. If this value is set to a negative number, the depth of the image is the same as the depth of the input source image, otherwise, it needs to be set according to the depth of the source image

For example, if src.depth()=CV_8U, ddepth=-1 / CV_16S / CV_32F / CV_64F; if src.depth() = CV_16U/CV_16S, ddepth = - 1 / cv_32f / CV f; if src.depth() =CV_32F, ddepth = - 1 / cv_32f / CV f; if src.depth() = CV_64F, ddepth = - 1 / CV f

The fourth parameter kernel is convolution kernel operator, which is a single channel floating-point matrix. If different volume accounting sub calculation is applied to multiple channels, it needs to be separated into a single channel first and then operated on a single channel. The parameter anchor is the convolution kernel anchor, and the default value (- 1, - 1) is the convolution kernel center. The parameter delta is the smoothing coefficient, which can be used for the smoothing operation of the target image by setting this value before the target image is generated. The last parameter represents the boundary type, with the default value of border [default)

It should be noted that this function is often used in linear filtering technology. When the image target point calculated by convolution kernel operator is outside the image, the specified boundary needs to be interpolated. In fact, this function calculates the correlation of the image, not the convolution operation. Its calculation formula is as follows:

Where 0 < = x '< kernel.cols, 0 < = y' < kernel.rows

Appendix 2: Color space conversion cvtColor()

void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 );

Parameter interpretation:
. InputArray src: the input image is the original image to be color space transformed, which can be a Mat class
. OutputArray dst: the output image is stored after color space transformation, and can also be a Mat class
. int code: the converted code or logo, that is to say, here we will determine the format of the picture to be converted into the format of the picture. Later, we will detail
. int dstCn = 0: number of target image channels. If the value is 0, it is determined by src and code

122 original articles published, 277 praised, 980000 visitors+
His message board follow

Tags: OpenCV

Posted on Sat, 14 Mar 2020 04:15:58 -0400 by illushinz