Package: cv2module
cv2moduleThis 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.InstallationProvided you already have NumPy and OpenCV installed, thecv2modulea package can be simply installed usingpip.$pipinstallcv2moduleWhatâ€™s In This DocumentImport and UsageResize ImageRotate ImageGenerate Mask for ImageGenerate Mask for Video FeedFuture DevelopmentLicenseImport and UsageImport as given belowfromcv2moduleimportcmaskParameters and returned valueshsv_range,mask,res=cmask(Image)This cmask function will return the HSV lower and upper bound, mask and the resultant image.• • • •Resize ImageTo 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 usingcv2module.resizefunction 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 ImageResized Image• • • •Rotate ImageTo 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 usingcv2module.rotateyou 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 = 0loss = 1• • • •Generate Mask for ImageSometimes 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 exampleRefer.Output :Input ImageOutput Image• • • •Generate Mask for Video FeedSometimes 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=0hsv_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 exampleRefer.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 DevelopmentI am planning to create different functions for thiscv2modulesuch as cropping an image, creating a scanner, creating different haar cascades for different objects, etc.LicenseLicensed under theMIT License.