Image Processing Grayscale Transformation

1 Introduction to Grayscale Transformation

  Grayscale transformation is a transformation that maps each pixel of an image according to a grayscale mapping function, which acts on each pixel. Grayscale transformation is generally used for image enhancement, improving the contrast of the image, improving the grayscale distribution of the image, etc. According to the difference of the gray scale transformation function, the gray scale transformation can be divided into linear function transformation, logarithmic function transformation and power function transformation.
pouti , j = f ( pini , j ) pout_{i,j}=f(pin_{i,j})pouti,j=f(pini,j)

   f f f is the grayscale transformation function,iii andjjj is the subscript index of the image,pinij pin_{ij}pinijbecause i , ji,ji,The pixel value at j .
  The grayscale transformation function can be any function, and different grayscale transformation functions can be selected according to different usage scenarios.
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-eGtF9Dcf-1663634507062)(imgs/funcs.png)]

  The simplest implementation of grayscale transformation is as follows, traversing each pixel and then using the function Func to process each pixel

template<class Func>
static Mat pixelWiseTransform(const Mat &img, Func f) {
    
    
    Mat retMat = img.clone();
    for (int i = 0; i < img.rows; i++) {
    
    
        for (int j = 0; j < img.cols; j++) {
    
    
            for (int k = 0; k < img.channels(); k++) {
    
    
                if (img.channels() == 3) {
    
    
                    retMat.at<cv::Vec3b>(i, j)[k] = static_cast<uchar>(imgValueLmit(f(retMat.at<cv::Vec3b>(i, j)[k])));
                }
                else if (img.channels() == 1) {
    
    
                    retMat.at<uchar>(i, j) = static_cast<uchar>(imgValueLmit(f(retMat.at<uchar>(i, j))));
                }
                else {
    
    
                    abort();
                }
            }
        }
    }

    return std::move(retMat);
}

2 Linear grayscale transformation

  The simplest grayscale transformation function is linear grayscale transformation. The grayscale transformation function of linear grayscale transformation is a linear function:
f ( x ) = ax + bf(x)=ax+bf(x)=ax+b
   a a a is the slope,bbbyyy- intercept,xxx is the pixel value of the input point,f ( x ) f(x)f ( x ) is the output pixel value of the corresponding point.

  • a = 1 , b = 0 a = 1, b = 0 a=1,b=When 0 , it is a constant function, and the image will not change;

  • a = − 1 , b = 255 a = -1, b = 255 a=1,b=255 , the image is reversed;
    insert image description here

  • a = 1 , b ! = 0 a = 1, b != 0 a=1,b!=When 0 , the value in the image increases b as a whole, and the image becomes darker or brighter as a whole;
    insert image description here

  • a < 0 , b is constant a < 0 , b is constanta<0,When b is constant ;
    insert image description here

  • a > 1 , b constant a > 1 , b constanta>1,When b is constant , the contrast of the image increases, the overall brightness becomes brighter, and the black becomes gray;

  • a < 1 & a > 0 , b constant a < 1 \And a > 0, b constanta<1&a>0,When b is constant , the image contrast decreases and the overall darkens;
    insert image description here

3 logarithmic transformation

  The general form of the logarithmic transformation is:
pouti , j = clog ( p 1 + pini , j ) pout_{i,j}=clog(p1 + pin_{i,j})pouti,j=clog(p1+pini,j)
  logarithmic transformation, which maps the narrower low grayscale value in the source image to a wider grayscale interval, and maps the wider high grayscale value interval to a narrower grayscale interval, thereby expanding the The value of the dark pixel compresses the value of the high gray level, which can enhance the low gray level details in the image.
insert image description here

4 power transformation

  The basic form of power transformation is
pouti , j = cpini , jr pout_{i,j}=c{pin_{i,j}}^{r}pouti,j=c p i ni,jThe effect of r
  gamma transformation is somewhat similar to logarithmic transformation, whenγ > 1 γ>1c>When 1 , the low gray value of the narrower range is mapped to the gray value of the wider range, and the high gray value of the wider range is mapped to the gray value of the narrower range at the same time; when γ < 1 γ<1c<When 1 , the situation is reversed, which is similar to the antilog transformation.

  • r is 1.4 r is 1.4r for 1.4 transformccc
    insert image description here

  • c = 1 c=1 c=1 transformrrr
    insert image description here

  The gamma transformation is most commonly used to correct the brightness of the color displayed on the display.

5 piecewise function transformation

  Segmented function transformation can design multi-segment mapping functions according to specific needs to process the colors of different gray scale segments, and the mapping of each segment can be linear or nonlinear.
insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/GrayOnDream/article/details/126945751