参考学习:OpenCV-Python学习—基础知识

test1

import cv2
import matplotlib.pyplot as plt
'''
opencv是一个强大的图像处理和计算机视觉库
opencv采用BGR模式,而不是RGB
'''

img = cv2.imread(r'../Figure/obama.jpg', 1)
'''
imread(img_path,flag) 读取图片,返回图片对象
    img_path: 图片的路径,即使路径错误也不会报错,但打印返回的图片对象为None
    flag:cv2.IMREAD_COLOR,读取彩色图片,图片透明性会被忽略,为默认参数,也可以传入1
          cv2.IMREAD_GRAYSCALE,按灰度模式读取图像,也可以传入0
          cv2.IMREAD_UNCHANGED,读取图像,包括其alpha通道,也可以传入-1
'''

print(img.shape)
#img.shape   #返回(280, 450, 3), 宽280(rows),长450(cols),3通道(channels)

# cv2.imshow('Obama',img)
'''
imshow(window_name,img):显示图片,窗口自适应图片大小
    window_name: 指定窗口的名字
    img:显示的图片对象
    可以指定多个窗口名称,显示多个图片
'''

pixel = img[100,100]
print(pixel)
b = img[100,100,0]
g = img[100,100,1]
r = img[100,100,2]
print(b,g,r)
img[100,100,2] = 0

print(img.itemset((100,100,2),99))#替换
print(img.item(100,100,2))#获取


roi = img[0:100,0:100]
img[200:300,200:300] = roi

b,g,r = cv2.split(img)##截取三个通道
print(b,g,r)

'''
cv2.copyMakeBorder()
    参数:
        img:图像对象
        top,bottom,left,right: 上下左右边界宽度,单位为像素值
        borderType:
            cv2.BORDER_CONSTANT, 带颜色的边界,需要传入另外一个颜色值(只能用列表)
            cv2.BORDER_REFLECT, 边缘元素的镜像反射做为边界
            cv2.BORDER_REFLECT_101/cv2.BORDER_DEFAULT
            cv2.BORDER_REPLICATE, 边缘元素的复制做为边界
            CV2.BORDER_WRAP
        value: borderType为cv2.BORDER_CONSTANT时,传入的边界颜色值,如[0,255,0]
'''
img2 = cv2.imread(r'../Figure/p1.jpg', 1)
img2 = cv2.cvtColor(img2,cv2.COLOR_BGR2RGB)
#图像颜色空间转换,从BGR转换为RGB

content = cv2.copyMakeBorder(img2,20,20,20,20,cv2.BORDER_CONSTANT,value=[255,0,0])
reflect = cv2.copyMakeBorder(img2,20,20,20,20,cv2.BORDER_REFLECT)
reflect01 = cv2.copyMakeBorder(img2,20,20,20,20,cv2.BORDER_REFLECT_101)
replicate = cv2.copyMakeBorder(img2,20,20,20,20,cv2.BORDER_REPLICATE)
wrap = cv2.copyMakeBorder(img2,20,20,20,20,cv2.BORDER_WRAP)
titles = ["constant","reflect","reflect01","replicate","wrap"]
images = [content,reflect,reflect01,replicate,wrap]

for i in range(5):
    plt.subplot(2,3,i+1)
    '''
    可以依次画出这些子图
    三个整数是行数、列数和索引值(索引值从1开始)
    '''
    plt.imshow(images[i])
    plt.title(titles[i])
    plt.xticks([])
    plt.yticks([])
    #x,y轴为空
plt.show()

key = cv2.waitKey(0)
'''
waitKey(millseconds)  键盘绑定事件,阻塞监听键盘按键,返回一个数字(不同按键对应的数字不同)
    millseconds: 传入时间毫秒数,在该时间内等待键盘事件;传入0时,会一直等待键盘事件
'''
if key == 27:
    print(key)
    cv2.destroyAllWindows()
    '''
    destroyAllWindows(window_name) 
    window_name: 需要关闭的窗口名字,不传入时关闭所有窗口
    '''

'''
imwrite(img_path_name,img)
    img_path_name:保存的文件名
    img:文件对象
'''

test2

'''
cv2.createTrackbar() 为窗口添加trackbar
    参数:
        trackbarname: trackbar的名字
        winname: 窗口的名字
        value: trackbar创建时的值
        count:trackbar能设置的最大值,最小值总为0
        onChange:trackbar值发生变化时的回调函数,trackbar的值作为参数传给onchange

    cv2.getTrackbarPos() 获取某个窗口中trackbar的值
    参数:
        trackbarname: trackbar的名字
        winname: 窗口的名字
'''
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np

def nothing():
    pass

img = cv.imread(r'../Figure/p2.jpg',1)
img_hsv = cv.cvtColor(img,cv.COLOR_BGR2HSV)
#六角锥体模型 色调(H),饱和度(S),明度(V)
#在 HSV 颜色空间下,比 BGR 更容易跟踪某种颜色的物体,常用于分割指定颜色的物体。

cv.namedWindow('tracks')
cv.createTrackbar('LH','tracks',0,255,nothing)
cv.createTrackbar('LS','tracks',0,255,nothing)
cv.createTrackbar('LV','tracks',0,255,nothing)

cv.createTrackbar("UH","tracks",255,255,nothing)
cv.createTrackbar("US","tracks",255,255,nothing)
cv.createTrackbar("UV","tracks",255,255,nothing)
while (1):

    l_h = cv.getTrackbarPos("LH", "tracks")
    l_s = cv.getTrackbarPos("LS", "tracks")
    l_v = cv.getTrackbarPos("LV", "tracks")
    u_h = cv.getTrackbarPos("UH", "tracks")
    u_s = cv.getTrackbarPos("US", "tracks")
    u_v = cv.getTrackbarPos("UV", "tracks")

    lower_b = np.array([l_h, l_s, l_v])
    upper_b = np.array([u_h, u_s, u_v])

    mask = cv.inRange(img_hsv, lower_b, upper_b)
    res = cv.add(img, img, mask=mask)

    cv.imshow("img", img)
    cv.imshow("mask", mask)
    cv.imshow("res", res)
    k = cv.waitKey(1)
    if k == 27:
        break
cv.destroyAllWindows()

opencv dnn pytorch 模型 opencv gmm python_opencv


opencv dnn pytorch 模型 opencv gmm python_读取图像_02