# Extract object from the image of a box having object.

Hello Everyone, I have a box, transparent from the front and i am placing camera on the front transparent panel to capture the image of the internal, most of the time the box is empty, but suppose someone places an object inside this box, then i have to just extract this object from the image captured.

(My real aim is to recognize the object placed inside the box, but first step is to extract the object and then extract features to generate a training model, but for now i am only interested in extracting the object from the image)

I am new to OpenCV and using it with Python and used found few OpenCV functions which can help me.

• GrabCut, this works perfectly for me, i am able to just extract the object, provided that i mark the rectangle over the object, but as object can be anywhere inside the box so its not possible to draw the exact size rectangle of the object.
• Difference of Image, Since i have empty cavity box image and when the object is present, i can cv2.absdiff function to calculate the difference between the image, but this doesn't work properly in most of the cases, as it uses pixel by pixel difference calculations, and due to this results are weird, plus change in light conditions also makes it difficult.
• Back Ground Subtraction, i read few post on this and it looks this is what i need, but the example i got is of video, and i didn't understand how to make it work with just two images.

The code for back ground subtraction is as follow, even it doesn't work that much properly for short distances.

cap = cv2.VideoCapture(0)
fgbg = cv2.createBackgroundSubtractorMOG2()
fgbg2 = cv2.createBackgroundSubtractorKNN()

while True:
cv2.namedWindow('Real', cv2.WINDOW_NORMAL)
cv2.namedWindow('MOG2', cv2.WINDOW_NORMAL)
cv2.namedWindow('KNN', cv2.WINDOW_NORMAL)
cv2.namedWindow('MOG2_ERODE', cv2.WINDOW_NORMAL)
cv2.namedWindow('KNN_ERODE', cv2.WINDOW_NORMAL)
cv2.imshow('Real', frame)
kernel = np.ones((3,3), np.uint8)