Shandong University Digital Image Processing Experiment (2)

foreword

  • This time is "Contrast and Brightness Adjustment" and "Two Experiments of Background Subtraction"
  • The source address is at the end of the text

Contrast and Brightness Adjustment Experiment

Problems encountered and solved during the experiment

  • Question one

    • Problem: I only know how to use the sigmoidfunction , but I don't know what kind of sigmoidfunction to use
    • Solution: Search the Internet and find out that the general adjustment formula for contrast and brightness is: when adjusting the contrast, a coefficient multiplied by it will be added in front of the pixel value. When adjusting the brightness, a variable is used to add or subtract the pixel value
  • Question two:

    • Problem: Only the change trend of the sigmoidfunction , but the specific value is not considered, and the function value only changes significantly within [-5,5]

    • Solution: make adjustments on the pixel transform formula

      • The original formula is

        int t = source_image.at<Vec3b>(y, x)[c];
        transformed_image.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(t * ((contrast_value*0.1 / (1.00 + exp(-t))) + 1));
        
      • The adjusted formula is

        double t = ((source_image.at<Vec3b>(y, x)[c] - 127) / 255.00) * contrast_value * 0.1;//[-10, 10]
        transformed_image.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(source_image.at<Vec3b>(y, x)[c] * ((1.00 / (1.00 + exp(-t))) + 0.3) + bright_value - 100);
        
      • -127It is to make the result positive and negative, so that small pixels will lead to 1 + e − t 1+e^{-t}1+and− The larger t is, the smaller the pixel value is after the contrast adjustment. Conversely, the adjustment of the large pixel value is not as drastic as the small pixel value, so it will cause the pixel difference between them to be larger, so the contrast is adjusted.

      • /255is to limit ttThe range of t , so that it changes as far as possible in the area where the power function changes more obviously

      • contrast_valueIt is the value that the slider needs to change for contrast adjustment

      • 1 1 + e − t + 0.3 \frac{1}{1+e^{-t}} + 0.3 1 + andt1+0.3 , a coefficient is added because, if no coefficient is added, only adjusting the contrast will cause the transformed pixel value to always be smaller than the original pixel value, which is not what we want. Another reason is that this can balance thettThe role of t , do not letttt has such a large degree of influence. (The size of the added value can be controlled by yourself)

      • bright_valueIt is easier to understand, that is, simply adjust the pixel value for brightness adjustment

  • Problem 3: It is difficult to obtain the original image by using the adjustment formula. Personally think it is because of the use of nonlinear transformation, there is no solution at present

Result analysis and experience

The adjustment effect is more obvious and normal, and the experimental results achieve the expected effect.

  • It is difficult to get the original image using the sigmoid function

  • If the contrast exceeds a certain range, the realism of the image will be lost
    insert image description here

insert image description here

Background subtraction experiment

Problems encountered and solved during the experiment

  • Question one

    • Problem: The effect of the image obtained by subtracting the corresponding position pixels of the two images is very poor

insert image description here
False detection:

  • When the pixel value in the image is not much different from the pixel value of the corresponding position in the background image, it is difficult to distinguish. After subtraction, the position of the foreground image is basically black, which does not meet the expected requirements.

  • Taking the above picture as an example, when the pixel value of the person is smaller than the background pixel value, the pixel value of the person minus the background pixel value is processed to prevent overflow, and the pixel value of the corresponding position of the resulting image is 0, which does not meet the expected requirements.

    • Solution: Find a better solution, use median filter to remove noise points. The sum is obtained by summing the squares of the differences of the three channels of each pixel of the two images, and then taking the square of the sum. Manually set a threshold, when the sum is greater than this threshold, the three channels of the pixel are set to 255 (white), otherwise set to 0 (black). The results found that the best threshold is 75-100, and the final selected threshold is 90

Result analysis and experience

It can be seen that the foreground of the character is basically extracted, but there is still noise

  • For background subtraction experiments, you can look at some noise reduction algorithms, because it is not easy to determine the standard of subtraction

  • The result obtained when the threshold value of the background subtraction experiment is selected as 90. Clear contours to achieve the desired effect

insert image description here

Knowledge points encountered

at function

  • For the single-channel image "picture1", picture1.at(i,j) represents the pixel value at row i and column j.

  • For multi-channel images such as RGB image "picture2", picture2.at(i,j)[c] can be used to represent the pixel value at (i,j) position in a certain channel.

Common type Vec3b

Vec3b can be regarded as vector<uchar, 3>, in short, it is a vector vector of uchar type with a length of 3.

由于在OpenCV中,使用imread读取到的Mat图像数据,都是用uchar类型的数据存储,对于RGB三通道的图像,每个点的数据都是一个Vec3b类型的数据。使用at定位方法如下:
 
img.at<Vec3b>(row, col)[0] = 255;  // 这是指修改B通道数据
img.at<Vec3b>(row, col)[1] = 255;  // 这是指修改G通道数据
img.at<Vec3b>(row, col)[2] = 255;  // 这是指修改R通道数据

point

srcImage.at<uchar>(j, i) //表示的是  j 行 i 列 的这个像素
srcImage.at<uchar>(Point(j, i)) //表示的是 坐标(j,i)的像素

saturate_cast

  • saturate_cast is mainly to prevent the color overflow operation
  • In terms of image processing, whether it is addition, subtraction, multiplication or division, it will exceed the range of a pixel's gray value (0 to 255). The function of the saturate_cast function is: when the result is negative after the operation, it is converted to 0, and the result If it exceeds 255, it will be 255.

Mat::zeros

  • Create a map with 0 for each channel of each pixel
Mat m = Mat::zeros(2, 2, CV_8UC3);//直接指明size和类型

transformed_image = Mat::zeros(source_image.size(), source_image.type());//创建一个和source_image一样类型的图

createTrackbar

Create a slider

  • The callback function is used with createTrackbar
CV_EXPORTS int createTrackbar(const String& trackbarname,
                              const String& winname,
                              int* value, 
                              int count,                                        
                              TrackbarCallback onChange = 0,
                              void* userdata = 0);    
  • slider name
  • The name of the window where the slider is located
  • Used to set the initial value of the slider, and record the position of the slider later (the user changes the position of the slider, so the value will change)
  • The maximum value of the track, the range that the slider can slide is [0, count]
  • Callback
  • The default is 0, the data passed by the user to the callback function, if the third value is a global variable, ignore this value
namedWindow("Transformed Window", WINDOW_AUTOSIZE);
createTrackbar("contrast", "Transformed Window", &contrast_value, 200, ContrastAndBright , 0);
createTrackbar("bright", "Transformed Window", &bright_value, 200, ContrastAndBright , 0);

static void ContrastAndBright(int pos , void* userdata) {
    //value传值给pos , createTrackbar的第六个参数传递给userdata
    for (int y = 0; y < source_image.rows; y++)
        for (int x = 0; x < source_image.cols; x++)
            for (int c = 0; c < 3; c++){
                double t = ((source_image.at<Vec3b>(y, x)[c] - 127) / 225.00) * contrast_value * 0.1;
                transformed_image.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(source_image.at<Vec3b>(y, x)[c] * ((1.00 / (1.00 + exp(-t))) + 0.3) + bright_value - 100);
            }
    imshow("Display Window", source_image);
    imshow("Transformed Window", transformed_image);
}

Source address: Computer-Vision/Experiment 1/Source code at main SDU-NSY/Computer-Vision (github.com)

Guess you like

Origin blog.csdn.net/qq_52852138/article/details/127038134