PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 5, 2022

[FIXED] How do I normalize the pixel value of an image to 0~1?

 May 05, 2022     image, normalization, numpy, python     No comments   

Issue

The type of my train_data is 'Array of unit 16'. The size is (96108,7,7). Therefore, there are 96108 images.

The image is different from the general image. My image has a sensor of 7x7 and 49 pixels contain the number of detected lights. And one image is the number of light detected for 0 to 1 second. Since the sensor detects randomly for a unit time, the maximum values of the pixel are all different.

If the max value of all images is 255, I can do 'train data/255', but I can't use the division because the max value of the image I have is all different. I want to make the pixel value of all images 0 to 1. What should I do?


Solution

import numpy as np

data = np.random.normal(loc=0, scale=1, size=(96108, 7, 7))
data_min = np.min(data, axis=(1,2), keepdims=True)
data_max = np.max(data, axis=(1,2), keepdims=True)

scaled_data = (data - data_min) / (data_max - data_min)

EDIT: I have voted for the other answer since that is a cleaner way (in my opinion) to do it, but the principles are the same.

EDIT v2: I saw the comment and I see the difference. I will rewrite my code so it is "cleaner" with less extra variables but still correct using min/max:

data -= data.min(axis=(1,2), keepdims=True)
data /= data.max(axis=(1,2), keepdims=True)

First the minimum value is moved to zero, thereafter one can take the maximum value to get the full range (max-min) of the specific image.

After this step np.array_equal(data, scaled_data) = True.



Answered By - 3dSpatialUser
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing