analitics

Pages

Monday, July 8, 2013

Using findContours from OpenCV python module.

Today I will show something nice about OpenCV Analysis and Shape Descriptors.

This function finds contours in a binary image.

All detected contours is stored as a vector of points for each contour.


#!/usr/bin/python2.7
import cv2
im = cv2.imread('your_image.jpg')
img_gray = cv2.cvtColor(im,cv2.COLOR_RGB2GRAY)
ret,thresh = cv2.threshold(img_gray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im,contours,-1,(250,250,250),2)
cv2.imshow('your_image.jpg',im)
cv2.waitKey()
cv2.destroyAllWindows()

If you got this error:

findContours error 'support only 8uC1 images'

then the main reason it's findContours requires a monochrome image.

Let's see the result of the python script.


The contour it's draw with 250,250,250 color.

Friday, June 21, 2013

Simple way to find your script under linux.

Many python users have a lot of scripts.

They use some words for classes or some functions.

Sometime is hard to remember where it's this scripts.

So the easy way to do that is to find the script where is some words.

For example you need to find this : word_in_your_script

To do that just see next linux command:

$ find ~/ -type f -iname "*.py" -exec grep -l 'word_in_your_script' {} \;