Monday, 19 January 2015

Convert RGB to gray / other color spaces

Convert RGB to gray / other color spaces



cv2.cvtColor(src, code[, dst[, dstCn]]) → dst

Parameters:
  • src – input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC... ), or single-precision floating-point.
  • dst – output image of the same size and depth as src.
  • code – color space conversion code (see the description below).
  • dstCn – number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code .
This function converts one image from one colorspace to another.The default color format in openCV is RGB.But is is actually BGR(byte reversed).so in an 24 bit color image the first 8 bits are blue components,2nd byte is green and third one is red.

The conventional ranges for R, G, and B channel values are:
  • 0 to 255 for CV_8U images
  • 0 to 65535 for CV_16U images
  • 0 to 1 for CV_32F images

Example:

Converting to grayscale:

Steps:

  1. Load an image 
  2. Convert to gray scale
  3. Show result
Code:

------------------------------------------

import cv2

# Load a color image
img = cv2.imread('image1.jpg',1)

#convert RGB image to Gray
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

#Display the color image
cv2.imshow('color_image',img)

#Display the gray image
cv2.imshow('gray_image',gray)

cv2.waitKey(0)
cv2.destroyAllWindows()


-------------------------------------------

Wednesday, 24 September 2014

Basic Drawing Examples

Basic Drawing Examples


Drawing a line:


cv2.line(img, Point pt1, Point pt2, color[,thickness[,lineType[,shift]]]) --> img
Parameters:
  • img – Image.
  • pt1 – First point of the line segment.
  • pt2 – Second point of the line segment.
  • color – Line color.
  • thickness – Line thickness.
  • lineType –
    Type of the line:
    • LINE_8 (or omitted) - 8-connected line.
    • LINE_4 - 4-connected line.
    • LINE_AA - antialiased line.
  • shift – Number of fractional bits in the point coordinates.
Example 1: Drawing a line:
--------------

import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a blue line with thickness of 5 px
cv2.line(img,(15,20),(70,50),(255,0,0),5)

#Display the image
cv2.imshow("img",img)

cv2.waitKey(0)
--------------

Drawing a Circle:


cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]]) → img
Parameters:
  • img – Image where the circle is drawn.
  • center – Center of the circle.
  • radius – Radius of the circle.
  • color – Circle color.
  • thickness – Thickness of the circle outline, if positive. Negative thickness means that a filled circle is to be drawn.
  • lineType – Type of the circle boundary. 
  • shift – Number of fractional bits in the coordinates of the center and in the radius value.
Example 2: Drawing a Circle:
---------------
import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a red closed circle
cv2.circle(img,(200,200), 40, (0,0,255), -1)

#Display the image
cv2.imshow("img",img)

cv2.waitKey(0)
---------------

Drawing an Ellipse:


cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]]) → img
cv2.ellipse(img, box, color[, thickness[, lineType]]) → img

Parameters:
  • img – Image.
  • center – Center of the ellipse.
  • axes – Half of the size of the ellipse main axes.
  • angle – Ellipse rotation angle in degrees.
  • startAngle – Starting angle of the elliptic arc in degrees.
  • endAngle – Ending angle of the elliptic arc in degrees.
  • box – Alternative ellipse representation via RotatedRect or CvBox2D. This means that the function draws an ellipse inscribed in the rotated rectangle.
  • color – Ellipse color.
  • thickness – Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that a filled ellipse sector is to be drawn.
  • lineType – Type of the ellipse boundary.
  • shift – Number of fractional bits in the coordinates of the center and values of axes.
Example 3: Drawing an Ellipse:
----------------
import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw ellipse
cv2.ellipse(img,(200,200),(80,50),0,0,360,(0,255,0),-1)
cv2.ellipse(img,(200,200),(80,50),45,0,360,(0,0,255),1)

#Display the image
cv2.imshow("img",img)

cv2.waitKey(0)
----------------

Drawing a Rectangle:


cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → img

Parameters:
  • img – Image.
  • pt1 – Vertex of the rectangle.
  • pt2 – Vertex of the rectangle opposite to pt1 .
  • rec – Alternative specification of the drawn rectangle.
  • color – Rectangle color or brightness (grayscale image).
  • thickness – Thickness of lines that make up the rectangle. Negative values, like CV_FILLED , mean that the function has to draw a filled rectangle.
  • lineType – Type of the line..
  • shift – Number of fractional bits in the point coordinates.
Example 4: Drawing a Rectangle:
--------------------
import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw rectangle(not filled)
cv2.rectangle(img,(15,20),(70,50),(0,255,0),3)

# Draw filled rectangle
cv2.rectangle(img,(115,120),(170,150),(255,0,0),-1)

#Display the image
cv2.imshow("img",img)

cv2.waitKey(0)
--------------------

Drawing a Polygon:


cv2.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) → img
Parameters:
  • img – Image.
  • pts – Array of polygonal curves.
  • npts – Array of polygon vertex counters.
  • ncontours – Number of curves.
  • isClosed – Flag indicating whether the drawn polylines are closed or not. If they are closed, the function draws a line from the last vertex of each curve to its first vertex.
  • color – Polyline color.
  • thickness – Thickness of the polyline edges.
  • lineType – Type of the line segments.
  • shift – Number of fractional bits in the vertex coordinates.
Example 4: Drawing a Polygon:
-----------------------
import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a polygon
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,(0,255,255))

#Display the image
cv2.imshow("img",img)

cv2.waitKey(0)
-----------------------

Putting Text in image:


cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) → None

