0

I'm working on MODIS NDVI composite data (single band). I converted the image to a numpy array and performed filtering operations. When i'm saving the new numpy array as an image I get a 3 band image instead of single band image. How can I save it as a single band image?

#read an image
img = cv2.imread("D:/trial/MOD09_NDVI.A2017001.tif")
cv2.namedWindow('Unfiltered Image', cv2.WINDOW_NORMAL)
cv2.imshow('Unfiltered Image',img)
cv2.waitKey(0)
#apply filter
myIMG = cv2.blur(img, (5,5))
cv2.namedWindow('Blur Image', cv2.WINDOW_NORMAL)
cv2.imshow('Blur Image', myIMG)
cv2.waitKey(0)
scipy.misc.imsave('D:/trial/blur_55.tif', myIMG)
------------
scipy.misc.imsave('D:/trial/blur_55.tif', myIMG)

saves a 3 band image instead of original single band image

Bera
81.6k14 gold badges84 silver badges197 bronze badges
asked Feb 13, 2018 at 5:49
1

2 Answers 2

1

Why are you using for saving Scipy? I think you can use function imwrite from CV2.

For example:

cv2.imwrite('D:/trial/blur_55.tif', myIMG)

or you can use also another libraries. For exammple PIL or Matplotlib.

Example for Matplotlib:

from matplotlib import pyplot as plt
plt.imshow(myIMG)
plt.show()
answered Feb 15, 2018 at 16:54
1
  • I was using imwrite as well but I needed to assign spatial references as well. I ended up using GDAL functions for it. Thank you for the reply Commented Feb 16, 2018 at 9:54
1

You can use the gdal driver.Create(filename, xsize, ysize, [bands], [data_type], [options]) function.

For example:

from osgeo import gdal
driver = gdal.GetDriverByName('GTiff')
out_ds = driver.Create('raster.tif', array.shape[0], array.shape[1], 1)
out_band = out_ds.GetRasterBand(1)
out_band.WriteArray(array)
out_ds.FlushCache()
del out_ds, out_band
answered Feb 15, 2018 at 17:20
1
  • I ended up using something similar. Thanks for the reply Commented Feb 16, 2018 at 9:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.