How to implement global contrast normalization in python?
$begingroup$
I try to implement global contrast normalization in python from Yoshua Bengio's deep learning book. From the book, to get normalized image using global contrast normalization we use this equation
$$mathsf{X}^{prime}_{i,j,k}=sfrac{mathsf{X}_{i,j,k}-overline{mathsf{X}}}{maxleftlbrace epsilon, sqrt{lambda+frac{1}{3rc}sum_{i=1}^{r}sum_{j=1}^{c}sum_{k=1}^{3}(mathsf{X}_{i,j,k}-overline{mathsf{X}})^2}rightrbrace }$$ where $mathsf{X}_{i,j,k}$ is tensor of the image and $mathsf{X}^{prime}_{i,j,k}$ is tensor of normalized image, and $overline{mathsf{X}} = frac{1}{3rc}sum_{i=1}^{r}sum_{j=1}^{c}sum_{k=1}^{3} mathsf{X}_{i,j,k}$ is the average value of the pixels of the original image $epsilon$ and $lambda$ is some constant it is usually set $lambda=10$ and $epsilon$ is set to be a very small number, and here is my implementation:
import Image
import numpy as np
import math
def global_contrast_normalization(filename, s, lmda, epsilon):
X = np.array(Image.open(filename))
X_prime=X
r,c,u=X.shape
contrast =0
su=0
sum_x=0
for i in range(r):
for j in range(c):
for k in range(u):
sum_x=sum_x+X[i][j][k]
X_average=float(sum_x)/(r*c*u)
for i in range(r):
for j in range(c):
for k in range(u):
su=su+((X[i][j][k])-X_average)**2
contrast=np.sqrt(lmda+(float(su)/(r*c*u)))
for i in range(r):
for j in range(c):
for k in range(u):
X_prime[i][j][k] = s * (X[i][j][k] - X_average) / max(epsilon, contrast)
Image.fromarray(X_prime).save("result.jpg")
global_contrast_normalization("cat.jpg", 1, 10, 0.000000001)
original image
result image
I got an unexpected result. What is wrong my implementation ?
python image-classification computer-vision preprocessing
$endgroup$
add a comment |
$begingroup$
I try to implement global contrast normalization in python from Yoshua Bengio's deep learning book. From the book, to get normalized image using global contrast normalization we use this equation
$$mathsf{X}^{prime}_{i,j,k}=sfrac{mathsf{X}_{i,j,k}-overline{mathsf{X}}}{maxleftlbrace epsilon, sqrt{lambda+frac{1}{3rc}sum_{i=1}^{r}sum_{j=1}^{c}sum_{k=1}^{3}(mathsf{X}_{i,j,k}-overline{mathsf{X}})^2}rightrbrace }$$ where $mathsf{X}_{i,j,k}$ is tensor of the image and $mathsf{X}^{prime}_{i,j,k}$ is tensor of normalized image, and $overline{mathsf{X}} = frac{1}{3rc}sum_{i=1}^{r}sum_{j=1}^{c}sum_{k=1}^{3} mathsf{X}_{i,j,k}$ is the average value of the pixels of the original image $epsilon$ and $lambda$ is some constant it is usually set $lambda=10$ and $epsilon$ is set to be a very small number, and here is my implementation:
import Image
import numpy as np
import math
def global_contrast_normalization(filename, s, lmda, epsilon):
X = np.array(Image.open(filename))
X_prime=X
r,c,u=X.shape
contrast =0
su=0
sum_x=0
for i in range(r):
for j in range(c):
for k in range(u):
sum_x=sum_x+X[i][j][k]
X_average=float(sum_x)/(r*c*u)
for i in range(r):
for j in range(c):
for k in range(u):
su=su+((X[i][j][k])-X_average)**2
contrast=np.sqrt(lmda+(float(su)/(r*c*u)))
for i in range(r):
for j in range(c):
for k in range(u):
X_prime[i][j][k] = s * (X[i][j][k] - X_average) / max(epsilon, contrast)
Image.fromarray(X_prime).save("result.jpg")
global_contrast_normalization("cat.jpg", 1, 10, 0.000000001)
original image
result image
I got an unexpected result. What is wrong my implementation ?
python image-classification computer-vision preprocessing
$endgroup$
add a comment |
$begingroup$
I try to implement global contrast normalization in python from Yoshua Bengio's deep learning book. From the book, to get normalized image using global contrast normalization we use this equation
$$mathsf{X}^{prime}_{i,j,k}=sfrac{mathsf{X}_{i,j,k}-overline{mathsf{X}}}{maxleftlbrace epsilon, sqrt{lambda+frac{1}{3rc}sum_{i=1}^{r}sum_{j=1}^{c}sum_{k=1}^{3}(mathsf{X}_{i,j,k}-overline{mathsf{X}})^2}rightrbrace }$$ where $mathsf{X}_{i,j,k}$ is tensor of the image and $mathsf{X}^{prime}_{i,j,k}$ is tensor of normalized image, and $overline{mathsf{X}} = frac{1}{3rc}sum_{i=1}^{r}sum_{j=1}^{c}sum_{k=1}^{3} mathsf{X}_{i,j,k}$ is the average value of the pixels of the original image $epsilon$ and $lambda$ is some constant it is usually set $lambda=10$ and $epsilon$ is set to be a very small number, and here is my implementation:
import Image
import numpy as np
import math
def global_contrast_normalization(filename, s, lmda, epsilon):
X = np.array(Image.open(filename))
X_prime=X
r,c,u=X.shape
contrast =0
su=0
sum_x=0
for i in range(r):
for j in range(c):
for k in range(u):
sum_x=sum_x+X[i][j][k]
X_average=float(sum_x)/(r*c*u)
for i in range(r):
for j in range(c):
for k in range(u):
su=su+((X[i][j][k])-X_average)**2
contrast=np.sqrt(lmda+(float(su)/(r*c*u)))
for i in range(r):
for j in range(c):
for k in range(u):
X_prime[i][j][k] = s * (X[i][j][k] - X_average) / max(epsilon, contrast)
Image.fromarray(X_prime).save("result.jpg")
global_contrast_normalization("cat.jpg", 1, 10, 0.000000001)
original image
result image
I got an unexpected result. What is wrong my implementation ?
python image-classification computer-vision preprocessing
$endgroup$
I try to implement global contrast normalization in python from Yoshua Bengio's deep learning book. From the book, to get normalized image using global contrast normalization we use this equation
$$mathsf{X}^{prime}_{i,j,k}=sfrac{mathsf{X}_{i,j,k}-overline{mathsf{X}}}{maxleftlbrace epsilon, sqrt{lambda+frac{1}{3rc}sum_{i=1}^{r}sum_{j=1}^{c}sum_{k=1}^{3}(mathsf{X}_{i,j,k}-overline{mathsf{X}})^2}rightrbrace }$$ where $mathsf{X}_{i,j,k}$ is tensor of the image and $mathsf{X}^{prime}_{i,j,k}$ is tensor of normalized image, and $overline{mathsf{X}} = frac{1}{3rc}sum_{i=1}^{r}sum_{j=1}^{c}sum_{k=1}^{3} mathsf{X}_{i,j,k}$ is the average value of the pixels of the original image $epsilon$ and $lambda$ is some constant it is usually set $lambda=10$ and $epsilon$ is set to be a very small number, and here is my implementation:
import Image
import numpy as np
import math
def global_contrast_normalization(filename, s, lmda, epsilon):
X = np.array(Image.open(filename))
X_prime=X
r,c,u=X.shape
contrast =0
su=0
sum_x=0
for i in range(r):
for j in range(c):
for k in range(u):
sum_x=sum_x+X[i][j][k]
X_average=float(sum_x)/(r*c*u)
for i in range(r):
for j in range(c):
for k in range(u):
su=su+((X[i][j][k])-X_average)**2
contrast=np.sqrt(lmda+(float(su)/(r*c*u)))
for i in range(r):
for j in range(c):
for k in range(u):
X_prime[i][j][k] = s * (X[i][j][k] - X_average) / max(epsilon, contrast)
Image.fromarray(X_prime).save("result.jpg")
global_contrast_normalization("cat.jpg", 1, 10, 0.000000001)
original image
result image
I got an unexpected result. What is wrong my implementation ?
python image-classification computer-vision preprocessing
python image-classification computer-vision preprocessing
edited 17 mins ago
AreTor
1054
1054
asked Nov 14 '16 at 17:35
Kiki Rizki ArpiandiKiki Rizki Arpiandi
130210
130210
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
there are multiple issues with the code:
You force the values in the image to be uint8 (8-bit integer). Since the values are floats they will be casted/rounded to either 0 or 1.
This will later be interpreted as image in black and the darkest form of gray (1 out of 255).Once you have proper floats as values PIL or pillow can't handle the array (they only do images with values in [0, 255])
The first problem happened because you/numpy wants the array to be a uint8.
The normalize version will have floats.
You should have used:
X_prime = X.astype(float)
Here is a working version of the code:
import numpy
import scipy
import scipy.misc
from PIL import Image
def global_contrast_normalization(filename, s, lmda, epsilon):
X = numpy.array(Image.open(filename))
# replacement for the loop
X_average = numpy.mean(X)
print('Mean: ', X_average)
X = X - X_average
# `su` is here the mean, instead of the sum
contrast = numpy.sqrt(lmda + numpy.mean(X**2))
X = s * X / max(contrast, epsilon)
# scipy can handle it
scipy.misc.imsave('result.jpg', X)
global_contrast_normalization("cat.jpg", 1, 10, 0.000000001)
PS: X_prime = X
will make X_prime
reference X
. So changing X_prime
will also change X
.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "557"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f15110%2fhow-to-implement-global-contrast-normalization-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
there are multiple issues with the code:
You force the values in the image to be uint8 (8-bit integer). Since the values are floats they will be casted/rounded to either 0 or 1.
This will later be interpreted as image in black and the darkest form of gray (1 out of 255).Once you have proper floats as values PIL or pillow can't handle the array (they only do images with values in [0, 255])
The first problem happened because you/numpy wants the array to be a uint8.
The normalize version will have floats.
You should have used:
X_prime = X.astype(float)
Here is a working version of the code:
import numpy
import scipy
import scipy.misc
from PIL import Image
def global_contrast_normalization(filename, s, lmda, epsilon):
X = numpy.array(Image.open(filename))
# replacement for the loop
X_average = numpy.mean(X)
print('Mean: ', X_average)
X = X - X_average
# `su` is here the mean, instead of the sum
contrast = numpy.sqrt(lmda + numpy.mean(X**2))
X = s * X / max(contrast, epsilon)
# scipy can handle it
scipy.misc.imsave('result.jpg', X)
global_contrast_normalization("cat.jpg", 1, 10, 0.000000001)
PS: X_prime = X
will make X_prime
reference X
. So changing X_prime
will also change X
.
$endgroup$
add a comment |
$begingroup$
there are multiple issues with the code:
You force the values in the image to be uint8 (8-bit integer). Since the values are floats they will be casted/rounded to either 0 or 1.
This will later be interpreted as image in black and the darkest form of gray (1 out of 255).Once you have proper floats as values PIL or pillow can't handle the array (they only do images with values in [0, 255])
The first problem happened because you/numpy wants the array to be a uint8.
The normalize version will have floats.
You should have used:
X_prime = X.astype(float)
Here is a working version of the code:
import numpy
import scipy
import scipy.misc
from PIL import Image
def global_contrast_normalization(filename, s, lmda, epsilon):
X = numpy.array(Image.open(filename))
# replacement for the loop
X_average = numpy.mean(X)
print('Mean: ', X_average)
X = X - X_average
# `su` is here the mean, instead of the sum
contrast = numpy.sqrt(lmda + numpy.mean(X**2))
X = s * X / max(contrast, epsilon)
# scipy can handle it
scipy.misc.imsave('result.jpg', X)
global_contrast_normalization("cat.jpg", 1, 10, 0.000000001)
PS: X_prime = X
will make X_prime
reference X
. So changing X_prime
will also change X
.
$endgroup$
add a comment |
$begingroup$
there are multiple issues with the code:
You force the values in the image to be uint8 (8-bit integer). Since the values are floats they will be casted/rounded to either 0 or 1.
This will later be interpreted as image in black and the darkest form of gray (1 out of 255).Once you have proper floats as values PIL or pillow can't handle the array (they only do images with values in [0, 255])
The first problem happened because you/numpy wants the array to be a uint8.
The normalize version will have floats.
You should have used:
X_prime = X.astype(float)
Here is a working version of the code:
import numpy
import scipy
import scipy.misc
from PIL import Image
def global_contrast_normalization(filename, s, lmda, epsilon):
X = numpy.array(Image.open(filename))
# replacement for the loop
X_average = numpy.mean(X)
print('Mean: ', X_average)
X = X - X_average
# `su` is here the mean, instead of the sum
contrast = numpy.sqrt(lmda + numpy.mean(X**2))
X = s * X / max(contrast, epsilon)
# scipy can handle it
scipy.misc.imsave('result.jpg', X)
global_contrast_normalization("cat.jpg", 1, 10, 0.000000001)
PS: X_prime = X
will make X_prime
reference X
. So changing X_prime
will also change X
.
$endgroup$
there are multiple issues with the code:
You force the values in the image to be uint8 (8-bit integer). Since the values are floats they will be casted/rounded to either 0 or 1.
This will later be interpreted as image in black and the darkest form of gray (1 out of 255).Once you have proper floats as values PIL or pillow can't handle the array (they only do images with values in [0, 255])
The first problem happened because you/numpy wants the array to be a uint8.
The normalize version will have floats.
You should have used:
X_prime = X.astype(float)
Here is a working version of the code:
import numpy
import scipy
import scipy.misc
from PIL import Image
def global_contrast_normalization(filename, s, lmda, epsilon):
X = numpy.array(Image.open(filename))
# replacement for the loop
X_average = numpy.mean(X)
print('Mean: ', X_average)
X = X - X_average
# `su` is here the mean, instead of the sum
contrast = numpy.sqrt(lmda + numpy.mean(X**2))
X = s * X / max(contrast, epsilon)
# scipy can handle it
scipy.misc.imsave('result.jpg', X)
global_contrast_normalization("cat.jpg", 1, 10, 0.000000001)
PS: X_prime = X
will make X_prime
reference X
. So changing X_prime
will also change X
.
edited 17 mins ago
AreTor
1054
1054
answered Feb 16 '17 at 5:43
someonesomeone
10612
10612
add a comment |
add a comment |
Thanks for contributing an answer to Data Science Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f15110%2fhow-to-implement-global-contrast-normalization-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown