图像识别

'''
    图像识别:
        OpenCV基础:OpenCV是一个开源的计算机视觉库。提供了很多图像处理常用的工具。
        图像的本质是三维数组
'''
import cv2 as cv
import numpy as np

# 读取图片
img = cv.imread('./ml_data/forest.jpg')
print(type(img), img.shape, img[0, 0, :])
cv.imshow('figure title', img)
# 显示图片某个颜色通道的图像
blue = np.zeros_like(img)
green = np.zeros_like(img)
red = np.zeros_like(img)
blue[:, :, 0] = img[:, :, 0]
green[:, :, 1] = img[:, :, 1]
red[:, :, 2] = img[:, :, 2]
print(blue)
print(green)
print(red)

# 图像裁剪,相当于三维数组的切片
h, w = img.shape[:2]
l, t = int(w / 4), int(h / 4)
r, b = int(h / 4 * 3), int(h / 4 * 3)
cropped = img[t:b, l:r]
cv.imshow('cropped', cropped)

# 图像缩放
scale1 = cv.resize(img, (int(w / 4), int(h / 2)))
cv.imshow('scale1', scale1)

# 图像保存
cv.imwrite('./green.jpg', green)

cv.imshow('blue', blue)
cv.imshow('green', green)
cv.imshow('red', red)
cv.waitKey()


输出结果:
<class 'numpy.ndarray'> (397, 600, 3) [ 75 187 170]
[[[ 75   0   0]
  [ 81   0   0]
  [ 54   0   0]
  ...
  [ 29   0   0]
  [ 24   0   0]
  [  3   0   0]]

 [[ 22   0   0]
  [ 43   0   0]
  [ 88   0   0]
  ...
  [ 23   0   0]
  [ 23   0   0]
  [ 10   0   0]]

 [[ 11   0   0]
  [  2   0   0]
  [101   0   0]
  ...
  [  0   0   0]
  [  1   0   0]
  [ 22   0   0]]

 ...

 [[ 29   0   0]
  [ 14   0   0]
  [  0   0   0]
  ...
  [  6   0   0]
  [  3   0   0]
  [  5   0   0]]

 [[ 13   0   0]
  [  9   0   0]
  [  8   0   0]
  ...
  [  4   0   0]
  [  6   0   0]
  [  9   0   0]]

 [[ 29   0   0]
  [ 25   0   0]
  [ 20   0   0]
  ...
  [  9   0   0]
  [  9   0   0]
  [  9   0   0]]]
[[[  0 187   0]
  [  0 187   0]
  [  0 175   0]
  ...
  [  0 176   0]
  [  0 149   0]
  [  0 120   0]]

 [[  0 134   0]
  [  0 148   0]
  [  0 198   0]
  ...
  [  0 159   0]
  [  0 149   0]
  [  0 121   0]]

 [[  0 122   0]
  [  0 102   0]
  [  0 184   0]
  ...
  [  0 115   0]
  [  0 127   0]
  [  0 120   0]]

 ...

 [[  0  50   0]
  [  0  38   0]
  [  0  17   0]
  ...
  [  0 105   0]
  [  0 108   0]
  [  0 111   0]]

 [[  0  29   0]
  [  0  24   0]
  [  0  27   0]
  ...
  [  0 101   0]
  [  0 108   0]
  [  0 114   0]]

 [[  0  40   0]
  [  0  35   0]
  [  0  33   0]
  ...
  [  0 100   0]
  [  0 105   0]
  [  0 107   0]]]
[[[  0   0 170]
  [  0   0 180]
  [  0   0 171]
  ...
  [  0   0 184]
  [  0   0 157]
  [  0   0 127]]

 [[  0   0 116]
  [  0   0 139]
  [  0   0 194]
  ...
  [  0   0 163]
  [  0   0 154]
  [  0   0 129]]

 [[  0   0 100]
  [  0   0  90]
  [  0   0 182]
  ...
  [  0   0 112]
  [  0   0 128]
  [  0   0 130]]

 ...

 [[  0   0  51]
  [  0   0  38]
  [  0   0  21]
  ...
  [  0   0  95]
  [  0   0  95]
  [  0   0  98]]

 [[  0   0  28]
  [  0   0  26]
  [  0   0  30]
  ...
  [  0   0  91]
  [  0   0  96]
  [  0   0 101]]

 [[  0   0  38]
  [  0   0  35]
  [  0   0  35]
  ...
  [  0   0  91]
  [  0   0  94]
  [  0   0  95]]]

原文地址:https://www.cnblogs.com/yuxiangyang/p/11241950.html