RGB图像转事件图像,事件相机可视化模拟,非模拟器,python代码

最近需要制作可视化的展示,有些场景图像没有对应的事件可视化图像,于是写了一个代码,可以将彩色图像转成事件相机里面事件累积的图像。从测试来看效果还可以。

做法就是把图像转成灰度图之后,看图像偏移位置的像素值之间的差,如果达到阈值10,那么就记录数据。

下面是代码:

import numpy as np
import os
import sys
import cv2

image_path = "utils\\00000.jpg"
gray_img=cv2.imread(image_path)
gray_img=cv2.cvtColor(gray_img,cv2.COLOR_BGR2GRAY)
print(gray_img.shape)
print(gray_img)
# 偏移方向 建议设置区间[-5,5]
xshift = 3
yshift = -3
xlong = gray_img.shape[1]-2*abs(xshift)
ylong = gray_img.shape[0]-2*abs(yshift)
pic_shape = [ylong,xlong,3]
print(pic_shape)
img = np.full(pic_shape, 0, dtype=np.uint8)

for i in range(abs(yshift),ylong):
    for j in range(abs(xshift),xlong):
        if int(gray_img[i+yshift][j+xshift])-int(gray_img[i][j])>10:
            img[i][j]=[0,255,0]
        if int(gray_img[i+yshift][j+xshift])-int(gray_img[i][j])<-10:
            img[i][j]=[0,0,255]
cv2.imshow("image", img)   
cv2.waitKey(0)

结果展示:              

输入图像:    

输出图像:  

事件图像:

可以看出来和真实事件图像基本上是一样的,不过就是真实事件图像因为环境原因噪点会更多一些。

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: 下面是一个 Python 实现的 RGB 图像函数,可以旋任意角度且不会出现黑边: ```python import numpy as np from scipy import ndimage def rotate_image(image, angle): # 将角度换为弧度 angle = np.deg2rad(angle) # 计算旋后的图像大小 width, height = image.shape[:2] new_width = int(np.round(abs(width * np.cos(angle)) + abs(height * np.sin(angle)))) new_height = int(np.round(abs(height * np.cos(angle)) + abs(width * np.sin(angle)))) # 计算旋后的中心点 cx, cy = width / 2, height / 2 new_cx, new_cy = new_width / 2, new_height / 2 # 构建矩阵 rotation_matrix = np.array([ [np.cos(angle), -np.sin(angle), (1 - np.cos(angle)) * cx + np.sin(angle) * cy], [np.sin(angle), np.cos(angle), (1 - np.cos(angle)) * cy - np.sin(angle) * cx], [0, 0, 1] ]) # 应用旋变换 rotated_image = ndimage.interpolation.affine_transform( image, rotation_matrix[:2, :2], offset=rotation_matrix[:2, 2], output_shape=(new_height, new_width), order=3, mode='constant', cval=0 ) return rotated_image.astype(image.dtype) ``` 这个函数使用了 `numpy` 和 `scipy` 库中的函数。它将输入图像给定的角度,而且不会出现黑边。 ### 回答2: 在Python中,可以使用PIL库(Python Imaging Library)来实现旋RGB图像且没有黑边的效果。以下是一个示例代码: ```python from PIL import Image def rotate_image(image_path, angle): # 打开图像 image = Image.open(image_path) # 获得旋后的图像 rotated_image = image.rotate(angle, expand=True) # 创建一个新图像,大小与旋图像相同,背景为白色 new_image = Image.new("RGB", rotated_image.size, "white") # 计算新图像的位置,使得旋图像居中显示 x = int((rotated_image.size[0] - image.size[0]) / 2) y = int((rotated_image.size[1] - image.size[1]) / 2) # 将旋后的图片粘贴到新图像中 new_image.paste(rotated_image, (x, y)) # 显示或保存旋后的图像 new_image.show() # new_image.save("rotated_image.jpg") # 调用函数进行旋 rotate_image('image.jpg', 30) ``` 这段代码首先使用`Image.open()`打开输入图像,并使用`rotate()`函数对图像进行旋,`angle`是旋角度。设置`expand=True`参数可以确保旋后的图像不会被裁剪。 接下来,根据旋图像的尺寸,创建一个新的图像对象(背景为白色),并计算旋图像与新图像的位置偏移量,确保旋后的图像居中显示。 最后,使用`paste()`函数将旋后的图像粘贴到新的图像对象中,然后可以选择显示或保存旋后的图像。在上述代码中,使用`show()`函数来显示旋后的图像,如果需要保存旋后的图像,可以使用`save()`函数。 以上代码可以实现对RGB图像进行任意角度的旋,旋后的图像会居中显示,不会出现黑边。 ### 回答3: 可以使用OpenCV库来实现Python RGB图像任何角度且没有黑边的代码。下面是一个简单的示例代码: ``` import cv2 import numpy as np def rotate_image(image, angle): # 获取图像的宽度和高度 height, width = image.shape[:2] # 构造旋矩阵 rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), angle, 1) # 计算新图像的宽度和高度 new_width = int(np.ceil(width * abs(np.cos(np.radians(angle)))) + np.ceil(height * abs(np.sin(np.radians(angle)))))) new_height = int(np.ceil(height * abs(np.cos(np.radians(angle)))) + np.ceil(width * abs(np.sin(np.radians(angle)))))) # 调整旋矩阵的平移分量,以确保图像在旋后仍然完全可见 rotation_matrix[0, 2] += (new_width - width) / 2 rotation_matrix[1, 2] += (new_height - height) / 2 # 执行旋操作 rotated_image = cv2.warpAffine(image, rotation_matrix, (new_width, new_height)) return rotated_image # 读取RGB图像 image = cv2.imread('input_image.jpg', cv2.IMREAD_COLOR) # 设置旋角度为45度 angle = 45 # 旋图像 rotated_image = rotate_image(image, angle) # 显示旋后的图像 cv2.imshow('Rotated Image', rotated_image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这个代码中,首先读取了一个RGB图像,然后定义了一个`rotate_image`函数用来旋图像。这个函数使用`getRotationMatrix2D`来获取旋矩阵,并且计算出新图像的宽度和高度。为了确保旋后的图像没有黑边,还需要调整旋矩阵的平移分量。最后,使用`warpAffine`函数执行实际的图像操作。在示例中,旋角度设置为45度,可以根据需求修改。最后,使用`imshow`函数显示旋后的图像

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值