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

文章目录

  • 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)