Saturday, September 26, 2009

Python - sun image detector - outline objects in an image


The input:
Where oh where is the sun?


from PIL import Image
 
# find brightest region of image 
# and visually identify the region 
 
class TheOutliner(object):
    def __init__(self):
        self.outlineColor = 0, 255, 255
        self.pic = None
        self.picn = None
        self.minX = 0
        self.minY = 0
        self.maxX = 0
        self.maxY = 0
    def doEverything(self, imgPath, dictPoints, theoutfile):
        self.loadImage(imgPath)
        self.loadBrightPoints(dictPoints)
        self.drawBox()
        self.saveImg(theoutfile)
    def loadImage(self, imgPath):
        self.pic = Image.open(imgPath)
        self.picn = self.pic.load()
    def loadBrightPoints(self, dictPoints):
        # iterate through all points and 
        #   gather max/min x/y 
 
 
        # an x from the pool (the max/min 
        #   must be from dictPoints) 
        self.minX = dictPoints.keys()[0][0]
        self.maxX = self.minX
        self.minY = dictPoints.keys()[0][1]
        self.maxY = self.minY
 
 
        for point in dictPoints.keys():
            if point[0] < self.minX:
                self.minX = point[0]
            elif point[0] > self.maxX:
                self.maxX = point[0]
 
            if point[1] < self.minY:
                self.minY = point[1]
            elif point[1] > self.maxY:
                self.maxY = point[1]
    def drawBox(self):
        # drop box around bright points 
        for x in xrange(self.minX, self.maxX):
            # top bar 
            self.picn[x, self.minY] = self.outlineColor
            # bottom bar 
            self.picn[x, self.maxY] = self.outlineColor
        for y in xrange(self.minY, self.maxY):
            # left bar 
            self.picn[self.minX, y] = self.outlineColor
            # right bar 
            self.picn[self.maxX, y] = self.outlineColor
    def saveImg(self, theoutfile):
        self.pic.save(theoutfile, "JPEG")
 
 
 
class CollectBrightPoints(object):
    def __init__(self):
        self.brightThreshold = 240, 240, 240
        self.pic = None
        self.picn = None
        self.brightDict = {}
    def loadImage(self, imgPath):
        self.pic = Image.open(imgPath)
        self.picn = self.pic.load()
    def collectBrightPoints(self):
        for x in xrange(self.pic.size[0]):
            for y in xrange(self.pic.size[1]):
                r,g,b = self.picn[x,y]
                if r>self.brightThreshold[0] and \
                    g > self.brightThreshold[1] and \
                    b > self.brightThreshold[2]:
                    # then it is brighter than our threshold 
                    self.brightDict[x,y] = r,g,b
 
 
if __name__ == "__main__":
    print "Start Process";
 
    # assumes that the test.jpg is in the 
    #   working directory  
    theFile = "four.jpg" 
    theOutFile = "four.output.jpg" 
 
    cbp = CollectBrightPoints()
    cbp.loadImage(theFile)
    cbp.collectBrightPoints()
    brightDict = cbp.brightDict
 
    drawer = TheOutliner()
    drawer.doEverything(theFile, brightDict, theOutFile)
    print "Process complete" 
 
The output:
The sun has been detected!



No comments:

Post a Comment