Draw a rectangular shape and extract objects using Python’s OpenCV
OpenCV is an open-source computer vision and machine learning software library. Various image processing operations such as manipulating images and applying tons of filters can be done with the help of it. It is broadly used in Object detection, Face Detection, and other Image processing tasks.
Let’s see how to draw rectangular shape on image and extract the objects using OpenCV.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Above piece of code will work with only black background image. But rectangles can be drawn to any images. We can write a program which allows us to select desired portion in an image and extract that selected portion as well. The task includes following things –
- draw shape on any image
- re-select the extract portion for in case bad selection
- extract particular object from the image
Run : Save the file as capture_events.py and for testing select a demo picture which is located in the same directory. Now, execute the following command –
python capture_events.py --image demo.jpg
Output: First select the desired portion from the image. In addition, we can remove bad selection by pressing ‘r’ as programmed for making a new proper selection.
Fig: Selected Portion
Now after selecting a proper selection like above, just press ‘c’ to extract, as programmed.
Fig: Cut Portion