Image Processing #6 - Negative Image

You can download the source codes here(https://github.com/raspberry-pi-maker/OpenCV)

A positive image is a normal image. A negative image is a total inversion, in which light areas appear dark and vice versa. A negative color image is additionally color-reversed,[1] with red areas appearing cyan, greens appearing magenta, and blues appearing yellow, and vice versa.

Film negatives usually have less contrast, but a wider dynamic range, than the final printed positive images. The contrast typically increases when they are printed onto photographic paper. When negative film images are brought into the digital realm, their contrast may be adjusted at the time of scanning or, more usually, during subsequent post-processing. (from https://en.wikipedia.org/wiki/Negative_(photography))


Create negative image using bitwise_not function 

Both grayscale and color images are applicable.


Run the code.



#-*- coding: utf-8 -*-
"""
"""
from __future__ import print_function
import numpy as np
import cv2 
import argparse


ap = argparse.ArgumentParser()
ap.add_argument("--file", required = True, help = "Path to the image")
args = parser.parse_args()

print ('cv2.__version__(%s)'%(cv2.__version__))

img = cv2.imread(args.file, cv2.IMREAD_COLOR)
if img is None:
    print ('Image[%s] open error' %(args["image"]))
    exit(0)

cv2.imshow('Original', img)

img_nega = cv2.bitwise_not(img)

cv2.imshow('Negative',img_nega)
cv2.waitKey(0)
cv2.destroyAllWindows()



Create negative image using numpy

Negative image;s pixel value is (255 - original value). So white pixel RGB(255,255,255) becomes black(0, 0, 0) and dark gray pixel( 64, 64, 64) becomes (191, 191, 191).

Numpy can do this job very easily. This code produces exactly the same result as the image above.

Becareful : However, since the alpha channel value also changes, use the above bitwise_not function for alpha channel image.



#-*- coding: utf-8 -*-
"""
"""
from __future__ import print_function
import numpy as np
import cv2 
import argparse


parser = argparse.ArgumentParser(description="OpenCV Example")
parser.add_argument("--file", type=str, required=True, help="filename of the input image to process")
args = parser.parse_args()

print ('cv2.__version__(%s)'%(cv2.__version__))

img = cv2.imread(args.file, cv2.IMREAD_COLOR)
if img is None:
    print ('Image[%s] open error' %(args["image"]))
    exit(0)

cv2.imshow('Original', img)

img_nega = 255 - img

cv2.imshow('Negative',img_nega)
cv2.waitKey(0)
cv2.destroyAllWindows()


Instead of
img_nega = cv2.bitwise_not(img)

just use this line .
img_nega = 255 - img

This is numpy calculation. scalar value 255 - numpy.array means subtracting all the values ​​in the array from 255. It can be handled simply without the need for recursive operations using the for statement.


















댓글

이 블로그의 인기 게시물

Image Processing #7 - OpenCV Text

Playing YouTube videos using OpenCV

OpenCV Installation - Rasbian Buster, Jessie, DietPi Buster