基于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 ...
随机推荐
- 腾讯开源项目phxpaxos的编译步骤
#paxos的一般编译流程在项目文档<中文详细编译手册>里面已经有介绍,这里重点介绍一下编译samples目录下的代码: #我的环境是ubuntu; #设置paxos根目录 phx_dir ...
- Unity - Photon PUN 本地与网络同步的逻辑分离 (一)
服务器大家可以使用Photon官网提供的,这样会变得很简单,直接搭建下就好.或者下载到本地开启本地端Photon服务器 (大家也可以使用和我一样方式有时间做了个winform 程序用来管理本地服务器开 ...
- Sublime 中文乱码问题
今天在Windows上使用Sublime Text 3的时候,发现一些txt文本打开以后,中文都是乱码.于是搜了一下,找到了解决方案. 步骤: 在Sublime Text里,按ctrl+`,打开Con ...
- 从Excel表中导入数据时日期格式的验证问题解决
#region IsDateTimeType 私有方法判断导入数据是否是日期格式 /// <summary> /// 私有方法判断导入数据是否是日期格式 /// </summary& ...
- mybatis递归查询
<!--mybatis递归查询--><resultMap id="recursionMenuMap" type="AgentMenu" ext ...
- 山东省ACM多校联盟省赛个人训练第六场 poj 3335 D Rotating Scoreboard
山东省ACM多校联盟省赛个人训练第六场 D Rotating Scoreboard https://vjudge.net/problem/POJ-3335 时间限制:C/C++ 1秒,其他语言2秒 空 ...
- 渗透测试的理论部分3——ISSAF的详细描述
ISSAF即信息系统安全评估框架(Information Systems Security Assessment Framework)是另外一种开放源代码的安全性测试和安全分析框架.为了解决安全评估工 ...
- [转]etcd 启用 https
1, 生成 TLS 秘钥对 2,拷贝密钥对到所有节点 3,配置 etcd 使用证书 4,测试 etcd 是否正常 5,配置 kube-apiserver 使用 CA 连接 etcd 6,测试 kube ...
- 数据结构C语言版-栈
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <iostream> ...
- shell搭建CentOS_7基础环境
#!/bin/bash#Auth:Darius#CentOS_7配置实验环境eno=`ifconfig|awk '{print $1}'|head -1|awk -F ":" '{ ...