0

I really need some help to understand Python and class.

I'm defining an ImageProcessing Class with several methods.

class ImageProcessing:
    """
    Class containing different processing operations 
    """
    def __init__(self, name, height, width):
        """ Defining name, height and width of image processing object """
        self.name = name
        self.height = height
        self.width = width

    # several methods here with def.
    # These methods mainly used some open cv operations.
    def masking(self):
    """ Method to mask | All masks are applied manually 
    - self MUST BE a numpy array or a scalar
    """
    mask = cv2.rectangle(self, (0,0), (250,1200), (0,0,0), -1)
    return(mask)

When I create an object, it is defined as belonging to ImageProcessing Class.

diff = ImageProcessing("diff",1200,1600)
diff = diff.masking()

And so this object called "diff" could not used my others methods, such as masking, defined in my class ImageProcessing because these methods are waiting for scalar or arrays to be used and the type of my object is ImageProcessing one.

I'm lost guys! Need some help on this basic things. How can I defined an object in a Class and giving it the right to be used by array methods (methods which are located in the same class)?

Vince
  • 101
  • 1
  • 3

1 Answers1

0

You're trying to use a function that expects a Numpy array (or scalar) on your new ImageProcessing object. That's like trying to groom a hatstand - it doesn't make sense.

Indeed, the docstring for the function you've written specifies that the input must be a Numpy array or a scalar.

You could try creating a Numpy array as part of your function:

import numpy as np

def masking(self):
    new_numpy_array = np.empty((self.width, self.height))
    mask = cv2.rectangle(new_numpy_array, (0,0), (250,1200), (0,0,0), -1)
    return(mask)

Or, if you're likely to be doing this kind of thing a lot, you could create your Numpy array at the same time as your object:

import numpy as np

def __init__(self, name, height, width):
    """ Defining name, height and width of image processing object """
    self.name = name
    self.height = height
    self.width = width
    self.numpy_repr = np.empty((width, height))

def masking(self):
    mask = cv2.rectangle(self.numpy_repr, (0,0), (250,1200), (0,0,0), -1)
    return(mask)

In either case, diff = diff.masking() won't work in the manner you'd like, since cv2.rectangle(...) returns an array, not an ImageProcessing object.

ACascarino
  • 4,340
  • 1
  • 13
  • 16
  • Thanks ! I got a data type error, I needed to write --> self.numpy_repr = np.empty((width, height)) but now i have another error dur to what you said. The error mentionned numpy.ndarray has no attributes 'mask'. How is the correct way to apply masking to diff so ? – Vince Jun 11 '18 at 13:37
  • Oops. Yep that makes sense, I'll edit that into the answer. – ACascarino Jun 11 '18 at 13:38
  • I could not apply other methods belonging to my Imageprocessing Class to diff, because diff becomes a nd.array after i write diff = diff.masking(). And 'numpy.ndarray' has no attributes 'name of my other method' – Vince Jun 11 '18 at 13:42
  • Exact traceback is 'numpy.ndarray' object has no attribute 'detecting-object' where detetcing object is another method of ImageProcessing. but only because diff becomes a nd.array now and no more an object of ImageProcessing... – Vince Jun 11 '18 at 13:43
  • Yes, that's what I said in the final sentence - you fundamentally can't groom a hatstand, you have to groom the cat instead, but then you can't expect to be able to hang a hat on the cat! Similarly, you can't `rectangle` an `ImageProcessing`, you have to `rectangle` an `ndarray` and then store this rectangle somewhere. You fundamentally can't use `rectangle` on something incompatible, such as an `ImageProcessing`. – ACascarino Jun 11 '18 at 13:44