convert to negative Algorithm
The convert to negative algorithm is a widely used image processing technique that involves inverting the color values of an image to create a negative representation. This transformation is particularly useful in enhancing the appearance of certain features, such as edges and textures, which can be beneficial in tasks like image analysis, object recognition, and image enhancement. The algorithm works by subtracting the current pixel value from the maximum possible value, effectively reversing the color spectrum, thereby producing an image that appears as a photographic negative.
In the context of digital images, the convert to negative algorithm operates on a pixel-by-pixel basis, altering the intensity values of each color channel (red, green, and blue) within the image. For a grayscale image, the algorithm would simply subtract the pixel intensity value from the maximum possible grayscale value (usually 255 for an 8-bit image), while for a colored image, the process would be applied independently to each color channel. The resulting image exhibits a high degree of contrast, with dark areas appearing light and vice versa, while preserving the overall structure and composition of the original image. This makes the convert to negative algorithm not only a valuable tool in image processing applications but also an interesting artistic effect in photography and design.
"""
Implemented an algorithm using opencv to convert a colored image into its negative
"""
from cv2 import imread, imshow, waitKey, destroyAllWindows
def convert_to_negative(img):
# getting number of pixels in the image
pixel_h, pixel_v = img.shape[0], img.shape[1]
# converting each pixel's color to its negative
for i in range(pixel_h):
for j in range(pixel_v):
img[i][j] = [255, 255, 255] - img[i][j]
return img
if __name__ == "__main__":
# read original image
img = imread("image_data/lena.jpg", 1)
# convert to its negative
neg = convert_to_negative(img)
# show result image
imshow("negative of original image", img)
waitKey(0)
destroyAllWindows()