I am looking at this project to implement some changes.

The project is written in C++ using openCV library. Its goal is to detect cars in parking area to calculate the number of free spots. It does this in real-time (takes the input of a camera and processes it frame by frame).

The detection is performed on areas of the image that are rectangles whose vertices are loaded from a file. For each park (i.e. rectangle) to be monitored there is an instance of the class Parking defined in the files Parking.*** and all of them are collected in a vector (named parking_data in the following lines of code)


As far as I understood the processing starts on line 83 of the file main.cpp. The interesting lines are these:

cv::cvtColor(frame, frame_gray, cv::COLOR_BGR2GRAY);
cv::GaussianBlur(frame_gray, frame_blur, blur_kernel, 3, 3);
for (Parking& park : parking_data)
  // Check if parking is occupied
  roi = frame_blur(park.getBoundingRect());
  cv::Laplacian(roi, laplacian, CV_64F);                                
  delta = cv::mean(cv::abs(laplacian), park.getMask());
  park.setStatus( delta[0] < atof(ConfigLoad::options["PARK_LAPLACIAN_TH"].c_str()) );

cvtColor converts the frame to grayscale then a gaussian blurring filter is applied. Then it iterates to the elements of the vector parking_data containing the instances of the class Parking which represent the area in the image corresponding to the parking spots.

getBoundingRect should return the vertices of the rectangle so to get a subimage of the blurred frame. Then a Laplacian filter is applied.


The line I do not understand is delta = cv::mean(cv::abs(laplacian), park.getMask());.

I understand it is calcualating a mean of the absolute value of the laplacian subimage. The mask is defined only in line 49 and used in the followings of the file Parking.cpp as

mask = cv::Mat::zeros(bounding_rect.size(), CV_8UC1);
for (cv::Point p :
  polygon_points_in_bounding_rect.push_back(cv::Point(p.x - bounding_rect.x, p.y - bounding_rect.y));
vector<vector<cv::Point>> contours;
cv::drawContours(mask, contours, -1, cv::Scalar(255), CV_FILLED);

so it is a matrix of 0s. Then the value is compared to a given threshold.

OpenCV on the function says about the mean function.

When all the mask elements are 0's, the functions return Scalar::all(0)

So what is the mean function calculating when passing a mask of zeros?

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.