I am looking for an algorithm or function in GDAL that can remove black pixels out of image boundaries. Note that I've tried gdal_fillnodata (gdal.FillNoData) and the result was good in most cases; But in some images I can find a lot of pixels in middle of image which are black and so they will be removed like out of boundary pixels.

What can I do for making difference between interior and exterior pixels and then removing them?

I use gdal with python.


QGIS or. GDAL NEARBLACK is what you are looking for http://www.gdal.org/nearblack.html

Convert nearly black/white borders to black.

nearblack [-of format] [-white | [-color c1,c2,c3...cn]*] [-near dist] [-nb non_black_pixels] 

I would recommend using the gdal_calc.py here is an implementation and you can check other examples to work into your problem.

I idea would be to assign a new value to a pixel needed.

  • Update: I used gdl_calc to create image mask and gdal_fillnodata for removing interior black pixels, but now faced another problem. Because of filling of NoData pixels with the function gdal.FillNoData(), some exterior pixels merge with interior ones and so the image's boundary enlarges while I need the real and precise mask for the image. – hELLISh Aug 28 '17 at 16:14

I found that numpy's binary_fill_holes function is the solution. I converted the dataset image (I) to array by I.ReadAsArray() function and then used ndimage.binary_fill_holes on the created array. NearBlack takes some time to delete border's black pixels. So for deleting unnecessary border pixels I labeled pixels using ndimage.measurements.label function and then removed labels contained less that 30 percent of image pixels.

Thank you all guys for your recommendations

  • Could you show the code you have used so that other people with similar question can use it? – wondim May 10 '19 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.