python + openCV图像处理(一)

版权声明:博客内容仅参考,如有错误,请不吝指出,在此谢过。 https://blog.csdn.net/zhaiax672/article/details/88712008

引言

此系列博客记录 网易云课堂 python + openCV图像处理课程的学习过程。

本篇博客将介绍 读取、显示、保存图像读取、修改 像素值(openCV 与 numpy) 三个部分。

介绍

OpenCV (Open Source Computer Vision Library) is released under a BSD license and hence it’s free for both academic and commercial use. It has C++, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android.

OpenCV was designed for computational efficiency and with a strong focus on real-time applications. Written in optimized C/C++, the library can take advantage of multi-core processing.

Enabled with OpenCL(一个为异构平台编写程序的框架), it can take advantage of the hardware acceleration of the underlying heterogeneous compute platform.

图像基础

1、图像都是由像素构成的,在同等面积下,像素点越多,图像越细腻。

2、图像分类:

  • 二值图像:图像只有 黑、白 两种颜色(像素点 0 或 1
  • 灰度图像:图像只有 黑、白、灰 三种颜色(像素点 0 - 255,其中 0 为黑色,255 为白色,1-254 之间均为灰色)
  • RGB图像彩色图像

tips: 二值和灰度图像 均为 单通道,RGB图像 为 三通道
在这里插入图片描述
3、彩色图像(RGB):由 红、绿、蓝 三色 按照某种比例混合而成的。

  • 比如:(255,182,193)表示浅粉红色,更常用的16进制 - #FFB6C1
    在这里插入图片描述

4、彩色图像(BGR):openCV库 专用,顺序为 蓝、绿、红 。
在这里插入图片描述

代码

1、读取、显示、保存图像

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

import cv2 # 导入 openCV 库

i = cv2.imread(r"C:\workspace\python\openCV\test.jpg") # 读取
cv2.imshow("Demo",i) # 显示
cv2.waitKey(0) # 窗口等待
cv2.destroyAllWindows() # 从内存中释放
cv2.imwrite(r"C:\workspace\python\openCV\test1.jpg",i) # 保存

其中,# -*- coding: utf-8 -*- 设置编码格式为 utf-8 , 仅 python2 才需要(若没有,则无法使用中文注释)

imread("path",param),其中 path 为图片的路径,param 为显示控制参数:

  • cv2.IMREAD_UNCHANGED - 以 原图形式 读入
  • cv2.IMREAD_GRAYSCALE - 以 灰度形式 读入
  • cv2.IMREAD_COLOR - 以 彩色形式 读入

imshow("name",image),其中 name 为窗口名称,image 为读取的图片对象。

waitKey(delay),其中 delay 为窗口的等待时间。

  • delay > 0 - 等待 delay 毫秒
  • delay < 0 - 等待键盘单击
  • delay = 0 - 无限等待
  • delay = null - 等待键盘单击

imwrite("path",image),其中 path 为保存图片的路径(自定义名称),image 为读取的图片对象


2、读取、修改 像素值(openCV)

import cv2

#BGR图像

i = cv2.imread(r"C:\workspace\python\openCV\color.jpg",cv2.IMREAD_UNCHANGED) # BGR图像,三通道
print(i[20,20]) # 结果 [255 248 255]

'''
修改像素方式一:统一修改
'''
i[20,20] = [0,0,0]
print(i[20,20]) # 结果 [0 0 0]

'''
修改像素方式二:逐 通道 修改
'''
i[20,20,0] = 0 # 蓝 Blue 通道
i[20,20,1] = 1 # 绿 Green 通道
i[20,20,2] = 2 # 红 Red 通道

print(i[20,20]) # 结果 [0 1 2]

#灰度图像

g = cv2.imread(r"C:\workspace\python\openCV\gray.jpg",cv2.IMREAD_UNCHANGED) # 灰度图像,单通道
print(g[20,20]) # 结果 255

g[20,20] = 0
print(g[20,20]) # 结果 0

其中,i[20,20] 表示 图片 i 中第20行,20列的像素


3、读取、修改 像素值(numpy)

import cv2
import numpy as np

i = cv2.imread(r"C:\workspace\python\openCV\color.jpg",cv2.IMREAD_UNCHANGED) # BGR图像,三通道

print(i.item(20,20,0)) # B 通道 255
i.itemset((20,20,0),0) 
print(i.item(20,20,0)) # 结果 0

print(i.item(20,20,1)) # G 通道 248
i.itemset((20,20,1),1) 
print(i.item(20,20,1)) # 结果 1

print(i.item(20,20,2)) # R 通道 255
i.itemset((20,20,2),2) 
print(i.item(20,20,2)) #结果 2

g = cv2.imread(r"C:\workspace\python\openCV\gray.jpg",cv2.IMREAD_UNCHANGED) # 灰度图像,单通道
print(g.item(20,20)) # 结果 255

g.itemset((20,20),7)
print(g.item(20,20)) # 结果 7

item(x,y) - 读取 (x,y) 坐标的像素值,单通道

item(x,y,n) - 读取 (x,y) 坐标的像素值, 三通道,n 是通道(0-B,1-G,2-R)

itemset((x,y),k) - 修改 (x,y) 坐标的像素值,k是新赋的值

itemset((x,y,n),k) - 修改 (x,y) 坐标的像素值,k是新赋的值,n 是通道(0-B,1-G,2-R)


猜你喜欢

转载自blog.csdn.net/zhaiax672/article/details/88712008