cv2module

A package that provides various functions to assist the OpenCV workflow


Keywords
cv2, color, mask, opencv-add-ons, opencv-python, python
License
MIT
Install
pip install cv2module==0.1.2

Documentation

cv2module

This is a package is created to assist the smooth workflow with OpenCV by providing essentials functions such as creating Color Masks of any image/video feed or resizing and rotating it.

Installation

Provided you already have NumPy and OpenCV installed, the cv2module a package can be simply installed using pip.

$ pip install cv2module

What’s In This Document

Import and Usage

  • Import as given below
from cv2module import cmask
  • Parameters and returned values
hsv_range, mask, res = cmask(Image)

This cmask function will return the HSV lower and upper bound, mask and the resultant image.


• • • •

Resize Image

To resize an image in OpenCV, cv2.resize function is used. However, you have to use your intuitions in a selection of width and height to maintain the aspect ratio. And sometimes it is hard to predict so by using cv2module.resize function you can simply specify width or else you can also specify both (width and height). So the control is in your hands.

Example :

image = cv2module.resize(ip_image, 500)

Output :

Original Image Resized Image
input img output img


• • • •

Rotate Image

To rotate an image in OpenCV, cv2.getRotationMatrix2D and cv2.warpAffine is used. However, if you use these functions then you will lose some of the image parts. But by using cv2module.rotate you have the control over that loss.

Example :

    # loss = 0 means there is no loss of image while rotating it.
    # loss = 1 means there is a loss of image while rotating it.
    res = cv2module.rotate(ip_image, 40, loss=1)
- The default value of the loss is 1 so it will crop out the image when it's been rotated.

Output :

loss = 0 loss = 1
input img output img


• • • •

Generate Mask for Image

Sometimes it is hard to predict the HSV bound for a given image. So, this is an example of how to create a mask from a particular color from an image. See the code given below, here we are reading a watch image and attempting to create a mask for the yellow color. The cmask (color mask) function is been imported from the cv2module. This function will open a window with the sliders to set the HSV bounds of your choice.

Example :

img = cv2.imread("../input/watch_image.jpg", cv2.IMREAD_COLOR)

hsv_range, mask, res = cmask(img)

print(hsv_range)
cv2.imshow('Original frame', img)
cv2.imshow('mask', mask)
cv2.imshow('resultant', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
- When you are done with selecting the values:
-   Click ESC or 'q' key to get the values.
-   To save the mask and res image generated by the function click 's'.

To view the full example Refer.

Output :

Input Image Output Image
input img output img


• • • •

Generate Mask for Video Feed

Sometimes it is hard to predict the HSV bound for a given video frame. So, this is an example of how to create a mask from a particular color from a video frame. See the code given below, here we are reading a live webcam frame and attempting to create a mask for the green color. For the very first frame, the cmask function will open a window with the sliders to set the HSV bounds of your choice [You can exit and get the values or can save the mask and resultant image].

Example :

if((len(hsv_range) == 0) & (redo == 1)):
    redo = 0
    hsv_range, static_mask, static_res = cmask(frame)
else:
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    lower_red = hsv_range[0]
    upper_red = hsv_range[1]

    mask = cv2.inRange(hsv, lower_red, upper_red)
    res = cv2.bitwise_and(frame, frame, mask=mask)

    cv2.imshow('frame', frame)
    cv2.imshow('mask', mask)
    cv2.imshow('res', res)
- After this webcam feed will remain ON and you have two choices as:
-   if you want to again create the mask for another frame then click 'r' to repeat.
-   else click ESC or 'q' to exit.

To view the full example Refer.

Output :
The following is the resultant video captured by webcam. In this I'm holding the mobile with the green color on the screen.

output video


• • • •

Future Development

I am planning to create different functions for this cv2module such as cropping an image, creating a scanner, creating different haar cascades for different objects, etc.

License

Licensed under the MIT License.