Skip to content

Instantly share code, notes, and snippets.

@hideyukisaito
Created August 23, 2016 10:35
Show Gist options
  • Save hideyukisaito/a33ecfea5f28a77bbc9dc7620bb9100f to your computer and use it in GitHub Desktop.
Save hideyukisaito/a33ecfea5f28a77bbc9dc7620bb9100f to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import numpy as np
import cv2
import math
def main():
img = cv2.imread('images/sample_8.jpg')
height, width, channels = img.shape
window = cv2.namedWindow('img', cv2.WINDOW_NORMAL)
div_y = 32
div_x = 32
cell_height, cell_width = round(height / div_y), round(width / div_x)
# canvas = np.zeros(img.shape, 'uint8')
canvas = np.zeros([cell_height * div_y, cell_width * div_x, channels], 'uint8')
for y in range(div_y):
for x in range(div_x):
pos_y = cell_height * y
pos_x = cell_width * x
# 平均化対象のセル
target = img[pos_y:pos_y + cell_height, pos_x:pos_x + cell_width]
# 平均化
averaged_color_per_row = np.average(target, axis=0)
averaged_color = np.average(averaged_color_per_row, axis=0).astype('uint8')
# averaged_color = np.uint8(averaged_color)
averaged_img = np.array([[averaged_color] * cell_width] * cell_height, np.uint8)
# clip = img[pos_y:pos_y + cell_height, pos_x:pos_x + cell_width]
# canvas[pos_y:pos_y + cell_height, pos_x:pos_x + cell_width] = clip
canvas[pos_y:pos_y + cell_height, pos_x:pos_x + cell_width] = averaged_img
cv2.imshow('img', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment