基于python的OpenCV图像1
1. 读入图片并显示
import cv2
img = cv2.imread("longmao.jpg")
cv2.imshow("longmao", img)
cv2.waitKey(0) #等待按键,0表示永久等待
cv2.destroyAllWindows() #完成之后销毁窗体

2. RGB通道分离
基于numpy数组的方法
r = img[:, :, 2]
g = img[:, :, 1]
b = img[:, :, 0]
注意:OpenCV读取的顺序是BGR。
**基于OpenCV的RGB通道分离
b, g, r = cv2.split(img) #得到三个通道的值
b = cv2.split(img)[0]
3. RGB通道分离
img2 = cv2.merge([b, g, r])
4. 单像素处理
img[9, 9, 2] #通过数组索引的方式获取某个像素值,
#获取第10行,第10列(从0开始)的单独R颜色分量
5. 遍历图像
img = cv2.imread("longmao.jpg")
img2 = np.zeros(img.shape)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
img2[i, j, 0] = img[i, j, 0] #b分量
#img2[i, j, 1] = img[i, j, 1] #g分量
#img2[i, j, 2] = img[i, j, 3] #R分量
#img2[i, j] = cv2.merge([img2[i, j, 0], img2[i, j, 1], img2[i, j, 2]])
cv2.imshow("copy", img2)
cv2.waitKey(0)
cv2.destroyAllWindows() #完成之后销毁窗体
图片的蓝色分量显示

5. 给图片添加椒盐噪声
import numpy as np
import cv2
def addPepperAndSalt(img, n):
img2 = img
for i in range(n):
x = int(np.random.random() * img.shape[0])
y = int(np.random.random() * img.shape[1])
img2[x, y, 0] = 255
img2[x, y, 1] = 255
img2[x, y, 2] = 255
return img2
img = cv2.imread("longmao.jpg")
img2 = addPepperAndSalt(img, 5000)
cv2.imshow("salt and pepper", img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
注:np.random.random():返回[0.0, 1)的随机数,默认是一个,括号里面可以选返回随机数的数量

6. 添加椒盐噪声进阶版
import cv2
import numpy as np
def peppersalt(img, n, m):
"""
Add peppersalt to image
:param img: the image you want to add noise
:param n: the total number of noise (0 <= n <= width*height)
:param m: different mode
m=1:add only white noise in whole image
m=2:add only black noise in whole image
m=3:add black and white noise in whole image
m=4:add gray scale noise range from 0 to 255
m=5:add color noise in whole image,RGB is combined randomly with every channel ranges from 0 to 255
:return: the processed image
"""
img2 = img
if m == 1:
for i in range(n):
x = int(np.random.random() * img.shape[0])
y = int(np.random.random() * img.shape[1])
img2[x, y, 0] = 255 #添加白色噪声
img2[x, y, 1] = 255
img2[x, y, 2] = 255
elif m == 2:
for i in range(n):
x = int(np.random.random() * img.shape[0])
y = int(np.random.random() * img.shape[1])
img2[x, y, 0] = 0 #黑色
img2[x, y, 1] = 0
img2[x, y, 2] = 0
elif m == 3:
for i in range(n):
x = int(np.random.random() * img.shape[0])
y = int(np.random.random() * img.shape[1])
flag = np.random.random() * 255 #随机添加白色或黑色
if flag > 128:
img2[x, y, 0] = 255
img2[x, y, 1] = 255
img2[x, y, 2] = 255
else:
img2[x, y, 0] = 0
img2[x, y, 1] = 0
img2[x, y, 2] = 0
elif m == 4:
for i in range(n):
x = int(np.random.random() * img.shape[0])
y = int(np.random.random() * img.shape[1])
flag = int(np.random.random() * 255) #随机颜色
img2[x, y, 0] = flag
img2[x, y, 1] = flag
img2[x, y, 2] = flag
elif m == 5:
for i in range(n):
x = int(np.random.random() * img.shape[0])
y = int(np.random.random() * img.shape[1])
f1 = int(np.random.random() * 255) #彩色
f2 = int(np.random.random() * 255)
f3 = int(np.random.random() * 255)
img2[x, y, 0] = f1
img2[x, y, 1] = f2
img2[x, y, 2] = f3
return img2
if __name__ == "__main__":
img = cv2.imread("longmao.jpg")
img = peppersalt(img, 500, 5)
cv2.imshow("salt and pepper", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
7. 实现下雪demo

def snow2pic(img, n):
"""
:param img: input a rgb picture
:param n: density of the snow
:return: the pic with snow in the top
"""
#length, width = img.shape
#top_length = length * 0.35
for i in range(n):
x = int(np.random.random() * img.shape[0] * 0.35)
y = int(np.random.random() * img.shape[1])
img[x, y, 0] = 255 # 添加白色噪声
img[x, y, 1] = 255
img[x, y, 2] = 255
for i in range(200):
x = int(np.random.random() * img.shape[0] * 0.8)
y = int(np.random.random() * img.shape[1])
img[x, y, 0] = 255 # 添加白色噪声
img[x, y, 1] = 255
img[x, y, 2] = 255
return img
if __name__ == "__main__":
img = cv2.imread("longmao.jpg")
img = snow2pic(img, 400)
cv2.imshow("salt and pepper", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
基于python的OpenCV图像1的更多相关文章
- BugKu 2B+基于python的opencv的安装-------CTF 盲水印的套路
BugKu杂项-2B 下载图片后,binwalk下跑一跑,发现有个zip,分离. 值得一提的是,这个zip是伪加密的. 但是你在分离的时候,伪加密的图片也给你分离出来了.这两个图片2B和B2肉眼看起来 ...
- Python下opencv使用笔记(图像频域滤波与傅里叶变换)
Python下opencv使用笔记(图像频域滤波与傅里叶变换) 转载一只程序喵 最后发布于2018-04-06 19:07:26 阅读数 1654 收藏 展开 本文转载自 https://blog ...
- Python 图像处理 OpenCV (14):图像金字塔
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...
- 搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台
搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台 By 子敬叔叔 最近在学习麦好的<机器学习实践指南案例应用解析第二版>,在安装学习环境的时候 ...
- Python下opencv使用笔记(一)(图像简单读取、显示与储存)
写在之前 从去年開始关注python这个软件,途中间间断断看与学过一些关于python的东西.感觉python确实是一个简单优美.easy上手的脚本编程语言,众多的第三方库使得python异常的强大. ...
- Python 图像处理 OpenCV (3):图像属性、图像感兴趣 ROI 区域及通道处理
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 图像属性 图像 ...
- Python 图像处理 OpenCV (4):图像算数运算以及修改颜色空间
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...
- Python 图像处理 OpenCV (5):图像的几何变换
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...
- Python 图像处理 OpenCV (6):图像的阈值处理
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...
随机推荐
- python学习——用dictionary实现通过地区查询邮编
刚刚学习了python的基本语法,对自己学习的内容进行实践下. dictionary字典(类似map) 总结:1.dictionary比list读取速度快,但是占用内存大,适合存放不需修改,经常查询的 ...
- STS中springmvc.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring的compentScan注解扫描类机制
1主要通过ConfigurationClassParser类的doProcessConfigurationClass(ConfigurationClass configClass, SourceCla ...
- Jquery源码学习日记(1)
https://jquery.com/ 最新源码下载链接:jquery3.0 135-231定义了一些jquery的通用方法 233-301行定义了一些继承的方法 302-477定义了一些工具类方法 ...
- Leetcode——Two Sum(easy)
题目:Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1] 代码: ...
- java中如何使用break跳出多重循环
java 中已知的三种跳出多重循环的方式: System.out.println("---------java中跳出多重循环的三种方式:---------"); System.ou ...
- css响应式
<meta name="viewport" content="width=devicw-width,initial-scale=1.0"> 响应式图 ...
- Oracle中特殊的变量类型
1.%TYPE 允许用户动态地将数据库中某一列的数据类型与PL/SQL中某个变量关联.语法如下: variable_name table.column%TYPE 2.%ROWTYPE 允许用户定义 ...
- 如何安装ubuntu系统
https://www.cnblogs.com/Chinasf/archive/2010/05/06/1728840.html [Ubuntu 下挂ISO到虚拟光驱的方法] 各种方法参考如下论坛 ...
- Python TypeError: 'module' object is not callable 原因分析
今天尝试使用pprint进行输出,语句为 >>>import pprint >>>pprint(people) 结果报错,TypeError: 'module' o ...