[英]Masks for image with OpenCV (Python)
I made this code which creates 2 masks.我制作了这段代码,它创建了 2 个面具。 When applied, the result is this:
应用后,结果是这样的:
import cv2
import numpy as np
frame = cv2.imread('image.jpg')
h, w = frame.shape[:2]
upper_value = int(h / 10) * 5
lower_value = -(int(h / 10) * 3)
upper_mask = cv2.rectangle(frame, (0, 0), (w, upper_value), (0, 50, 255), -1)
lower_mask = cv2.rectangle(frame, (0, upper_value + int(h / 10) * 5), (w, upper_value + int(h / 10) * 2), (0, 50, 255), -1)
I know the code it's not good at all but does its job.我知道代码一点都不好,但可以完成它的工作。 How can I improve it?
我该如何改进?
Here are some suggestions:以下是一些建议:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.