Posts Tagged ‘opencv’

Hey guys, been reading OpenCV for python and thought of posting a tutorial on Programming a Grayscale Image Convertor. I encourage you to google them , there are lots and lots of examples and code snippets. This is on how to a convert any image to gray scale using Python and OpenCV.

A sample input image and output image are shown below (YEah, I am big Iron Man Fan! :B). You can click on image to enlarge:

I have placed an image named “ironman.png” in the current working directory (i.e. I have the original image in the same directory as the  place where I have saved my Python code).

First we import the cv2 module:

import cv2

Then ,read the image to a variable named “image” :

image = cv2.imread('ironman.png')

Now, to convert to gray-scale image and store it to another variable named “gray_image” use the function cv2.cvtColor() with parameters as  the “image” variable and  “cv2.COLOR_BGR2GRAY” :

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

So, now the variable “gray_image” will hold the gray-scale version of the input image.

Now, to write/save the converted gray-scale image to the hard disk, we use the function “cv2.imwrite()” with parameters as “the name of converted image” and the variable “gray_image” to which the converted image was stored:

cv2.imwrite('gray_image.png',gray_image)

So, now if you open the directory where you saved your python code, you can see a new image there : gray_image.png! Wow! 😀

Now, to display the original and the gray-scale ,we use function “cv2.imshow()” with parameters as the “window title” and the “image variable” :

cv2.imshow('color_image',image)             
cv2.imshow('gray_image',gray_image) 

Complete Code for GrayScale Image convertor:

# GrayScale Image Convertor
# https://extr3metech.wordpress.com

import cv2
image = cv2.imread('ironman.png')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imwrite('gray_image.png',gray_image)
cv2.imshow('color_image',image)
cv2.imshow('gray_image',gray_image) 
cv2.waitKey(0)                 # Waits forever for user to press any key
cv2.destroyAllWindows()        # Closes displayed windows

#End of Code

 
Hope you enjoyed this post, feel free to comment and don’t forget to follow my blog! 😀
 
ΞXΤЯ3МΞ

Advertisements

OpenCV (Open Source Computer Vision) is a library of programming functions for real time computer vision. I just finished installing OpenCV 2.4.1  in ma laptop. It look a while for me to download as my internet connection was laggy for a while.

Step 1: Download and Install Python:

Firstly, you will need Python installed in your system. If you can download it from here and follow the instructions which I have written to get Python successfully running in your computer in my previous blog entry here. Python will be installed in default folder “C:\Python27\

Step 2: Download Numpy:

Then you will need to download Numpy and install it .

Download Link for Numpy: Numpy

It will find the default Python Folder and you can just install it will the default settings.

Step 3: Download OpenCV:

Download Link for OpenCV : OpenCV

Now double-click OpenCV.exe. When it asks for extraction folder just give it as C:\ . The files will be now be extracted to C:\opencv .This might take a while.

Now, open folder “C:\opencv\build\python\2.7” and copy all the files in that folder to the folder “C:\Python27\Lib\site-packages” .

Step 4: Test it!

Open up your Python IDLE by clicking Start>All Programs>Python 2.7> IDLE (Python GUI) and type:

import cv2

If you have followed the described steps , then it would successfully import it and you can start programming right away! 😀

The OpenCV documentation can be found here.

ΞXΤЯ3МΞ