【python计算机视觉】3、求图像的幅度图

图像的幅度
python代码实现

import torch
import torch.nn as nn
import torch.nn.functional as F
#输入四维张量,输出四维张量
class Gradient(nn.Module):
    def __init__(self, channels):
        super(Gradient, self).__init__()
        kernel_gauss = torch.FloatTensor([[0.0924, 0.1192, 0.0924], [0.1192, 0.1538, 0.1192], [0.0924, 0.1192, 0.0924]]).unsqueeze(0).unsqueeze(0)
        kernel_gauss = np.repeat(kernel_gauss,channels,axis=0)
        kernel_x = torch.FloatTensor([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]).unsqueeze(0).unsqueeze(0)
        kernel_x = np.repeat(kernel_x,channels,axis=0)
        kernel_y = torch.FloatTensor([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]).unsqueeze(0).unsqueeze(0)
        kernel_y = np.repeat(kernel_y,channels,axis=0)

        self.channels =channels
        self.weight_gauss = nn.Parameter(data=kernel_gauss, requires_grad=False)
        self.weight_x = nn.Parameter(data=kernel_x, requires_grad=False)
        self.weight_y = nn.Parameter(data=kernel_y, requires_grad=False)

    def forward(self, image):
        b,c,w,h = image.size()
        
        gauss = F.conv2d(image, self.weight_gauss,padding=1,groups=self.channels)
        image = F.conv2d(image, self.weight_gauss, padding=1,groups=self.channels)
        image_x = F.conv2d(image, self.weight_x, padding=1,groups=self.channels)
        image_y = F.conv2d(image, self.weight_y, padding=1,groups=self.channels)
        image_x = torch.pow(image_x, 2)
        image_y = torch.pow(image_y, 2)

        edge = image_x + image_y
        edge = torch.sqrt(edge)
        return edge

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用OpenCV库中的cv2.goodFeaturesToTrack方法来检测物体的点,并使用cv2.calcOpticalFlowPyrLK方法来计算点的运动幅度。下面是一个简单的例子: ``` python import cv2 import numpy as np # 读取视频 cap = cv2.VideoCapture('video.avi') # Shi-Tomasi角点检测参数 feature_params = dict(maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7) # Lucas-Kanade光流法参数 lk_params = dict(winSize=(15, 15), maxLevel=2, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) # 随机颜色 color = np.random.randint(0, 255, (100, 3)) # 读取第一帧并进行角点检测 ret, old_frame = cap.read() old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) p0 = cv2.goodFeaturesToTrack(old_gray, mask=None, **feature_params) # 创建一个掩膜用于绘制轨迹 mask = np.zeros_like(old_frame) while True: # 读取一帧 ret, frame = cap.read() if ret: # 转换为灰度图像 frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 计算光流 p1, status, error = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params) # 选择好的点 good_new = p1[status == 1] good_old = p0[status == 1] # 绘制轨迹 for i, (new, old) in enumerate(zip(good_new, good_old)): a, b = new.ravel() c, d = old.ravel() mask = cv2.line(mask, (a, b), (c, d), color[i].tolist(), 2) frame = cv2.circle(frame, (a, b), 5, color[i].tolist(), -1) # 显示轨迹 img = cv2.add(frame, mask) cv2.imshow('frame', img) # 更新旧点 old_gray = frame_gray.copy() p0 = good_new.reshape(-1, 1, 2) # 按q键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() ``` 在这个例子中,我们首先使用cv2.goodFeaturesToTrack方法从第一帧图像中检测出物体的点,并使用它们来计算光流。然后,在每个后续帧中,我们使用cv2.calcOpticalFlowPyrLK方法来计算每个点的新位置,并使用cv2.line和cv2.circle方法绘制轨迹。最后,我们将轨迹图像与原始图像相加,并显示结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值