欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

有趣的Python OpenCV教程学习(上)

程序员文章站 2023-01-15 11:50:22
1.读取图像转化成灰度图。import cv2import numpy as npfrom matplotlib import pyplot as pltimg = cv2.imread('dog.jpg',cv2.IMREAD_GRAYSCALE)cv2.imshow('image',img)cv2.waitKey(0)cv2.destroyAllWindows()...

 

前置环境:

Windows:

pip install numpy
pip install matplotlib

 Linux:

pip3 install numpy 
pip3 install matplotlib 

 

1.读取图像转化成灰度图。

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('dog.jpg',cv2.IMREAD_GRAYSCALE)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

有趣的Python OpenCV教程学习(上)

cv2.imwrite('doggray.jpg',img)#保存

 

2.读取视频转化成灰度视频。

import numpy as np
import cv2

cap = cv2.VideoCapture('vtest.avi')

while(True):
    ret,frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    cv2.imshow('frame',frame)
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):#按q键退出
        break

cap.release()
cv2.destoryAllWindows()

有趣的Python OpenCV教程学习(上)

 

3.在图像上绘制和写字。

import numpy as np
import cv2

img = cv2.imread('dog.jpg',cv2.IMREAD_COLOR)

cv2.line(img,(130,180),(130,350),(0,255,255),45)#cv2.line()参数:图片,开始坐标,结束坐标,颜色(bgr),线条粗细。
cv2.rectangle(img,(15,25),(250,150),(0,0,255),15)#cv2.rectangle()参数: 图像,左上角坐标,右下角坐标,颜色, 线条粗细。
cv2.circle(img,(130,180),55,(0,255,0),-1)#cv2.circle()参数:图像,圆心,半径,颜色,粗细为-1(填充)。


font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV!',(0,100),font,2,(200,255,155),5)#cv2.putText()参数:图像,显示字符,左上角坐标,定义字体,字体大小,颜色,字体粗细


cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

有趣的Python OpenCV教程学习(上)

4.图像混合。

import cv2
import numpy as np

# Load two images
img1 = cv2.imread('1.jpeg')
img2 = cv2.imread('2.png')

# I want to put logo on top-left corner, So I create a ROI
rows,cols,channels = img2.shape
roi = img1[0:rows, 0:cols ]

# Now create a mask of logo and create its inverse mask
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)

# add a threshold
ret, mask = cv2.threshold(img2gray, 220, 255, cv2.THRESH_BINARY_INV)

mask_inv = cv2.bitwise_not(mask)

# Now black-out the area of logo in ROI
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2,img2,mask = mask)

dst = cv2.add(img1_bg,img2_fg)
img1[0:rows, 0:cols ] = dst

cv2.imshow('res',img1)
cv2.imwrite('xian2.jpg',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

有趣的Python OpenCV教程学习(上)

就用上面代码解决这个问题吧!

有趣的Python OpenCV教程学习(上)


ret, mask = cv2.threshold(img2gray, 220, 255, cv2.THRESH_BINARY_INV)
#cv2.threshold()参数:图像,【其中220是阀值,所有低于220的将会转化为0(黑色),高于220的将会转化为1(白色)】,最大值,阀值类型。

自行调节阀值可达到以下效果。

有趣的Python OpenCV教程学习(上)

 

5.阀值进阶。

import cv2
import numpy as np

img = cv2.imread('bookpage.jpg')
retval, threshold = cv2.threshold(img, 10, 255, cv2.THRESH_BINARY)
cv2.imshow('original',img)
cv2.imshow('threshold',threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()
原图地址:https://pythonprogramming.net/static/images/opencv/bookpage.jpg

 

有趣的Python OpenCV教程学习(上)

import cv2
import numpy as np

img = cv2.imread('bookpage.jpg')
grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval, threshold = cv2.threshold(grayscaled, 10, 255, cv2.THRESH_BINARY)
th = cv2.adaptiveThreshold(grayscaled, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)#自适应调节阀值
cv2.imshow('original',img)
cv2.imshow('Adaptive threshold',th)
cv2.waitKey(0)
cv2.destroyAllWindows()

有趣的Python OpenCV教程学习(上)

 

6.Canny边缘检测。

import cv2
import numpy as np

cap = cv2.VideoCapture('vtest.avi')

while(1):

    _, frame = cap.read()
    cv2.imshow('Original',frame)
    edges = cv2.Canny(frame,100,200)
    cv2.imshow('Edges',edges)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()
cap.release()

 有趣的Python OpenCV教程学习(上)

 

 

 

参考:https://www.jianshu.com/p/91f5bce4420d

本文地址:https://blog.csdn.net/qq_34717531/article/details/107402545

相关标签: opencv