0

I am trying to get just the image of a human hand separated from background. I first captured the background which will be static in my case and then captured image with a hand in it. Then I used cv2.absdiff() to subtract both the images but the result is not as expected. Please help me get this right. Here is the output:

Original_Image

Background_Image

Output

Code:

while(cap.isOpened()):

    grabbed, frame = cap.read()

    if not grabbed:
        continue

    Original_Image = frame.copy()
    Original_Image = cv2.cvtColor(contourFrame,cv2.COLOR_BGR2GRAY)

    Output_Image = cv2.absdiff(Background_Image, Original_Image)
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Murali
  • 1
  • 4
  • i think you've added wrong image as a `Background image` because it is the same as output and doesn't look like a background one. – Dmitrii Z. Sep 11 '18 at 21:24
  • Thank you so much. I have changed it. – Murali Sep 11 '18 at 22:00
  • Possible duplicate of [how to remove background image and get fore image](https://stackoverflow.com/questions/2100064/how-to-remove-background-image-and-get-fore-image) – Cris Luengo Sep 11 '18 at 22:04
  • take a look at [this question](https://stackoverflow.com/questions/7958786/opencv-background-substraction) Simply calling absdiff is obviously not enough. You need to find a mask of foreground object and apply it to the image. – Dmitrii Z. Sep 11 '18 at 22:09
  • choose Diff_Image = cv2.absdiff(Background_Image, Original_Image) and Output_Mask = Diff_Image > 15 then copy only masked pixels to output image – Micka Sep 12 '18 at 04:04

1 Answers1

0

If you only subtract the background image from the foreground image you are not able to separate foreground objects because you are changing them by subtracting the background they conceal from them (which will result in the two ugly dark lines visible in your image). The possible solution to that: Subtract the background from the foreground only in areas where both images are equal (or comparable with a arbitrary tolerance to account for slightly changed lighting conditions). For image you most likely have to add a specific tolerance for differences between background and foreground pixels(maybe 15 to 25?) because the hand is blocking incoming light and thus the background image is slightly lighter than the foreground image.

SilverMonkey
  • 1,003
  • 7
  • 16