【图像处理:OpenCV-Python基础操作】
- [1 读取图像](#1 读取图像)
- [2 显示图像](#2 显示图像)
- [3 保存图像](#3 保存图像)
- [4 图像二值化、灰度图、彩色图,像素替换](#4 图像二值化、灰度图、彩色图,像素替换)
- [5 通道处理(通道拆分、合并)](#5 通道处理(通道拆分、合并))
- [6 调整尺寸大小](#6 调整尺寸大小)
- [7 提取感兴趣区域、掩膜](#7 提取感兴趣区域、掩膜)
- [8 乘法、逻辑运算](#8 乘法、逻辑运算)
- [9 HSV色彩空间,获取特定色彩区域](#9 HSV色彩空间,获取特定色彩区域)
- [10 滤波处理(均值,高斯、中值、形态学)](#10 滤波处理(均值,高斯、中值、形态学))
参考:李立宗. 计算机视觉40例从入门到深度学习(OpenCV-Python)[M],电子工业出版社,2022.
python
pip install opencv-python opencv-contrib-python(贡献库有诸多算法,有必要安装)
1 读取图像
python
import cv2
lena=cv2.imread("lenacolor.png")
print(lena)
2 显示图像
python
import cv2
lena=cv2.imread("lena.bmp")
cv2.imshow("demo1", lena )
cv2.imshow("demo2", lena )
cv2.waitKey()
cv2.destroyAllWindows()
3 保存图像
python
import cv2
lena=cv2.imread("lena.bmp")
r=cv2.imwrite("result.bmp",lena)
4 图像二值化、灰度图、彩色图,像素替换
python
import cv2
import numpy as np
img=np.zeros((8,8),dtype=np.uint8)
print("img=\n",img)
cv2.imshow("one",img)
print("读取像素点img[0,3]=",img[0,3])
img[0,3]=255
print("修改后img=\n",img)
print("读取修改后像素点img[0,3]=",img[0,3])
cv2.imshow("two",img)
cv2.waitKey()
cv2.destroyAllWindows()
python
import cv2
img=cv2.imread("lena.bmp",0)
cv2.imshow("before",img)
print("img[50,90]原始值:",img[50,90])
img[10:100,80:100]=255
print("img[50,90]修改值:",img[50,90])
cv2.imshow("after",img)
cv2.waitKey()
cv2.destroyAllWindows()
python
import cv2
img=cv2.imread("lenacolor.png")
cv2.imshow("before",img)
print("访问img[0,0]=",img[0,0])
print("访问img[0,0,0]=",img[0,0,0])
print("访问img[0,0,1]=",img[0,0,1])
print("访问img[0,0,2]=",img[0,0,2])
print("访问img[50,0]=",img[50,0])
print("访问img[100,0]=",img[100,0])
#区域1:白色
img[0:50,0:100,0:3]=255
#区域2:灰色
img[50:100,0:100,0:3]=128
#区域3 :黑色
img[100:150,0:100,0:3]=0
#区域4 :红色
img[150:200,0:100]=(0,0,255)
#显示
cv2.imshow("after",img)
print("修改后img[0,0]=",img[0,0])
print("修改后img[0,0,0]=",img[0,0,0])
print("修改后img[0,0,1]=",img[0,0,1])
print("修改后img[0,0,2]=",img[0,0,2])
print("修改后img[50,0]=",img[50,0])
print("修改后img[100,0]=",img[100,0])
cv2.waitKey()
cv2.destroyAllWindows()
5 通道处理(通道拆分、合并)
python
import cv2
lena=cv2.imread("lenacolor.png")
cv2.imshow("lena",lena)
b=lena[:,:,0]
g=lena[:,:,1]
r=lena[:,:,2]
cv2.imshow("b",b)
cv2.imshow("g",g)
cv2.imshow("r",r)
lena[:,:,0]=0
cv2.imshow("lenab0",lena)
lena[:,:,1]=0
cv2.imshow("lenab0g0",lena)
cv2.waitKey()
cv2.destroyAllWindows()
python
import cv2
lena=cv2.imread("lenacolor.png")
b,g,r=cv2.split(lena)
bgr=cv2.merge([b,g,r])
rgb=cv2.merge([r,g,b])
cv2.imshow("lena",lena)
cv2.imshow("bgr",bgr)
cv2.imshow("rgb",rgb)
cv2.waitKey()
cv2.destroyAllWindows()
6 调整尺寸大小
python
import cv2
img=cv2.imread("test.bmp")
rows,cols=img.shape[:2]
size=(int(cols*0.9),int(rows*0.5))
rst=cv2.resize(img,size)
print("img.shape=",img.shape)
print("rst.shape=",rst.shape)
img.shape= (512, 51, 3)
rst.shape= (256, 45, 3)
7 提取感兴趣区域、掩膜
python
import cv2
a=cv2.imread("lenacolor.png",cv2.IMREAD_UNCHANGED)
face=a[220:400,250:350]
cv2.imshow("original",a)
cv2.imshow("face",face)
cv2.waitKey()
cv2.destroyAllWindows()
python
import cv2
import numpy as np
m1=np.zeros([600,600],np.uint8)
m1[200:400,200:400]=255
m2=np.zeros([600,600],np.uint8)
m2[200:400,200:400]=1
cv2.imshow('m1',m1)
cv2.imshow('m2',m2)
cv2.imshow('m2*255',m2*255)
cv2.waitKey()
cv2.destroyAllWindows()
8 乘法、逻辑运算
python
import cv2
import numpy as np
o=cv2.imread("lenacolor.png",1)
h,w,c=o.shape
m=np.zeros((h,w,c),dtype=np.uint8)
m[100:400,200:400]=1
m[100:500,100:200]=1
result=m*o
cv2.imshow("o",o)
cv2.imshow("mask",m*255) #m*255,确保能显示
cv2.imshow("result",result)
cv2.waitKey()
cv2.destroyAllWindows()
python
import cv2
import numpy as np
o=cv2.imread("lenacolor.png",1)
h,w,c=o.shape
m=np.zeros((h,w,c),dtype=np.uint8)
m[100:400,200:400]=255
m[100:500,100:200]=255
result=cv2.bitwise_and(o,m)
cv2.imshow("original",o)
cv2.imshow("mask",m)
cv2.imshow("result",result)
cv2.waitKey()
cv2.destroyAllWindows()
python
import cv2
import numpy as np
o=cv2.imread("lenacolor.png",1)
t=cv2.imread("text.png",1)
h,w,c=o.shape
m=np.zeros((h,w),dtype=np.uint8)
m[100:400,200:400]=255
m[100:500,100:200]=255
r=cv2.add(o,t,mask=m)
cv2.imshow("orignal",o)
cv2.imshow("text",t)
cv2.imshow("mask",m)
cv2.imshow("result",r)
cv2.waitKey()
cv2.destroyAllWindows()
9 HSV色彩空间,获取特定色彩区域
python
import cv2
import numpy as np
img=cv2.imread("x.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
min_HSV = np.array([0 ,10,80], dtype = "uint8")
max_HSV = np.array([33, 255, 255], dtype = "uint8")
mask = cv2.inRange(hsv, min_HSV, max_HSV)
reusult = cv2.bitwise_and(img,img, mask= mask)
cv2.imshow("img",img)
cv2.imshow("reusult",reusult)
cv2.waitKey()
cv2.destroyAllWindows()
10 滤波处理(均值,高斯、中值、形态学)
也可以叫平滑处理
(1)均值滤波
python
import cv2
o=cv2.imread("lenaNoise.png")
r3=cv2.blur(o,(3,3))
r11=cv2.blur(o,(11,11))
cv2.imshow("original",o)
cv2.imshow("result3",r3)
cv2.imshow("result11",r11)
cv2.waitKey()
cv2.destroyAllWindows()
(2)高斯滤波
python
import cv2
o=cv2.imread("lenaNoise.png")
r1=cv2.GaussianBlur(o,(5,5),0,0)
r2=cv2.GaussianBlur(o,(5,5),0.1,0.1)
r3=cv2.GaussianBlur(o,(5,5),1,1)
cv2.imshow("original",o)
cv2.imshow("result1",r1)
cv2.imshow("result2",r2)
cv2.imshow("result3",r3)
cv2.waitKey()
cv2.destroyAllWindows()
(3)中值滤波
python
import cv2
o=cv2.imread("lenaNoise.png")
r=cv2.medianBlur(o,3)
cv2.imshow("original",o)
cv2.imshow("result",r)
cv2.waitKey()
cv2.destroyAllWindows()
(4)膨胀、腐蚀
python
import cv2
import numpy as np
o=cv2.imread(r"erode.bmp",cv2.IMREAD_UNCHANGED)
kernel1 = np.ones((3,3),np.uint8)
erosion1 = cv2.erode(o,kernel1)
kernel2 = np.ones((7,7),np.uint8)
erosion2 = cv2.erode(o,kernel2,iterations = 5)
cv2.imshow("orriginal",o)
cv2.imshow("erosion1",erosion1)
cv2.imshow("erosion2",erosion2)
cv2.waitKey()
cv2.destroyAllWindows()
python
import cv2
import numpy as np
o=cv2.imread("dilation.bmp",cv2.IMREAD_UNCHANGED)
kernel = np.ones((5,5),np.uint8)
dilation1 = cv2.dilate(o,kernel)
dilation2 = cv2.dilate(o,kernel,iterations = 9)
cv2.imshow("original",o)
cv2.imshow("dilation1",dilation1)
cv2.imshow("dilation2",dilation2)
cv2.waitKey()
cv2.destroyAllWindows()