Exemplo n.º 1
0
def zoneActive(zone_active, framebuffer, iteration):
    critere.max_iter = 5
    hauteur = hauteur_image / pow(2, iteration + 1)
    largeur = largeur_image / pow(2, iteration + 1)
    for i in range(0, len(zone_active)):
        zone = cv.cvRect(zone_active[i].x, zone_active[i].y, largeur, hauteur)
        origine_x_zone = zone_active[i].x
        origine_y_zone = zone_active[i].y
        index = 0
        for x in range(origine_x_zone, origine_x_zone + (largeur * 2),
                       largeur):
            for y in range(origine_y_zone, origine_y_zone + (hauteur * 2),
                           hauteur):
                zone.x = x
                zone.y = y
                trouver = 0
                trouver = cv.cvMeanShift(framebuffer, zone, critere,
                                         cv.CvConnectedComp())
                if trouver != 0:
                    #cv.cvRectangle(frame, cv.cvPoint(x,y), cv.cvPoint((x+largeur), (y+hauteur)), color)
                    if index == 0:
                        zone_active[i] = cv.cvRect(x, y, largeur, hauteur)
                    else:
                        zone_active += [cv.cvRect(x, y, largeur, hauteur)]
                    index = index + 1
    return zone_active
Exemplo n.º 2
0
def depthmatch(x,y,leftimage,rightimage,roi=80,buf=50,baseline=2.7,focal_length=80):
    """depthmatch function
    x,y : (int) pixel position of target in left image
    leftimage, rightimage : (IplImage) stereo images
    roi: (int) region of interest around x,y to use in matching
    buf: (int) buffer outside of a straight horizontal search for a match
    """
    #print "Match",x,y
    info = cv.cvGetSize(leftimage)
    width = info.width
    height = info.height
    centerx = width/2
    centery = height/2
    
    (y1,x1,y2,x2) = (y-roi,x-roi,y+roi,x+roi)
    if y1<0: y1 = 0
    if x1<0: x1 = 0
    if y2>height: y2 = height
    if x2>width: x2 = width
    # copy subregion roi x roi

    template_rect = cv.cvRect(x1,y1,(x2-x1),(y2-y1))
    template = cv.cvGetSubRect(leftimage, template_rect)
    
    #(y3,x3,y4,x4) = (y-roi-buf,x-roi-buf,y+roi+buf,width) # +/- 20 pixels in vertical direction, -20 to the right edge

    (y3,x3,y4,x4) = (y-roi-buf,0,y+roi+buf,x+roi+buf) # +/- buf pixels in vertical direction, +buf to the left edge
    if x3<0: x3 = 0
    if y3<0: y3 = 0
    if x4>=width: x4 = width-1
    if y4>height: y4 = height
    #cv.cvSetImageROI(rightimage, (y3,x3,y4,x4))

    rightsub_rect = cv.cvRect(x3,y3,(x4-x3),(y4-y3))
    rightsub = cv.cvGetSubRect(rightimage, rightsub_rect)
    # result matrix should be (W - w + 1) x (H - h + 1) where WxH are template dimensions, wxh are rightsub dimensions
    W = x4-x3
    H = y4-y3
    w = x2-x1
    h = y2-y1

    resy = (y4-y3)-(y2-y1)+1
    resx = (x4-x3)-(x2-x1)+1

    resultmat = cv.cvCreateImage((resx, resy), 32, 1)
    cv.cvZero(resultmat)
    # match template image in a subportion of rightimage
    cv.cvMatchTemplate(rightsub, template, resultmat, cv.CV_TM_SQDIFF)
    min_val, max_val, min_point, max_point = cv.cvMinMaxLoc(resultmat)
    cv.cvNormalize(resultmat, resultmat, 1, 0, cv.CV_MINMAX)
    depth = plane2point(x-centerx, y-centery, x3+min_point.x+roi-centerx, y3+min_point.y+roi-centery, baseline, focal_length)
    #print "Found match at", min_point.x+x3, min_point.y+y3
    return (depth, (x,y), (x3+min_point.x+roi, y3+min_point.y+roi))
