OpenCV: Implementing the negative of an image

negative

Negative film is a word that is often encountered in photography. In the earliest film photo printing, it refers to the image obtained after exposure and development. The negative operation is also called inverse color in many image processing software. Its light and shade are opposite to the original image, and its color is the complementary color of the original image. For example, color value A and color value B are complementary colors to each other, and the sum of their values ​​is 255. That is, the color of a certain point in the RGB image is (0, 0, 255), and its complementary color is (255, 255, 0).

Since the negative operation process is relatively simple, OpenCV does not encapsulate the negative function separately. So you need to implement it yourself. It is very simple to implement. The steps are as follows:

  • Need to split a picture into individual color channel matrices
  • Then process each color channel matrix separately
  • Finally, reassemble it into a picture

The original picture is as follows:

Insert image description here

The sample code is as follows:

import cv2
import numpy as np

# 读入图片
img = cv2.imread('input_file.png')

# 获取高度和宽度
height = img.shape[0]
width = img.shape[1]

# 生成一个空的相同尺寸的图像
negative_file = np.zeros((height, width, 3))

# 拆分三通道,注意顺序
b, g, r = cv2.split(img)

# 进行负片处理,求每个通道颜色的补色
r = 255 - r
b = 255 - b
g = 255 - g

# 将处理后的结果赋值到前面生成的三维张量
negative_file[:, :, 0] = b
negative_file[:, :, 1] = g
negative_file[:, :, 2] = r

# 将生成的图片保存起来
cv2.imwrite('negative_file.jpg', negative_file)

Insert image description here

Summarize

Negative film is a special form of photo processing that was popular in the days of film photography and is now used in digital image processing.

A negative film is an image in which the color and brightness are inverted, that is, the originally dark areas in the image will become bright, the originally bright areas will become dark, and the color of the image will also be reversed. This effect can be achieved by "inverting" the image in digital image processing software.

In digital photography, negatives are widely used. For example, many photographers use the negative effect to create a special artistic effect or for post-processing of photos. Additionally, negatives can be used for image enhancement, especially where certain details need to be highlighted, such as in medical image processing.

Overall, negative film is a very interesting and practical way of special photo processing that can help you create unique artistic effects and also has great potential in image enhancement and post-processing.

Guess you like

Origin blog.csdn.net/uncle_ll/article/details/132675455