Implementing "ImageClass" (python) class in PlantCV #775
DannieSheng
started this conversation in
Ideas
Replies: 1 comment
-
This is the prototype class I put together previously that just adds class Image (np.ndarray):
# The Image class extends the np.ndarray class by adding attributes.
# Documentation from NumPy here:
def __new__(cls, input_array, imgtype, filename):
obj = np.asarray(input_array).view(cls)
# New attribute imgtype stores the image type metdata
# (e.g. BGR, RGB, GRAY, etc.)
obj.imgtype = imgtype
# New attribute filename stores the path and filename
# of the source file
obj.filename = filename
return obj
def __array_finalize__(self, obj):
if obj is None: return
self.imgtype = getattr(obj, "imgtype", None)
self.filename = getattr(obj, "filename", None)
def __getitem__(self, key):
# Enhance the np.ndarray __getitem__ method
# Slice the array as requested but return an array of the same class
# Idea from NumPy examples of subclassing:
value = super(Image, self).__getitem__(key)
# If the slice reduces the dimensions of the ndarray from 3 to 2,
# set imgtype to GRAY
# Note that we can also handle this when we apply functions to
# an image, but need to handle user manipulation too
if len(self.shape) == 3 and len(value.shape) == 2:
value.imgtype = "GRAY"
return value |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We've discussed implementing "ImageClass" in PlantCV by subclassing ndarray (attaching attributes to images as python NumPy arrays).
Some potential attributes are:
The motivation/goal for implementing "ImageClass" :
1)Some functions can take fewer input variables
2)Some checks (e.g. datatype checks, rgb or bgr checks) can be done internally
Everybody is welcomed to brainstorm and add attributes. And please feel free to give suggestions to this implementation by thinking about questions like
Beta Was this translation helpful? Give feedback.
All reactions