Exemplo n.º 3
0
def on_mouse(event, x, y, flags, param=[]):
    global mouse_selection
    global mouse_origin
    global mouse_select_object
    if event == highgui.CV_EVENT_LBUTTONDOWN:
        print("Mouse down at (%i, %i)" % (x, y))
        mouse_origin = cv.cvPoint(x, y)

        mouse_selection = cv.cvRect(x, y, 0, 0)
        mouse_select_object = True
        return
    if event == highgui.CV_EVENT_LBUTTONUP:
        print("Mouse up at (%i,%i)" % (x, y))
        mouse_select_object = False
        if (mouse_selection.width > 0 and mouse_selection.height > 0):
            global track_object
            track_object = -1
        return
    if mouse_select_object:
        mouse_selection.x = min(x, mouse_origin.x)
        mouse_selection.y = min(y, mouse_origin.y)
        mouse_selection.width = mouse_selection.x + cv.CV_IABS(x -
                                                               mouse_origin.x)
        mouse_selection.height = mouse_selection.y + cv.CV_IABS(y -
                                                                mouse_origin.y)
        mouse_selection.x = max(mouse_selection.x, 0)
        mouse_selection.y = max(mouse_selection.y, 0)
        mouse_selection.width = min(mouse_selection.width, frame.width)
        mouse_selection.height = min(mouse_selection.height, frame.height)
        mouse_selection.width -= mouse_selection.x
        mouse_selection.height -= mouse_selection.y
Exemplo n.º 4
0
def on_mouse(event, x, y, flags, param):

    global select_object, selection, image, origin, select_object, track_object

    if image is None:
        return

    if image.origin:
        y = image.height - y

    if select_object:
        selection.x = min(x,origin.x)
        selection.y = min(y,origin.y)
        selection.width = selection.x + cv.CV_IABS(x - origin.x)
        selection.height = selection.y + cv.CV_IABS(y - origin.y)
        
        selection.x = max( selection.x, 0 )
        selection.y = max( selection.y, 0 )
        selection.width = min( selection.width, image.width )
        selection.height = min( selection.height, image.height )
        selection.width -= selection.x
        selection.height -= selection.y

    if event == highgui.CV_EVENT_LBUTTONDOWN:
        origin = cv.cvPoint(x,y)
        selection = cv.cvRect(x,y,0,0)
        select_object = 1
    elif event == highgui.CV_EVENT_LBUTTONUP:
        select_object = 0
        if( selection.width > 0 and selection.height > 0 ):
            track_object = -1
Exemplo n.º 5
0
def on_mouse( event, x, y, flags, param = [] ):
    global mouse_selection
    global mouse_origin
    global mouse_select_object
    if event == highgui.CV_EVENT_LBUTTONDOWN:
        print("Mouse down at (%i, %i)" % (x,y))
        mouse_origin = cv.cvPoint(x,y)

        mouse_selection = cv.cvRect(x,y,0,0)
        mouse_select_object = True
        return
    if event == highgui.CV_EVENT_LBUTTONUP:
        print("Mouse up at (%i,%i)" % (x,y))
        mouse_select_object = False
        if( mouse_selection.width > 0 and mouse_selection.height > 0 ):
            global track_object
            track_object = -1
        return
    if mouse_select_object:
        mouse_selection.x = min(x,mouse_origin.x)
        mouse_selection.y = min(y,mouse_origin.y)
        mouse_selection.width = mouse_selection.x + cv.CV_IABS(x - mouse_origin.x)
        mouse_selection.height = mouse_selection.y + cv.CV_IABS(y - mouse_origin.y)
        mouse_selection.x = max( mouse_selection.x, 0 )
        mouse_selection.y = max( mouse_selection.y, 0 )
        mouse_selection.width = min( mouse_selection.width, frame.width )
        mouse_selection.height = min( mouse_selection.height, frame.height )
        mouse_selection.width -= mouse_selection.x
        mouse_selection.height -= mouse_selection.y
