使用OpenCV(Python)的图像遮罩

使用OpenCV(Python)的图像遮罩,python,opencv,mask,Python,Opencv,Mask,我制作了这个代码,创建了两个面具。应用时,结果如下: 原始图像 输出 我知道代码,它一点也不好,但它能完成它的工作。如何改进它?以下是一些建议: import cv2 import numpy as np frame = cv2.imread('image.jpg') h, w = frame.shape[:2] mask_color = (0, 50, 255) # isolate a repeating constant # avoid setting a variable tha

我制作了这个代码,创建了两个面具。应用时,结果如下:

原始图像

输出


我知道代码,它一点也不好,但它能完成它的工作。如何改进它?

以下是一些建议:

import cv2
import numpy as np

frame = cv2.imread('image.jpg')

h, w = frame.shape[:2]
mask_color = (0, 50, 255) # isolate a repeating constant

# avoid setting a variable that is used only once, only if you REALLY need it to improve readability
# that's why `upper_value` and `lower_value` were removed. 
# BTW, `lower_value` was unused in your code.

upper_mask = cv2.rectangle(frame, (0, 0), (w, int(0.5 * h)), mask_color, -1)
lower_mask = cv2.rectangle(frame, (0, h), (w, int(0.7 * h)), mask_color, -1)

以下是一些建议:

import cv2
import numpy as np

frame = cv2.imread('image.jpg')

h, w = frame.shape[:2]
mask_color = (0, 50, 255) # isolate a repeating constant

# avoid setting a variable that is used only once, only if you REALLY need it to improve readability
# that's why `upper_value` and `lower_value` were removed. 
# BTW, `lower_value` was unused in your code.

upper_mask = cv2.rectangle(frame, (0, 0), (w, int(0.5 * h)), mask_color, -1)
lower_mask = cv2.rectangle(frame, (0, h), (w, int(0.7 * h)), mask_color, -1)

你在寻求什么改进?一个更可读的代码?是的,可读并且可能有效。谢谢。你想要什么改进?一个更可读的代码?是的,可读并且可能有效。谢谢