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.
Metadata
Release files for cv2module 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| cv2module-0.1.2.tar.gz | 6.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cv2module-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.2 kB
Release files / cv2module-0.1.2.tar.gz
| Download URL | cv2module-0.1.2.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ccc930cde14a839afc839ce77acbf4117810273c925b6c9ee58793acee5b8b67
|
|
BLAKE2b-256 checksum How to use checksums |
713bdaf31d27cdad5f67fefb33beee342fcfb5adf1ebe95281dcd7c45f6c2043
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / cv2module-0.1.2-py3-none-any.whl
| Download URL | cv2module-0.1.2-py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0838f8268d7041f3cf2cd1806d51a6a34f3a7904a81c3db1f4ae481b62cb7a43
|
|
BLAKE2b-256 checksum How to use checksums |
d8effa3b54e14928a13dacffb347eadc16a4ab6e87b475c6d9468d67a42b2b59
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for cv2module 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.