当前位置:   article > 正文

【opencv之python版】图像的读取、显示、保存、像素遍历以及结合matplotlib使用_python 遍历mat像素值

python 遍历mat像素值

1. 图像的表示

在opencv的C++代码中,表示图像有专门的结构cv::Mat,在python中有numpy工具可以使用。
在这里插入图片描述

2. 图像读取、显示与保存

cv2.imread(path_of_image, intflag)
  • 1

标记intflag的格式:

  • cv2.IMREAD_COLOR: 加载彩色图像。任何图像的透明度都将被忽略。它是默认标志
  • cv2.IMREAD_GRAYSCALE:以灰度模式加载图像
  • cv2.IMREAD_UNCHANGED:保留读取图片原有的颜色通道
  • 1 :等同于cv2.IMREAD_COLOR
  • 0 :等同于cv2.IMREAD_GRAYSCALE
  • -1 :等同于cv2.IMREAD_UNCHANGED
import cv2

file_path = "../../pic/pic_1.png" #图片相对位置

#读取图像
img = cv2.imread(file_path)

#显示图像
cv2.imshow("image", img) #名字和显示的对象

#保存图像
cv2.imwrite('new_image.png',img)

# 指定jpg质量,范围从1~100,默认95,值越高画质越好,文件越大
cv2.imwrite('good_img.jpg',img,(cv2.IMWRITE_JPEG_QUALITY,100))

# 等待键盘输入,为毫秒级
cv2.waitKey(0)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

输出:
在这里插入图片描述

3. 简单使用

3.1 读取图像大小

使用shape方法,可以获取图像的height、width和channels的值。

height = img.shape[0]        #将img中的元素取出,赋值给height,width,channels
width = img.shape[1]
channels = img.shape[2]
  • 1
  • 2
  • 3

3.2 绘制基本图形

下列代码展示了怎么绘制线段、矩形、圆形等简单元素。

import cv2
import numpy as np

img = np.ones((512,512,3), np.uint8)
height = img.shape[0]        #将img中的元素取出,赋值给height,width,channels
width = img.shape[1]
channels = img.shape[2]
print(width, height, channels) # 512 * 512 * 3
img = 255*img # 白色
img = cv2.line(img, (100,100), (400,400),(255, 0, 0), 5)
img = cv2.rectangle(img,(200, 20),(400,120),(0,255,0),3)
img = cv2.circle(img,(100,400), 50, (0,0,255), 2)
img = cv2.circle(img,(250,400), 50, (0,0,255), 0)
img = cv2.ellipse(img,(256,256),(100,50),0,0,180,(0, 255, 255), -1)
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
img = cv2.polylines(img,[pts],True,(0, 0, 0), 2)

cv2.imshow('img', img)
#cv2.imwrite("1.png",img),保存图像
#当指定waitKey(0) == 27时当敲击键盘 Esc 时便销毁所有窗口
if cv2.waitKey(0) == 27:
    cv2.destroyAllWindows()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

显示结果如下:
在这里插入图片描述

3.3 图像的元素遍历

下面我们以一个将图像元素取反的例子为例,说明怎么实现元素的遍历。

import cv2 as cv
import numpy as np

def access_pixels(frame):
    print(frame.shape)  #shape内包含三个元素:按顺序为高、宽、通道数
    height = frame.shape[0]
    weight = frame.shape[1]
    channels = frame.shape[2]
    print("weight : %s, height : %s, channel : %s" %(weight, height, channels))
    
    for row in range(height):            #遍历高
        for col in range(weight):         #遍历宽
            for c in range(channels):     #便利通道
                pv = frame[row, col, c]     
                frame[row, col, c] = 255 - pv     #全部像素取反,实现一个反向效果
    cv.imshow("fanxiang", frame)

file_path = "../../pic/pic_1.png" #图片相对位置
src = cv.imread(file_path)
cv.imshow("Picture", src)
access_pixels(src)
cv.waitKey(0)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

图像像素取反后的效果如下图所示
在这里插入图片描述

4. 结合Matplotlib显示图像

在OpenCV中,颜色模式的默认设置顺序为BGR,而 Matplotlib则是用我们熟悉的RGB模式来显示图像的 ,所以如果不对颜色模式进行转换直接将图像显示出来,那显示的图像将不是我们原本想要获取的。下例展示了使用cvtColor转换图像颜色后,使用matplotlib显示。

import numpy as np
from matplotlib import pyplot as plt
import cv2

file_path = "../../pic/pic_1.png" #图片相对位置

#读取图像
img = cv2.imread(file_path)
#图像转换,将BGR转换为RGB
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)

#matplotlib显示图像
plt.imshow(img)
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

输出:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/822930
推荐阅读
相关标签
  

闽ICP备14008679号