Exemplo n.º 6
0
def on_mouse(event, x, y, flags, param):

    global select_object, selection, image, origin, select_object, track_object

    if image is None:
        return

    if image.origin:
        y = image.height - y

    if select_object:
        selection.x = min(x, origin.x)
        selection.y = min(y, origin.y)
        selection.width = selection.x + cv.CV_IABS(x - origin.x)
        selection.height = selection.y + cv.CV_IABS(y - origin.y)

        selection.x = max(selection.x, 0)
        selection.y = max(selection.y, 0)
        selection.width = min(selection.width, image.width)
        selection.height = min(selection.height, image.height)
        selection.width -= selection.x
        selection.height -= selection.y

    if event == highgui.CV_EVENT_LBUTTONDOWN:
        origin = cv.cvPoint(x, y)
        selection = cv.cvRect(x, y, 0, 0)
        select_object = 1
    elif event == highgui.CV_EVENT_LBUTTONUP:
        select_object = 0
        if (selection.width > 0 and selection.height > 0):
            track_object = -1
Exemplo n.º 7
0
def zoneActivePremier(zone_active, framebuffer):
    critere.max_iter = 1
    zone_active = []
    zone = cv.cvRect(0, 0, largeur_image / 2, hauteur_image / 2)
    for x in range(0, largeur_image, largeur_image / 2):
        for y in range(0, hauteur_image, hauteur_image / 2):
            zone.x = x
            zone.y = y
            trouver = cv.cvMeanShift(framebuffer, zone, critere,
                                     cv.CvConnectedComp())
            if trouver != 0:
                zone_active += [
                    cv.cvRect(x, y, largeur_image / 2, hauteur_image / 2)
                ]
    #print "zoneActivePremier() a trouver "+str(len(zone_active))+" zone active"
    return zone_active
Exemplo n.º 8
0
    def get_area(self, haar_csd, roi=None, orig=None):
        """
        Gets an area using haarcascades. It is possible to get areas inside areas
        passing the roi rectangle and the origin point.

        Arguments:
        - self: the main object pointer.
        - haar_csd: The haartraining file
        - roi: The roi image coords if needed.
        - orig: The roi's origin if needed.
        """

        if roi is None:
            return self.__camera.get_haar_points(haar_csd)

        roi = cv.cvRect(roi["start"], roi["end"], roi["width"], roi["height"])
        return self.__camera.get_haar_roi_points(haar_csd, roi, orig)
        sys.exit (1)
        
    # create an image to put in the histogram
    histimg = cv.cvCreateImage (cv.cvSize (320,240), 8, 3)

    # init the image of the histogram to black
    cv.cvSetZero (histimg)

    # capture the 1st frame to get some propertie on it
    frame = highgui.cvQueryFrame (capture)

    # get some properties of the frame
    frame_size = cv.cvGetSize (frame)

    # compute which selection of the frame we want to monitor
    selection = cv.cvRect (0, 0, frame.width, frame.height)

    # create some images usefull later
    hue = cv.cvCreateImage (frame_size, 8, 1)
    mask = cv.cvCreateImage (frame_size, 8, 1)
    hsv = cv.cvCreateImage (frame_size, 8, 3 )

    # create the histogram
    hist = cv.cvCreateHist ([hdims], cv.CV_HIST_ARRAY, hranges, 1)

    while 1:
        # do forever

        # 1. capture the current image
        frame = highgui.cvQueryFrame (capture)
        if frame is None:
