How to detect a human contour

Hello, I have a problem with getting a human contour. How can I make it clearer without noise and unnecessary objects. Below is the code and the result I have at the moment. Thanks for the reply.

Image Processing:

Imgproc.adaptiveThreshold(img4, img4, 255,
Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
Imgproc.THRESH_BINARY_INV, 7, 3);
Imgproc.Canny(img4, img4, 150, 255);
Imgproc.GaussianBlur(img4, img4, new Size(5, 5), 0);

Search contour:

Mat hierarchy = new Mat();
contours = new ArrayList();
Imgproc.findContours(img4, contours, hierarchy,
Imgproc.RETR_EXTERNAL,
Imgproc.CHAIN_APPROX_NONE);

Drawing a contour:

for (int i = 0; i < contours.size(); i++){
double cont_area = Imgproc.contourArea(contours.get(i));
if (cont_area > 1000) {
Imgproc.drawContours(hMat, contours, i, new Scalar(130, 0, 15), 4);
}
}

Result:
image

let’s start with the preprocessing:
please explain each step, why it is there, and why in that order

then, explain the purpose of your program. are you trying to detect humans this way ?

I’m converting the image to gray. For the best selection of canny, I use the adaptive threshold method. Next, I select canny and use them to search for external contours.
My purpose is to draw a human contour. Initially, I used this to search for a person on a monochrome background and I was satisfied with the result. Now my background has various noises, how can I better find a human contour in this case?

where did you get that idea ?
Canny needs gradients for input, thresholding outputs binary
findContours also needs binary input, and you’re destroying those with final blurring.

so, put the blur first (to remove noise), throw out the canny (you dont need it, & it’s harmful !), thresh & then find contours
visualize all intermediate steps & adjust params until it looks good

finally, this is a terrible way to detect humans !

rather use a modern cnn, like yolov5, which already detects humans & gives you a pixel mask (segmentation)

I would add that, while the approach you are using might not be the best way to detect people in an image, it is a good way to learn. So depending on your goals, you might want to keep playing around with the image processing steps and seeing how different steps and different ordering affects the results.

1 Like

if you don’t feel ready to use neural networks yet, pedestrians can be detected with a classifier that uses Histogram of Oriented Gradients as the underlying feature map. OpenCV comes with a HOG model for pedestrians, if I remember correctly. I don’t know how well it’ll work.