OpenCV案例教程(数字图像处理Python实现,含⽬录)⼀、图像基本知识
1.读⼊图像
#导⼊opencv的python版本依赖库cv2
import cv2
#使⽤opencv中imread函数读取图⽚,
#0代表灰度图形式打开,1代表彩⾊形式打开
img = cv2.imread('split.jpg',1)
print(img.shape)
#print(img)
2.显⽰图像及保存图像
#导⼊opencv依赖库
import cv2
#读取图像,读取⽅式为彩⾊读取
img = cv2.imread('split.jpg',1)
#
cv2.imshow('photo',img)
k = cv2.waitKey(5000)
if k ==27:# 输⼊ESC键退出
cv2.destroyAllWindows()
elif k ==ord('s'):# 输⼊S键保存图⽚并退出
cv2.imwrite('split_.jpg',img)
cv2.destroyAllWindows()
#加载第三⽅包
import cv2
from matplotlib import pyplot as plt
#使⽤Matplotlib导⼊图像
img = cv2.imread('test_image.png',0)
plt.imshow(img, cmap ='gray', interpolation ='bicubic')
#隐藏X、Y轴上的刻度
plt.show()
3.通道转化,三通道转为单通道灰度图
#导⼊opencv
import cv2
#读⼊原始图像,使⽤cv2.IMREAD_UNCHANGED
img = cv2.imread("girl.jpg",cv2.IMREAD_UNCHANGED)
#查看打印图像的shape
shape = img.shape
print(shape)
#判断通道数是否为3通道或4通道
if shape[2]==3or shape[2]==4:
#将彩⾊图转化为单通道图
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow("gray_image",img_gray)
cv2.imshow("image", img)
cv2.waitKey(5000)
cv2.destroyAllWindows()
4.通道转化,单通道转为三通道灰度图
import cv2
#读⼊灰度图
img = cv2.imread("girl_gray.jpg",cv2.IMREAD_UNCHANGED)
#查看打印图像的shape
shape = img.shape
print(shape)
img_color = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
print(img_color.shape)
cv2.imshow("color_image",img_color)
cv2.imshow("image", img)
cv2.waitKey(5000)
cv2.destroyAllWindows()
5.图像三通道分离
#加载opencv
import cv2
src=cv2.imread('split.jpg')
cv2.imshow('before',src)
#调⽤通道分离
b,g,r=cv2.split(src)
#三通道分别显⽰
cv2.imshow('blue',b)
cv2.imshow('green',g)
cv2.imshow('red',r)
cv2.waitKey(5000)
cv2.destroyAllWindows()
#导⼊opencv模块
import numpy as np
import cv2
image=cv2.imread("split.jpg")#读取要处理的图⽚
cv2.imshow("src",image)
cv2.waitKey(5000)
B,G,R = cv2.split(image)#分离出图⽚的B,R,G颜⾊通道
zeros = np.zeros(image.shape[:2],dtype="uint8")#创建与image相同⼤⼩的零矩阵cv2.imshow("BLUE",([B,zeros,zeros]))#显⽰(B,0,0)图像
cv2.imshow("GREEN",([zeros,G,zeros]))#显⽰(0,G,0)图像
cv2.imshow("RED",([zeros,zeros,R]))#显⽰(0,0,R)图像
cv2.waitKey(5000)
手自一体变速箱cv2.destroyAllWindows()
浙菜代表菜有哪些print(image.shape[:2])
print(image.shape[1:])
6.图像通道合并
#加载opencv
import cv2
src=cv2.imread('split.jpg')
cv2.imshow('before',src)
#调⽤通道分离
b,g,r=cv2.split(src)
#将Blue通道数值修改为0
g[:]=0
#合并修改后的通道
img_([b,g,r])
cv2.imshow('merge',img_merge)
cv2.waitKey(5000)
朱砂有毒cv2.destroyAllWindows()
7.通道分离与合并⼩结
import numpy as np
src=cv.imread('split.jpg')
#cv.namedWindow('before',cv.WINDOW_NORMAL)
cv.imshow('before',src)
cv.waitKey(5000)
#通道分离
b,g,r=cv.split(src)
cv.imshow('blue',b)
cv.imshow('green',g)
cv.imshow('red',r)
cv.waitKey(5000)
#通道合并
甘草的功效和作用
([b,g,r])
cv.imshow('merge',src)
cv.waitKey(5000)
# 修改某个通道
src[:,:,2]=100
cv.imshow('single',src)
cv.waitKey(5000)
cv.destroyAllWindows()
8.RGB与BGR转化
import cv2
import matplotlib.pyplot as plt
img = cv2.imread("test2.png", cv2.IMREAD_COLOR)
cv2.imshow("Opencv_win", img)
# ⽤opencv⾃带的⽅法转
img_cv_method = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# ⽤numpy转,img[:,:,::-1]列左右翻转
img_numpy_method = img[:,:,::-1]# 本来是BGR 现在逆序,变成RGB # ⽤matplot画图
艺字成语plt.subplot(1,3,1)
plt.imshow(img_cv_method)大闸蟹做法
plt.subplot(1,3,2)
plt.imshow(img_numpy_method)
plt.subplot(1,3,3)
plt.imshow(img)
plt.savefig("./plt.png")
plt.show()
#保存图⽚
cv2.imwrite("opencv.png", img)
cv2.waitKey(5000)
cv2.destroyAllWindows()
9.RGB与HSV转化
import cv2
#⾊彩空间转换函数蒸鱼的调料
def color_space_demo(image):
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',gray)
hsv=cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
#print(hsv)
cv2.imshow('hsv',hsv)
#读⼊⼀张彩⾊图
src=cv2.imread('girl.jpg')
cv2.imshow('before',src)
#调⽤color_space_demo函数进⾏⾊彩空间转化
color_space_demo(src)
cv2.waitKey(0)
cv2.destroyAllWindows()
10.直⽅图绘制
#加载第三⽅库
from matplotlib import pyplot as plt
import cv2
girl = cv2.imread("girl.jpg")
cv2.imshow("girl", girl)
# girl.ravel()函数是将图像的三位数组降到⼀维上去,
#256为bins的数⽬,[0, 256]为范围
plt.hist(girl.ravel(),256,[0,256])
plt.show()
cv2.waitKey(5000)
cv2.destroyAllWindows()
from matplotlib import pyplot as plt
import cv2
import numpy as np
img = cv2.imread('girl.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(img_gray, ay)
hist = cv2.calcHist([img],[0],None,[256],[0,256])
plt.figure()
plt.title("Grayscale Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
plt.plot(hist)
plt.xlim([0,256])
plt.show()
11.三通道直⽅图绘制
from matplotlib import pyplot as plt
import cv2
girl = cv2.imread("girl.jpg")
cv2.imshow("girl", girl)
color =("b","g","r")
#使⽤for循环遍历color列表,enumerate枚举返回索引和值for i, color in enumerate(color):
hist = cv2.calcHist([girl],[i],None,[256],[0,256])
plt.title("girl")
plt.xlabel("Bins")
plt.ylabel("num of perlex")
plt.plot(hist, color = color)
plt.xlim([0,260])
plt.show()
cv2.waitKey(5000)
cv2.destroyAllWindows()
⼆、图像基本处理
1.第⼀节:基本操作
1.绘制线段
import numpy as np
import cv2
# 创建⼀张⿊⾊的背景图
s((512,512,3), np.uint8)
cv2.imshow("black", img)
cv2.waitKey(5000)
cv2.destroyWindow("black")
# 绘制⼀条线宽为5的线段
cv2.line(img,(0,0),(200,500),(0,0,255),5)
winname ='example'
cv2.namedWindow(winname)
cv2.imshow(winname, img)
cv2.waitKey(5000)
cv2.destroyWindow(winname)
2.绘制矩形
import numpy as np
import cv2
# 创建⼀张⿊⾊的背景图
s((512,512,3), np.uint8)
# 画⼀个绿⾊边框的矩形,参数2:左上⾓坐标,参数3:右下⾓坐标angle(img,(384,0),(510,128),(0,255,255),10)
winname ='example'
cv2.namedWindow(winname)
cv2.imshow(winname, img)
cv2.waitKey(5000)
cv2.destroyWindow(winname)
3.绘制圆
import numpy as np
import cv2
# 创建⼀张⿊⾊的背景图
s((512,512,3), np.uint8)
# 画⼀个填充红⾊的圆,参数2:圆⼼坐标,参数3:半径
cv2.circle(img,(447,63),63,(0,0,255),-1)
winname ='example'
cv2.namedWindow(winname)
cv2.imshow(winname, img)
学生考试cv2.waitKey(5000)
cv2.destroyWindow(winname)
4.绘制椭圆