Exemplo n.º 10
0
	def detect_squares(self, img_grey, img_orig):
		""" Find squares within the video stream and draw them """
		cv.cvClearMemStorage(self.faces_storage)
		N										= 11
		thresh									= 5
		sz										= cv.cvSize(img_grey.width & -2, img_grey.height & -2)
		timg									= cv.cvCloneImage(img_orig)
		pyr										= cv.cvCreateImage(cv.cvSize(sz.width/2, sz.height/2), 8, 3)
		# create empty sequence that will contain points -
		# 4 points per square (the square's vertices)
		squares									= cv.cvCreateSeq(0, cv.sizeof_CvSeq, cv.sizeof_CvPoint, self.squares_storage)
		squares									= cv.CvSeq_CvPoint.cast(squares)

		# select the maximum ROI in the image
		# with the width and height divisible by 2
		subimage								= cv.cvGetSubRect(timg, cv.cvRect(0, 0, sz.width, sz.height))

		cv.cvReleaseImage(timg)

		# down-scale and upscale the image to filter out the noise
		cv.cvPyrDown(subimage, pyr, 7)
		cv.cvPyrUp(pyr, subimage, 7)
		cv.cvReleaseImage(pyr)
		tgrey									= cv.cvCreateImage(sz, 8, 1)
		# find squares in every color plane of the image
		for c in range(3):
			# extract the c-th color plane
			channels							= [None, None, None]
			channels[c]							= tgrey
			cv.cvSplit(subimage, channels[0], channels[1], channels[2], None)
			for l in range(N):
				# hack: use Canny instead of zero threshold level.
				# Canny helps to catch squares with gradient shading
				if(l == 0):
					# apply Canny. Take the upper threshold from slider
					# and set the lower to 0 (which forces edges merging)
					cv.cvCanny(tgrey, img_grey, 0, thresh, 5)
					# dilate canny output to remove potential
					# holes between edge segments
					cv.cvDilate(img_grey, img_grey, None, 1)
				else:
					# apply threshold if l!=0:
					#     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
					cv.cvThreshold(tgrey, img_grey, (l+1)*255/N, 255, cv.CV_THRESH_BINARY)

				# find contours and store them all as a list
				count, contours					= cv.cvFindContours(img_grey,
																	self.squares_storage,
																	cv.sizeof_CvContour,
																	cv.CV_RETR_LIST,
																	cv.CV_CHAIN_APPROX_SIMPLE,
																	cv.cvPoint(0,0))

				if not contours:
					continue

				# test each contour
				for contour in contours.hrange():
					# approximate contour with accuracy proportional
					# to the contour perimeter
					result						= cv.cvApproxPoly(contour,
																	cv.sizeof_CvContour,
																	self.squares_storage,
																	cv.CV_POLY_APPROX_DP,
																	cv.cvContourPerimeter(contours)*0.02, 0)
					# square contours should have 4 vertices after approximation
					# relatively large area (to filter out noisy contours)
					# and be convex.
					# Note: absolute value of an area is used because
					# area may be positive or negative - in accordance with the
					# contour orientation
					if(result.total == 4 and abs(cv.cvContourArea(result)) > 1000 and cv.cvCheckContourConvexity(result)):
						s						= 0
						for i in range(5):
							# find minimum angle between joint
							# edges (maximum of cosine)
							if(i >= 2):
								t				= abs(self.squares_angle(result[i], result[i-2], result[i-1]))
								if s<t:
									s			= t
						# if cosines of all angles are small
						# (all angles are ~90 degree) then write quandrange
						# vertices to resultant sequence
						if(s < 0.3):
							for i in range(4):
								squares.append(result[i])

		cv.cvReleaseImage(tgrey)
		return squares
Exemplo n.º 11
0
        sys.exit(1)

    # create an image to put in the histogram
    histimg = cv.cvCreateImage(cv.cvSize(320, 240), 8, 3)

    # init the image of the histogram to black
    cv.cvSetZero(histimg)

    # capture the 1st frame to get some propertie on it
    frame = highgui.cvQueryFrame(capture)

    # get some properties of the frame
    frame_size = cv.cvGetSize(frame)

    # compute which selection of the frame we want to monitor
    selection = cv.cvRect(0, 0, frame.width, frame.height)

    # create some images usefull later
    hue = cv.cvCreateImage(frame_size, 8, 1)
    mask = cv.cvCreateImage(frame_size, 8, 1)
    hsv = cv.cvCreateImage(frame_size, 8, 3)
    backproject = cv.cvCreateImage(frame_size, 8, 1)

    # create the histogram
    hist = cv.cvCreateHist([hdims], cv.CV_HIST_ARRAY, hranges, 1)
    obj_hist = cv.cvCreateHist([hdims], cv.CV_HIST_ARRAY, hranges, 1)
    while 1:
        # do forever

        # 1. capture the current image
        frame = highgui.cvQueryFrame(capture)
