open cv学习 (一)像素的操作

open cv 入门

像素的操作

demo1
import cv2
import os
import numpy as np

# 1、读取图像
# imread()方法

# 设置图像的路径
Path = "./img.png"
# 设置读取颜色类型默认是1代表彩色图 0 代表灰度图
# 彩色图
flag = 1
# 灰度图
#flag = 0

# 读取图像,返回值是一个图像对象

image = cv2.imread(Path, flag)

# 0 ~ 255代表黑色到纯白色

# 打印该图像输出的是部分像素值
# print(image)


# 2、显示图像

# 显示图像

cv2.imshow("QQ", image)

# 等待按键按下,单位是ms

cv2.waitKey()

# 按下任意按键后摧毁所有窗口
cv2.destroyAllWindows()


# 3、保存图像

# 创建目录
# 在当前目录下创建一个目录
directory = "./My_Test_Photos"
# 如果不存在则创建
if not os.path.exists(directory):
    os.makedirs(directory)

# 保存图像到指定目录
file_path = os.path.join(directory, "test01.jpg")
cv2.imwrite(file_path, image)

# 4、获取图像属性

# shape (垂直像素数 水平像素数 通道数)

# size  (像素点总个数 = 垂直像素数 × 水平像素数 × 通道数) 灰度图通道数为 1

# dtype 图像的数据类型

image_Color = cv2.imread(Path,0)
print(image_Color.shape)
print(image_Color.size)
print(image_Color.dtype)
demo2
import cv2

image = cv2.imread("./cat.jpg")

cv2.imshow("cat", image)

for i in range(241, 292):
    for j in range(168, 219):
        image[i, j] = [255, 255, 255]
cv2.imshow("mycat", image)
cv2.waitKey()
cv2.destroyAllWindows()
demo3
import numpy as np

# 创建一维数组和二维数组
n1 = np.array([1, 2, 3])
n2 = np.array([0.1, 0.2, 0.3])
n3 = np.array([[1, 2], [3, 4]])

print(n1, n2, n3)

# 创建浮点数类型
my_list = [1, 2, 3]

n1 = np.array(my_list, dtype=np.float_)

# n1 = np.array(my_list, dtype=float)
print(n1)
print(n1.dtype)
print(type(n1[0]))

# 创建三维数组

nd1 = [1, 2, 3]
nd2 = np.array(nd1, ndmin=3)

print(nd2)
demo4
import numpy as np
# 指定维度数据类型未定义
n = np.empty([2, 3])

print(n)

# 创建全零数组
n1 = np.zeros((3, 3), dtype=np.uint8)


print(n1)
# 创建全一数组
ones = np.ones((4, 4), np.float_)
print(ones)

# 创建随机数组,三行三列,范围0-20
rand = np.random.randint(0, 20, (3, 3))

print(rand)

demo5

import cv2
import numpy as np
# 在opencv中黑白图像是一个二维数组,彩色图像是一个三维数组

# 创建黑白图像
flag = True
if flag:
    width = 200
    height = 100
    img = np.zeros((height, width), np.uint8)
    img[25:75, 50:100] = 255
    cv2.imshow("img", img)
    cv2.waitKey()
    cv2.destroyAllWindows()

else:
    width = 200
    height = 100
    img = np.ones((height, width), np.uint8)*255
    cv2.imshow("img", img)
    cv2.waitKey()
    cv2.destroyAllWindows()
demo6
import cv2
import numpy as np

width = 200
height = 100

img = np.zeros((height, width), np.uint8)
for i in range(0, width, 40):
    img[:, i+20] = 255
cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
demo7
import cv2
import numpy as np

width = 200
height = 100

# 创建指定宽高、3通道、像素值都为0的图像

img  = np.zeros((height, width, 3), np.uint8)

blue = img.copy()

blue[:, :, 0] = 255  # 通道1的所有像素值都为255

green = img.copy()

green[:, :, 1] = 255   # 通道2的所有像素值都为255

red = img.copy()

red[:, :, 2] = 255  # 通道3的所有像素值都为255
cv2.imshow("blue", blue)
cv2.imshow("green", green)
cv2.imshow("red", red)
cv2.waitKey()
cv2.destroyAllWindows()
demo8
import cv2
import numpy as np

# 创建随机图像
width = 200
height = 100

img = np.random.randint(256, size=(height, width, 3), dtype=np.uint8)

cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
demo9
import cv2
import numpy as np

# 拼接图像

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.array([7, 8, 9])
result = np.hstack((a, b, c))

print(result)
demo10
import cv2
import numpy as np
# 垂直拼接
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.array([7, 8, 9])
result = np.vstack((a, b, c))

print(result)
demo11
import cv2
import numpy as np
Path = "./cat.jpg"
img = cv2.imread(Path)
img_h = np.hstack((img, img))
img_v = np.vstack((img, img))

cv2.imshow("img_h", img_h)
cv2.imshow("img_v", img_v)

cv2.waitKey()
cv2.destroyAllWindows()

你可能感兴趣的:(学习,opencv,计算机视觉)