OpenCV:图像读取/保存、绘制直线/圆形/矩形、获取并修改图像中的像素点、图像通道的拆分与合并

日萌社

人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新)


图像的基础操作

学习目标

  • 掌握图像的读取和保存方法

  • 能够使用OpenCV在图像上绘制几何图形

  • 能够访问图像的像素

  • 能够获取图像的属性,并进行通道的分离和合并

  • 能够实现颜色空间的变换


1 图像的IO操作

这里我们会给大家介绍如何读取图像,如何显示图像和如何保存图像。

1.1 读取图像

  1. API
cv.imread()

参数:

  • 要读取的图像

  • 读取方式的标志

    • cv.IMREAD*COLOR:以彩色模式加载图像,任何图像的透明度都将被忽略。这是默认参数。

    • cv.IMREAD*GRAYSCALE:以灰度模式加载图像

    • cv.IMREAD_UNCHANGED:包括alpha通道的加载图像模式。

      可以使用1、0或者-1来替代上面三个标志

  • 参考代码

    import numpy as np
    import cv2 as cv
    # 以灰度图的形式读取图像
    img = cv.imread('messi5.jpg',0)
    

注意:如果加载的路径有错误,不会报错,会返回一个None值

1.2显示图像

1 . API

cv.imshow()

参数:

  • 显示图像的窗口名称,以字符串类型表示
  • 要加载的图像

注意:在调用显示图像的API后,要调用cv.waitKey()给图像绘制留下时间,否则窗口会出现无响应情况,并且图像无法显示出来

另外我们也可使用matplotlib对图像进行展示。

  1. 参考代码

    # opencv中显示
    cv.imshow('image',img)
    cv.waitKey(0)
    # matplotlib中展示
    plt.imshow(img[:,:,::-1])
    

1.3 保存图像

  1. API

    cv.imwrite()
    

    参数:

    • 文件名,要保存在哪里
    • 要保存的图像
  2. 参考代码

    cv.imwrite('messigray.png',img)
    

    1.4 总结

    我们通过加载灰度图像,显示图像,如果按's'并退出则保存图像,或者按ESC键直接退出而不保存。

    import numpy as np
    import cv2 as cv
    import matplotlib.pyplot as plt
    # 1 读取图像
    img = cv.imread('messi5.jpg',0)
    # 2 显示图像
    # 2.1 利用opencv展示图像
    cv.imshow('image',img)
    # 2.2 在matplotplotlib中展示图像
    plt.imshow(img[:,:,::-1])
    plt.title('匹配结果'), plt.xticks([]), plt.yticks([])
    plt.show()
    k = cv.waitKey(0)
    # 3 保存图像
    cv.imwrite('messigray.png',img)

2 绘制几何图形

2.1 绘制直线

cv.line(img,start,end,color,thickness)

参数:

  • img:要绘制直线的图像
  • Start,end: 直线的起点和终点
  • color: 线条的颜色
  • Thickness: 线条宽度

2.2 绘制圆形

cv.circle(img,centerpoint, r, color, thickness)

参数:

  • img:要绘制圆形的图像
  • Centerpoint, r: 圆心和半径
  • color: 线条的颜色
  • Thickness: 线条宽度,为-1时生成闭合图案并填充颜色

2.3 绘制矩形

cv.rectangle(img,leftupper,rightdown,color,thickness)

参数:

  • img:要绘制矩形的图像
  • Leftupper, rightdown: 矩形的左上角和右下角坐标
  • color: 线条的颜色
  • Thickness: 线条宽度

2.4 向图像中添加文字

cv.putText(img,text,station, font, fontsize,color,thickness,cv.LINE_AA)

参数:

  • img: 图像
  • text:要写入的文本数据
  • station:文本的放置位置
  • font:字体
  • Fontsize :字体大小

2.5 效果展示

我们生成一个全黑的图像,然后在里面绘制图像并添加文字

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 创建一个空白的图像
img = np.zeros((512,512,3), np.uint8)
# 2 绘制图形
cv.line(img,(0,0),(511,511),(255,0,0),5)
cv.rectangle(img,(384,0),(510,128),(0,255,0),3)
cv.circle(img,(447,63), 63, (0,0,255), -1)
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv.LINE_AA)
# 3 图像展示
plt.imshow(img[:,:,::-1])
plt.title('匹配结果'), plt.xticks([]), plt.yticks([])
plt.show()

结果:

3 获取并修改图像中的像素点

我们可以通过行和列的坐标值获取该像素点的像素值。对于BGR图像,它返回一个蓝,绿,红值的数组。对于灰度图像,仅返回相应的强度值。使用相同的方法对像素值进行修改。

