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
73 views
in Technique[技术] by (71.8m points)

python - Opencv addWeighted to same matrix

So I have the following goal, I want to do a addWeighted to the same np_array as one of the inputs. So without returning the adjusted frame.

    def function(frame):
        polygon_overlay = np.zeros(frame.shape, dtype=np.uint8)
        cv2.fillPoly(polygon_overlay, polygon, color, 8, 0)

        frame = cv2.addWeighted(frame, .5, polygon_overlay, .5, 0)

    frame = cv2.imread('someImg.ext')
    function(frame)
    cv2.imshow('result', frame)
    

So in this example the frame shown should have the polygon showing. I know this should be possible since this code does work.

    def function(frame):
        cv2.fillPoly(frame, polygon, color, 8, 0)

    frame = cv2.imread('someImg.ext')
    function(frame)
    cv2.imshow('result', frame)

I suspect this should be possible with some kind of numpy replacement method but I have no clue how.

question from:https://stackoverflow.com/questions/66061706/opencv-addweighted-to-same-matrix

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

1 Answer

0 votes
by (71.8m points)

This turned out to be quite easy:

def function(frame):
    polygon_overlay = np.zeros(frame.shape, dtype=np.uint8)
    cv2.fillPoly(polygon_overlay, polygon, color, 8, 0)

    frame[0:frame.shape[0], 0:frame.shape[1]]  = cv2.addWeighted(frame, .5, polygon_overlay, .5, 0)

frame = cv2.imread('someImg.ext')
function(frame)
cv2.imshow('result', frame)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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

56.8k users

...