2

I need to detect the main (front) car in the image to put it in another image with no or plain background. Below is an example image:

Original image

I am developing in Python 3 with OpenCV 4.

I tried HaarCascade, but it does not work well, even with many different hyper parameters in detectMultiScale:

car_cascade = cv2.CascadeClassifier('./haarcascade/haarcascade_car.xml') 

car_detected = car_cascade.detectMultiScale(img_gray1, 2.2, 4)

cars_with_detections = np.copy(img1)

for (x, y, w, h) in car_detected:
    cv2.rectangle(cars_with_detections, (x, y), (x+w, y+h), (255, 0, 0), 5)

plt.figure(figsize=(25,15))
plt.imshow(cars_with_detections)

Haarcascade

Sergio Pantano
  • 111
  • 1
  • 10

1 Answers1

0

The background for this image is quite difficult to remove (all while preserving the car's shape) due to the other red cars and misc objects.
I tried various methods: blurring (gaussian filter, box filter, etc.).

If you only need to do this for one image, and the put just the car in another image, you may have better luck using Photoshop and the Magic Wand tool.

However, I was able to get pretty close via just pure OpenCV processing via experimentation in the GRIP (Graphically Represented Image Processing) software.

Here is my pipeline: Image Processing Pipeline

Original Image: enter image description here

  1. CV Subtract (subtracting the car's "red" color to get a dark blob for the car)
    Red (Red) Subtract

  2. HSV Threshold (to create a mask with the car's body) Threshold

  3. Blur (blur the HSV threshold to get a more solid mask) Blur

  4. Find contours (to find the outline of the car's body via the blurred mask) Contours

  5. Mask (apply the mask to the original image to just get the car's pixels) Mask

I know my code may not have produced the result you hoped for, but I hope you appreciate my effort and time as well as my alternate solutions.

Here is the link to download GRIP

Here is the link to download the image processing pipeline code as a Python file and the GRIP file

Jacob K
  • 742
  • 6
  • 13