Search code examples
opencvimage-processingscikit-image

regarding the ways of removing surrounding background areas during processing the image


The attached image, showed as follows

enter image description here

It includes some surrounding areas that represents the noise or background introduce while getting the image.

enter image description here

How to remove this part while processing the image. For instance, when I try to segment the original image, I got the following result, where the background areas are also included.

enter image description here


Solution

  • You could use a flood fill starting at the bottom-right corner to fill all pixels less than some "fuzziness" distance (in shades of grey terms rather than in geometric distance terms) with black so they all come out to the same class.

    Here I do it with ImageMagick just in Terminal, and colour using red and blue, rather than black, to show the concept:

    convert input.jpg -fuzz 15% -fill red -floodfill +1140+760  black  result15.jpg
    

    enter image description here

    Or, allowing slightly fewer colours (darker) to match via fuzziness:

    convert input.jpg -fuzz 10% -fill blue -floodfill +1140+760  black  result10.jpg
    

    enter image description here

    You can do this with OpenCV in Python, and Wand and other tools. Here is an example showing how to do a floodfill withPIL/Pillow.