Image bitwise operation bitwise(), color space transformation cvtColor, color map applyColorMap()

1. Bitwise negation bitwise_not()
Bitwise negation is to change the value to 0 and 0 to 1 according to each bit bit. For example, 0xf0 becomes 0x0f if it is bitwise negated. If it is uint8 type data, negate it. The result of adding the data before and after is 0xff(255).

img_ret1 = cv2.bitwise_not(img1)

2. Bitwise AND bitwise_and(), or bitwise_or(), XOR bitwise_xor() bitwise
AND, OR, XOR operations require the interaction of 2 image objects, or 1 image object and 1 scalar data.

dst = cv2.bitwise_and(src1, src2[, dst[, mask]] )
dst = cv2.bitwise_or(src1, src2[, dst[, mask]] )
dst = cv2.bitwise_xor(src1, src2[, dst[, mask]] )

The bitwise operations on the two images are the same as the arithmetic operations, and they also require the two images to be the same size and have the same number of channels. The data types of the two images in bit operations must also be consistent.
A good programming habit is to use a quadruple to represent this scalar regardless of how many channels the image has. If you do not want to perform bit operations on certain channels, use the corresponding all-0 or all-f instead, such as a 3-channel uint8 type. For images, you only need to AND 2 channels with 0x33, and the constructed quadruple is (0xff, 0x33, 0xff, 0xff).

img_ret1 = cv2.bitwise_and(img1,(0x3f,0x3f,0x3f,0))

Bit operations on floating-point types are rarely used.

Color conversion cvtColor()

In digital images, the most common color model is the RGB model (red, green, blue, the order of color image organization in OpenCV is BGR, still RGB model), this model is a common model for hardware processing, such as image acquisition CCD sensors, monitors that display images, etc., are consistent with the HSV (chroma, saturation, brightness) model that describes human eye observation.

OpenCV provides interfaces for converting various color models (color spaces) to each other. For example, it can be converted from BGR to HSV, HSV to BGR, or BGR to grayscale.
dst=cv2.cvtColor(src, code[, dst[, dstCn]])
src is the source image object;
code is the macro constant defined by the color space in OpenCV. You can traverse all color space conversions through the method colors = [i for i in dir(cv) if i.startswith('COLOR_')] Name, there are 274 conversion methods in version 4.5.2.
The more commonly used ones are COLOR_BGR2GRAY, COLOR_GRAY2BGR, COLOR_BGR2HSV, COLOR_BGR2RGB.
dstCn is the number of channels of the target image. If set to 0, the number of channels of the target image will be automatically calculated from the source image.

img_ret2 = cv2.cvtColor(img2,cv2.COLOR_BGR2HSV)

HSV color space

color map

applyColorMap(), which can convert grayscale images into color images, with as many as 22 conversion modes.

cv2.applyColorMap(src, colormap[, dst]) ->dst

Where src is the input image, which can be a single-channel or 3-channel 8bit image.
Colormap is a color map mode, and you can pass in integers 0~21 corresponding to various color maps.

import cv2 
img_gray = cv2.imread("ha.jpg",cv2.IMREAD_GRAYSCALE)
for i in range(0,21):
    dst = cv2.applyColorMap(img_gray,i) 
    cv2.imshow('map',dst) 
    cv2.waitKey(500)
    cv2.imwrite("map-"+str(i)+".jpg",dst)

Guess you like

Origin blog.csdn.net/aqiangdeba/article/details/129764360