import cv2
import numpy as np
from .utils import Image
from osgeo import gdal, gdalconst
[docs]def Brovery(src:Image, pan:Image):
"""
Using Brovery method to fusion image
The source image should not be upsampled
:param src: `source` image, the image should be RGB color space
:param pan: Panchromatic image, the image should be grayscale
:return: fusion image
:rtype: `pyfusion.utils.Image`
"""
assert src.image.RasterCount == 3, "src image should be 3 bands"
assert pan.image.RasterCount == 1, "pan image should be 1 band"
# use VRT to create result image
driver = gdal.GetDriverByName("VRT")
dstDs = driver.Create("", pan.image.RasterXSize, pan.image.RasterYSize, 3, options=["INTERLEAVE=PIXEL"])
dstImage = Image(dstDs)
dstImage.data = _Brovery(src.data, pan.data)
return dstImage
def _Brovery(src, pan):
"""
Using Brovery method to fusion image
The source image should not be upsampled
:param src: `source` image, the image should be RGB color space
:param pan: Panchromatic image, the image should be grayscale
:return: fusion image
:rtype: `numpy.ndarray`
"""
img_bgr = cv2.resize(src, pan.shape)
pan = pan.astype(np.float32)
r,g,b = cv2.split(img_bgr)
b = b.astype(np.float32)
g = g.astype(np.float32)
r = r.astype(np.float32)
rf = (3*pan)*r/(b+g+r)
gf = (3*pan)*g/(b+g+r)
bf = (3*pan)*b/(b+g+r)
bf = bf.astype(np.uint8)
gf = gf.astype(np.uint8)
rf = rf.astype(np.uint8)
img_brovery = cv2.merge((rf,gf,bf))
return img_brovery