标签:Python image cv2 height width 图像处理 版本 np imgInfo
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
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。