OpenCV - Apply mask to a color image

后端 未结 5 1499
甜味超标
甜味超标 2020-11-28 06:43

How can I apply mask to a color image in latest python binding (cv2)? In previous python binding the simplest way was to use cv.Copy e.g.

cv.Copy(

5条回答
  •  盖世英雄少女心
    2020-11-28 07:25

    Answer given by Abid Rahman K is not completely correct. I also tried it and found very helpful but got stuck.

    This is how I copy image with a given mask.

    x, y = np.where(mask!=0)
    pts = zip(x, y)
    # Assuming dst and src are of same sizes
    for pt in pts:
       dst[pt] = src[pt]
    

    This is a bit slow but gives correct results.

    EDIT:

    Pythonic way.

    idx = (mask!=0)
    dst[idx] = src[idx]
    

提交回复
热议问题