Digital Image Processing Using Python & OpenCV
Using OpenCV library in Python 3.6 to apply auto-contrast to a grayscale image.
import numpy as np
import cv2
imgloc="C:/Users/ASUS/Desktop/images/abgs1.jpg"
img=cv2.imread(imgloc,0) #0 for grayscale ,1 for color, -1 for alpha channels(png)
height=img.shape[0]
width=img.shape[1]
min=255 #While comparing if any pixel value<min we set it to min
max=0 #While comparing if any pixel value>max we set it to max
for i in np.arange(height):
for j in np.arange(width):
a=img.item(i,j) #Getting the pixel value
if a>max:
max=a
if a<min:
min=a
for i in np.arange(height):
for j in np.arange(width):
a=img.item(i,j)
b=float(a-min)/(max-min)*255 #By proportion b:255=(a-min):(max-min)
img.itemset((i,j),b)
imgloc="C:/Users/ASUS/Desktop/images/abautocontrast.jpg"
cv2.imwrite(imgloc,img)
cv2.imshow('image',img) #'image' is our window name
cv2.waitKey(0) #waits indefinitely for a keystroke
cv2.destroyAllWindows()
Original Image:
Image after running above code:
Comments
Post a Comment