Let`s see how to draw a rectangular shape on an image and extract objects using OpenCV.
The above piece of code will only work with a black background image. But rectangles can be drawn on any images. We can write a program that allows us to select the desired portion of the image and extract that selected portion. The task includes the following things —
Execute : save the file as capture_events.py and for testing select a demo picture located in the same directory. Now run the following command —
python capture_events.py --image demo.jpg
Output : First select the part of the image you want. Alternatively, we can remove the incorrect selection by pressing "r" as programmed to create a new correct selection.
Now after choosing the correct selection as above, just press" c "to extract as programmed.
Fig: Cut part