Opencv 运动图案生成

问题描述 投票:0回答:1

大家好,我是 opencv 和 python 的新手,我有以下代码:

```
import numpy as np
import cv2

def Vstripes(width, height, num_stripes, color1, color2, speed):

    image = np.ones((height, width, 3), dtype=np.uint8) * 255
    # Calculate the width of each stripe
    stripe_width = width // num_stripes
    # Calculate the starting horizontal shift
    start_shift = 0

    while True:

        # Draw the vertical stripes with the current horizontal shift
        for i in range(num_stripes):

            # Calculate the starting and ending x-coordinates of each stripe with the shift
            start_x = (i * stripe_width + start_shift) % width
            end_x = ((i + 1) * stripe_width + start_shift) % width
            # Draw the stripe with the specified color
            color = color1 if i % 2 == 0 else color2
            image[:, start_x:end_x, :] = color


        cv2.imshow("Moving Vertical Stripes", image)

        key = cv2.waitKey(50)  # Adjust the delay time between frames (in milliseconds)


        # Check for key press (break the loop if any key is pressed)
        if key != -1:

            break


        # Update the horizontal shift for the next frame

        start_shift = (start_shift + speed) % (width * num_stripes)


    cv2.destroyAllWindows()

# Set the parameters
width = 800
height = 600
num_stripes = 10
color1 = (255, 0, 0)  # Blue
color2 = (0, 0, 0)  # Black
num_frames = 1000  # Number of frames for the animation
speed = 1  # Speed of the vertical movement
# Generate and display the moving stripes infinitely (press any key to stop)

Vstripes(width, height, num_stripes, color1, color2, speed)
```

此代码的作用是根据给定的参数创建条纹尺寸,然后交替颜色。然后它会从左向右移动,直到到达终点,然后重置。这是我遇到的一个小问题,当它重置时,移动模式看起来并不总是从左向右移动。就像它重置一样,但它是块状的(?不知道如何正确解释它)。知道我该如何修复它吗?我想要它,以便生成的图案看起来好像它无限期地从左向右移动。

我尝试编辑 start_x 行以及图像行,但没有得到我想要的结果。

python opencv animation design-patterns
1个回答
0
投票

发生这种情况是因为您需要处理边缘情况:第一个和最后一个条纹。假设在某个时刻

start_shift = 73
。那么
start_x
end_x
将具有以下值:

73 153
153 233
233 313
313 393
393 473
473 553
553 633
633 713
713 793
793 73

如果您使用代码

image[:, start_x:end_x, :] = color
,那么:

  1. 0到73的像素不会更新
  2. image[:, 793:73, :]
    不会更新任何像素。

最简单的方法是使用 numpy.roll 函数。完整示例:

import numpy as np
import cv2


def Vstripes(width, height, num_stripes, color1, color2, speed):
    image = np.ones((height, width, 3), dtype=np.uint8) * 255
    # Calculate the width of each stripe
    stripe_width = width // num_stripes

    for i in range(num_stripes):
        color = color1 if i % 2 == 0 else color2
        image[:, i * stripe_width : (i + 1) * stripe_width, :] = color

    while True:
        image = np.roll(image, shift=speed, axis=1)
        cv2.imshow("Moving Vertical Stripes", image)
        key = cv2.waitKey(10)  # Adjust the delay time between frames (in milliseconds)

        if key != -1:
            break

    cv2.destroyAllWindows()


width = 800
height = 600
num_stripes = 10
color1 = (255, 0, 0)  # Blue
color2 = (0, 0, 0)  # Black
num_frames = 1000  # Number of frames for the animation
speed = 1  # Speed of the vertical movement
Vstripes(width, height, num_stripes, color1, color2, speed)

外观如何:

© www.soinside.com 2019 - 2024. All rights reserved.