文章目录

  • image basics简单输入输出
  • pixel单个像素和局部像素
  • drawing画简单的图形

image basics简单输入输出

使用命令行切到该代码目录,然后使用python load_display_save.py --image ../images/trex.png命令运行。

注意,在切换盘符的时候要用cd /d D:\python\...这个命令

最核心的几个函数片段:

# 读取文件
image = cv2.imread(args["image"])
# 图像显示
cv2.imshow("Image", image)
cv2.waitKey(0)
# 图像导出
cv2.imwrite("newimage.jpg", image)

完整代码

# USAGE
# python load_display_save.py --image ../images/trex.png# Import the necessary packages
from __future__ import print_function
import argparse # 用来解析命令行的库
import cv2# Construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
# 主要是去获取image在硬盘上的相对位置,存在一个dictionary中
ap.add_argument("-i", "--image", required = True,help = "Path to the image")
args = vars(ap.parse_args())# Load the image and show some basic information on it
image = cv2.imread(args["image"])
print("width: {} pixels".format(image.shape[1]))
print("height: {} pixels".format(image.shape[0]))
print("channels: {}".format(image.shape[2]))# Show the image and wait for a keypress
cv2.imshow("Image", image)
cv2.waitKey(0)# Save the image -- OpenCV handles converting filetypes
# automatically
cv2.imwrite("newimage.jpg", image)

pixel单个像素和局部像素

2个注意点

在OpenCV中,颜色通道的顺序是BGR(可能是按照字母顺序)排列的。

另外,image[0:100, 0:100]中的四个参数分别表示start y, end y, start x, end x

核心代码

# 获取单个像素点的RGB数值
(b, g, r) = image[0, 0]
print("Pixel at (0, 0) - Red: {}, Green: {}, Blue: {}".format(r, g, b))
# 图片裁切
corner = image[0:100, 0:100]
cv2.imshow("Corner", corner)
# 局部图像赋值
image[0:100, 0:100] = (0, 255, 0)

完整代码

# USAGE
# python getting_and_setting.py --image ../images/trex.png# Import the necessary packages
from __future__ import print_function
import argparse
import cv2# Construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,help = "Path to the image")
args = vars(ap.parse_args())# Load the image and show it
image = cv2.imread(args["image"])
cv2.imshow("Original", image)# Images are just NumPy arrays. The top-left pixel can be
# found at (0, 0)
(b, g, r) = image[0, 0]
print("Pixel at (0, 0) - Red: {}, Green: {}, Blue: {}".format(r, g, b))# Now, let's change the value of the pixel at (0, 0) and
# make it red
image[0, 0] = (0, 0, 255)
(b, g, r) = image[0, 0]
print("Pixel at (0, 0) - Red: {}, Green: {}, Blue: {}".format(r, g, b))# Since we are using NumPy arrays, we can apply slicing and
# grab large chunks of the image. Let's grab the top-left
# corner
corner = image[0:100, 0:100]
cv2.imshow("Corner", corner)# Let's make the top-left corner of the image green
image[0:100, 0:100] = (0, 255, 0)# Show our updated image
cv2.imshow("Updated", image)
cv2.waitKey(0)

drawing画简单的图形

创建一个空白的画布

canvas = np.zeros((300, 300, 3), dtype = "uint8")

画基本的线条

green = (0, 255, 0)
cv2.line(canvas, (0, 0), (300, 300), green)

画基本的矩形

# -1表示用颜色完全填充
cv2.rectangle(canvas, (200, 50), (225, 125), blue, -1)

画基本的圆形

cv2.circle(canvas, (centerX, centerY), radius, white)

画出一个半径,圆心和颜色都随机的圆形

for i in range(0,25):radius = np.random.randint(5,high = 200)# size = (3,)表示生成维度为3的向量# tolist()color = np.random.randint(0,high = 256,size = (3,)).tolist()pt = np.random.randint(0,high = 300,size = (2,))# 位置必须是tuple类型cv2.circle(canvas,tuple(pt),radius,color,-1)
cv2.imshow("canvas",canvas)
cv2.waitKey(0)

效果如下