import numpy as np
import cv2 as cv
img = cv.imread('messi5.jpg')
# 获取某个像素点的值
px = img[100,100]
# 仅获取蓝色通道的强度值
blue = img[100,100,0]
# 修改某个位置的像素值
img[100,100] = [255,255,255]

4 获取图像的属性

图像属性包括行数,列数和通道数,图像数据类型,像素数等。

5 图像通道的拆分与合并

有时需要在B,G,R通道图像上单独工作。在这种情况下,需要将BGR图像分割为单个通道。或者在其他情况下,可能需要将这些单独的通道合并到BGR图像。你可以通过以下方式完成。

# 通道拆分
b,g,r = cv.split(img)
# 通道合并
img = cv.merge((b,g,r))

6 色彩空间的改变

OpenCV中有150多种颜色空间转换方法。最广泛使用的转换方法有两种,BGR↔Gray和BGR↔HSV。

API:

cv.cvtColor(input_image,flag)

参数:

  • input_image: 进行颜色空间转换的图像
  • flag: 转换类型
    • cv.COLOR_BGR2GRAY : BGR↔Gray
    • cv.COLOR_BGR2HSV: BGR→HSV

总结:

  1. 图像IO操作的API:

    cv.imread(): 读取图像

    cv.imshow():显示图像

    cv.imwrite(): 保存图像

  2. 在图像上绘制几何图像

    cv.line(): 绘制直线

    cv.circle(): 绘制圆形

    cv.rectangle(): 绘制矩形

    cv.putText(): 在图像上添加文字

  3. 直接使用行列索引获取图像中的像素并进行修改

  4. 图像的属性

  1. 拆分通道:cv.split()

    通道合并:cv.merge()

  2. 色彩空间的改变

    cv.cvtColor(input_image,flag)


01.图像的IO操作.py

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

# 1 读取图像
img = cv.imread("image/dili.jpg",0)

# 2 显示图像
# 2.1 OPencv
# cv.imshow("dili",img)
# cv.waitKey(0)
# cv.destroyAllWindows()

# 2.2 matplotlib
plt.imshow(img,cmap=plt.cm.gray)
plt.show()

# 3 图像保存
cv.imwrite("image/dilireba.png",img)

px = img[100,100]

02.图像绘制图形.py 

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

# 1 创建图像
img = np.zeros((512,512,3),np.uint8)

# 2 绘制图形
cv.line(img,(0,0),(511,511),(255,0,0),5)
cv.circle(img,(256,256),60,(0,0,255),-1)
cv.rectangle(img,(100,100),(400,400),(0,255,0),5)
cv.putText(img,"hello",(100,150),cv.FONT_HERSHEY_COMPLEX,5,(255,255,255),3)


# 3 显示结果
plt.imshow(img[:,:,::-1])
plt.show()

In [1]:

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

In [3]:

img = np.zeros((256,256,3),np.uint8)

In [4]:

plt.imshow(img[:,:,::-1])

Out[4]:

<matplotlib.image.AxesImage at 0x11fe05f90>

In [5]:

img[100,100]

Out[5]:

array([0, 0, 0], dtype=uint8)

In [6]:

img[100,100,0]

Out[6]:

0

In [7]:

img[100,100] = (0,0,255)

In [8]:

plt.imshow(img[:,:,::-1])

Out[8]:

<matplotlib.image.AxesImage at 0x11b4aadd0>

In [9]:

img[100,100]

Out[9]:

array([  0,   0, 255], dtype=uint8)

In [11]:

img.shape

Out[11]:

(256, 256, 3)

In [12]:

img.dtype

Out[12]:

dtype('uint8')

In [13]:

img.size

Out[13]:

196608

In [15]:

dili = cv.imread("./image/dili.jpg")

In [17]:

plt.imshow(dili[:,:,::-1])

Out[17]:

<matplotlib.image.AxesImage at 0x12333b210>

In [18]:

b,g,r = cv.split(dili)

In [22]:

plt.imshow(b,cmap=plt.cm.gray)

Out[22]:

<matplotlib.image.AxesImage at 0x122c7eb50>

In [23]:

img2 = cv.merge((b,g,r))

In [24]:

plt.imshow(img2[:,:,::-1])

Out[24]:

<matplotlib.image.AxesImage at 0x123f64e50>

In [25]:

gray = cv.cvtColor(dili,cv.COLOR_BGR2GRAY)

In [26]:

plt.imshow(gray,cmap=plt.cm.gray)

Out[26]:

<matplotlib.image.AxesImage at 0x1238dc310>

In [27]:

hsv = cv.cvtColor(dili,cv.COLOR_BGR2HSV)

In [28]:

plt.imshow(hsv)

Out[28]:

<matplotlib.image.AxesImage at 0x123ffe610>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

あずにゃん

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值