Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c14aacc

Browse files
authored
Image contrast adjusting filter (avinashkranjan#1105)
1 parent 92a8ef6 commit c14aacc

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from PIL import Image
4+
import os.path
5+
6+
img_path = input("Enter the path here: ") #example -> C:\Users\xyz\OneDrive\Desktop\project\image.jpg
7+
img = Image.open(img_path)
8+
9+
# convert our image into a numpy array
10+
img = np.asarray(img)
11+
#print(img.shape)
12+
# put pixels in a 1D array by flattening out img array
13+
flat = img.flatten()
14+
15+
# create our own histogram function
16+
def get_histogram(image, bins):
17+
# array with size of bins, set to zeros
18+
histogram = np.zeros(bins)
19+
20+
# loop through pixels and sum up counts of pixels
21+
for pixel in image:
22+
histogram[pixel] += 1
23+
24+
# return our final result
25+
return histogram
26+
27+
# execute our histogram function
28+
hist = get_histogram(flat, 256)
29+
30+
# execute the fn
31+
cs = np.cumsum(hist)
32+
33+
# numerator & denomenator
34+
nj = (cs - cs.min()) * 255
35+
N = cs.max() - cs.min()
36+
37+
# re-normalize the cumsum
38+
cs = nj / N
39+
40+
# cast it back to uint8 since we can't use floating point values in images
41+
cs = cs.astype('uint8')
42+
43+
# get the value from cumulative sum for every index in flat, and set that as img_new
44+
img_new = cs[flat]
45+
46+
# put array back into original shape since we flattened it
47+
img_new = np.reshape(img_new, img.shape)
48+
49+
# set up side-by-side image display
50+
fig = plt.figure()
51+
fig.set_figheight(15)
52+
fig.set_figwidth(15)
53+
54+
# display the real image
55+
fig.add_subplot(1,2,1)
56+
plt.imshow(img, cmap='gray')
57+
plt.title("Image 'Before' Contrast Adjustment")
58+
59+
# display the new image
60+
fig.add_subplot(1,2,2)
61+
plt.imshow(img_new, cmap='gray')
62+
plt.title("Image 'After' Contrast Adjustment")
63+
filename = os.path.basename(img_path)
64+
65+
plt.savefig("./Image Contrast Adjusting Filter/(Contrast Adjusted)"+filename)
66+
67+
plt.show()
369 KB
Loading[フレーム]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Image Contrast Adjusting Filter
2+
3+
Images captured in dim light mostly not have a much better contrast so an (Image Contrast Adjusting filter) filter helps accordingly to adjusts and increase its contrast and hence we are having a better version of the image. Hence this filter helps to get a high contrast image.
4+
5+
## Libraries used
6+
Firstly import the following python libraries
7+
* Matplotlib,
8+
* NumPy
9+
* PIL
10+
Save the image and your code in the same folder
11+
Run the python code
12+
13+
## Detailed explanation of the method used
14+
15+
* Imported the required libraries ( Numpy, Matplotlib, PIL, IPython.display)
16+
* Read the input image using Image from PIL library
17+
* Converted the image into an array and then flatten it making it a 1d array
18+
* Count the number of occurance of each pixel in the image and hence getting an array of frequency count of each pixels
19+
* Then making cdf from the frequency count list
20+
* Normalizing the cdf
21+
* Converting the cdf shape into the shape of given image
22+
* Finally converted the image into contrast adjusted image
23+
24+
## Comparison With The Result
25+
<img src="Images/Result.jpg" height="300px">
26+
27+
## Author(s)
28+
[Akriti](https://github.com/A-kriti)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
numpy==1.18.5
2+
matplotlib==3.2.2
3+
Pillow==8.2.0

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /