A package that provides various functions to assist the OpenCV workflow
Project description
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
- Resize Image
- Rotate Image
- Generate Mask for Image
- Generate Mask for Video Feed
- Future Development
- License
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 |
---|---|
• • • •
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 |
---|---|
• • • •
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 |
---|---|
• • • •
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.
• • • •
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cv2module-0.1.2.tar.gz
.
File metadata
- Download URL: cv2module-0.1.2.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccc930cde14a839afc839ce77acbf4117810273c925b6c9ee58793acee5b8b67 |
|
MD5 | a3aa6de436bed49777d34c369017ac81 |
|
BLAKE2b-256 | 713bdaf31d27cdad5f67fefb33beee342fcfb5adf1ebe95281dcd7c45f6c2043 |
File details
Details for the file cv2module-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: cv2module-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0838f8268d7041f3cf2cd1806d51a6a34f3a7904a81c3db1f4ae481b62cb7a43 |
|
MD5 | 6cfa25be151f1153f6ac7bb270a988d6 |
|
BLAKE2b-256 | d8effa3b54e14928a13dacffb347eadc16a4ab6e87b475c6d9468d67a42b2b59 |