开发者

Python, inter-class relationships and object dependents

Edit

Apologies for previous generality!

I hereby try to be clearer. I am testing an image, so I am to write a number of methods which will run to analyse the image. Not all images tested will be the same, and so I want an object for each image. Many of the methods I am to write don't run on all of the image. For example, I have a method which returns the distance of an array point from the centre of the image, using a pixel spacing constant which is an attribute of the image. Here I have an example, I read an image and thereby create the object image, and some attributes which will be useful to me. But my point objects, which "belong" in that image, wont have access to those attributes if they have a class of their own; it is useful to have a point class, because other analysis also uses points, and uses the same methods.

    class image(object):
    def __init__(self, filepath)
    #creates an instance of the image from filepath
        xscale = self.read_header(xspacing)
        xscale = self.read_header(yspacing) #pixels per mm property of image

    class point(object):
    def __init__(slef, i,j)
    self.i = i # array position in image.
    self.j = j
    self.x = self.i * xscale #real position in mm in image
    self.y = self.i * yscale ##but xscale and y scale are not in scope here.

So the question is... how do I give point objects attributes like distance that depend on image attribute like xscale and yscale. In my mind i want image.point.x to be allowed, where image.point.x is depended to image.xscale. The slight complication, which I think we can probably ignore for now, is that a point should not be allowed to exist outside of the context of an image, because it wont mean anything to have the point with out the image attributes, like scale. I hope that is clearer, and generally more useful. Thank you.

Origional question.

I'd like to have an open image. The image should be an object, as plenty of methods are associated with it.. e开发者_JAVA技巧.g. reading header info and giving the object lots of attributes like title, sizex, sizey...

Then the images are made up of points, obviously = so I thought I'd have a points class. I could then define a Point object, and it could have attributes, like position (depending on some pixel spacing constants from the image header). And this class would have lots of point methods, like distance, change value, move, scale, polt between two points.... etc

My problem is two fold. The creation of point objects should really depend on the existance of the image object. I don't know how python handles this kind of conditional creation of objects. And second, The point object must be aware of the image object that it is relavent to. Ideally, I would have Image.Point(x,y) and that would mean that the point x,y exists within the image, and so it could have access to all the varialbes created when the image object was initialised. I hope all that makes sense. I have tried super, but it doesn't seem to do the conditional bit, and it runs a method in the base class, where I really just want to have some variables availalbe.

Thanks for any help.


so you want image to have a method that returns points in that image (a point factory) like:

class Image:
    #...
    def point_at(self, x,y):
        return PointInImage(self, ...)
    #...

class PointInImage:
    #...
    def __init__(self, image, ...):
        self.image = image
        #...
    #...


You could do it like this:

class Point(object):
    def __init__(self, image):
        self._image = image

class Image(object):
    def __init__(self, width, height):
        self._points = [Point(self) for unused in range(width * height)]

This way, each point requires an image on initialization, and each image has a list of points associated to it. Note that, since you would create a lot of Point-instances, which could be very memory consuming, you should consider using slots for Point.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