Parameters:
  • img – Image.
  • text – Text string to be drawn.
  • org – Bottom-left corner of the text string in the image.
  • font – CvFont structure initialized using InitFont().
  • fontFace – Font type. One of FONT_HERSHEY_SIMPLEXFONT_HERSHEY_PLAINFONT_HERSHEY_DUPLEX,FONT_HERSHEY_COMPLEXFONT_HERSHEY_TRIPLEXFONT_HERSHEY_COMPLEX_SMALL,FONT_HERSHEY_SCRIPT_SIMPLEX, or FONT_HERSHEY_SCRIPT_COMPLEX, where each of the font ID’s can be combined with FONT_ITALIC to get the slanted letters.
  • fontScale – Font scale factor that is multiplied by the font-specific base size.
  • color – Text color.
  • thickness – Thickness of the lines used to draw a text.
  • lineType – Line type. See the line for details.
  • bottomLeftOrigin – When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.
Example 5: Putting Text in image:
--------------------
import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Write some Text
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'Hello World!',(10,500), font, 1,(255,255,255),2)

#Display the image
cv2.imshow("img",img)

cv2.waitKey(0)
---------------------

Saturday, 20 September 2014

Capture Video from Camera

Capture Video from Camera


This example helps to capture a video using python & openCV.

Example:
-------------------------

import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Display the resulting frame
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

#release the capture
cap.release()
cv2.destroyAllWindows()
-------------------------

You can capture a video from a web camera connected to a system or using the built-in  webcam of a laptop. I am using my own built-in laptop camera .

We use cv2.VideoCapture( ) to capture a video.

cv2.VideoCapture()

cv2.VideoCapture(argument) ->  <VideoCapture Object>

So we create a VideoCapture Object by passing an argument into it.The argument can be the device index (to specify to use which camera) or the name of the file name. I have one built-in camera connected. so I simply pass 0( or -1). We can also pass 1 for the secondary camera.

for e.g.-  cap=cv2.VideoCapture(0)

cap.read( )

cap.read([,image]) ->  retval,image

for e.g.-  ret, frame = cap.read( )
It returns True if frame is read correctly, otherwise it returns false.

We can access some of the properties of the video using cap.get(propld). where propld is a number from 0 to 18. each no. indicating a different property of the video.

propId – Property identifier. It can be one of the following:
  • 0- Current position of the video file in milliseconds or video capture timestamp.
  • 1-based index of the frame to be decoded/captured next.
  • 2- Relative position of the video file: 0 - start of the film, 1 - end of the film.
  • 3- Width of the frames in the video stream.
  • 4- Height of the frames in the video stream.
  • 5- Frame rate.
  • 6-character code of codec.
  • 7- Number of frames in the video file.
  • 8- Format of the Mat objects returned by retrieve() .
  • 9- Backend-specific value indicating the current capture mode.
  • 10-Brightness of the image (only for cameras).
  • 11- Contrast of the image (only for cameras).
  • 12- Saturation of the image (only for cameras).
  • 13- Hue of the image (only for cameras).
  • 14- Gain of the image (only for cameras).
  • 15- Exposure (only for cameras).
  • 16- Boolean flags indicating whether images should be converted to RGB.
  • 17- Currently not supported
  • 18- Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
For e.g. :- To check the frame's height and width I can use cap.get(3) and cap.get(4) respectively.
Note:- We can also modify the properties as :- cap.set(propld,value)  
            where value-new value you want 
            For e.g:- To modify the height and width to 640x480, I use
                           ret=cap.set(3,640) &
                           ret=cap.set(4,480).
cap.release( )

It releases the capture.

Wednesday, 17 September 2014

Load, Display and Save an image

Load, Display and Save an image
This post helps you to load,display and save an image using python and openCV.


Steps:

  1. Load image using cv2.imread().
  2. Display image using cv2.namedWindow() and cv2.imshow().
  3. Save the image using cv2.imwrite().
  4. Wait for keyboard button press using cv2.waitKey().
  5. Destroy all windows using cv2.destroyAllWindows().
cv2.imread(filename[,flags])
Parameters:
  • filename – Name of file to be loaded.
  • flags –
    Flags specifying the color type of a loaded image:
    • CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
    • CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
    • CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
    • >0 Return a 3-channel color image.
    • =0 Return a grayscale image.
    • <0 Return the loaded image as is (with alpha channel)

cv2.imwrite(filename,img[,params])
Parameters:
  • filename – Name of the file.
  • image – Image to be saved.
  • params –
    Format-specific save parameters encoded as pairs paramId_1, paramValue_1, paramId_2,paramValue_2, ... . The following parameters are currently supported:
    • For JPEG, it can be a quality ( CV_IMWRITE_JPEG_QUALITY ) from 0 to 100 (the higher is the better). Default value is 95.
    • For WEBP, it can be a quality ( CV_IMWRITE_WEBP_QUALITY ) from 1 to 100 (the higher is the better). By default (without any parameter) and for quality above 100 the lossless compression is used.
    • For PNG, it can be the compression level ( CV_IMWRITE_PNG_COMPRESSION ) from 0 to 9. A higher value means a smaller size and longer compression time. Default value is 3.
    • For PPM, PGM, or PBM, it can be a binary format flag ( CV_IMWRITE_PXM_BINARY ), 0 or 1. Default value is 1.

cv2.imshow(winname,image)
Parameters:
  • winname – Name of the window
  • image – Image to be shown
cv2.waitKey([delay])
Parameters:
  • delay – Delay in milliseconds. 0 is the special value that means “forever”.The function waitKey waits for a key event infinitely (when delay<=0 ) or for delay milliseconds, when it is positive. 
Example:

----------------------

import cv2

# Load an color image in grayscale
img = cv2.imread('image1.jpg',0)

#Display the image
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
cv2.imshow('image',img)

#Save the image
cv2.imwrite('result.jpg',img)

cv2.waitKey(0)
cv2.destroyAllWindows()
----------------------