Package: opencv-wrapper
Simple wrapper for opencv-pythonOpenCV Wrapper is a simpler wrapper for theopencv-pythonpackage. As the mentioned package only gives access to OpenCV functions, in a C++ style, it can be tedious to write. There is also no support for the OpenCV classes like Rect, Point etc. OpenCV Wrapper attempts to fix that.The package is at an early state, and contributions are welcome! The contents of the package
have been a demand-and-supply model, where functionality is added as new tedious things inopencv-pythonare found. Do not hesitate to file an issue, requesting new functionality or
enhancement proposals!InstallationGetting started is easy, using pip or pipenv!pip(env)installopencv-wrapperThis also installs the dependenciesopencv-pythonandnumpy, if not already present.ExamplesReading videosThis code speaks for itself.Vanilla OpenCV:importcv2ascvvideo=cv.VideoCapture("path/to/file")ifnotvideo.isOpened():raiseValueError("Could not open video")whileTrue:ok,frame=video.read()ifnotok:breakcv.imshow("Frame",frame)ifcv.waitKey(0)&0xFF==ord('q'):breakvideo.release()opencv_wrapper:importcv2ascvimportopencv_wrapperascvwwithcvw.load_video("path/to/file")asvideo:forframeincvw.read_frames(video,start,stop,step):cv.imshow("Frame",frame)ifcvw.wait_key(0)==ord('q'):breakRotate A Color WheelSay we have the following color wheel image, which we want to rotate.We of course want to rotate it at it's center, which is not in the center
of the image. A possible solution using OpenCV would beimportcv2ascvimportrandomimg=cv.imread("resources/color_wheel_invert.png")gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)_,otsu=cv.threshold(gray,250,255,cv.THRESH_BINARY_INV)_,contours,_=cv.findContours(otsu,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)contour=contours[0]rect=cv.boundingRect(contour)# Gives a tuple (x, y, w, h)x,y,w,h=rectcolor=[random.randint(0,255)for_inrange(3)]degrees=60center=(x+w/2),(y+h/2)rotation_matrix=cv.getRotationMatrix2D(center,degrees,1)rotated_image=cv.warpAffine(img,rotation_matrix,gray.shape[::-1])cv.rectangle(rotated_image,(x,y),(x+w,y+h),color)cv.imshow("Image",rotated_image)cv.waitKey(0)We first convert the image to gray scale. The color wheel in gray scale does not
contain any values of pure white. We can therefore threshold the image at a high
threshold, to segment the color wheel.We then find contours in the image (which in this case only will be one contour), and
find the bounding rectangle enclosing the contour. From this rectangle we can find the center
point by the means of the top left corner, the height and width. We use this to create
a rotation matrix, and call the affine warp function. Lastly, we draw a rectangle around
the found contour. This is just for viewing pruposes.We get the following result.Although a perfectly fine solution, we cannot help but rotate the whole image.
Here is a solution using opencv_wrapper.opencv_wrapper:importcv2ascvimportopencv_wrapperascvwimg=cv.imread("resources/color_wheel_invert.png")gray=cvw.bgr2gray(img)otsu=cvw.threshold_binary(gray,250,inverse=True)contours=cvw.find_external_contours(otsu)contour=contours[0]rect=contour.bounding_rect# Gives a Rect objectdegrees=60center=rect.center# Gives a Point objecttop_left=rect.tl# Gives a Point objectnew_center=center-top_leftimg[rect.slice]=cvw.rotate_image(img[rect.slice],new_center,degrees,unit=cvw.AngleUnit.DEGREES)cvw.rectangle(img,rect,cvw.Color.RANDOM)cv.imshow("Image",img)cvw.wait_key(0)We again follow the same approach. However, with the Contour class, we can
simply call the bounding rect property. This yields a Rect object, which
has a center property. Convenient.Where we before were left with no (obvious) choice but to rotate the whole image,
we can now simply slice the image at the rectangle, only rotating the figure itself.
For this exact purpose, it doesn't make much difference, but it is a demonstration.
We find the new center from which to rotate, and simply call the rotate image function.
We can here choose whether to use degrees or radians. Lastly we draw a rectangle with
a random color.We get the following result.Not only is this a tad less tedious to write, but we are also easily able to
rotate only the relevant part of the circle by slicing¹. The contour, rectangle
and point objects are also an ease to work with.Other Area of EaseWhile not providing examples, there are many other parts of the OpenCV
that become an ease to work with, when using opencv_wrapper. Areas includeMorphologyImage normalizationColor conversionThresholdingImage smoothing¹Disclosure: The slicing is not that hard to accomplish, fromx, y, w, h.
We can create it like thisour_slice=(slice(y,y+h),slice(x,x+w))