opencv学习笔记二——图像基本操作

1. POI区域:感兴趣区域
2. 边缘填充
3. 数值运算
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from PIL import Image

image1 = mpimg.imread('1.jpg')
image2 = mpimg.imread('2.jpg')
plt.imshow(image1)

请添加图片描述

plt.imshow(image2)

请添加图片描述

图像加法
image3 = image1+image2
plt.imshow(image3)

请添加图片描述

图像裁剪
plt.imshow(image1[100:-100, 100:-100, :]);

请添加图片描述

图像缩放

fx和fy表示缩放倍数

image4 = cv2.resize(image1, (0, 0), fx=3, fy=1)
plt.imshow(image4);

请添加图片描述

4. 图像阈值

ret, dst = cv2.threshold(src, thresh, maxval, type)

  • src: 输入图像,只能是单通道图像
  • dst:输出图
  • thresh:阈值
  • maxval:当像素值超过了阈值或小于阈值时的取值
  • type:二值化的类型
    • cv2.THRESH_BINARY 超过阈值取maxval,否则取0
    • cv2.THRESH_BINARY_INV THRESH_BINARY的反转
    • cv2.THRESH_TRUNC 大于阈值设置阈值,其余不变
    • cv2.THRESH_TOZERO 大于阈值不变,其余设为0
    • cv2.THRESH_TOZERO_INV THRESH_TOZERO的反转
image1_gray = image1[:,:,0]
res, thresh1 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_BINARY)
res, thresh2 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_BINARY_INV)
res, thresh3 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_TRUNC)
res, thresh4 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_TOZERO)
res, thresh5 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_TOZERO_INV)

titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
images = [image1_gray, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
    plt.subplot(2,3,i+1)
    plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([])
    plt.yticks([])
plt.show()

请添加图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值