opencv(二) 图片处理

本文详细介绍使用OpenCV进行图像处理的各种技术,包括像素操作、颜色空间转换、几何变换等。通过实例展示了如何读取、修改图像像素,实现物体追踪,以及应用不同的图像变换如缩放、旋转和平移。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

opencv 图片处理

opencv 图片像素操作

  • 取像素点操作
  • 设置像素点
  • 取图片块
  • 分离,合并 b, g, r
import numpy as np
import cv2 as cv

img = cv.imread('/Users/guoyinhuang/Desktop/G77.jpeg')

# 获取像素值
px = img[348, 120]  # 0 是y, 1 是x
print(px)

blue = img[100, 100, 0]
print(blue)
cv.namedWindow('img', 0)

# 更改像素值
img[100, 100] = [255, 255, 255]
print(img[100, 100])
# 更好的获取像素值
img.item(10, 10, 2)
img.itemset((10, 10, 2), 100)

vg = img.item(10, 10, 2)
print(vg)

print(img.size)

print(img.dtype)

ball = img[708:1142, 680:930]
img[571:1005, 240:490] = ball

# 分离合并 b,g,r
b, g, r = cv.split(img)
img = cv.merge((b,g,r))
print(b)

b = img[:, :, 0]
img[:, :, 2] = 0
cv.imshow('img', img)
cv.waitKey(0)
cv.destoryAllWindows()

opencv 改变彩色空间

import cv2 as cv
import numpy as np

flags = [i for i in dir(cv) if i.startswith('COLOR_')]
print(flags)

cap = cv.VideoCapture(0)
while True:
    _, frame = cap.read()
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
    lower_blue = np.array([110, 50, 50])
    upper_blue = np.array([130, 255, 255])

    mask = cv.inRange(hsv, lower_blue, upper_blue)
    res = cv.bitwise_and(frame, frame, mask=mask)
    cv.imshow('frame', frame)
    cv.imshow('mask', mask)
    cv.imshow('res', res)
    k = cv.waitKey(5) & 0xFF
    if k == 27:
        break
cv.destroyAllWindows()

这是物体追踪最简单的方法, 当你学习了关于轮廓的方法, 你可以做更多丰富的事情:例如找到物体质心并利用它追踪物体

opencv 图片的几何旋转操作

  • 缩放
  • 位移
  • 平面旋转
  • 仿射旋转
  • 透视旋转
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

img = cv.imread('/Users/guoyinhuang/Desktop/G77.jpeg')
# res = cv.resize(img, None, fx=2, fy=2, interpolation=cv.INTER_CUBIC)
# cv.namedWindow('img', 0)
# # OR
# height, width = img.shape[:2]
# res = cv.resize(img, (2*width, 2*height), interpolation=cv.INTER_CUBIC)

# translation
# rows, cols = img.shape[:2]
# M = np.float32([[1, 0, 100], [0, 1, 50]])
# dst = cv.warpAffine(img, M, (cols, rows))

# Rotation
# rows, cols = img.shape[:2]
# M = cv.getRotationMatrix2D(((cols - 1)/2.0, (rows - 1)/2.0), 90, 1)
# dst = cv.warpAffine(img, M, (cols, rows))


# Affine Transformation
rows, cols, ch = img.shape
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
M = cv.getAffineTransform(pts1, pts2)
dst = cv.warpAffine(img, M, (cols, rows))
plt.subplot(121), plt.imshow(img), plt.title('Input')
plt.subplot(122), plt.imshow(dst), plt.title('0utput')
plt.show()


# perspective Transformation 透视旋转
rows, cols, ch = img.shape
pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])
pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
M = cv.getPerspectiveTransform(pts1, pts2)
dst = cv.warpPerspective(img, M, (300,300))
plt.subplot(121), plt.imshow(img), plt.title('Input')
plt.subplot(122), plt.imshow(dst), plt.title('Output')
plt.show()

# cv.imshow('img', dst)
cv.waitKey(0)
cv.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值