OpenCV+Python

OpenCV+Python

  • 一、基础
    • (一)基础函数
    • (二)找中心
    • (三)绘制
    • (四)读取图片和视频
    • (五)二值化
    • (六)变换
  • 二、进阶
    • (一)图片基本运算
    • (二)滤波
    • (三)颜色转换
    • (四)梯度计算
    • (五)直方图
    • (六)掩饰
    • (七)视频播放
    • (八)通道分离与合并
  • 三、总结
  • 参考链接

一、基础

(一)基础函数

1.源代码·

import cv2 as cv
import numpy as np

def stackImages(scale, imgArray):
    """
        将多张图像压入同一个窗口显示
        :param scale:float类型,输出图像显示百分比,控制缩放比例,0.5=图像分辨率缩小一半
        :param imgArray:元组嵌套列表,需要排列的图像矩阵
        :return:输出图像
    """
    rows = len(imgArray)
    cols = len(imgArray[0])
    rowsAvailable = isinstance(imgArray[0], list)
    width = imgArray[0][0].shape[1]
    height = imgArray[0][0].shape[0]
    if rowsAvailable:
        for x in range(0, rows):
            for y in range(0, cols):
                if imgArray[x][y].shape[:2] == imgArray[0][0].shape[:2]:
                    imgArray[x][y] = cv.resize(imgArray[x][y], (0, 0), None, scale, scale)
                else:
                    imgArray[x][y] = cv.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]),
                                                None, scale, scale)
                if len(imgArray[x][y].shape) == 2: imgArray[x][y] = cv.cvtColor(imgArray[x][y], cv.COLOR_GRAY2BGR)
        imageBlank = np.zeros((height, width, 3), np.uint8)
        hor = [imageBlank] * rows
        hor_con = [imageBlank] * rows
        for x in range(0, rows):
            hor[x] = np.hstack(imgArray[x])
        ver = np.vstack(hor)
    else:
        for x in range(0, rows):
            if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
                imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
            else:
                imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None, scale, scale)
            if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
        hor = np.hstack(imgArray)
        ver = hor
    return ver


# Read in an image
# 读取图片
img = cv.imread('../Resources/Photos/park.jpg')


# Converting to grayscale
# 转灰度
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)


# Blur
# 高斯滤波
blur = cv.GaussianBlur(img, (7,7), cv.BORDER_DEFAULT)


# Edge Cascade
# Canny算子边缘检测
canny = cv.Canny(blur, 125, 175)


# Dilating the image
# 膨胀
dilated = cv.dilate(canny, (7,7), iterations=3)


# Eroding
# 腐蚀
eroded = cv.erode(dilated, (7,7), iterations=3)


# Resize
# 重新设置图片大小
resized = cv.resize(img, (500,500), interpolation=cv.INTER_CUBIC)


# Cropping
# 剪切图片
cropped = img[50:200, 200:400]


cv.putText(img, "Park", (0, 100), cv.FONT_HERSHEY_SIMPLEX, 3.0, (255, 0, 0), 3)
cv.putText(gray, "Gray", (0, 100), cv.FONT_HERSHEY_SIMPLEX, 3.0, (255, 0, 0), 3)
cv.putText(blur, "blur",(0, 100), cv.FONT_HERSHEY_SIMPLEX, 3.0, (255, 0, 0), 3)
cv.putText(canny, "Canny Edges", (0, 100), cv.FONT_HERSHEY_SIMPLEX, 3.0, (255, 0, 0), 3)
cv.putText(dilated, "Dilated", (0, 100), cv.FONT_HERSHEY_SIMPLEX, 3.0, (255, 0, 0), 3)
cv.putText(eroded, "Eroded", (0, 100), cv.FONT_HERSHEY_SIMPLEX, 3.0, (255, 0, 0), 3)
cv.putText(resized, "Resized", (0, 100), cv.FONT_HERSHEY_SIMPLEX, 3.0, (255, 0, 0), 3)
cv.putText(cropped, "Cropped", (0, 100), cv.FONT_HERSHEY_SIMPLEX, 3.0, (255, 0, 0), 3)

imgStack = stackImages(0.5,([img,gray,blur,canny],[dilated,eroded,resized,cropped]))
cv.imshow("imgStack", imgStack)
cv.waitKey(0)

2.运行结果

(1)读取图片
OpenCV+Python_第1张图片
(2)灰度图片
OpenCV+Python_第2张图片

