OpenCV study notes mask operation

One, mask operation

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

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

    //! 进行边界处理
    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());
    //! 构造核函数因子
    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;
}

 The filter2D function is detailed in Appendix 1.

The code is detailed in " OpenCV Mask Operation Practice "

Reference materials:

Appendix 1: filter2D function

The filter2D function is provided in OpenCV for the operation of calculating image convolution. First, let's briefly introduce this function:

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 implement the image convolution operation. The first two parameters represent the input image and the output image respectively. The third parameter ddepth represents the depth of the image. If this value is set to a negative number, the depth of the image The depth of the input source image is the same, otherwise it needs to be set according to the depth of the source image

For example, if src.depth()=CV_8U, then ddepth=-1 / CV_16S / CV_32F / CV_64F, if src.depth() = CV_16U/CV_16S, then ddepth = -1 / CV_32F / CV_64F, if src.depth() =CV_32F, then ddepth= ​​-1 / CV_32F / CV_64F, if src.depth() = CV_64F, then ddepth = -1 / CV_64F.

The fourth parameter kernel is the convolution kernel operator, which is a single-channel floating-point matrix. If you apply different convolution kernel operator calculations to multiple channels, you need to first separate them into single channels and then perform operations on single channels. The parameter anchor is the anchor point of the convolution kernel. The default value is (-1, -1) which means the center of the convolution kernel. The parameter delta is a smoothing coefficient, and this value can be used to smooth the target image before the target image is generated. The last parameter represents the boundary type, with a default value BORDER_DEFAULT)

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

其中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 explanation:. 
InputArray src: The input image is the original image to be color space transformed, which can be the Mat class 
. OutputArray dst: The output image is the color space transformed to store the image, or it can be the Mat class 
. int code: the converted code or Identifier, that is, here to determine what format picture will be converted into what format picture, later will be detailed 
. int dstCn = 0: target image channel number, if the value is 0, it is determined by src and code 

Guess you like

Origin blog.csdn.net/a8039974/article/details/104861383