Python版本图像处理学习3-工具篇

1.图片缩放:

import cv2,random
import numpy as np
image = cv2.imread("lena.tiff") #读取一张图片
sp = image.shape
imgWidth = sp[0]
imgHeight = sp[1]
dstHeight = int(imgWidth*0.5)  #缩放后的图高度
dstWidth = int(imgHeight*0.5)  #缩放后的图宽度
dst = cv2.resize(image,(dstWidth,dstHeight))
cv2.imshow('EdgeDetect',dst)
k = cv2.waitKey(0)
if k == 27:
    cv2.destroyAllWindows()

图片缩放cv2.resize底层实现:

import cv2,random
import numpy as np
image = cv2.imread("lena.tiff") #读取一张图片
sp = image.shape
imgWidth = sp[0]
imgHeight = sp[1]
dstHeight = int(imgWidth*0.5)  #缩放后的图高度
dstWidth = int(imgHeight*0.5)  #缩放后的图宽度
dstImage = np.zeros((dstHeight,dstWidth,3),np.uint8)
for i in range(0,dstHeight):
    for j in range(0,dstWidth):
        iNew = int(i*(imgHeight*1.0/dstHeight))
        jNew = int(j*(imgWidth*1.0/dstWidth))
        dstImage[i,j] = image[iNew,jNew]

cv2.imshow('EdgeDetect',dstImage)
k = cv2.waitKey(0)
if k == 27:
    cv2.destroyAllWindows()

2.图片保存

cv2.imwrite('mashiro2.png',dst,[cv2.IMWRITE_PNG_COMPRESSION,0])

3.裁剪图片一片区域

import cv2,random
import numpy as np
image = cv2.imread("lena.tiff") #读取一张图片
sp = image.shape
width = sp[0]
height = sp[1]
dst = image[100:450,100:450]

cv2.imshow('Clip Image',dst)
k = cv2.waitKey(0)
if k == 27:
    cv2.destroyAllWindows()

效果图:
clipImage
4.图片混合

import cv2,random
import numpy as np
image = cv2.imread("lena.tiff") #读取一张图片
mask = cv2.imread('aisi.png')
sp = image.shape
width = sp[0]
height = sp[1]
dst1 = cv2.resize(image,(512,512))
dst2 = cv2.resize(mask,(512,512))
dst3 = cv2.addWeighted(dst1,0.5,dst2,0.5,1.0)

cv2.imshow('Clip Image',dst3)
k = cv2.waitKey(0)
if k == 27:
    cv2.destroyAllWindows()

效果图:
mixImage
note:
addWeighted函数要求原图1,原图2必须大小相同…。。

cv2.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]]) → dst
参数说明:
src1:原图1
alpha:原图1的混合因子
src2:原图2
beta:原图2的混合因子

参考链接:https://blog.csdn.net/zh_jessica/article/details/77992578

5.绘图测试

import cv2,random
import numpy as np
image = cv2.imread("lena.tiff") #读取一张图片
sp = image.shape
width = sp[0]
height = sp[1]
dst = np.zeros(sp,np.uint8)
# 1.图片 2.左上角坐标 3.右下角坐标 4.颜色 5.是否填充(大于零:线条宽度,小于零:是否填充)
cv2.rectangle(dst,(50,100),(200,300),(255,0,0),-1)
# 1.图片 2.圆心 3.半径 4.颜色
cv2.circle(dst,(250,250),(50),(255,0,255),-1,cv2.LINE_AA)
# 椭圆 1.图片 2.椭圆圆心 3.长轴和短轴的长度 4.偏转角度 5.圆弧起始角度 6.圆弧终止角度 7.颜色 8.是否填充
cv2.ellipse(dst,(256,256),(150,100),0,0,180,(255,255,0),-1,cv2.LINE_AA)
# 定义任意角度
points = np.array([[150,50],[140,140],[200,170],[250,250],[150,50]],np.int32)  # (5,2)
points = points.reshape((-1,1,2))  # (5,1,2)
cv2.polylines(dst,[points],True,(0,255,255))

cv2.imshow('Draw Test',dst)
k = cv2.waitKey(0)
if k == 27:
    cv2.destroyAllWindows()

效果图:
DrawTest
6.文本绘制

import cv2,random
import numpy as np
image = cv2.imread("lena.tiff") #读取一张图片
sp = image.shape
width = sp[0]
height = sp[1]

font = cv2.FONT_HERSHEY_COMPLEX
# 1.图片 2.文字的内容 3.写入的坐标 4.字体 5.字体大小 6.颜色 7,字体的粗细 8.线条类型
cv2.putText(image,'hello, i am mashiro····',(100,300),font,1,(200,100,255),2,cv2.LINE_AA)

cv2.imshow('Draw Fonts ',image)
k = cv2.waitKey(0)
if k == 27:
    cv2.destroyAllWindows()

效果图:
Draw Fonts
7.绘制图片

import cv2,random
import numpy as np
image = cv2.imread("lena.tiff") #读取一张图片
sp = image.shape
width = sp[0]
height = sp[1]

mask = cv2.resize(image,(int(width/3),int(height/3)))
for i in range(int(width/3)):
    for j in range(int(height/3)):
        image[i+100,j+100]=mask[i,j]

cv2.imshow('Draw Image ',image)
k = cv2.waitKey(0)
if k == 27:
    cv2.destroyAllWindows()

效果图:
DrawImage

参考链接:
https://www.cnblogs.com/traditional/p/9043931.html
https://blog.csdn.net/zh_jessica/article/details/77992578

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值