SNAP vs QGIS images

I have been cropping RBG images from sentinel 2 datasets using SNAP and QGIS in order to be tested by an algorithm that classifies the data into two categories, agricultural and non agricultural. The weird thing is that the same image gives different results if its cropped using SNAP and they are not correct. The differences of the images are visible yet i don’t understand how or why.

sliced_QGIS sliced_SNAP

Also, below are the square sub-boxes of each. From 227x227 we keep their bottom right track. Specifically from 221 to 227 for columns and lines. If that is of any importance.

SNAP [ꓯ vijϵΑ: 0≤vij≤255]

49    84   131   119   109    83   112

62   116   154    68   101    74   107

16    86   157   128   150   107   123

81   142   188   202   212   150   142

231   230   247   250   243   168    79

241   240   255   253   227   147    52

248   246   255   251   238   156    55

QGIS [ꓯ vijϵΑ: 25≤vij≤196]

53    60    65    61    60    57    61

58    61    68    58    59    57    60

52    57    67    65    66    61    62

53    62    69    70    73    66    65

70    66    95    83    79    64    59

90    87    97   101    82    63    53

89    90    91    98    89    65    53

Some possible reasons:

  1. Did you somehow export the data to another format than BEAM DIMAP?

  2. Is a scaling factor (explained here) applied to the S2 image?

  3. Does QGIS show the true pixel value or the stretched one according to the min/max settings in the properties (explained here and here)?

Both images are exported as jpeg.

There is no scaling in the information section of the original product.

SNAP min/max in green vs QGIS min/max in green

Is this what you mean in the min/max settings?

I don’t think a JPEG conserves the actual pixel values very well. As you see the data is scaled between 0 and 255 (8 bit).

Why don’t you simply use the BEAM DIMAP data directly? You find an img in the data folder which you can load in QGIS and go sure that both programs use the same information.

How does that affect the testing algorithm though? Does QGIS exporting mechanism is more reliable than SNAP’s? The jpeg format is better all in all?

for classification purposes, it probably doesn’t make a big difference but your questions was why the values changed.

I wouldn’t use the JPEG still, because it involves compression and doesn’t provide the image quality you need.

Actually the question was if the difference in the values of the resulting jpeg image made it harder for the algorithm to classify the images correctly.
The same product was used (from copernicus sentinel hub) to select and crop the patches. Yet the results were different.That is why i am buffled.
The use of jpeg is a requirement in the project i am working on that I cannot change.

depends on the algorithm, I’d say. A random forest, for example, works with thresholding. It doesn’t care if its scaled between 0 and 1 or between 1 and 1000. But others, especially clustering algorithms will maybe produce a different result.

Still I am worried about using JPGs for such fine-scaled objectives.

It’s difficult to answer your question without knowing how your algorithm works.
There are different approaches and formats you can use in QGIS to crop an image. It looks like you are just exporting a rendered image thus altering your original data.
If I had to do it in order to perform a later analysis I’d do it in a way that the original values were not altered so I would extract the raster without compressing or changing pixel depth. This way you would be sure that your cropping isn’t affecting the results, otherwise…you will have to check it yourself.

1 Like

How would you extract it without changing pixel depth?

Raster > Extraction > Clip Raster by Mask Layer
Advanced parameters: Profile > No compression
Use input layer data type

You can test it with a .jpg but I don’t think is a good option since it is a compression format and your values might change, just manually check the pixels or perform a raster subtraction (Raster > Raster calculator) with your old and new raster and see if there are changes.