Object detection in uneven lighting in opencv c ++

I am performing function detection in a video / live stream / image using OpenCV C ++. The lighting state changes in different parts of the video, which causes some parts to be ignored when converting RGB images to binary images.

The lighting state in a specific part of the video also changes during the video. I tried the Histogram function, but that did not help.

I got a working solution in MATLAB at the following link:

http://in.mathworks.com/help/images/examples/correcting-nonuniform-illumination.html

However, most of the features used in the link above are not available in OpenCV.

Can anyone suggest an alternative to this MATLAB code in OpenCV C ++?

+3
source share
2 answers

OpenCV has an adaptive threshold paradigm, available at: http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#adaptivethreshold

The function prototype looks like this:

void adaptiveThreshold(InputArray src, OutputArray dst, 
                      double maxValue, int adaptiveMethod, 
                      int thresholdType, int blockSize, double C);

The first two parameters are the input image and the storage location for the output threshold image. maxValue- this is the threshold value assigned to the output pixel, if it passes the criteria, adaptiveMethod- this is the method used for the adaptive threshold value, thresholdType- the type of threshold that you want to execute (later), blockSize- the size of the windows to check (later), and C- this constant to subtract from each window. I never had to use this, and I usually set it to 0.

adaptiveThreshold - blockSize x blockSize , C. , maxValue, 0. , .

, , - :

// Include libraries
#include <cv.h>
#include <highgui.h>

// For convenience
using namespace cv;

// Example function to adaptive threshold an image
void threshold() 
{
   // Load in an image - Change "image.jpg" to whatever your image is called
   Mat image;
   image = imread("image.jpg", 1);

   // Convert image to grayscale and show the image
   // Wait for user key before continuing
   Mat gray_image;
   cvtColor(image, gray_image, CV_BGR2GRAY);

   namedWindow("Gray image", CV_WINDOW_AUTOSIZE);
   imshow("Gray image", gray_image);   
   waitKey(0);

   // Adaptive threshold the image
   int maxValue = 255;
   int blockSize = 25;
   int C = 0;
   adaptiveThreshold(gray_image, gray_image, maxValue, 
                     CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 
                     blockSize, C);

   // Show the thresholded image
   // Wait for user key before continuing
   namedWindow("Thresholded image", CV_WINDOW_AUTOSIZE);
   imshow("Thresholded image", gray_image);
   waitKey(0);
}

// Main function - Run the threshold function
int main( int argc, const char** argv ) 
{
    threshold();
}
+2

adaptiveThreshold .

Matlab OpenCV, . , Matlab, OpenCV.

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{   
    // Step 1: Read Image
    Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);

    // Step 2: Use Morphological Opening to Estimate the Background
    Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(15,15));
    Mat1b background;
    morphologyEx(img, background, MORPH_OPEN, kernel);

    // Step 3: Subtract the Background Image from the Original Image
    Mat1b img2;
    absdiff(img, background, img2);

    // Step 4: Increase the Image Contrast
    // Don't needed it here, the equivalent would be  cv::equalizeHist

    // Step 5(1): Threshold the Image
    Mat1b bw;
    threshold(img2, bw, 50, 255, THRESH_BINARY);

    // Step 6: Identify Objects in the Image
    vector<vector<Point>> contours;
    findContours(bw.clone(), contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);


    for(int i=0; i<contours.size(); ++i)
    {
        // Step 5(2): bwareaopen
        if(contours[i].size() > 50)
        {
            // Step 7: Examine One Object
            Mat1b object(bw.size(), uchar(0));
            drawContours(object, contours, i, Scalar(255), CV_FILLED);

            imshow("Single Object", object);
            waitKey();
        }
    }

    return 0;
}
+1

Source: https://habr.com/ru/post/1598715/


All Articles