Exemplo n.º 12
0
    def detect_squares(self, img):
        """ Find squares within the video stream and draw them """
        N = 11
        thresh = 5
        sz = cv.cvSize(img.width & -2, img.height & -2)
        timg = cv.cvCloneImage(img)
        gray = cv.cvCreateImage(sz, 8, 1)
        pyr = cv.cvCreateImage(cv.cvSize(sz.width / 2, sz.height / 2), 8, 3)
        # create empty sequence that will contain points -
        # 4 points per square (the square's vertices)
        squares = cv.cvCreateSeq(0, cv.sizeof_CvSeq, cv.sizeof_CvPoint,
                                 self.storage)
        squares = cv.CvSeq_CvPoint.cast(squares)

        # select the maximum ROI in the image
        # with the width and height divisible by 2
        subimage = cv.cvGetSubRect(timg, cv.cvRect(0, 0, sz.width, sz.height))

        # down-scale and upscale the image to filter out the noise
        cv.cvPyrDown(subimage, pyr, 7)
        cv.cvPyrUp(pyr, subimage, 7)
        tgray = cv.cvCreateImage(sz, 8, 1)
        # find squares in every color plane of the image
        for c in range(3):
            # extract the c-th color plane
            channels = [None, None, None]
            channels[c] = tgray
            cv.cvSplit(subimage, channels[0], channels[1], channels[2], None)
            for l in range(N):
                # hack: use Canny instead of zero threshold level.
                # Canny helps to catch squares with gradient shading
                if (l == 0):
                    # apply Canny. Take the upper threshold from slider
                    # and set the lower to 0 (which forces edges merging)
                    cv.cvCanny(tgray, gray, 0, thresh, 5)
                    # dilate canny output to remove potential
                    # holes between edge segments
                    cv.cvDilate(gray, gray, None, 1)
                else:
                    # apply threshold if l!=0:
                    #     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
                    cv.cvThreshold(tgray, gray, (l + 1) * 255 / N, 255,
                                   cv.CV_THRESH_BINARY)

                # find contours and store them all as a list
                count, contours = cv.cvFindContours(gray, self.storage,
                                                    cv.sizeof_CvContour,
                                                    cv.CV_RETR_LIST,
                                                    cv.CV_CHAIN_APPROX_SIMPLE,
                                                    cv.cvPoint(0, 0))

                if not contours:
                    continue

                # test each contour
                for contour in contours.hrange():
                    # approximate contour with accuracy proportional
                    # to the contour perimeter
                    result = cv.cvApproxPoly(
                        contour, cv.sizeof_CvContour, self.storage,
                        cv.CV_POLY_APPROX_DP,
                        cv.cvContourPerimeter(contours) * 0.02, 0)
                    # square contours should have 4 vertices after approximation
                    # relatively large area (to filter out noisy contours)
                    # and be convex.
                    # Note: absolute value of an area is used because
                    # area may be positive or negative - in accordance with the
                    # contour orientation
                    if (result.total == 4
                            and abs(cv.cvContourArea(result)) > 1000
                            and cv.cvCheckContourConvexity(result)):
                        s = 0
                        for i in range(5):
                            # find minimum angle between joint
                            # edges (maximum of cosine)
                            if (i >= 2):
                                t = abs(
                                    self.squares_angle(result[i],
                                                       result[i - 2],
                                                       result[i - 1]))
                                if s < t:
                                    s = t
                        # if cosines of all angles are small
                        # (all angles are ~90 degree) then write quandrange
                        # vertices to resultant sequence
                        if (s < 0.3):
                            for i in range(4):
                                squares.append(result[i])

        i = 0
        while i < squares.total:
            pt = []
            # read 4 vertices
            pt.append(squares[i])
            pt.append(squares[i + 1])
            pt.append(squares[i + 2])
            pt.append(squares[i + 3])

            # draw the square as a closed polyline
            cv.cvPolyLine(img, [pt], 1, cv.CV_RGB(0, 255, 0), 3, cv.CV_AA, 0)
            i += 4

        return img
