目录
Lean镇楼!!
1、读写图像
先安装 OpenCV
# encoding: utf-8
import cv2
import matplotlib.pyplot as plt
path = r"E:\PythonProjects\python_study\opencv\lena.jpg"
write_path = r"E:\PythonProjects\python_study\opencv\lena_cpoy.jpg"
img = cv2.imread(path)
# 1
# plt.imshow(img)
# plt.show()
# 2
cv2.imshow("demo",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# write
cv2.imwrite(write_path,img)
2、图像处理基础知识
(1)入门知识
在彩色图像中,图像可用一个二维矩阵表示,其中每个元素是RGB值,RGB每个占一个字节(8位),总共三个字节,每个字节取值范围是0-2^8(255),也可以说是三通道的,分别是R,G,B,比如(255,0,0)代表红色,(0,255,0)代表绿色,(0,0,255)代表蓝色,其他颜色是这三个颜色的混合。
在灰色图像中,每个元素只有单通道,即只有一个值。
位深度:位深度是把通道数转化为位,即3通道位深度为3*8=24位。
分辨率:分辨率即为高像素*宽像素,如1080*1440分辨率
DPI(Dots Per Inch,每英寸像素点数):
通过DPI和像素可以求出图片的实际尺寸,如1080*1440分辨率,DPI96,那么图片实际高为1080/96 = 11.25英寸,宽为1440/96=15英寸。
图片清晰程度不是由像素决定,而是用DPI来决定,DPI越大,清晰度越高。
注:
鼠标的DPI参数指的是鼠标在桌面上移动1英寸的距离的同时,鼠标光标能够在屏幕上移动多少“点”。越高,移动越快越灵敏。
OpenCV中图像的三原色顺序为 BGR
(2)处理像素
# encoding: utf-8
import cv2
import matplotlib.pyplot as plt
import copy
def show_img(list_img):
plt.figure()
plt.subplot(1,2,1)
plt.imshow(list_img[0])
plt.subplot(1,2,2)
plt.imshow(list_img[1])
plt.show()
path = r"E:\PythonProjects\python_study\opencv\lena.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
# bgr图像
print("*****************bgr***************")
#1
b = img[78,125,0]
g = img[78,125,1]
r = img[78,125,2]
img[78,125,0]=100# 赋值,修改值
bgr=img[78,125]
print("b={}\ng={}\nr={}\nbgr={}".format(b,g,r,bgr))
#2
print(img.item(100,100,0))#查看值
img.itemset((100,100,0),255)#修改值
print(img.item(100,100,0))
print("========修改多个行列数值=========")
#1
i1=copy.deepcopy(img)
img[100:150,100:150]=[255,255,255]
show_img([i1,img])
# 灰度图
print("*****************gray***************")
p = img[78,125]
print("p={}".format(p))
img[78,125]=125# 赋值
print("p={}\n".format(p))
#2
print(gray.item(100,100))#查看值
gray.itemset((100,100),255)#修改值
print(gray.item(100,100))
(3)获取图像属性
# encoding: utf-8
import cv2
import matplotlib.pyplot as plt
import copy
import numpy as np
path = r"E:\PythonProjects\python_study\opencv\lena.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
print("*****************bgr***************")
# bgr图像
print(img.shape)
print(img.size)#行*列*通道数,计算出像素个数
print(img.dtype)#图像中数据的数据类型
print("*****************gray***************")
# 灰度图
print(gray.shape)
print(gray.size)
print(gray.dtype)
(4)感兴趣趋于ROI
# encoding: utf-8
import cv2
import matplotlib.pyplot as plt
import math
import copy
import numpy as np
def show_img(img):
plt.figure()
plt.imshow(img)
plt.show()
path = r"E:\PythonProjects\python_study\opencv\lena.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
print("*****************bgr***************")
# bgr图像
print("=======根据下标的方式取roi========")
face =np.ones((180,100,3))
face = img[220:400,250:350]
show_img(face)
show_img(face)
img[0:180,0:100]=face
show_img(img)
(5)通道的拆分与合并
# encoding: utf-8
import cv2
import matplotlib.pyplot as plt
import math
import copy
import numpy as np
def show_img(img):
plt.figure()
plt.imshow(img)
plt.show()
path = r"E:\PythonProjects\python_study\opencv\lena.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
print("===============通道拆分==================")
#1
b1 = img[:,:,0]
g1 = img[:,:,1]
r1 = img[:,:,2]
#2
b2,g2,r2 = cv2.split(img)
b3 = cv2.split(img)[0]
g3 = cv2.split(img)[1]
r3 = cv2.split(img)[2]
# show_img(b2)
print("=============通道合并,注意顺序==============")
#1
m = cv2.merge([b1,g1,r1])
# show_img(m)
#2
bt = cv2.split(img)[0]
rows,cols,chn = img.shape
gt = np.zeros((rows,cols),dtype=img.dtype)
rt = np.zeros((rows,cols),dtype=img.dtype)
mt = cv2.merge([bt,gt,rt])
show_img(mt)