Python版本图像处理学习2-效果篇
作者:互联网
1.马赛克效果:
import cv2
image = cv2.imread("lena.tiff") #读取一张图片
imgInfo = image.shape #获取图像的宽高信息
height = imgInfo[0] #图像宽度值
width = imgInfo[1] #图像高度值
mashiroLeft = 0 #马赛克起始X坐标
mashiroBottom = 0 #马赛克起始Y坐标
mashiroWidth = width #马赛克区域宽
mashiroHeight = height #马赛克区域高
masiroUnit = 15 #马赛克单元长
for i in range(mashiroLeft,mashiroWidth-masiroUnit):
for j in range(mashiroBottom,mashiroHeight-masiroUnit):
if i%masiroUnit==0 and j%masiroUnit==0:
for m in range(0,masiroUnit):
for n in range(0,masiroUnit):
b,g,r = image[i,j]
image[i+m,j+n] = [b,g,r]
cv2.imshow('mashiro',image)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
效果图:
马赛克单位长4与马赛克单位长15效果如下:
2.毛玻璃效果
import cv2,random
import numpy as np
image = cv2.imread("lena.tiff") #读取一张图片
imgInfo = image.shape #获取图像的宽高信息
height = imgInfo[0] #图像宽度值
width = imgInfo[1] #图像高度值
dst = np.zeros((height,width,3),np.uint8)
mm = 16
for m in range(0,height-mm):
for n in range(0,width-mm):
index = int(random.random()*mm)
b,g,r = image[m+index,n+index]
dst[m,n] = b,g,r
cv2.imshow('glassBlur',dst)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
效果图:
3.反色
import cv2,random
import numpy as np
image = cv2.imread("lena.tiff") #读取一张图片
imgInfo = image.shape #获取图像的宽高信息
height = imgInfo[0] #图像宽度值
width = imgInfo[1] #图像高度值
dst = np.zeros((height,width,3),np.uint8)
mm = 16
for m in range(0,height-mm):
for n in range(0,width-mm):
b,g,r = image[m,n]
gray = (int(b)+int(g)+int(r))/3
image[m,n] = [255-gray,255-gray,255-gray]
cv2.imshow('glassBlur',image)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
效果图:
4.浮雕效果
import cv2,random
import numpy as np
image = cv2.imread("lena.tiff") #读取一张图片
imgInfo = image.shape #获取图像的宽高信息
height = imgInfo[0] #图像宽度值
width = imgInfo[1] #图像高度值
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
dst = np.zeros((height,width,3),np.uint8)
for i in range(0,width):
for j in range(0,height-1):
gray1 = int(gray[i,j])
gray2 = int(gray[i,j+1])
graySub = gray2 - gray1 + 100
if graySub>255:
graySub = 255
elif graySub<0:
graySub = 0
dst[i,j] = graySub
cv2.imshow('Gray Image',dst)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
效果图:
5.边缘检测
import cv2,random
import numpy as np
image = cv2.imread("lena.tiff") #读取一张图片
imgInfo = image.shape #获取图像的宽高信息
height = imgInfo[0] #图像宽度值
width = imgInfo[1] #图像高度值
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
imgG = cv2.GaussianBlur(gray,(5,5),0)
dst = cv2.Canny(imgG,50,50)
cv2.imshow('EdgeDetect',dst)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
效果图:
标签:Python,image,cv2,height,width,图像处理,版本,np,imgInfo 来源: https://blog.csdn.net/sky_person/article/details/93407933