Python opencv 处理图片例子

# -*- coding: utf-8 -*-

import cv2.cv2 as cv2
import numpy as np

# 全局变量
picFromPath  = r'./Demo_800x600.jpg'
picSaveFolder= r'./'
picSaveType  = '.jpg'
alertWindow0 = 'Demo_00'
alertWindow1 = 'Demo_01'
alertWindow2 = 'Demo_02'
alertWindow3 = 'Demo_03'
alertWindow4 = 'Demo_04'


# 图片旋转自定义函数(来自:https://blog.csdn.net/qq_37674858/article/details/80708393)
# image:图片对象
# angle:旋转角度,正数逆时针,负数顺时针
def rotate_bound(image, angle):
    # grab the dimensions of the image and then determine the
    # center
    (h, w) = image.shape[:2]
    (cX, cY) = (w // 2, h // 2)
    # grab the rotation matrix (applying the negative of the
    # angle to rotate clockwise), then grab the sine and cosine
    # (i.e., the rotation components of the matrix)
    M = cv2.getRotationMatrix2D((cX, cY), angle, 1.0)
    cos = np.abs(M[0, 0])
    sin = np.abs(M[0, 1])
    # compute the new bounding dimensions of the image
    nW = int((h * sin) + (w * cos))
    nH = int((h * cos) + (w * sin))
    # adjust the rotation matrix to take into account translation
    M[0, 2] += (nW / 2) - cX
    M[1, 2] += (nH / 2) - cY
    # perform the actual rotation and return the image
    return cv2.warpAffine(image, M, (nW, nH))

# ================ 原始图片
imgOrigin0   = cv2.imread(picFromPath,1)
size0        = imgOrigin0.shape
height0      = size0[0]
width0       = size0[1]
print(u'---> 原图像大小(px):宽度='+str(width0)+',高度='+str(height0))


# ================ 调整大小
#
# cv2.resize(src,dsize,fx,fy,interpolation)
# fx:宽度缩放比例
# fy:高度缩放比例
#
# INTER_AREA - 基于局部像素的重采样(resampling using pixel area relation)。一般用于缩小图像。
# INTER_CUBIC - 基于4x4像素邻域的3次插值法。一般用于放大图像。
# INTER_NEAREST - 最近邻插值法
# INTER_LINEAR - 双线性插值法(默认)
# INTER_LANCZOS4 - 基于8x8像素邻域的Lanczos插值
#
# imgOrigin    = cv2.resize(imgOrigin0,(320,240),interpolation=cv2.INTER_AREA) # 指定像素缩放
imgOrigin= cv2.resize(imgOrigin0,(0,0),fx=0.4,fy=0.4,interpolation=cv2.INTER_AREA) # 按比例缩放
imgInfo1 = imgOrigin.shape
height1  = imgInfo1[0]
width1   = imgInfo1[1]
cv2.imwrite(picSaveFolder+alertWindow0+picSaveType,imgOrigin)
print(u'---> 新图像大小(px):宽度='+str(width1)+',高度='+str(height1)+',保存到:'+picSaveFolder+alertWindow0+picSaveType)


# ================ 亮度增加
dst1 = np.zeros((height1,width1,3),np.uint8)
for i in range(0,height1):
    for j in range(0,width1):
        (b,g,r) = imgOrigin[i,j]
        bb = int(b)+40
        gg = int(g)+40
        rr = int(r)+40
        if bb>255:
            bb = 255
        if gg>255:
            gg = 255
        if rr>255:
            rr = 255
        dst1[i,j] = (bb,gg,rr)
cv2.imwrite(picSaveFolder+alertWindow1+picSaveType,dst1)
print(u'---> 亮度增加,保存到:'+picSaveFolder+alertWindow1+picSaveType)


# ================ 高斯模糊
dst2 = cv2.GaussianBlur(imgOrigin,(5,5),1.5)
cv2.imwrite(picSaveFolder+alertWindow2+picSaveType,dst2)
print(u'---> 高斯模糊,保存到:'+picSaveFolder+alertWindow2+picSaveType)


# ================ 美颜磨皮
dst3 = cv2.bilateralFilter(imgOrigin,15,35,35)
cv2.imwrite(picSaveFolder+alertWindow3+picSaveType,dst3)
print(u'---> 美颜磨皮,保存到:'+picSaveFolder+alertWindow3+picSaveType)


# ================ 图片旋转(已经缩放过)
# 逆时针 旋转90度 ,保持原图宽高比例(减少的那部分背景用黑色填充)
# 旋转矩阵:cv2.getRotationMatrix2D(旋转的中心点,旋转角度,旋转后图片的大小)
# rows4, cols4, channel4 = imgOrigin.shape
# M           = cv2.getRotationMatrix2D((rows4/2,cols4/2),-90,1)
# dst4        = cv2.warpAffine(imgOrigin,M,(rows4,cols4))
dst4 = rotate_bound(imgOrigin,-90)  # 该函数不会保留黑边
cv2.imwrite(picSaveFolder+alertWindow4+picSaveType,dst4)
print(u'---> 图片旋转,保存到:'+picSaveFolder+alertWindow4+picSaveType)


# ***************** 显示所有图片 *****************
# vtitch = np.vstack((imgOrigin, dst1, dst2,dst3)) # 垂直一列显示
# cv2.imshow(alertWindow2, vtitch)

htitch1  = np.hstack((imgOrigin, dst1, dst2, dst3))  # 水平并排显示
cv2.imshow('Demo Python OpenCV 1', htitch1)
cv2.imshow('Demo Python OpenCV 2', dst4)  # (高度或宽度不一样的,貌似不能并排,否则会报错,只能新开一个窗口)
cv2.waitKey(0)  # 此句必须有,否则图片窗口会一闪而过(相当于一打开就直接关闭)
cv2.destroyAllWindows()



 

 

 

(图片太大情况下,CSDN中默认缩放会变形,点击图片后会显示原图)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值