forked from JGoldstone/ImageSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_c18n.py
61 lines (52 loc) · 2.34 KB
/
frame_c18n.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
"""
Repository for image data gathered from an individual frame
===================
Defines a class that collects information on the distribution of tristimulus
samples in R3, both as a whole, and by octant.
"""
import numpy as np
import OpenImageIO as oiio
from OpenImageIO import ImageInput
from registers import Registers
from octant import Octant
class FrameC18n(object):
def __init__(self, path, most_neg=2, least_neg=-4, num_bins=None):
if not num_bins:
num_bins = 1 + most_neg - least_neg
self._path = path
self._img_size = self._path.stat().st_size
self._image_input = ImageInput.open(str(self._path))
if self._image_input is None:
# TODO check that the problem *is* actually the file is not there
raise FileNotFoundError(f"could not read image `{self._path}': {oiio.geterror()}")
roi = self._image_input.spec().roi
# n.b. ROI xend and yend are range-style 'one beyond the end' values
self._x = roi.xbegin
self._width = roi.xend - roi.xbegin
self._y = roi.ybegin
self._height = roi.yend - roi.ybegin
self._overall_registers = Registers(f"rewgisters for entire image", self._image_input.spec().channelnames)
self.octants = {}
for octant in Octant.octant_keys():
self.octants[octant] = Octant(self._image_input.spec(), octant, most_neg, least_neg, num_bins)
def tally(self):
img_array = self._image_input.read_image()
print(f"starting overall image tally")
self._overall_registers.tally(img_array, np.full((self._image_input.spec().height,
self._image_input.spec().width), True))
print(f"ending overall image tally")
print(f"starting octant tallies")
for octant in self.octants.values():
print(f"starting tally for octant {octant}:")
octant.tally(img_array)
print(f"ending octant tallies")
def __str__(self):
desc = []
img_size_desc = f"{self._img_size}-byte image, {self._width*self._height*2}"
desc.append(img_size_desc)
overall_bin_desc = str(self._overall_registers)
desc.append(overall_bin_desc)
for octant in self.octants:
desc.append(str(octant))
return '\n'.join(desc)