OpenCV-python学习笔记(一)——image basics输入输出,像素处理和绘制图形相关推荐

  1. opencv进阶学习笔记6:使用鼠标在图像上绘制矩形框或者多边形框

    基础版笔记传送门: python3+opencv学习笔记汇总目录(适合基础入门学习) 进阶版笔记目录: python+opencv进阶版学习笔记目录(适合有一定基础) 感兴趣区域传统绘制: openc ...

  2. openCV Python学习笔记(二)画几何

    此处要花几何图形,我们需要用到如下几个函数: cv2.line 画线函数 cv2.circle 画圆函数 cv2.ellipse 画椭圆 cv2.rectangle 画矩形 1.画线 img=cv2. ...

  3. OpenCV Python学习笔记(5)—— 边缘保留滤波(EPF)

    1 边缘保留滤波 高斯双边 均值迁移 2 测试 import cv2 as cv import numpy as npdef bi_demo(image):dst = cv.bilateralFilt ...

  4. python eval 入门_Python学习笔记整理3之输入输出、python eval函数

    Python学习笔记整理3之输入输出.python eval函数 来源:中文源码网    浏览: 次    日期:2018年9月2日 Python学习笔记整理3之输入输出.python eval函数 ...

  5. OpenCV之Python学习笔记(1)(2): 图像的载入、显示和保存 图像元素的访问、通道分离与合并

    OpenCV之Python学习笔记 一直都在用Python+OpenCV做一些算法的原型.本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段.现在看到一本国外的新书< ...

  6. OpenCV之Python学习笔记

    RSS订阅 登陆 注册 原文链接地址:http://www.itozi.net/19477.html OpenCV之Python学习笔记 ITOZI 发布于 2015-08-06 分类:OpenSta ...

  7. python学习笔记目录

    人生苦短,我学python学习笔记目录: week1 python入门week2 python基础week3 python进阶week4 python模块week5 python高阶week6 数据结 ...

  8. 廖Python学习笔记一

    1. 廖Python学习笔记 大的分类 如函数 用二级标题,下面的用三级 如输入输出 1.1.1. 输入输出 1.1.1.1. 输出 用 print() 在括号里加上字符串,就可以向屏幕上输出指定的文 ...

  9. Python学习笔记:Day 16 编写移动App

    前言 最近在学习深度学习,已经跑出了几个模型,但Pyhton的基础不够扎实,因此,开始补习Python了,大家都推荐廖雪峰的课程,因此,开始了学习,但光学有没有用,还要和大家讨论一下,因此,写下这些帖 ...

  10. Python学习笔记:Day15 部署Web App

    前言 最近在学习深度学习,已经跑出了几个模型,但Pyhton的基础不够扎实,因此,开始补习Python了,大家都推荐廖雪峰的课程,因此,开始了学习,但光学有没有用,还要和大家讨论一下,因此,写下这些帖 ...

最新文章

  1. helm命令的基本使用
  2. 39个超棒的免费高清专业纹理收藏集
  3. poj 2069 Super Star 模拟退火
  4. 扫地机器人开机充电还是关机充电器_适用于智能扫地机器人的充电方法与流程...
  5. 原生 JS 撸一个轮播图(支持拖拽切屏)
  6. C++ STL sort 函数的用法(自定义排序函数)
  7. 激活windows 2008 r2
  8. 对音频压缩概念的一些误解--记一次与音视频压缩专家的对话
  9. 压缩包文件打开密码如何破解
  10. 手把手教你用小米手机OTG功能连接打印机
  11. python 闲鱼_python之tk学习,闲鱼搜索-小记
  12. 【每日一练】21—CSS实现炫酷动画背景
  13. pandas, dataframe获取最后一行的三种方法
  14. 如何提升自己的短视频质量?三个小技巧来帮忙,助你做优质内容
  15. java里面的语法糖(糖衣语法)
  16. 网站签到时Cookie的获取方法
  17. Extmail修改模板及其它配置
  18. 曾国藩秘而不宣,老实人有这3个软肋,再闷头苦干也不会被重用
  19. LaTeX公式转成word公式(LaTeX公式转MathML / latex2mathml的使用)
  20. 低代码和云开发 区别

热门文章

  1. android学习笔记---42_服务的生命周期
  2. 汇编语言 HelloWorld (详细注释版)
  3. 杭电4530小Q系列故事——大笨钟
  4. android开发之android:padding和android:margin的区别
  5. 计算机二级access上机题,计算机二级ACCESS上机题库
  6. shell读取mysql_shell读取mysql数据库
  7. httpclient 不支持国密ssl_Hyperledger Fabric成都见面会:TWGC国密改造介绍
  8. 随想录(程序语言只是SE的敲门砖)
  9. linux看python包的路径_linux下 彻底修改python的包/模块导入路径
  10. python高级含金量技巧_2020年最新Python开发的高级技巧,面试必学