jueves, 2 de junio de 2016

Simple Object Detection in 3 lines of Code (OpenCV/Python)

https://machinelearning1.wordpress.com/2014/08/05/simple-object-detection-in-3-lines-of-code-opencvpython/


Simple Object Detection in 3 lines of Code (OpenCV/Python)

The main 3 lines of code is as follows:
1
2
3
img_filt = cv2.medianBlur(cv2.imread('f.jpg',0), 5)
img_th = cv2.adaptiveThreshold(img_filt,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
contours, hierarchy = cv2.findContours(img_th, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
We read the original image in the first line using the imread function. By passing the 0 flag to this function the output would be a grayscale image. The resulting image is then passed to the medianBlur filter (to make the image blur). The aperture linear size is selected to be 5. The filtered image is then passed to adaptiveThreshold with a Gaussian adaptive method and a binary threshold method. The thresholded image is then used for extracting contours as can be seen in the last image (bottom right). The obtained contours are used to plot some rectangles on the original image (input image – top left).
And the result can be seen in the following images:
figure_1

No hay comentarios:

Publicar un comentario