The following is the resultant video captured by webcam. 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. bitwise_and ( frame, frame, mask = mask ) cv2. inRange ( hsv, lower_red, upper_red ) res = cv2. COLOR_BGR2HSV ) lower_red = hsv_range upper_red = hsv_range mask = cv2. Įxample : if (( len ( hsv_range ) = 0 ) & ( redo = 1 )): redo = 0 hsv_range, static_mask, static_res = cmask ( frame ) else : hsv = cv2. For the very first frame, the cmask function will open a window with the sliders to set the HSV bounds of your choice. See the code given below, here we are reading a live webcam frame and attempting to create a mask for the green color. So, this is an example of how to create a mask from a particular color from a video frame. Sometimes it is hard to predict the HSV bound for a given video frame. To save the mask and res image generated by the function click 's'. destroyAllWindows () - When you are done with selecting the values: - Click ESC or 'q' key to get the values. IMREAD_COLOR ) hsv_range, mask, res = cmask ( img ) print ( hsv_range ) cv2. imread ( "./input/watch_image.jpg", cv2. This function will open a window with the sliders to set the HSV bounds of your choice.Įxample : img = cv2. The cmask (color mask) function is been imported from the cv2module. See the code given below, here we are reading a watch image and attempting to create a mask for the yellow color. So, this is an example of how to create a mask from a particular color from an image. Sometimes it is hard to predict the HSV bound for a given image. 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. # loss = 1 means there is a loss of image while rotating it. But by using cv2module.rotate you have the control over that loss.Įxample : # loss = 0 means there is no loss of image while rotating it. However, if you use these functions then you will lose some of the image parts. To rotate an image in OpenCV, cv2.getRotationMatrix2D and cv2.warpAffine is used. So the control is in your hands.Įxample : image = cv2module. 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). However, you have to use your intuitions in a selection of width and height to maintain the aspect ratio. To resize an image in OpenCV, cv2.resize function is used. This cmask function will return the HSV lower and upper bound, mask and the resultant image. Provided you already have NumPy and OpenCV installed, the cv2module a package can be simply installed using pip. Return cv2.resize(image, dim, interpolation=inter)Ĭv2.imshow('width_100', maintain_aspect_ratio_resize(image, width=100))Ĭv2.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. # Calculate the ratio of the width and construct the dimensions # We are resizing width if height is none # Calculate the ratio of the height and construct the dimensions # We are resizing height if width is none # Return original image if no need to resize # Grab the image size and initialize dimensions Here's a function to upscale or downscale an image by desired width or height while maintaining aspect ratio # Resizes a image and maintains aspect ratioĭef maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA): Thumbnail = cv.resize(im, (round(width / 10), round(height / 10)), interpolation=cv.INTER_AREA) Using your code with cv2 import cv2 as cv Img = cv2.resize(img, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) Scaling_factor = max_width / float(width) If max_width/float(width) < scaling_factor: Scaling_factor = max_height / float(height) If max_height < height or max_width < width: # only shrink if img is bigger than required Example shrink image to fit a max height/width (keeping aspect ratio) import cv2 To shrink an image, it will generally look best with INTER_AREA interpolation, whereas to enlarge an image, it will generally look best with INTER_CUBIC (slow) or INTER_LINEAR (faster but still looks OK). Where fx is the scaling factor along the horizontal axis and fy along the vertical axis. The new size can be specified:ĭst = cv2.resize(src, (2*width, 2*height), interpolation = cv2.INTER_CUBIC)ĭst = cv2.resize(src, None, fx = 2, fy = 2, interpolation = cv2.INTER_CUBIC),
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |