深度学习

图像色彩操作

二值化与反二值化

# 二值化处理
import cv2 as cv

# 读取图像
img = cv.imread("../data/lena.jpg", 0)
cv.imshow("img", img)  # 显示原始图像

# 二值化
t, rst = cv.threshold(img, 127, 255, cv.THRESH_BINARY)
cv.imshow("rst", rst)  # 显示二值化图像

# 反二值化
t, rst2 = cv.threshold(img, 127, 255, cv.THRESH_BINARY_INV)
cv.imshow("rst2", rst2)  # 显示反二值化图像

cv.waitKey()
cv.destroyAllWindows()

图像形态操作

图像相加

# 图像相加示例
import cv2
a=cv2.imread('../img_data/lena.jpg',0)
b=cv2.imread('../img_data/lily_square.png',0)
dst1=cv2.add(a,b)#图像直接相加,会导致图像过亮,过白
# 加权求和:addWeighted
# 图像进行加权和计算时,要求src1和src2必须大小,类型相同
dst2=cv2.addWeighted(a,0.6,b,0.4,0)#最后一个参数为亮度调节量

cv2.imshow('a',a)
cv2.imshow('b',b)
cv2.imshow('dst1',dst1)
cv2.imshow('dst2',dst2)

cv2.waitKey()
cv2.destroyAllWindows()

在这里插入图片描述

图像裁剪

# 2021-01-19 星期二 09:27
# 02_crop_demo.py
# 图像裁剪示例(通过数组切片完成)
import numpy as np
import cv2


# 随机裁剪
def random_crop(im, w, h):
    # 产生裁剪起始位置
    start_x = np.random.randint(0, im.shape[1])
    start_y = np.random.randint(0, im.shape[0])
    new_img = im[start_y:start_y + h, start_x:start_x + w]  # 切片
    return new_img


# 中心裁剪
def center_crop(im, w, h):
    start_x = int(im.shape[1] / 2) - int(w / 2)  # 起始x
    start_y = int(im.shape[0] / 2) - int(h / 2)  # 起始y
    new_img = im[start_y:start_y + h, start_x:start_x + w]  # 切片
    return new_img


if __name__ == '__main__':
    im = cv2.imread('../img_data/banana_1.png', 1)
    # 随机裁剪
    new_img = random_crop(im, 200, 200)
    cv2.imshow('rand_crop', new_img)
    # 中心裁剪
    new_img2 = random_crop(im, 200, 200)
    cv2.imshow('center_crop', new_img2)

    cv2.waitKey()
    cv2.destroyAllWindows()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值