EDIT: I wrote a Python script for this.
As your objective is blurring (for privacy protection), you basically need a high recall detector as a first step. Here’s how to go about doing this. The included code hints use OpenCV with Python.
- Convert to Grayscale.
Apply Gaussian Blur.
img = cv2.imread('input.jpg',1) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_gray = cv2.GaussianBlur(img_gray, (5,5), 0)
Let the input image be the following.
- Apply Sobel Filter to detect vertical edges.
Threshold the resultant image using strict threshold or OTSU’s binarization.
cv2.Sobel(image, -1, 1, 0) cv2.threshold()
Apply a Morphological Closing operation using suitable structuring element. (I used 16×4 as structuring element)
se = cv2.getStructuringElement(cv2.MORPH_RECT,(16,4)) cv2.morphologyEx(image, cv2.MORPH_CLOSE, se)
Resultant Image after Step 5.
Find external contours of this image.
cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
For each contour, find the
- Select rectangles based on aspect ratio, minimum and maximum area, and angle with the horizontal. (I used 2.2 <= Aspect Ratio <= 8, 500 <= Area <=15000, and angle <= 45 degrees)
minAreaRect()s are shown in orange and the one which satisfies our criteria is in green.
- There may be false positives after this step, to filter it, use edge density. Edge Density is defined as the number of white pixels/total number of pixels in a rectangle. Set a threshold for edge density. (I used 0.5)
- Blur the detected regions.
You can apply other filters you deem suitable to increase recall and precision. The detection can also be trained using HOG+SVM to increase precision.