Exemplo n.º 13
0
def depthmatch(x,y,leftimage,rightimage,roi=20,buf=10,debug=False):
    __doc__ = """depthmatch function
    x,y : (int) pixel position of target in left image
    leftimage, rightimage : (IplImage) stereo images
    roi: (int) region of interest around x,y to use in matching
    buf: (int) buffer outside of a straight horizontal search for a match
    """
    info = cv.cvGetSize(leftimage)
    width = info.width
    height = info.height

    (y1,x1,y2,x2) = (y-roi,x-roi,y+roi,x+roi)
    #template = cv.cvCreateImage((roi*2,roi*2), 8, 3)
    if y1<0: y1 = 0
    if x1<0: x1 = 0
    if y2>height: y2 = height
    if x2>width: x2 = width
    #cv.cvSetZero(template)
    # copy subregion roi x roi

    template_rect = cv.cvRect(x1,y1,(x2-x1),(y2-y1))
    template = cv.cvGetSubRect(leftimage, template_rect)
    (y3,x3,y4,x4) = (y-roi-buf,x-roi-buf,y+roi+buf,width) # +/- 20 pixels in vertical direction, -20 to the right edge
    if x3<0: x3 = 0
    if y3<0: y3 = 0
    if x4>=width: x4 = width-1
    if y4>height: y4 = height
    #cv.cvSetImageROI(rightimage, (y3,x3,y4,x4))

    rightsub_rect = cv.cvRect(x3,y3,(x4-x3),(y4-y3))
    rightsub = cv.cvGetSubRect(rightimage, rightsub_rect)
    # result matrix should be (W - w + 1) x (H - h + 1) where WxH are template dimensions, wxh are rightsub dimensions
    W = x4-x3
    H = y4-y3
    w = x2-x1
    h = y2-y1

    resy = (y4-y3)-(y2-y1)+1
    resx = (x4-x3)-(x2-x1)+1

    resultmat = cv.cvCreateImage((resx, resy), 32, 1)
    cv.cvZero(resultmat)
    # match template image in a subportion of rightimage
    cv.cvMatchTemplate(rightsub, template, resultmat, cv.CV_TM_SQDIFF)
    min_val, max_val, min_point, max_point = cv.cvMinMaxLoc(resultmat)
    cv.cvNormalize(resultmat, resultmat, 1, 0, cv.CV_MINMAX)
    depth = stereo.depth(x, x3+min_point.x, max_pixels=width/2)
    
    if debug:
        print "Input image: %ix%i, target: (%i,%i)" % (width,height,x,y)
        print "Template box: (%i,%i) to (%i,%i)" % (x1, y1, x2, y2)
        print "Search area: (%i,%i) to (%i,%i)" % (x3, y3, x4, y4)
        print "%ix%i, %ix%i" % (W,H,w,h)
        print "Result matrix %ix%i" % (resx, resy)
        print "stereo.depth(%i,%i,max_pixels=%i)" % (x, min_point.x+x3,width/2)
        if depth[0]:
            print "Depth: ", depth[0], "(cm)"
        #cv.cvRectangle(rightimage, cv.cvPoint(x1,y1), cv.cvPoint(x2,y2), (255,0,0))
        cv.cvRectangle(rightimage, cv.cvPoint(min_point.x+x3,min_point.y+y3), cv.cvPoint(min_point.x+x3+roi*2,min_point.y+y3+roi*2), (0,255,0))
        cv.cvRectangle(rightimage, cv.cvPoint(x3,y3), cv.cvPoint(x4,y4), (0,0,255))
        cv.cvRectangle(leftimage, cv.cvPoint(x1,y1), cv.cvPoint(x2,y2), (255,0,0))
        #cv.cvRectangle(leftimage, cv.cvPoint(min_point.x+x3,min_point.y+y3), cv.cvPoint(min_point.x+x3+roi*2,min_point.y+y3+roi*2), (0,255,0))
        cv.cvRectangle(leftimage, cv.cvPoint(x3,y3), cv.cvPoint(x4,y4), (0,0,255))
        if depth[0]:
            cv.cvPutText(leftimage, "%5f(cm)" % depth[0], (x1,y1), font, (255,255,255))
        highgui.cvShowImage("depthmatch - template", template)
        highgui.cvShowImage("depthmatch - match", resultmat)
        highgui.cvShowImage("depthmatch - right", rightimage)
        highgui.cvShowImage("depthmatch - left", leftimage)
Exemplo n.º 14
0
	def getBoundingBox(self):
		return cv.cvRect(self.x, self.y, self.width, self.height)
