如何在opencv中绘制正弦波?

2024-09-30 22:26:47 发布

您现在位置:Python中文网/ 问答频道 /正文

因为我们知道正弦波的振幅可以计算为:Sine formula

假设我们将时间作为图像的像素,则公式为: enter image description here

其中ij是像素的索引。随着频率的变化,生成的图像应如下所示:

enter image description here

其中显示了使用3个不同频率值构建的3个图像。但使用我的代码,我只能得到:

enter image description here

即使频率值不同,频率看起来也很小。我不能对这个问题全神贯注。我使用的代码是:

import numpy as np
import cv2 as cv
from math import pi, sin

a = np.random.rand(500, 500, 3)

f = 7000000000000

rows, cols , channel= a.shape

print(f'Rows are: {rows}')
print(f'Cols are: {cols}')
print(f'Channels are: {channel}')

OldMax = 1
OldMin = -1
NewMax = 1
NewMin = 0

# A function to normalize values b/w 0 and 1
def re_range(OldValue):
    OldRange = (OldMax - OldMin)
    NewRange = (NewMax - NewMin)
    NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin
    return NewValue


for i in range(rows):
    for j in range(cols):
        a[i, j, :] = re_range(sin(2 * pi * f * (i + j))) 


cv.imshow('image', a)
cv.waitKey(0)
cv.destroyAllWindows()

有人能解释出哪里出了问题吗


Tags: 代码图像importasnprange像素cv