Blogger Widgets

Search This Blog

Tuesday, 18 December 2012

OpenCV - Open Computer Vision in Python

                 OpenCV is a library used for real time computer vision.It is open source under BSD License. Here a quick start of OpenCV in python is explained.It is very useful module for robotics , Image processing, Object tracking etc.

We can directly explain a Helloworld example before actually study this - Here we open an image and change its pixel values and save its inverted colour image.
    image                              processed image


#!/usr/bin/python
import cv2

cv2.namedWindow("Image")
cv2.namedWindow("Processed Image")

if __name__ == '__main__':
    import sys
    try: image = cv2.imread(sys.argv[1])
    except: image = cv2.imread("lena.jpg")

processedImage = image.copy()

height, width, nColors = processedImage.shape

for y in xrange(height):
    for x in xrange(width):
        B = processedImage.item(y, x, 0)
        G = processedImage.item(y, x, 1)
        R = processedImage.item(y, x, 2)
        processedImage.itemset(y, x, 0, 255 - B)
        processedImage.itemset(y, x, 1, 255 - G)
        processedImage.itemset(y, x, 2, 255 - R)

cv2.imshow("Image", image)
cv2.imshow("Processed Image", processedImage)

cv2.waitKey(0)
cv2.destroyAllWindows()


                    Where we first import opencv module and create two windows for image and processed image. And copy the given image to a variable named  "processedImage".Then we access its parameters height, width. The color of all pixels are inverted using for loop by accessing individual pixel informations.Then shows both the image.




No comments:

Post a Comment

Blogger Widgets