0

I have 100+ raster images with the exact same extent. They consist of values which are either 0 or 1. I would like to have 1 single image with all values for that pixel summed together.

I have done it in a certain way, but it creates unwanted/unexplainable edges at the places where water and land touch. I except water to be somewhat brighter but especially the size of the blocks touching the water confuses me. Can this be solved or be done in a different way so this doesn't happen?

My code:

def Summing(images_to_sum):
    """
    Creates one image out of all images in given list.
    """
    path_out = r"C:\Documents\UFV.jp2"
    images_used = 2

    def Summ(path_1, path_2):
        """
        Sums up the two images from given paths.
        """
        with rio.open(path_1) as src_1:
            array_1 = src_1.read()
            profile = src_1.profile

            with rio.open(path_2) as src_2:
                array_2 = src_2.read()

                # Sum the two
                result = array_1 + array_2

        with rio.open(path_out, 'w', **profile) as dst:
            dst.write(result)

    # Creating first image, so others can be added
    Summ(images_to_sum[0], images_to_sum[1])
    images_to_sum.pop(0)
    images_to_sum.pop(0)

    # Adding all the other images for this granule
    for image in images_to_sum:
        Summ(path_out, image)
        images_used += 1

This is a part of the result. As you can see there are some big blocks at the edges of the land. enter image description here

Niek
  • 51
  • 8
  • I see you write as JPG. Do you keep all images as JPG? Maybe it is problem because you have JPG files - and JPG has to replace some pixels to make it simpler to compress. – furas Jun 03 '22 at 14:50
  • Yes, all images were JPEG2000 format. I thought of this as well and tried to do everything but in tiff format. This is a lot more precise (since JPG does indeed compress a lot) but unfortunately doesn't solve the problem. – Niek Jun 03 '22 at 14:59
  • did you test it with smaller number of images? Maybe some images have wrong values and this makes problem. Other idea - if you have more than 256 images then you may get value bigger than 256 and it may reduce it to 256 because `8-bit` image can't have biggere value. maybe you should convert all values to range `0...255`. – furas Jun 03 '22 at 15:06
  • I have tested it with smaller images. Until 10-15 the blocks aren't visible. After that they slowly start to appear the more images are added. The number of images used is not higher than 256. – Niek Jun 04 '22 at 12:31
  • every image may have small difference in this region and you can't see this difference - but when you add values then you cummulate differences and you see it. Problem can be in original images which are JPG - compression images to JPG could create small differences (for better compression) and now you can't remove it. – furas Jun 04 '22 at 13:04

0 Answers0