Posts

GrabCut: object extraction by separating the foreground and background of an image

Image
GrabCut is an algorithm that is used to extract the foreground from an image.  OpenCV has a python implementation of this algorithm which we can use for our purpose. GrabCut in OpenCV OpenCV provides the implementation of the GrabCut as a function cv2.grabCut()  . The function takes seven parameters as arguments and returns three parameters. cv.grabCut(img, mask, rect, bgdModel, fgdModel, iterCount[, mode]) Parameters: img : take an image as input ( 8-bit 3-channel image). mask : input/output 8-bit single-channel mask. When we set parameter mode to cv2.GC_INIT_WITH_RECT ,  the mask will be initialized automatically.  rect : coordinates of a rectangle or bounding box which includes the foreground object in the format (x, y, w, h) . The parameter is only used when the parameter mode==GC_INIT_WITH_RECT . bgdModel : temporary array for the background model and use it int

OpenCV: extraction of the image part within the bounding box

Image
We can extract the part of an image within the bounding box. The bounding box can be a rectangle, triangle, or polygon. OpenCV provides functions to draw geometric shapes in the image, with these functions we can do our extraction job. Extraction of the image part within the bounding box We will extract the part from the below image which is lies within the blue rectangle. We will do our job by following these steps Load and visualize the image. Create an empty image. Draw the bounding box. Extract the part within the bounding box. Visualize the result. Let's implement the code. Import necessary packages # Import necessary packages import numpy as np import cv2 import matplotlib.pyplot as plt Load and visualize We will load the image into memory with the OpenCV function cv2.imread() and visualize the image by plotting with the matplotlib package. # Load image f

Popular posts from this blog

GrabCut: object extraction by separating the foreground and background of an image

OpenCV: extraction of the image part within the bounding box