Inverting the image colour using OpenCV in Python
import numpy as np
import cv2
img=cv2.imread("C:/Users/ASUS/Desktop/images/google.jpg",0)
height=img.shape[0]
width=img.shape[1]
cv2.imwrite("C:/Users/ASUS/Desktop/images/grayscalegoogle.jpg",img)
maxintensity=255
for i in np.arange(height):
for j in np.arange(width):
a=img.item(i,j) #Getting the pixel value
b=maxintensity-a
img.itemset((i,j),b) #Setting new pixel value to b
cv2.imwrite("C:/Users/ASUS/Desktop/images/invertgoogle.jpg",img)
cv2.imshow('Invertedimage',img)
cv2.waitKey(0) #waits indefinitely for a keystroke
cv2.destroyAllWindows()
Original Image:
Grayscaled image:
Inverted image:



Comments
Post a Comment