opencv

个人笔记 长期更新

### 创建一个图片
import cv2  # Not actually necessary if you just want to create an image.
import matplotlib.pyplot as plt #jupyter notebook 用cv2 show img有问题
import numpy as np
height=300
width=300
blank_image = np.zeros((height,width,3), np.uint8)
print(blank_image.shape)
#blank_image[:,0:width//2] = (128,128,128)      # (B, G, R)
#blank_image[:,width//2:width] = (128,128,128)
white_image = blank_image.copy()
white_image[...]=(255,255,255)
plt.imshow(white_image)
plt.show()

gray_image = blank_image.copy()
gray_image[...]=(128,128,128)
plt.imshow(gray_image)
plt.show()
#

white_in_center_img = blank_image.copy()
white_in_center_img[100:200,100:200,:]=(128,128,128)
plt.imshow(white_in_center_img)
plt.show()

opencv_第1张图片
opencv_第2张图片
opencv_第3张图片

读取一个图片

opencv_第4张图片

import cv2
img = cv2.imread("/nfsserver/test.jpg")
print(img.shape)

输出(640, 480, 3).

opencv里内存里的存储顺序

https://stackoverflow.com/questions/37040787/opencv-in-memory-mat-representation
opencv_第5张图片
举个具体的例子,比如8 x 8的彩色图片。像素坐标从(0,0)到(7,7),每个像素有r,g,b三个值。
存储顺序为(0,0,b),(0,0,g),(0,0,r),(0,1,b),(0,1,g),(0,1,r)......(7,7,b),(7,7,g),(7,7,r)
所以第i行,第j列,第c个channel对应的index即为 i x j x 3 + j x 3 + c.

resize

import cv2
import matplotlib.pyplot as plt
import numpy as np
height=3
width=5
blank_image = np.zeros((height,width,3), np.uint8)
blank_image[1:2,3:4,]=(255,0,0)
plt.imshow(blank_image)
plt.show()

cv2.imwrite('/home/su/Desktop/test.jpg',blank_image)

blank_image = cv2.resize(blank_image, (416, 416), interpolation=cv2.INTER_CUBIC)
cv2.imwrite('/home/su/Desktop/test2.jpg',blank_image)

你可能感兴趣的:(opencv)