51c视觉~CV~合集10
我自己的原文哦~ https://blog.51cto.com/whaosoft/13241694
一、CV创建自定义图像滤镜
热图滤镜
这组滤镜提供了各种不同的艺术和风格化光学图像捕捉方法。例如,热滤镜会将图像转换为“热图”,而卡通滤镜则提供生动的图像,这些图像看起来就像是漫画书制作的。最接近自然色彩以及海滩和自然场景的是 VSCO 滤镜。如果要减少工业感,可以对 Instagram 应用滤镜进行大量投资。将这个简单的灰度图转换为彩色图像。这将是灰度滤镜之一。最后,让我们考虑油画滤镜,OpenCV 通过一种风格化技术实现了该滤镜,该技术可创建看起来像油画的纹理效果。用户只需几行代码即可通过 OpenCV 和 Python 轻松使用它们来增强图像。
热成像非常适合在夜间或存在轻微雾、雨或烟等遮挡物的情况下生成图像。例如,前视红外或 FLIR 摄像机可用于为军用和民用飞机提供夜视功能,或用于安全和监视。
import cv2
img = cv2.imread('image.jpg')
#applying filter
color_image = cv2.applyColorMap(img, cv2.COLORMAP_JET)
cv2.imshow('Image',color_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
卡通滤镜
使用我们举世闻名的 Cartoonizer 效果将任何照片变成卡通!只需单击一下即可了解为什么它是我们最喜爱的艺术类别。
这是读取图像后的片段代码,我们必须应用灰色,然后模糊图像。
import cv2
image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurImage = cv2.medianBlur(image, 1)
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
color = cv2.bilateralFilter(image, 9, 200, 200)
cartoon = cv2.bitwise_and(color, color, mask = edges)
cv2.imshow('Image',cartoon)
cv2.waitKey(0)
cv2.destroyAllWindows()
VSCO 滤镜
要创建 VSCO 风格的滤镜效果,您需要使用鲜艳的预设。类似 VSCO 的滤镜非常适合各种图像。让您的图像呈现出色彩鲜艳、充满活力的外观,非常适合自然和海滩场景等主题。
import cv2
import numpy as np
def colorful_vibrant_filter(image):
"""
Apply a colorful and vibrant filter to the input image.
Args:
image (numpy.ndarray): The input image.
Returns:
numpy.ndarray: The filtered image.
"""
# Convert the image to HSV color space
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Increase the saturation by 50%
hsv_image[..., 1] = np.clip(hsv_image[..., 1] * 1.5, 0, 255)
# Increase the value (brightness) by 20%
hsv_image[..., 2] = np.clip(hsv_image[..., 2] * 1.2, 0, 255)
# Convert the image back to BGR color space
filtered_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
return filtered_image
# Load an example image
image = cv2.imread('image.jpg')
# Apply the colorful vibrant filter
filtered_image = colorful_vibrant_filter(image)
# Display the original and filtered images
cv2.imshow('Original Image', image)
cv2.imshow('Filtered Image', filtered_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
灰度滤镜
使用 Fotors 的“灰度”、“镀铬”和“黑白”选项,在几秒钟内将您的照片变成黑白色!“褪色白色”滤镜还添加了微妙的仿旧效果。
import cv2
def grayscale_filter(image):
"""
Apply a grayscale filter to the input image.
Args:
image (numpy.ndarray): The input image.
Returns:
numpy.ndarray: The grayscale image.
"""
# Convert the image to grayscale using cv2.cvtColor
grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return grayscale_image
# Load an example image
image = cv2.imread('image.jpg')
# Apply the grayscale filter
grayscale_image = grayscale_filter(image)
# Display the original and grayscale images
cv2.imshow('Original Image', image)
cv2.imshow('Grayscale Image', grayscale_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
油画滤镜
厌倦了必须打开 Photoshop 才能为照片添加油画滤镜?只需在“油画”下单击几下即可添加!“光泽”可让所有东西都呈现出绿色,非常适合绿叶照片。
import cv2
# Load the image
img = cv2.imread('image.jpg')
# Apply oil painting filter
output = cv2.stylization(img, sigma_s=60, sigma_r=0.6)
# Display the output
cv2.imshow('Oil Painting', output)
cv2.waitKey(0)
cv2.destroyAllWindows()
这些图像滤镜提供了一种富有创意和艺术感的方式来增强和转换您的图像。使用 OpenCV 和 Python,用户可以轻松应用这些滤镜来创建各种时尚且具有视觉吸引力的转换效果,从热和卡通转换到充满活力的 VSCO 风格外观和经典的灰度转换。
二、MoveNet Lightning 和 CV 实现实时姿势检测
在本文中,我们将探讨如何使用 TensorFlow Lite 的 MoveNet Lightning 模型和 OpenCV 构建实时姿势检测系统。这个项目使我们能够使用网络摄像头检测身体关节并动态地可视化运动。
MoveNet Lightning 概述
MoveNet 是由 TensorFlow 开发的最先进的姿态估计模型,专为实时应用程序而设计。MoveNet 的 Lightning 变体针对速度和准确性进行了优化,使其适用于健身跟踪、运动分析等任务。
第 1 步:安装所需的库
在开始之前,请确保您已安装以下 Python 库:
pip install tensorflow numpy opencv-python matplotlib
这些库对于加载 MoveNet 模型、处理视频帧和可视化结果至关重要。
第 2 步:加载 MoveNet 模型
首先,我们将加载 TensorFlow Lite MoveNet Lightning 模型并分配张量进行推理。
import tensorflow as tf
import numpy as np
import cv2
# Load the TensorFlow Lite model
interpreter = tf.lite.Interpreter(model_path='3.tflite')
interpreter.allocate_tensors()
第 3 步:定义辅助函数
为了可视化检测到的姿势,我们需要在每一帧上绘制关键点 (关节) 和连接 (骨骼)。
绘制关键点
def draw_keypoints(frame, keypoints, confidence_threshold):
"""
Draws keypoints on the frame if their confidence exceeds the threshold.
"""
y, x, c = frame.shape
shaped = np.squeeze(np.multiply(keypoints, [y, x, 1]))
for kp in shaped:
ky, kx, kp_conf = kp
if kp_conf > confidence_threshold:
cv2.circle(frame, (int(kx), int(ky)), 4, (0, 255, 0), -1)
绘制连接
EDGES = {
(0, 1): 'm', (0, 2): 'c', (1, 3): 'm', (2, 4): 'c',
(0, 5): 'm', (0, 6): 'c', (5, 7): 'm', (7, 9): 'm',
(6, 8): 'c', (8, 10): 'c', (5, 6): 'y', (5, 11): 'm',
(6, 12): 'c', (11, 12): 'y', (11, 13): 'm', (13, 15): 'm',
(12, 14): 'c', (14, 16): 'c'
}
def draw_connections(frame, keypoints, edges, confidence_threshold):
"""
Draws connections (edges) between keypoints if both exceed the threshold.
"""
y, x, c = frame.shape
shaped = np.squeeze(np.multiply(keypoints, [y, x, 1]))
for edge, color in edges.items():
p1, p2 = edge
y1, x1, c1 = shaped[p1]
y2, x2, c2 = shaped[p2]
if (c1 > confidence_threshold) & (c2 > confidence_threshold):
cv2.line(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)
第 4 步:实时姿势检测
使用 OpenCV,我们将从网络摄像头捕获帧,并通过 MoveNet 处理它们以进行姿势检测。
# Initialize webcam capture
cap = cv2.VideoCapture(1) # Use '0' for the default camera
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Preprocess the frame for MoveNet
img = frame.copy()
img = tf.image.resize_with_pad(np.expand_dims(img, axis=0), 192, 192)
input_image = tf.cast(img, dtype=tf.float32)
# Get input and output tensor details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Run inference
interpreter.set_tensor(input_details[0]['index'], np.array(input_image))
interpreter.invoke()
keypoints_with_scores = interpreter.get_tensor(output_details[0]['index'])
# Draw connections and keypoints on the frame
draw_connections(frame, keypoints_with_scores, EDGES, 0.4)
draw_keypoints(frame, keypoints_with_scores, 0.4)
# Display the frame
cv2.imshow('MoveNet Lightning', frame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
如何运行
- 模型加载:TensorFlow Lite MoveNet 模型已加载并准备好进行推理。
- 帧预处理:每个网络摄像头帧的大小都会调整并填充,以匹配模型的预期输入尺寸。
- 姿势检测:该模型预测每帧的关键点及其置信度分数。
- 可视化:关键点和连接叠加在框架上,实时动态更新。
应用
该项目具有多种应用:
- 健身追踪和体型校正。
- 交互式系统的手势识别。
- 运动中的实时运动分析。
通过利用 TensorFlow Lite 的 MoveNet 和 OpenCV,我们创建了一个功能强大且高效的姿势检测系统。这种设置是轻量级的,非常适合边缘设备上的实时应用程序。通过将该系统集成到健身或游戏应用程序中来进一步探索!
源码下载:
https://github.com/iamramzan/Real-Time-Pose-Detection-Using-MoveNet-Lightning-and-OpenCV