Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

python - cv2.drawContours in a loop not working as expected

I am making a simple Python script that will make a stars flicker on picture with stars. Using a cv2.drawContours in a loop, I am trying to write frame into a queue "star by star" (at least one frame for each star), then adding same frame couple of times, and then a reverse frame sequence added into queue. However, this is working not as expected, as if loop was unrolled (?) or something (caching?) and cv2.drawContours drawing to the frame a couple of highlighted stars, so it looks not smooth, also I can't see the reverse frames effect, so need some fix, but I can't locate the problem. Here is the script:

import numpy as np
import argparse
import cv2
import random as rnd
import resource

resource.setrlimit(resource.RLIMIT_AS, (6442450944,6442450944))

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
orig = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

minSize = 0
maxSize = 15
thresholdValue = 25
rounds = 20
fps = 10
outputFilename = 'sky.mp4'

th, threshed = cv2.threshold(gray, thresholdValue, 255,
          cv2.THRESH_BINARY)#|cv2.THRESH_OTSU)
contours = cv2.findContours(threshed, cv2.RETR_LIST,
                    cv2.CHAIN_APPROX_SIMPLE)[-2]

sized_contours = []

for contour in contours:
    if minSize < cv2.contourArea(contour) < maxSize:
        sized_contours.append(contour)

frames = []
for x in range(rounds):
        output = orig.copy()
        not_reversed_frames = []
        reversed_frames = []

        subsetSize = rnd.randrange(len(sized_contours)//rnd.randint(16,32))
        sizedContoursUpdatedList = rnd.sample(sized_contours, subsetSize)

        for y in sizedContoursUpdatedList:
            color = list(np.random.random(size=3) * 256)
            output = cv2.drawContours(output, y, -1, color, 1)

            #N = random.randint(1,3)
            i = 0
            while i < 10:
                not_reversed_frames.append(output)
                i +=1

        #N = random.randint(1,5)
        #i = 0
        #while i < N:
        #   not_reversed_frames.append(output)
        #   i +=1

        frames += not_reversed_frames
        reversed_frames = not_reversed_frames[::-1]
        frames += reversed_frames

        not_reversed_frames = []
        reversed_frames = []

        output = None

height, width, layers = orig.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(outputFilename, fourcc, fps, (width,height))

for frame in frames:
    video.write(frame)

cv2.destroyAllWindows()
video.release()

cv2.waitKey(0)

Here is the test picture: enter image description here

Run with: python3 flicker.py --image test.png

Thank you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...