初学数字图像处理,以下是对opencv-python的基础算法使用,包括:
加载图片、图片缩放、转灰度图片、图片二值化、图片模糊、加椒盐噪声、去除椒盐噪声、图片反向、图片拉伸、旋转、锐化、直方图、透明化
#-*- coding: UTF-8 -*-
from PIL import Image
import cv2
import numpy as np
from matplotlib import pyplot as plt
from math import cos,sin,radians
# 加载图片
def image_imread():
image = cv2.imread(r"C:/Users/admin/Desktop/sbl.png", 1)
return image
# 按照比例缩放,如x,y轴均放大一倍
def image_x2(image):
image_x2 = cv2.resize(image, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
cv2.imshow('image_x2', image_x2)
cv2.waitKey(0)
# 按照比例缩放,如x,y轴均缩小一倍
def image_x05(image):
blur_image_x05 = cv2.resize(image, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR)
cv2.imshow('blur_image_x0.5', blur_image_x05)
cv2.waitKey(0)
# 按照指定的宽度、高度缩放图片
def image_xdef(image):
blur_image_xdef = cv2.resize(image, (65, 75))
cv2.imshow("blur_image_xdef",blur_image_xdef)
cv2.waitKey(0)
#转成灰度图片
def image_2GRAY(image):
image_2GRAY = cv2.merge([image,image,image])
image_2GRAY = cv2.cvtColor(image_2GRAY, cv2.COLOR_BGR2GRAY)
cv2.imshow("blur_image_2GRAY", image_2GRAY)
cv2.waitKey(0)
# # 二值化
def image_2twoVal(image):
ret,img_2twoVal=cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
cv2.imshow("blur_image_2GRAY", img_2twoVal)
cv2.waitKey(0)
# 均值模糊
def image_blur(image):
image_blur = cv2.blur(image, (5, 5))
cv2.imshow("image_blur", image_blur)
cv2.waitKey(0)
# 加椒盐噪声
def noise(img,snr):
h=img.shape[0]
w=img.shape[1]
img1=img.copy()
sp=h*w # 计算图像像素点个数
NP=int(sp*(1-snr)) # 计算图像椒盐噪声点个数
for i in range (NP):
randx=np.random.randint(1,h-1) # 生成一个 1 至 h-1 之间的随机整数
randy=np.random.randint(1,w-1) # 生成一个 1 至 w-1 之间的随机整数
if np.random.random()<=0.5: # np.random.random()生成一个 0 至 1 之间的浮点数
img1[randx,randy]=0
else:
img1[randx,randy]=255
cv2.imshow("image_noise", img1)
return img1
# 中值模糊去除椒盐噪声
def median_blur_demo(image):
dst = cv2.medianBlur(image, 5)
cv2.imshow("median_blur_demo", dst)
cv2.waitKey(0)
# 反向
def image_revesal(image):
# --image 输入图像
# --flipCode 1沿y轴水平翻转,0沿x轴垂直翻转,-1水平垂直翻转
# 水平翻转图像
flipped = cv2.flip(image, 1)
cv2.imshow("Flipped Horizontally", flipped)
cv2.waitKey(0)
# 延长
def image_stretch(image):
# 指定新图片的维度与插值算法(interpolation)
image_stretch = cv2.resize(image, None, fx=2, fy=1)
cv2.imshow("image_stretch", image_stretch)
cv2.waitKey(0)
# 旋转
def image_spin1(image):
# 获取图像的维度,并计算中心
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
# 逆时针以图像中心旋转45度
# - (cX,cY): 旋转的中心点坐标
# - 45: 旋转的度数,正度数表示逆时针旋转,而负度数表示顺时针旋转。
# - 1.0:旋转后图像的大小,1.0原图,2.0变成原来的2倍,0.5变成原来的0.5倍
# OpenCV不会自动为整个旋转图像分配空间,以适应帧。旋转完可能有部分丢失。如果您希望在旋转后使整个图像适合视图,则需要进行优化,使用imutils.rotate_bound.
M = cv2.getRotationMatrix2D((cX, cY), 45, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
cv2.imshow("Rotated by 45 Degrees", rotated)
cv2.waitKey(0)
# 锐化
def image_sharpen(image):
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32) #定义一个核
image_sharpen = cv2.filter2D(image, -1, kernel=kernel)
cv2.imshow("image_sharpen", image_sharpen)
cv2.waitKey(0)
# 直方图
def image_hist(image):
plt.hist(image.ravel(),256,[0,256],facecolor ='black')
plt.show()
# 透明化
def addTransparency():
img = cv2.imread("C:/Users/admin/Desktop/sbl.png",1)
b_channel, g_channel, r_channel = cv2.split(img)
alpha_channel = np.ones(b_channel.shape, dtype=b_channel.dtype) * 0 # alpha通道每个像素点区间为[0,255], 0为完全透明,255是完全不透明
alpha_channel[:, :int(b_channel.shape[0] / 2)] = 100
img_BGRA = cv2.merge((b_channel, g_channel, r_channel, alpha_channel))
cv2.imshow("image_Trans",img_BGRA)
cv2.waitKey(0)
if __name__ == "__main__":
# 载入图片
image = image_imread()
cv2.imshow('image', image)
# 按照比例缩放,如x,y轴均放大一倍
# image_x2(image)
# 按照比例缩放,如x,y轴均缩小一倍
# image_x05(image)
# 按照指定的宽度、高度缩放图片
# image_xdef(image)
# 灰度
# image_2GRAY(image)
# 二值化
# image_2twoVal(image)
# 均值模糊
# image_blur(image)
# 加椒盐噪声
# image_noise=noise(image,0.6)
# 中止模糊消除椒盐噪声
# median_blur_demo(image_noise)
# 反向
# image_revesal(image)
# 拉伸
# image_stretch(image)
# 旋转
# image_spin1(image)
# 锐化
# image_sharpen(image)
#显示灰度直方图
# image_hist(image)
# 透明化
# addTransparency()
小白在此,以上均属初学心得,适合想入门opencv但未开始入门的白中白参考,希望可以帮到屏幕前的你哦~