What purpose does .astype("uint8") have here?

Issue

(score,diff)= structural_similarity(original_gray,tempered_gray,full=True)

diff = (diff*255).astype("uint8")
print("SSIM:{}".format(score))

The above mentioned code is a snippet from a program that matches two images using their SSIM score. What I don’t understand here is the function of .astype("uint8"), and why are we multiplying diff by 255?

Solution

Imagine a 2D array representing a greyscale (single channel) image. In general, the image data can come at you in one of two ways:

  1. Pixel values are floats ranging from 0 to 1, or…
  2. Pixel values are integers ranging from 0 to 255.

Occasionally you might also see levels expressed as percentages between 0 and 100, but that’s basically the same as (1) above. Note that (2) can only express 255 grey levels, whereas (1) has almost arbitrary precision.

Sometimes you need (1) but have (2), or vice versa. Because software or preference or whatever.

To convert from (1) to (2) you must multiply by 255 and ensure that you have integers. In order to use the least amount of memory, you only need 8-bit integers, but those come in two flavours: signed (int8, ranging -128 to +127) and unsigned (uint8, ranging 0 to 255). For images you always want the unsigned kind.

To convert from (2) to (1) you can just divide by 255, making sure the result can hold floating point numbers of the desired precision.

So in your code, diff is being converted from a floating point image to an 8-bit one.

Answered By – kwinkunks

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.