(3)高斯滤波
OpenCV+Python_第3张图片

(4)边缘检测
OpenCV+Python_第4张图片

(5)膨胀
OpenCV+Python_第5张图片

(6)腐蚀
OpenCV+Python_第6张图片
(7)改变图片尺寸
OpenCV+Python_第7张图片

(8)裁剪图片
OpenCV+Python_第8张图片

(二)找中心

1.源代码

import cv2 as cv
import numpy as np
#读取图片
img = cv.imread('../Resources/Photos/cats.jpg')
cv.imshow('Cats', img)
#图片变成全黑
blank = np.zeros(img.shape, dtype='uint8')
cv.imshow('Blank', blank)
#转为灰度
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)
#高斯滤波
blur = cv.GaussianBlur(gray, (5,5), cv.BORDER_DEFAULT)
cv.imshow('Blur', blur)
#边缘检测
canny = cv.Canny(blur, 125, 175)
cv.imshow('Canny Edges', canny)
#二值化
ret, thresh = cv.threshold(gray, 125, 255, cv.THRESH_BINARY)
cv.imshow('Thresh', thresh)
#找中心
contours, hierarchies = cv.findContours(canny, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
print(f'{len(contours)} contour(s) found!')
#绘制
cv.drawContours(blank, contours, -1, (0,0,255), 1)
cv.imshow('Contours Drawn', blank)

cv.waitKey(0)

2.运行结果
(1)读取图像
OpenCV+Python_第9张图片

(2)图片变成全黑
OpenCV+Python_第10张图片
(3)转为灰度
OpenCV+Python_第11张图片
(4)高斯滤波
OpenCV+Python_第12张图片
(5)边缘检测
OpenCV+Python_第13张图片
(7)二值化
OpenCV+Python_第14张图片
(8)找中心绘制结果
OpenCV+Python_第15张图片

(三)绘制

1.代码


import cv2 as cv
import numpy as np

def stackImages(scale, imgArray):
    """
        将多张图像压入同一个窗口显示
        :param scale:float类型,输出图像显示百分比,控制缩放比例,0.5=图像分辨率缩小一半
        :param imgArray:元组嵌套列表,需要排列的图像矩阵
        :return:输出图像
    """
    rows = len(imgArray)
    cols = len(imgArray[0])
    rowsAvailable = isinstance(imgArray[0], list)
    width = imgArray[0][0].shape[1]
    height = imgArray[0][0].shape[0]
    if rowsAvailable:
        for x in range(0, rows):
            for y in range(0, cols):
                if imgArray[x][y].shape[:2] == imgArray[0][0].shape[:2]:
                    imgArray[x][y] = cv.resize(imgArray[x][y], (0, 0), None, scale, scale)
                else:
                    imgArray[x][y] = cv.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]),
                                                None, scale, scale)
                if len(imgArray[x][y].shape) == 2: imgArray[x][y] = cv.cvtColor(imgArray[x][y], cv.COLOR_GRAY2BGR)
        imageBlank = np.zeros((height, width, 3), np.uint8)
        hor = [imageBlank] * rows
        hor_con = [imageBlank] * rows
        for x in range(0, rows):
            hor[x] = np.hstack(imgArray[x])
        ver = np.vstack(hor)
    else:
        for x in range(0, rows):
            if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
                imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
            else:
                imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None, scale, scale)
            if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
        hor = np.hstack(imgArray)
        ver = hor
    return ver

# 创造一个黑色背景板
img_1 = np.zeros((500,500,3), dtype='uint8')
# 绘制边缘线便于区分
cv.line(img_1, (0,0), (0,img_1.shape[0]), (255,255,255), thickness=3)
cv.line(img_1, (0,img_1.shape[0]), (img_1.shape[1],img_1.shape[0]), (255,255,255), thickness=3)


# 1. Paint the image a certain colour
# 把一个区域设置为指定颜色
img_2=img_1.copy()
img_2[200:300, 300:400]=0,0,255


