Calculating image may be a decimal value of binary arithmetic may be bitwise operations
An image adder
1 import cv2 2 import numpy as np 3 img1 = np.random.randint(0,256, size = [3,3] , dtype = np.uint8) 4 img2 = np.random.randint(0,256, size = [3,3] , dtype = np.uint8) 5 img3 = cv2.add(img1,img2) 6 print("img1 =\n" , img1) 7 print("img2 =\n" , img2) 8 print("img1+img2 =\n" , img1 + img2) 9 print("cv2.add(img1, img2) =\n" , img3)
img1 = [[ 106 40 179 ] [171 138 207] [131 34 55]] img2 = [[125 73 224] [132 229 201] [151 37 40]] img1 img2 = [[ 231 113 147 ] [ 47 111 152] [ 26 71 95]] cv2.add(img1, img2) = [[231 113 255] [255 255 255] [255 71 95]]
By adding the above code shows the addition operator and cv2.add () function
Addition operator
FIG two pixel values corresponding to the addition and less than 255, i.e., modulo 256, and
cv2.add () function
Will be the saturation value (maximum value) of the pixel values of two images, i.e., when greater than 255, 255. The other result is obtained and compared with the original values.
Operators two functions may be involved in computing two images, an image may be a value
Second, image weighting, and
I.e. weighted images and each image is a heavy weight into account, the formula is:
dst = saturate( src1 * a + src2 * b)
The results were the saturation value
1 import cv2 2 dog =cv2.imread("/home/miao/dog.jpg") 3 cat =cv2.imread("/home/miao/cat.jpg") 4 result = cv2.addWeighted(dog , 0.6 , cat , 0.4 , 0) 5 cv2.imshow("dog", dog) 6 cv2.imshow("cat" , cat) 7 cv2.imshow("result" , result) 8 cv2.waitKey() 9 cv2.destroyAllWindows()
Third, bitwise logical operations
Common bit operation function
Function name | The basic meaning of |
cv2.bitwise_and() | Bitwise AND |
cv2.bitwise_or() | Bitwise or |
cv2.bitwise_xor() | Bitwise XOR |
cv2.bitwise_nor | Bitwise |
These functions may be added in the third parameter mask may be a mask calculation result obtained designated part
The calculation value is converted to a binary image is about further calculates,
Bitwise AND, any numerical operations are located at the press 0 0
According to this characteristic configuration mask image that only two values 0 and 255
The image and the mask image can be a bitwise mask image in an image portion designated
1 import cv2 2 import numpy as np 3 a = np.random.randint(0 , 255, (5,5) , dtype = np.uint8) 4 b = np.zeros((5,5) , dtype = np.uint8) 5 b[0 : 3 , 0 : 3] = 255 6 b[4,4] = 255 7 c = cv2.bitwise_and(a,b) 8 print("a = \n" , a) 9 print("b = \n" , b) 10 print("c = \n" , c)
a = [[215 164 27 216 90] [184 33 40 101 247] [ 77 150 189 26 251] [101 30 32 49 86] [ 64 86 175 28 221]] b = [[255 255 255 0 0] [255 255 255 0 0] [255 255 255 0 0] [ 0 0 0 0 0] [ 0 0 0 0 255]] c = [[215 164 27 0 0] [184 33 40 0 0] [ 77 150 189 0 0] [ 0 0 0 0 0] [ 0 0 0 0 221]]
Fourth, the bit plane decomposition
8-bit grayscale image, each pixel using 8-bit binary representation, the value can be represented by
value = a7*2^7 + a6*2^6 + a5*2^5 + a4*2^4 + a3*2^3 + a2*2^2 + a1*2^1 + a0*2^0
Weight ai weight of each of different effects on the image size are different
Thus it can be decomposed original 8-bit binary image, i.e., eight bit planes
a7 weights highest correlation with the picture of the largest, most similar to the image
0 smallest weight, the minimum configuration of image correlation, typically looks messy
Extracting a bit-plane binary image, the display image is directly up almost black image, because the pixel values of the low ground plane, an image near black display, so that after the extraction process value greater than the bit plane of bit 0 255, will be a binary image is displayed as a monochrome image
1 import cv2 2 import numpy as np 3 dog = cv2.imread("/home/miao/dog.jpg" , 0) 4 cv2.imshow("dog" , dog) 5 r,c = dog.shape 6 x = np.zeros((r,c,8) , dtype = np.uint8) 7 for i in range(8): 8 x[:,:,i] = 2**i 9 y = np.zeros((r,c,8) , dtype = np.uint8) 10 for i in range(8): 11 y[:,:,i] = cv2.bitwise_and(dog , x[:,:,i]) 12 mask = y[:,:,i] > 0 13 y[mask] = 255 14 cv2.imshow(str(i) , y[:,:,i]) 15 cv2.waitKey() 16 cv2.destroyAllWindows()
Original grayscale image
A0:
a1
a2
a3
a4
a5
a6
a7
Fifth, the image encryption and decryption
By bitwise XOR encryption and decryption can be achieved
Exclusive OR operation is assumed by the rule:
xor(a,b) = c
Get
xor(c,b) = a
xor(c,a) = b
It can draw the following relationship
a: plaintext, the raw data
b: key
c: ciphertext
1 import cv2 2 import numpy as np 3 dog = cv2.imread("/home/miao/dog.jpg" , 0) 4 r,c = dog.shape 5 key = np.random.randint(0 , 256 , size = [r,c] , dtype = np.uint8) 6 encryption = cv2.bitwise_xor( dog , key) 7 decryption = cv2.bitwise_xor( encryption , key) 8 cv2.imshow("dog" , dog) 9 cv2.imshow("key" , key) 10 cv2.imshow("encryption" , encryption) 11 cv2.imshow("decryption" , decryption) 12 cv2.waitKey() 13 cv2.destroyAllWindows()
原图dog
密匙图像key
加密图像encryption
解密图像decryption
六、数字水印
即将一个需要隐藏的二值图像信息嵌入载体图像
将需要隐藏的二值图像嵌入在载体图像的最低有效位(即相关性最低,对图像影响最小)
嵌入过程
建立与图像大小相同,元素值为254的提取矩阵即:1111 1110
将图像与其按位与运算则可以将最低有效位清0,保留图像的高7位
也可以对像素右移一位,再进行左移一位
将水印图像(二值图像)的像素值255转化为1(二进制二值图像),
然后将二进制二值水印图像与最低有效位被置零后得到的原始载体图像进行按位或运算
就可以将水印信息嵌入原始载体图像内
提取过程
建立与图像大小相同,元素值为1,即:0000 0001
将图像内每个像素,将其与数值为1进行按位与操作,即可将图像的最低有效位提取出来