Exemplo n.º 15
0
def compute_saliency(image):
    global thresh
    global scale
    saliency_scale = int(math.pow(2,scale));
    bw_im1 = cv.cvCreateImage(cv.cvGetSize(image), cv.IPL_DEPTH_8U,1)
    cv.cvCvtColor(image, bw_im1, cv.CV_BGR2GRAY)
    bw_im = cv.cvCreateImage(cv.cvSize(saliency_scale,saliency_scale), cv.IPL_DEPTH_8U,1)
    cv.cvResize(bw_im1, bw_im)
    highgui.cvShowImage("BW", bw_im)
    realInput = cv.cvCreateImage( cv.cvGetSize(bw_im), cv.IPL_DEPTH_32F, 1);
    imaginaryInput = cv.cvCreateImage( cv.cvGetSize(bw_im), cv.IPL_DEPTH_32F, 1);
    complexInput = cv.cvCreateImage( cv.cvGetSize(bw_im), cv.IPL_DEPTH_32F, 2);

    cv.cvScale(bw_im, realInput, 1.0, 0.0);
    cv.cvZero(imaginaryInput);
    cv.cvMerge(realInput, imaginaryInput, None, None, complexInput);

    dft_M = saliency_scale #cv.cvGetOptimalDFTSize( bw_im.height - 1 );
    dft_N = saliency_scale #cv.cvGetOptimalDFTSize( bw_im.width - 1 );

    dft_A = cv.cvCreateMat( dft_M, dft_N, cv.CV_32FC2 );
    image_Re = cv.cvCreateImage( cv.cvSize(dft_N, dft_M), cv.IPL_DEPTH_32F, 1);
    image_Im = cv.cvCreateImage( cv.cvSize(dft_N, dft_M), cv.IPL_DEPTH_32F, 1);

    # copy A to dft_A and pad dft_A with zeros
    tmp = cv.cvGetSubRect( dft_A, cv.cvRect(0,0, bw_im.width, bw_im.height));
    cv.cvCopy( complexInput, tmp, None );
    if(dft_A.width > bw_im.width):
        tmp = cv.cvGetSubRect( dft_A, cv.cvRect(bw_im.width,0, dft_N - bw_im.width, bw_im.height));
        cv.cvZero( tmp );
    
    cv.cvDFT( dft_A, dft_A, cv.CV_DXT_FORWARD, complexInput.height );
    cv.cvSplit( dft_A, image_Re, image_Im, None, None );
    
    # Compute the phase angle 
    image_Mag = cv.cvCreateImage(cv.cvSize(dft_N, dft_M), cv.IPL_DEPTH_32F, 1);
    image_Phase = cv.cvCreateImage(cv.cvSize(dft_N, dft_M), cv.IPL_DEPTH_32F, 1);
    

    #compute the phase of the spectrum
    cv.cvCartToPolar(image_Re, image_Im, image_Mag, image_Phase, 0)

    log_mag = cv.cvCreateImage(cv.cvSize(dft_N, dft_M), cv.IPL_DEPTH_32F, 1);
    cv.cvLog(image_Mag, log_mag)
    #Box filter the magnitude, then take the difference
    image_Mag_Filt = cv.cvCreateImage(cv.cvSize(dft_N, dft_M), cv.IPL_DEPTH_32F, 1);
    filt = cv.cvCreateMat(3,3, cv.CV_32FC1);
    cv.cvSet(filt,cv.cvScalarAll(-1.0/9.0))
    cv.cvFilter2D(log_mag, image_Mag_Filt, filt, cv.cvPoint(-1,-1))

    cv.cvAdd(log_mag, image_Mag_Filt, log_mag, None)
    cv.cvExp(log_mag, log_mag)
    cv.cvPolarToCart(log_mag, image_Phase, image_Re, image_Im,0);

    cv.cvMerge(image_Re, image_Im, None, None, dft_A)
    cv.cvDFT( dft_A, dft_A, cv.CV_DXT_INVERSE, complexInput.height)
            
    tmp = cv.cvGetSubRect( dft_A, cv.cvRect(0,0, bw_im.width, bw_im.height));
    cv.cvCopy( tmp, complexInput, None );
    cv.cvSplit(complexInput, realInput, imaginaryInput, None, None)
    min, max = cv.cvMinMaxLoc(realInput);
    #cv.cvScale(realInput, realInput, 1.0/(max-min), 1.0*(-min)/(max-min));
    cv.cvSmooth(realInput, realInput);
    threshold = thresh/100.0*cv.cvAvg(realInput)[0]
    cv.cvThreshold(realInput, realInput, threshold, 1.0, cv.CV_THRESH_BINARY)
    tmp_img = cv.cvCreateImage(cv.cvGetSize(bw_im1),cv.IPL_DEPTH_32F, 1)
    cv.cvResize(realInput,tmp_img)
    cv.cvScale(tmp_img, bw_im1, 255,0)
    return bw_im1