# 2. Draw a Rectangle
# 画一个矩形
# 第一个参数为目标图片
# 第二个参数为矩形起始坐标,矩形左上角
# 第三个参数为矩形终止坐标,矩形右下角
# 第四个参数为颜色
# 第五个参数为线条宽度,-1表示填充
img_3=img_2.copy()
cv.rectangle(img_3, (0,0), (img_3.shape[1]//2, img_3.shape[0]//2), (0,255,0), thickness=-1)


# 3. Draw A circle
# 画一个圆
# 第一个参数为目标图片
# 第二个参数为圆心坐标
# 第三个参数为半径
# 第四个参数为颜色
# 第五个参数为线条宽度,-1表示填充
img_4=img_3.copy()
cv.circle(img_4, (img_4.shape[1]//2, img_4.shape[0]//2), 40, (0,0,255), thickness=-1)


# 4. Draw a line
# 画一条直线
# 第一个参数为目标图片
# 第二个参数为起始坐标
# 第三个参数为终止坐标
# 第四个参数为颜色
# 第五个参数为线条宽度
img_5=img_4.copy()
cv.line(img_5, (100,250), (300,400), (255,255,255), thickness=3)


# 5. Write text
# 在图片上写字
# 第一个参数为目标图片
# 第二个参数为显示内容
# 第三个参数为显示起始坐标
# 第四个参数为字体类型
# 第五个参数为字体大小
# 第六个参数为字体颜色
# 第七个参数为字体线条粗细
img_6=img_5.copy()
cv.putText(img_6, 'HelloWorld', (255,150), cv.FONT_HERSHEY_TRIPLEX, 1.0, (255,255,255), 2)


imgStack = stackImages(0.5,([img_1,img_2,img_3],[img_4,img_5,img_6]))
cv.imshow("imgStack", imgStack)
cv.waitKey(0)

2.运行结果
OpenCV+Python_第16张图片
OpenCV+Python_第17张图片

(四)读取图片和视频

1.代码

import cv2 as cv
#读取图片
img = cv.imread('../Resources/Photos/cats.jpg')
cv.imshow('Cats', img)

cv.waitKey(0)
#读取视频
# Reading Videos
capture = cv.VideoCapture('../Resources/Videos/dog.mp4')

while True:
    isTrue, frame = capture.read()
    
    # if cv.waitKey(20) & 0xFF==ord('d'):
    # This is the preferred way - if `isTrue` is false (the frame could 
    # not be read, or we're at the end of the video), we immediately
    # break from the loop. 
    if isTrue:    
        cv.imshow('Video', frame)
        if cv.waitKey(20) & 0xFF==ord('d'):
            break            
    else:
        break

capture.release()
cv.destroyAllWindows()

2运行结果
(1)图片
OpenCV+Python_第18张图片
(2)视频

(五)二值化

1.源代码

import cv2 as cv
#读取图像
img = cv.imread('../Resources/Photos/cats.jpg')
cv.imshow('Cats', img)
#转灰度
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)
#二值化
# Simple Thresholding
threshold, thresh = cv.threshold(gray, 150, 255, cv.THRESH_BINARY )
cv.imshow('Simple Thresholded', thresh)

threshold, thresh_inv = cv.threshold(gray, 150, 255, cv.THRESH_BINARY_INV )
cv.imshow('Simple Thresholded Inverse', thresh_inv)

# Adaptive Thresholding
adaptive_thresh = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 11, 9)
cv.imshow('Adaptive Thresholding', adaptive_thresh)

cv.waitKey(0)

2.运行结果
(1)原图和灰度图片

(2)二值化
OpenCV+Python_第19张图片
OpenCV+Python_第20张图片

(六)变换

1.源代码

import cv2 as cv
import numpy as np
#读取图像
img = cv.imread('../Resources/Photos/park.jpg')
cv.imshow('Park', img)

# 缩放
def translate(img, x, y):
    transMat = np.float32([[1,0,x],[0,1,y]])
    dimensions = (img.shape[1], img.shape[0])
    return cv.warpAffine(img, transMat, dimensions)

# -x --> Left
# -y --> Up
# x --> Right
# y --> Down

translated = translate(img, -100, 100)
cv.imshow('Translated', translated)

# 旋转
def rotate(img, angle, rotPoint=None):
    (height,width) = img.shape[:2]

    if rotPoint is None:
        rotPoint = (width//2,height//2)
    
    rotMat = cv.getRotationMatrix2D(rotPoint, angle, 1.0)
    dimensions = (width,height)

    return cv.warpAffine(img, rotMat, dimensions)

rotated = rotate(img, -45)
cv.imshow('Rotated', rotated)

rotated_rotated = rotate(img, -90)
cv.imshow('Rotated Rotated', rotated_rotated)

# Resizing
#调整
resized = cv.resize(img, (500,500), interpolation=cv.INTER_CUBIC)
cv.imshow('Resized', resized)

# Flipping
#翻转
flip = cv.flip(img, -1)
cv.imshow('Flip', flip)

# Cropping
#裁剪
cropped = img[200:400, 300:400]
cv.imshow('Cropped', cropped)

2.运行结果
(1)读取的图片
OpenCV+Python_第21张图片
(2)缩放
OpenCV+Python_第22张图片
(3)旋转

(4)调整
OpenCV+Python_第23张图片

(5)翻转
OpenCV+Python_第24张图片

(6)裁剪
OpenCV+Python_第25张图片

二、进阶

(一)图片基本运算

1.源代码

#pylint:disable=no-member

import cv2 as cv
import numpy as np

blank = np.zeros((400,400), dtype='uint8')

rectangle = cv.rectangle(blank.copy(), (30,30), (370,370), 255, -1)
circle = cv.circle(blank.copy(), (200,200), 200, 255, -1)

cv.imshow('Rectangle', rectangle)
cv.imshow('Circle', circle)

# bitwise AND --> intersecting regions
bitwise_and = cv.bitwise_and(rectangle, circle)
cv.imshow('Bitwise AND', bitwise_and)

# bitwise OR --> non-intersecting and intersecting regions
bitwise_or = cv.bitwise_or(rectangle, circle)
cv.imshow('Bitwise OR', bitwise_or)

# bitwise XOR --> non-intersecting regions
bitwise_xor = cv.bitwise_xor(rectangle, circle)
cv.imshow('Bitwise XOR', bitwise_xor)

# bitwise NOT
bitwise_not = cv.bitwise_not(circle)
cv.imshow('Circle NOT', bitwise_not)

cv.waitKey(0)

2.结果
OpenCV+Python_第26张图片

OpenCV+Python_第27张图片

(二)滤波

1.源代码

#pylint:disable=no-member

import cv2 as cv

img = cv.imread('../Resources/Photos/cats.jpg')
cv.imshow('Cats', img)

# Averaging
average = cv.blur(img, (3,3))
cv.imshow('Average Blur', average)

# Gaussian Blur
gauss = cv.GaussianBlur(img, (3,3), 0)
cv.imshow('Gaussian Blur', gauss)

# Median Blur
median = cv.medianBlur(img, 3)
cv.imshow('Median Blur', median)

# Bilateral
bilateral = cv.bilateralFilter(img, 10, 35, 25)
cv.imshow('Bilateral', bilateral)

cv.waitKey(0)

2.运行结果
(1)读取图片

OpenCV+Python_第28张图片
(2)处理效果

(三)颜色转换

1.源代码

#pylint:disable=no-member

import cv2 as cv
import matplotlib.pyplot as plt

img = cv.imread('../Resources/Photos/park.jpg')
cv.imshow('Park', img)

# plt.imshow(img)
# plt.show()

# BGR to Grayscale
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)

# BGR to HSV
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
cv.imshow('HSV', hsv)

# BGR to L*a*b
lab = cv.cvtColor(img, cv.COLOR_BGR2LAB)
cv.imshow('LAB', lab)

# BGR to RGB
rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
cv.imshow('RGB', rgb)

# HSV to BGR
lab_bgr = cv.cvtColor(lab, cv.COLOR_LAB2BGR)
cv.imshow('LAB --> BGR', lab_bgr)

cv.waitKey(0)

2.运行结果

(四)梯度计算

1.源代码


import cv2 as cv
import numpy as np

img = cv.imread('../Resources/Photos/park.jpg')
cv.imshow('Park', img)

gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)

# Laplacian
lap = cv.Laplacian(gray, cv.CV_64F)
lap = np.uint8(np.absolute(lap))
cv.imshow('Laplacian', lap)

# Sobel 
sobelx = cv.Sobel(gray, cv.CV_64F, 1, 0)
sobely = cv.Sobel(gray, cv.CV_64F, 0, 1)
combined_sobel = cv.bitwise_or(sobelx, sobely)

cv.imshow('Sobel X', sobelx)
cv.imshow('Sobel Y', sobely)
cv.imshow('Combined Sobel', combined_sobel)

canny = cv.Canny(gray, 150, 175)
cv.imshow('Canny', canny)
cv.waitKey(0)

2.运行结果
(1)读取原图
OpenCV+Python_第29张图片
(2)处理结果


(五)直方图

1.源代码

#pylint:disable=no-member

import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np

img = cv.imread('../Resources/Photos/cats.jpg')
cv.imshow('Cats', img)

blank = np.zeros(img.shape[:2], dtype='uint8')

gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)

