Last active
January 30, 2019 17:09
-
-
Save GongXinyuu/ac2e5dddd269853a7f0b69f9204d9fb1 to your computer and use it in GitHub Desktop.
integral image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| from imageio import imsave, imread | |
| import matplotlib.pyplot as plt | |
| img_name = 'xxx.jpg' | |
| img = imread(img_name) | |
| class IntegralImage(object): | |
| def __init__(self, img): | |
| assert len(np.shape(img))==2 | |
| self.height, self.width = np.shape(img) | |
| self.img = img | |
| self.sum_table = np.array([[None] * self.width] * self.height) | |
| self.prepare() | |
| def cum(self, y, x): | |
| if x<0 or y<0: | |
| return 0 | |
| elif self.sum_table[y][x] is None: | |
| self.sum_table[y][x] = self.cum(y-1, x) + self.cum(y, x-1) - self.cum(y-1, x-1) + self.img[y][x] | |
| return self.sum_table[y][x] | |
| else: | |
| return self.sum_table[y][x] | |
| def prepare(self): | |
| self.cum(self.height-1, self.width-1) | |
| def cal(self, x0, y0, x1, y1): | |
| assert x0 < x1 | |
| assert y0 < y1 | |
| summation = self.cum(y1, x1) + self.cum(y0, x0) - self.cum(y1, x0) - self.cum(y0, x1) | |
| #print(self.cum(y1, x1), self.cum(y0, x0), self.cum(y1, x0), self.cum(y0, x1)) | |
| return summation | |
| integral_img = IntegralImage(img.sum(2)) | |
| integral_img.cal(5,10,100,20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment