【发布时间】:2022-01-21 18:43:28
【问题描述】:
我的解决方法如下:
import cv2
import numpy as np
def sum_all_pixels(image, x1, y1, x2, y2, c1=0, c2=3):
'''
INPUTS: the image, the coordinates of the rectangle, and the channels
OUTPUT: sum of all pixels of the image within the rectangle
image: the path to the image
x1, y1: the coordinates of the top-left point of the rectangle
x2, y2: the coordinates of the bottom-right point of the rectangle
c1, c2: the range of the RGB color channels. By default, it assumed the sum is to be calculated across all 3 color channels
'''
img = cv2.imread(image)
return np.sum(img[y1:y2, x1:x2, c1:c2])
有没有更好、更高效的算法来做到这一点?
【问题讨论】:
-
如果单个图像有很多矩形,则应使用整体图像预处理。之后,每个矩形只需读取 4 个值即可计算所需的总和。
标签: python opencv computer-vision