1

i need a little bit of help :)

I am trying to remove the background of an image i took.

The picture shows a piece of wood that runs through a machine:

https://i.stack.imgur.com/KDA9k.jpg

Now i want to remove the background so that only the piece of wood is visible, it should look like this:

https://i.stack.imgur.com/6Drqk.jpg

I tried doing this with Color Detection, GrabCut and Edge detection. I was able to remove a little bit of the background but i am not really getting good results. I want to be able to use that little program to all images i will take in the future.

Additional Information:

  1. Lighting conditions of the image will probably be always a little bit different
  2. The width of the wood will always be slightly different. That means I can't just apply a "coordinate system" to it.
  3. The color of the wood will sometimes be slightly lighter or darker. That makes it difficult to just apply color detection for me.

Here is the Color Detection i tried for example:

from PIL import ImageGrab
from win32 import win32gui
import cv2
import numpy as np


while True:
    frame = cv2.imread('Wood.png')

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lower_blue = np.array([0, 0, 110])
    upper_blue = np.array([120, 255, 255])
    mask = cv2.inRange(hsv, lower_blue, upper_blue)
    result = cv2.bitwise_and(frame, frame, mask=mask)

    cv2.imshow("Result", result)
    key = cv2.waitKey(1)

    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()

Any ideas?

Thank you in advance.

Schong
  • 11
  • 3
  • This one might be difficult from a computer vision standpoint as the background is nearly the same color. You could try looking into edge detection and getting it that way. – luckyging3r Jul 23 '20 at 23:40
  • 4
    Do you have access to images of the background without the piece of wood? – stateMachine Jul 24 '20 at 00:16
  • Yes, i do have access to images of the Background without the piece of wood. Lighting conditions might be slightly different on each image though. – Schong Jul 24 '20 at 17:25
  • You can exploit the fact that you have the original background and your camera seems static. You can account for noise generated by subtle movement and lightning changes at the pixel level implementing a Gaussian Mixture Model (GMM) this is one of the most popular background subtracting algorithms. You can take things even further and implement a pixel classifier, sampling pixels from key frames during various lightning conditions and generating probability distributions to find the relation between light noise, the background model and the foreground object. – stateMachine Jul 25 '20 at 03:25
  • Check out OpenCV's GMM [implementation](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_video/py_bg_subtraction/py_bg_subtraction.html). – stateMachine Jul 25 '20 at 03:28

0 Answers0