mask = cv.circle(blank, (img.shape[1]//2,img.shape[0]//2), 100, 255, -1)

masked = cv.bitwise_and(img,img,mask=mask)
cv.imshow('Mask', masked)

#GRayscale histogram
gray_hist = cv.calcHist([gray], [0], mask, [256], [0,256] )

plt.figure()
plt.title('Grayscale Histogram')
plt.xlabel('Bins')
plt.ylabel('# of pixels')
plt.plot(gray_hist)
plt.xlim([0,256])
plt.show()

# Colour Histogram

plt.figure()
plt.title('Colour Histogram')
plt.xlabel('Bins')
plt.ylabel('# of pixels')
colors = ('b', 'g', 'r')
for i,col in enumerate(colors):
    hist = cv.calcHist([img], [i], mask, [256], [0,256])
    plt.plot(hist, color=col)
    plt.xlim([0,256])

plt.show()

cv.waitKey(0)

2.运行结果
(1)颜色直方图
OpenCV+Python_第30张图片
(2)灰度直方图
OpenCV+Python_第31张图片

(六)掩饰

1.源代码

#pylint:disable=no-member

import cv2 as cv
import numpy as np

img = cv.imread('../Resources/Photos/cats 2.jpg')
cv.imshow('Cats', img)

blank = np.zeros(img.shape[:2], dtype='uint8')
cv.imshow('Blank Image', blank)

circle = cv.circle(blank.copy(), (img.shape[1]//2 + 45,img.shape[0]//2), 100, 255, -1)

rectangle = cv.rectangle(blank.copy(), (30,30), (370,370), 255, -1)

weird_shape = cv.bitwise_and(circle,rectangle)
cv.imshow('Weird Shape', weird_shape)

masked = cv.bitwise_and(img,img,mask=weird_shape)
cv.imshow('Weird Shaped Masked Image', masked)

cv.waitKey(0)

2.运行结果
OpenCV+Python_第32张图片

(七)视频播放

1.源代码

import cv2 as cv

# img = cv.imread('../Resources/Photos/cat.jpg')
# cv.imshow('Cat', img)

def rescaleFrame(frame, scale=0.75):
    # Images, Videos and Live Video
    width = int(frame.shape[1] * scale)
    height = int(frame.shape[0] * scale)

    dimensions = (width,height)

    return cv.resize(frame, dimensions, interpolation=cv.INTER_AREA)

def changeRes(width,height):
    # Live video
    capture.set(3,width)
    capture.set(4,height)
    
# Reading Videos
capture = cv.VideoCapture('../Resources/Videos/dog.mp4')

while True:
    isTrue, frame = capture.read()

    frame_resized = rescaleFrame(frame, scale=.2)
    
    cv.imshow('Video', frame)
    cv.imshow('Video Resized', frame_resized)

    if cv.waitKey(20) & 0xFF==ord('d'):
        break

capture.release()
cv.destroyAllWindows()

2.效果

(八)通道分离与合并

1.源代码

import cv2 as cv
import numpy as np

img = cv.imread('../Resources/Photos/park.jpg')
cv.imshow('Park', img)

blank = np.zeros(img.shape[:2], dtype='uint8')

b,g,r = cv.split(img)

blue = cv.merge([b,blank,blank])
green = cv.merge([blank,g,blank])
red = cv.merge([blank,blank,r])


cv.imshow('Blue', blue)
cv.imshow('Green', green)
cv.imshow('Red', red)

print(img.shape)
print(b.shape)
print(g.shape)
print(r.shape)

merged = cv.merge([b,g,r])
cv.imshow('Merged Image', merged)

cv.waitKey(0)

2.运行结果
(1)读取原图
OpenCV+Python_第33张图片

(2)效果

三、总结

本次实验了解了很多关于opencv处理图像视频的基础函数,以及他们的用法,学会了用各种函数来对图像进行处理,达到需要的效果。

参考链接

OpenCV+Python简单实践

你可能感兴趣的:(网络通信,opencv,python,计算机视觉)