Search code examples
rmosaic

I am trying to run mosaic for two multi-band images.Output saved as Single band


This is final output I got, I'm supposed to get the final output as a single file with two bands:

enter image description here

Following is the code which I am using:

A11 <-brick("E:/Official/PROJECTS/R_Progrm/1.tif") // to read multiband image
B11<-brick("E:/Official/PROJECTS/R_Progrm/3.tif")  // To read multiband image
mos1 <- mosaic(A11,B11,fun=max,tolerance=0.5, 
filename="Mosaic_new",overwrite=TRUE)
plot(mos1,main="Mosaic_new1")
writeRaster(x=mos1,file="E:/Official/PROJECTS/R_Progrm/M11.tif",options="INTERLEAVE=BAND",format="GTiff",datatype="FLT8S",overwrite=TRUE)

Solution

  • The plot that you have shown in your question, is showing both the bands of your output image. So, there should not be any problem with your code and its output. If the problem is related to visualizing all the bands as an RGB Image, then you have to modify the parameters of plot function that means you have to provide the band combination. For example:

    plotRGB(a, r = 4, g = 3, b = 2, axes=TRUE, main="3 Band Color Composite Image")
    box(col="white")
    

    Also, you can try the code given below which is working fine for me, and I hope it will resolve your problem.

    a <- stack("Path to first raster")
    b <- stack("Path to second raster")
    rast.list <- list(a,b)     
    rast.list$fun <- mean
    rast.mosaic <- do.call(mosaic,rast.list)
    plot(rast.mosaic)
    writeRaster(rast.mosaic,"Output_Raster_Name",format="GTiff",overwrite=TRUE)