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 : contours_points.at(0))
  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?


1 Answer 1


The function cv:mean calculates the mean value of the marked out area in the InputArray as you said. So the function looks at the area which is filled with the mask (zeros) and calculates the mean value of the InputArray which is not filled with zeros.

I know the code you are showing here. I don't understand why the function cv:Laplacian is used here because in my opinion this is not relevant in this case. Maybe someone else knows why he used the Laplacian array instead of using the blured_gray array.

I hope I could help you.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.