openCV入门

2024-01-08 14:36:49

简介

?本文主要通过对啥都会一点研究生系列进行总结,对关键代码进行注释,方便使用以及复习。

1 基础功能

?1.1.显示图片

import cv2
# 读取图片
img = cv2.imread("Resources/lena.png")
# 显示图片
cv2.imshow("Lena Soderberg",img)
# 毫秒级延时 0表示一直延时 1000表示一秒
cv2.waitKey(0)

1. 2.显示视频

import cv2
# 导入视频
cap = cv2.VideoCapture("Resources/test_ video.mp4")
while True:
    # success:布尔值是否为真
    success, img = cap.read()
    # 视频框名称
    cv2.imshow("Result", img)
    # 按q 中断循环
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

1.3.摄像头显示?

import cv2
frameWidth = 640
frameHeight = 480
# 0:默认相机序号
cap = cv2.VideoCapture(0)
# 参数:3:在视频流的帧的宽度
# 参数:4:在视频流的帧的高度
# 参数:10:在视频流的帧的亮度
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10,150)
while True:
    success, img = cap.read()
    cv2.imshow("Result", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

1.4 图像预处理

import cv2
import numpy as np

img = cv2.imread("Resources/lena.png")
kernel = np.ones((5,5),np.uint8)
# 颜色转换成灰色图
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# 图像模糊化
# 7,7可以不同,数越大越模糊,但都必须为正数和奇数,也可以为零,标准差取0
imgBlur = cv2.GaussianBlur(imgGray,(7,7),0)
# 边缘检测
imgCanny = cv2.Canny(img,150,200)
# 膨胀处理
imgDialation = cv2.dilate(imgCanny,kernel,iterations=1)
# 腐蚀处理
imgEroded = cv2.erode(imgDialation,kernel,iterations=1)

cv2.imshow("Gray Image",imgGray)
cv2.imshow("Blur Image",imgBlur)
cv2.imshow("Canny Image",imgCanny)
cv2.imshow("Dialation Image",imgDialation)
cv2.imshow("Eroded Image",imgEroded)
cv2.waitKey(0)

2.调整图像大小

2.1 尺寸修改

import cv2
import numpy as np

img = cv2.imread("Resources/shapes.png")
# 打印图像尺寸
print(img.shape)

# 对于选中图像修改高宽
imgResize = cv2.resize(img,(1000,500))
print(imgResize.shape)

# 剪切图像
imgCropped = img[46:119,352:495]

cv2.imshow("Image",img)
# cv2.imshow("Image Resize",imgResize)
cv2.imshow("Image Cropped",imgCropped)

cv2.waitKey(0)

2.2 图形绘制与文字添加

import cv2
import numpy as np

# 创建矩阵
img = np.zeros((512,512,3),np.uint8)
#print(img)
#img[:]= 255,0,0

# 创建线条
cv2.line(img,(0,0),(img.shape[1],img.shape[0]),(0,255,0),3)
# 创建矩形
cv2.rectangle(img,(0,0),(250,350),(0,0,255),2)
# 创建圆形
cv2.circle(img,(400,50),30,(255,255,0),5)
# 添加字 
cv2.putText(img," OPENCV  ",(300,200),cv2.FONT_HERSHEY_COMPLEX,1,(0,150,0),3)

cv2.imshow("Image",img)

cv2.waitKey(0)

3.图像基础操作

3.1.图像透视

import cv2
import numpy as np

img = cv2.imread("Resources/cards.jpg")

width,height = 250,350
pts1 = np.float32([[111,219],[287,188],[154,482],[352,440]])
pts2 = np.float32([[0,0],[width,0],[0,height],[width,height]])

# 透视变换
# 使用getPerspectiveTransform()得到转换矩阵
matrix = cv2.getPerspectiveTransform(pts1,pts2)
#使用warpPerspective()进行透视变换
imgOutput = cv2.warpPerspective(img,matrix,(width,height))

cv2.imshow("Image",img)
cv2.imshow("Output",imgOutput)

cv2.waitKey(0)

3.2.图像拼接

import cv2
import numpy as np

img = cv2.imread('Resources/lena.png')
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

imgStack = stackImages(0.5,([img,imgGray,img],[img,img,img]))
# 水平拼接
imgHor = np.hstack((img,img))
# 垂直拼接
imgVer = np.vstack((img,img))

cv2.imshow("Horizontal",imgHor)
cv2.imshow("Vertical",imgVer)

缩小图像拼接

import cv2
import numpy as np


def stackImages(scale,imgArray):
    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] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
                else:
                    imgArray[x][y] = cv2.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]= cv2.cvtColor( imgArray[x][y], cv2.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 = cv2.imread('Resources/lena.png')
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

imgStack = stackImages(0.5,([img,imgGray,img],[img,img,img]))

# imgHor = np.hstack((img,img))
# imgVer = np.vstack((img,img))
#
# cv2.imshow("Horizontal",imgHor)
# cv2.imshow("Vertical",imgVer)
cv2.imshow("ImageStack",imgStack)

cv2.waitKey(0)

3.3 色块检测

import cv2
import numpy as np


# 图像拼接
def stackImages(scale,imgArray):
    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] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
                else:
                    imgArray[x][y] = cv2.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]= cv2.cvtColor( imgArray[x][y], cv2.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


# 图形轮廓函数
def getContours(img):
    # 输入参数:
    # img   : 单通道二值图像,白色是前景
    # RETR_EXTERNAL  : 只返回最外边的轮廓, hierarchy[i][2]=hierarchy[i][3]=-1
    # CHAIN_APPROX_NONE   :  存储轮廓上的所有点
    # 输出参数:
    # contours    : 轮廓  M*N  M是轮廓个数  N是每个轮廓的点
    # hierarchy   : 轮廓等级关系 M*4
    contours,hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    for cnt in contours:
        # 轮廓面积
        area = cv2.contourArea(cnt)
        print(area)
        if area>500:
            # 第一个参数是指明在哪幅图像上绘制轮廓;image为三通道才能显示轮廓
            # 第二个参数是轮廓本身,在Python中是一个list;
            # 第三个参数指定绘制轮廓list中的哪条轮廓,
            # 如果是 - 1,则绘制其中的所有轮廓。后面的参数很简单。
            # 其中thickness表明轮廓线的宽度,如果是 - 1(cv2.FILLED),则为填充模式
            cv2.drawContours(imgContour, cnt, -1, (255, 0, 0), 3)
            # 轮廓周长/弧长 第二参数:指定对象的形状是闭合的(True)
            #                     还是打开的(一条曲线)。
            peri = cv2.arcLength(cnt,True)
            #print(peri)
            # 轮廓逼近
            #该参数是一个正数,其值越小则逼近程度越高。
            # 通常建议使用轮廓周长的一定比例来计算该参数,常见的比例因子为0.01。
            approx = cv2.approxPolyDP(cnt,0.02*peri,True)
            print(len(approx))
            objCor = len(approx)
            x, y, w, h = cv2.boundingRect(approx)

            if objCor ==3: objectType ="Tri"
            elif objCor == 4:
                aspRatio = w/float(h)
                if aspRatio >0.98 and aspRatio <1.03: objectType= "Square"
                else:objectType="Rectangle"
            elif objCor>4: objectType= "Circles"
            else:objectType="None"
            cv2.rectangle(imgContour,(x,y),(x+w,y+h),(0,255,0),2)
            cv2.putText(imgContour,objectType,
                        (x+(w//2)-10,y+(h//2)-10),cv2.FONT_HERSHEY_COMPLEX,0.7,
                        (0,0,0),2)


path = 'Resources/shapes.png'
img = cv2.imread(path)

# 复制图像
imgContour = img.copy()
# 灰度图
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# 模糊
imgBlur = cv2.GaussianBlur(imgGray,(7,7),1)
# 边缘检测
imgCanny = cv2.Canny(imgBlur,50,50)
getContours(imgCanny)
# 定义空矩阵
imgBlank = np.zeros_like(img)
imgStack = stackImages(0.8,([img,imgGray,imgBlur],
                            [imgCanny,imgContour,imgBlank]))

cv2.imshow("Stack", imgStack)
cv2.waitKey(0)

文章来源:https://blog.csdn.net/weixin_44597885/article/details/135359623
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。