【OpenCV+TensorFlow】清华博士带你做项目!计算机视觉实战+深度学习,项目可写进简历!(附源码资料)-人工智能/深度学习框架/RNN/池化层/感受_哔哩哔哩_bilibili

pip install opencv-python

ctrl+L快速清屏

 计算机中rgb范围0-255

矩阵形状 [500,500,3]

import cv2  # 读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np


# img = cv2.imread('rabbit.jpg')  # 读取路径不能包含中文,否则会为none
# print(img)
# cv2.imshow('image', img)  # 第一个为窗口名字
# cv2.waitKey(0)  # 等待时间,毫秒级,0表示任意键终止
# cv2.destroyAllWindows()

def cv_show(name, img):
    cv2.imshow('image', img)  # 第一个为窗口名字
    cv2.waitKey(0)  # 等待时间,毫秒级,0表示任意键终止
    cv2.destroyAllWindows()


# img.shape()


# img = cv2.imread('flower.jpg', cv2.IMREAD_GRAYSCALE)  # cv2.IMREAD.COLOR 彩色图像;cv2.IMREAD_GRAYSCALE灰度图像
# print(img.shape)
# cv2.imshow('image', img)  # 图像的显示,也可以创建多个窗口
# cv2.waitKey(10000)
# cv2.destroyAllWindows()
# cv2.imwrite('mygreyflower.png', img)  # 保存
# type(img)
# print(img.size)
# print(img.dtype)


# # 视频读取
# girl = cv2.VideoCapture("美女.mp4")
# # 检查是否打开
# if girl.isOpened():
#     open, frame = girl.read()  # open为bool值,如果第一帧可以读入,为true,frame接收每一帧图像信息
# else:
#     open = False
#
# while open:
#     ret, frame = girl.read()
#     if frame is None:  # 视频为空停止执行
#         break
#     if ret == True:
#         gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # 转成黑白图像
#         cv2.imshow('result', gray)
#         print(frame)
#         if cv2.waitKey(100) & 0xFF == 27:  # 27为退出键 
#             break
# girl.release()
# cv2.destroyAllWindows()


# 截取部分图像数据
# img = cv2.imread('flower.jpg')
# flower = img[0:200, 0:200]
# cv_show('flower', flower)

# # 颜色通道提取
# b, g, r = cv2.split(img)  # 分别提出3个通道
# print(b)
# print(b.shape)
# print(g.shape)
#
# img = cv2.merge((b, g, r))  # 合并3个通道
# print(img.shape)


# # 只保留R
# img = cv2.imread('flower.jpg')
# cur_img = img.copy()
# cur_img[:, :, 0] = 0
# cur_img[:, :, 1] = 0
# cv_show('R', cur_img)


# # 边界填充
# img = cv2.imread('flower.jpg')
# top_size, bottom_size, left_size, right_size = (50, 50, 50, 50)
# replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,
#                                borderType=cv2.BORDER_REPLICATE)  # 复制最边缘像素
# reflect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,
#                              cv2.BORDER_REFLECT)  # 反射法,fedcba|abcdefgh|hgfedcb
# reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,
#                                 cv2.BORDER_REFLECT_101)  # gfedcb|abcdefgh|gfedcba
# wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_WRAP)  # cdefgh|abcdefgh|abcdefg
# constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_CONSTANT, value=0)  # 常数值填充
#
# plt.subplot(231), plt.imshow(img, 'gray'), plt.title("ORIGINAL")
# plt.subplot(232), plt.imshow(replicate, 'gray'), plt.title("REPLICATE")
# plt.subplot(233), plt.imshow(reflect, 'gray'), plt.title("REFLECT")
# plt.subplot(234), plt.imshow(reflect101, 'gray'), plt.title("REFLECT_101")
# plt.subplot(235), plt.imshow(wrap, 'gray'), plt.title("WRAP")
# plt.subplot(236), plt.imshow(constant, 'gray'), plt.title("CONSTANT")
# plt.show()

# # 图片加法
# img = cv2.imread('flower.jpg')
# img_flower2 = img + 10
# print(img_flower2[:5, :, 0])
# print((img_flower2 + img)[:5, :, 0])  # 越界取余
# print(cv2.add(img_flower2, img)[:5, :, 0])  # 越界取255


# # 图片融合
# img_flower = cv2.imread('flower.jpg')
# img_rabbit = cv2.imread('rabbit.jpg')
# print(img_flower.shape)
# print(img_rabbit.shape)
# img_rabbit = cv2.resize(img_rabbit, (319, 300))
# print(img_rabbit.shape)
# res = cv2.addWeighted(img_rabbit, 0.4, img_flower, 0.6, 0)
# plt.imshow(res)
# plt.show()


# img_flower = cv2.imread('flower.jpg')
# res = cv2.resize(img_flower, (0, 0), fx=3, fy=1)  # 倍数关系,变形
# plt.imshow(res)
# plt.show()


import cv2
import matplotlib.pyplot as plt
import numpy as np
img=cv2.imread(r"C:\Users\RSB\Desktop\flower.jpg")
print(img)
cv2.imshow('image', img)  # 第一个为窗口名字
cv2.waitKey(0)  # 等待时间,毫秒级,0表示任意键终止
cv2.destroyAllWindows()

cv2.waitKey()   等待时间   cv2.waitKey(1000) 1000比100慢10倍

# b, g, r = cv2.split(img) # 分别提出3个通道

 

两种加法的区别

 

 

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