Exemple #1
0
 def save(self, filename, cropFlag = False):
     """save cropped and rotated image"""
     if not cropFlag:
         cv.SaveImage(filename +'-vectors.png',self.render())
     else:
         tmp = rotate(self.cImage, self.center, self.cardinalOffset +\
                 self.north)
         #mask horizon
         mask = cv.CreateImage(self.res, 8, 1)
         cv.Zero(mask)
         cv.Circle(mask, self.center, self.radius, (255,255,255))
         cv.FloodFill(mask,(1,1),(0,0,0))
         cv.FloodFill(mask, self.center,
                 (255,255,255),lo_diff=cv.RealScalar(5))
         masked = cv.CloneImage(tmp)
         cv.Zero(masked)
         cv.Copy(tmp, masked, mask)
         cv.SaveImage(filename +'-cropped.png',crop(masked, self))
     #CSV output
     array = magnitudeToTheta(self.polarArray,self.radius)
     f = open(filename + '.csv', 'w')
     f.write('00\n')
     f.write(','.join([str(v[0]) for v in array]))
     f.write('\n')
     f.write(','.join([str(v[1]) for v in array]))
     f.write('\n')
     f.flush()
     f.close()
Exemple #2
0
def removeBadBackground(seg):
    threshUp = cv.CreateImage(cv.GetSize(seg), cv.IPL_DEPTH_8U, 1)
    comparison = cv.CreateImage(cv.GetSize(seg), cv.IPL_DEPTH_8U, 1)
    visitMask = cv.CreateImage(cv.GetSize(seg), cv.IPL_DEPTH_8U, 1)
    ffMask = cv.CreateImage((seg.width + 2, seg.height + 2), cv.IPL_DEPTH_8U,
                            1)
    cv.Threshold(seg, threshUp, 1, 255, cv.CV_THRESH_BINARY)
    cv.Zero(visitMask)
    cv.Zero(ffMask)
    for x in xrange(seg.width):
        for y in xrange(seg.height):
            if seg[y, x] != 96 or visitMask[y, x] == 255: continue
            comp = cv.FloodFill(threshUp, (x, y), 0, 0, 0,
                                4 + cv.CV_FLOODFILL_MASK_ONLY + (255 << 8),
                                ffMask)
            rect = comp[2]
            cv.SetImageROI(ffMask, cap.shiftRect(rect, 1, 1))
            cv.OrS(ffMask, 1, ffMask)
            cv.SetImageROI(seg, rect)
            cv.SetImageROI(comparison, rect)
            cv.Cmp(
                seg, ffMask, comparison,
                cv.CV_CMP_EQ)  # 'comparison' does not need to be zeroed later
            intersect = cv.CountNonZero(comparison)
            cv.SetImageROI(visitMask, rect)
            cv.Or(visitMask, ffMask, visitMask)
            cv.ResetImageROI(visitMask)
            if intersect == 0:
                cv.Set(seg, 0, ffMask)
            cv.Zero(ffMask)
            cv.ResetImageROI(seg)
            cv.ResetImageROI(ffMask)
    return seg
def main():
    root = "/Users/soswow/Documents/Face Detection/test/negative"
    #    root = "/Users/soswow/Documents/Face Detection/test/sets/negative"
    #    root = "/Users/soswow/Documents/Face Detection/test/edge_view/positive"
    #    root = "/Users/soswow/Documents/Face Detection/test/sobel/positive"
    #    root = "/Users/soswow/Documents/Face Detection/test/sets/positive"
    #    root = "/Users/soswow/Documents/Face Detection/test/falses"

    for folder in os.listdir(root):
        path = p.join(root, folder)
        if p.isdir(path):
            sum = cv.CreateMat(32, 32, cv.CV_32F)
            cv.Zero(sum)
            k = 0
            for path, _ in directory_files(path):
                try:
                    img = cv.LoadImage(path, iscolor=False)
                except IOError:
                    continue
                mat = cv.CreateMat(32, 32, cv.CV_32F)
                cv.Zero(mat)
                cv.Convert(cv.GetMat(img), mat)
                cv.Add(mat, sum, sum)
                k += 1
            avg = cv.CreateMat(32, 32, cv.CV_32F)
            cv.Zero(avg)
            count = cv.CreateMat(32, 32, cv.CV_32F)
            cv.Zero(count)
            cv.Set(count, k)
            cv.Div(sum, count, avg)
            new_img = cv.CreateImage((32, 32), 8, 0)
            cv.Zero(new_img)
            cv.Convert(avg, new_img)
            cv.SaveImage(p.join(root, "%s-avg.png" % folder), new_img)
Exemple #4
0
 def get_motion_mask(self, img, diff_threshold=30):
     self.timestamp = time.clock(
     ) / self.CLOCKS_PER_SEC  # get current time in seconds
     size = cv.GetSize(img)  # get current frame size
     idx1 = self.last
     if not self.mhi or cv.GetSize(self.mhi) != size:
         for i in range(self.N):
             self.buf[i] = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1)
             cv.Zero(self.buf[i])
         self.mhi = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
         cv.Zero(self.mhi)  # clear MHI at the beginning
         self.orient = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
         self.segmask = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
         self.mask = cv.CreateImage(size, 8, 1)
         self.test = cv.CreateImage(size, 8, 3)
     cv.CvtColor(img, self.buf[self.last],
                 cv.CV_BGR2GRAY)  # convert frame to grayscale
     #self.buf[self.last] = cv.CloneImage(img)
     idx2 = (self.last + 1) % self.N  # index of (last - (N-1))th frame
     self.last = idx2
     self.silh = self.buf[idx2]
     cv.AbsDiff(self.buf[idx1], self.buf[idx2],
                self.silh)  # get difference between frames
     cv.Threshold(self.silh, self.silh, diff_threshold, 1,
                  cv.CV_THRESH_BINARY)  # and threshold it
     cv.UpdateMotionHistory(self.silh, self.mhi, self.timestamp,
                            self.MHI_DURATION)  # update MHI
     cv.CvtScale(self.mhi, self.mask, 255. / self.MHI_DURATION,
                 (self.MHI_DURATION - self.timestamp) * 255. /
                 self.MHI_DURATION)
     #cv.ShowImage("motion mask", self.mask)
     max_rect = self.segment_motion()
     return self.mask
Exemple #5
0
    def segment_motion(self):
        min_area = 100
        size = cv.GetSize(self.mask)  # get current frame size
        temp = cv.CloneImage(self.mask)
        cv.CalcMotionGradient(self.mhi, temp, self.orient, self.MAX_TIME_DELTA,
                              self.MIN_TIME_DELTA, 3)
        #cv.ShowImage("orient", self.orient);
        if not self.storage:
            self.storage = cv.CreateMemStorage(0)
        seq = cv.SegmentMotion(self.mhi, self.segmask, self.storage,
                               self.timestamp, self.MAX_TIME_DELTA)
        #cv.ShowImage("segmask", self.segmask)

        max = 0
        max_idx = -1
        for i in range(len(seq)):
            (area, value, comp_rect) = seq[i]
            if area > max:
                max = area
                max_idx = i
        if max_idx == -1:
            cv.Zero(self.mask)
            return

        (area, value, comp_rect) = seq[max_idx]
        if (area < 100):
            cv.Zero(self.mask)
            return

        cv.Zero(self.mask)
        cv.Rectangle(
            self.mask, (comp_rect[0], comp_rect[1]),
            (comp_rect[0] + comp_rect[2], comp_rect[1] + comp_rect[3]),
            (255, 255, 255), cv.CV_FILLED)
Exemple #6
0
def homografia(real_width, real_height, width, height):
    global points, teclado    
    if len(points)!=4 :
        raise Exception('falto algun punto')
 
    p = cv.CreateMat(2,4,cv.CV_64FC1)
    h = cv.CreateMat(2,4,cv.CV_64FC1)
    p2h = cv.CreateMat(3,3,cv.CV_64FC1)
 
    cv.Zero(p)
    cv.Zero(h)
    cv.Zero(p2h)
 
    for i in range(4):
        (x,y) = points[i]
        p[0,i] = (float(real_width)/float(width)) * x
        p[1,i] = (float(real_height)/float(height)) * y
 
    h[0,0] = 0
    h[1,0] = real_height
 
    h[0,1] = real_width
    h[1,1] = real_height
 
    h[0,2] = real_width
    h[1,2] = 0
 
    h[0,3] = 0
    h[1,3] = 0
 
    cv.FindHomography(p,h,p2h)
    return p2h
def findCCs(image, erasecol=0, doContinue=None, doSkip=None, bRange=0, connectivity=8):
    """
    Finds all connected components in the image.
    doContinue is a function applied to the color of every new pixel in the image.
    If it is true, this pixel is ignored. Default: <= 128
    doSkip is a function applied to every new connected component found by the
    function. If it is true, this component will not be included in the result.
    Default: do not skip anything.
    """
    if doContinue is None:
        doContinue = lambda col: col <= 128
    if doSkip is None:
        doSkip = lambda comp: False
    mask = cv.CreateImage((image.width + 2, image.height + 2), cv.IPL_DEPTH_8U, 1)
    cv.Zero(mask)
    components = []
    for x in range(image.width):
        for y in range(image.height):
            if doContinue(image[y, x]):
                continue
            comp = cv.FloodFill(image, (x, y), 0, bRange, bRange, connectivity + cv.CV_FLOODFILL_MASK_ONLY + (255 << 8), mask) # here 3rd argument is ignored
            region = shiftRect(comp[2], 1, 1)
            if not doSkip(comp):
                seg = cvext.getSubImage(mask, region)
                components.append((comp[0], comp[1], comp[2], seg))
            cv.SetImageROI(image, comp[2])
            cv.SetImageROI(mask, region)
            cv.Set(image, erasecol, mask)
            cv.Zero(mask)
            cv.ResetImageROI(image)
            cv.ResetImageROI(mask)
    return components
Exemple #8
0
def main(argv):
    if len(argv) < 10:
        print('Usage: %s input-file fx fy cx cy k1 k2 p1 p2 output-file' %
              argv[0])
        sys.exit(-1)

    src = argv[1]
    fx, fy, cx, cy, k1, k2, p1, p2, output = argv[2:]

    intrinsics = cv.CreateMat(3, 3, cv.CV_64FC1)
    cv.Zero(intrinsics)
    intrinsics[0, 0] = float(fx)
    intrinsics[1, 1] = float(fy)
    intrinsics[2, 2] = 1.0
    intrinsics[0, 2] = float(cx)
    intrinsics[1, 2] = float(cy)

    dist_coeffs = cv.CreateMat(1, 4, cv.CV_64FC1)
    cv.Zero(dist_coeffs)
    dist_coeffs[0, 0] = float(k1)
    dist_coeffs[0, 1] = float(k2)
    dist_coeffs[0, 2] = float(p1)
    dist_coeffs[0, 3] = float(p2)

    src = cv.LoadImage(src)
    dst = cv.CreateImage(cv.GetSize(src), src.depth, src.nChannels)
    mapx = cv.CreateImage(cv.GetSize(src), cv.IPL_DEPTH_32F, 1)
    mapy = cv.CreateImage(cv.GetSize(src), cv.IPL_DEPTH_32F, 1)
    cv.InitUndistortMap(intrinsics, dist_coeffs, mapx, mapy)
    cv.Remap(src, dst, mapx, mapy,
             cv.CV_INTER_LINEAR + cv.CV_WARP_FILL_OUTLIERS, cv.ScalarAll(0))
    # cv.Undistort2(src, dst, intrinsics, dist_coeffs)

    cv.SaveImage(output, dst)
Exemple #9
0
def repaintCCs(image, doRepaint=None, returnMask=False, resizeMask=True, doFillBackground=True, bgPoint=(0, 0), newcol=255, connectivity=4):
    if doRepaint is None:
        doRepaint = lambda comp, col: False
    resultMask = cv.CreateImage((image.width + 2, image.height + 2), image.depth, image.nChannels)
    tempMask = cv.CreateImage((image.width + 2, image.height + 2), image.depth, image.nChannels)
    visitMask = cv.CreateImage((image.width + 2, image.height + 2), image.depth, image.nChannels)
    cv.Zero(resultMask)
    cv.Zero(tempMask)
    cv.Zero(visitMask)
    if doFillBackground:
        cv.FloodFill(image, bgPoint, 0, 0, 0, connectivity + cv.CV_FLOODFILL_MASK_ONLY + (255 << 8), visitMask)
    for x in xrange(image.width):
        for y in xrange(image.height):
            if visitMask[y + 1, x + 1] == 255:
                continue
            comp = cv.FloodFill(image, (x, y), 0, 0, 0, connectivity + cv.CV_FLOODFILL_MASK_ONLY + (255 << 8), tempMask)
            region = shiftRect(comp[2], 1, 1)
            cv.SetImageROI(tempMask, region)
            cv.SetImageROI(visitMask, region)
            cv.Or(tempMask, visitMask, visitMask)
            if doRepaint(comp, image[y, x]):
                cv.SetImageROI(resultMask, region)
                cv.Or(tempMask, resultMask, resultMask)
                cv.ResetImageROI(resultMask)
            cv.Zero(tempMask)
            cv.ResetImageROI(tempMask)
            cv.ResetImageROI(visitMask)
    if returnMask:
        if resizeMask: return cap.getSubImage(resultMask, (1, 1, image.width, image.height))
        else: return resultMask
    else:    
        cv.SetImageROI(resultMask, (1, 1, image.width, image.height))
        cv.Set(image, newcol, resultMask)
        return image
Exemple #10
0
def update_mhi(img, dst, diff_threshold):
    global last
    global mhi
    global mask
    timestamp = time.clock() / CLOCKS_PER_SEC  # get current time in seconds
    size = cv.GetSize(img)  # get current frame size
    idx1 = last
    if not mhi or cv.GetSize(mhi) != size:
        for i in range(N):
            buf[i] = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1)
            cv.Zero(buf[i])
        mhi = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
        cv.Zero(mhi)  # clear MHI at the beginning
        mask = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1)

    cv.CvtColor(img, buf[last], cv.CV_BGR2GRAY)  # convert frame to grayscale
    idx2 = (last + 1) % N  # index of (last - (N-1))th frame
    last = idx2
    silh = buf[idx2]
    cv.AbsDiff(buf[idx1], buf[idx2], silh)  # get difference between frames
    cv.Threshold(silh, silh, diff_threshold, 1,
                 cv.CV_THRESH_BINARY)  # and threshold it
    cv.UpdateMotionHistory(silh, mhi, timestamp, MHI_DURATION)  # update MHI
    cv.CvtScale(mhi, mask, 255. / MHI_DURATION,
                (MHI_DURATION - timestamp) * 255. / MHI_DURATION)
    cv.Zero(dst)
    cv.Merge(mask, None, None, None, dst)
Exemple #11
0
def camera():
	print "# Starting initialization..."
	#camera capture
	#cap = cv.CaptureFromCAM(0)
	intrinsics = cv.CreateMat(3, 3, cv.CV_64FC1)
	cv.Zero(intrinsics)
	#camera data
	intrinsics[0, 0] = 1100.850708957251072
	intrinsics[1, 1] = 778.955239997982062 
	intrinsics[2, 2] = 1.0
	intrinsics[0, 2] = 348.898495232253822
	intrinsics[1, 2] = 320.213734835526282
	dist_coeffs = cv.CreateMat(1, 4, cv.CV_64FC1)
	cv.Zero(dist_coeffs)
	dist_coeffs[0, 0] = -0.326795877008420
	dist_coeffs[0, 1] = 0.139445565548056
	dist_coeffs[0, 2] = 0.001245710462327
	dist_coeffs[0, 3] = -0.001396618726445
	#pFrame = cv.QueryFrame(cap)
	print "# intrinsics loaded!"

	#prepare memory
	capture = cv.CaptureFromCAM(0)
	src = cv.QueryFrame(capture)
	size = GetSize(src)
	dst0 = cv.CreateImage(size, src.depth, src.nChannels)
	# bg = cv.LoadImage("00000005.jpg")
	image_ROI = (0,70,640,340)
	size = (640,340)

	red = cv.CreateImage(size, 8, 1)
	green = cv.CreateImage(size, 8, 1)
	blue = cv.CreateImage(size, 8, 1)

	hue = cv.CreateImage(size, 8, 1)
	sat = cv.CreateImage(size, 8, 1)
	val = cv.CreateImage(size, 8, 1)
	ball = cv.CreateImage(size, 8, 1)
	yellow = cv.CreateImage(size, 8, 1)

	ballx = 0
	bally = 0

	ballmiss = 0
	yellowmiss = 0
	bluemiss = 0

	dst2 = cv.CreateImage(size, 8, 3)
	hsv = cv.CreateImage(size,8,3)
	print "# base images created..."
#####------------------ajustment data---------------------###############
#shadow
	high = 40
	low = 300

#threshold
	thresBallInit = 116
	thresYellowInit = 94
	thresBlueInit = 18
	ballRangeInit = 8.0
def update_mhi(img, dst, diff_threshold):
    global last
    global mhi
    global storage
    global mask
    global orient
    global segmask
    timestamp = time.clock() / CLOCKS_PER_SEC # get current time in seconds
    size = cv.GetSize(img) # get current frame size
    idx1 = last
    if not mhi or cv.GetSize(mhi) != size:
        for i in range(N):
            buf[i] = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1)
            cv.Zero(buf[i])
        mhi = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1)
        cv.Zero(mhi) # clear MHI at the beginning
        orient = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1)
        segmask = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1)
        mask = cv.CreateImage(size,cv. IPL_DEPTH_8U, 1)
    
    cv.CvtColor(img, buf[last], cv.CV_BGR2GRAY) # convert frame to grayscale
    idx2 = (last + 1) % N # index of (last - (N-1))th frame
    last = idx2
    silh = buf[idx2]
    cv.AbsDiff(buf[idx1], buf[idx2], silh) # get difference between frames
    cv.Threshold(silh, silh, diff_threshold, 1, cv.CV_THRESH_BINARY) # and threshold it
    cv.UpdateMotionHistory(silh, mhi, timestamp, MHI_DURATION) # update MHI
    cv.CvtScale(mhi, mask, 255./MHI_DURATION,
                (MHI_DURATION - timestamp)*255./MHI_DURATION)
    cv.Zero(dst)
    cv.Merge(mask, None, None, None, dst)
    cv.CalcMotionGradient(mhi, mask, orient, MAX_TIME_DELTA, MIN_TIME_DELTA, 3)
    if not storage:
        storage = cv.CreateMemStorage(0)
    seq = cv.SegmentMotion(mhi, segmask, storage, timestamp, MAX_TIME_DELTA)
    for (area, value, comp_rect) in seq:
        if comp_rect[2] + comp_rect[3] > 100: # reject very small components
            color = cv.CV_RGB(255, 0,0)
            silh_roi = cv.GetSubRect(silh, comp_rect)
            mhi_roi = cv.GetSubRect(mhi, comp_rect)
            orient_roi = cv.GetSubRect(orient, comp_rect)
            mask_roi = cv.GetSubRect(mask, comp_rect)
            angle = 360 - cv.CalcGlobalOrientation(orient_roi, mask_roi, mhi_roi, timestamp, MHI_DURATION)

            count = cv.Norm(silh_roi, None, cv.CV_L1, None) # calculate number of points within silhouette ROI
            if count < (comp_rect[2] * comp_rect[3] * 0.05):
                continue

            magnitude = 30.
            center = ((comp_rect[0] + comp_rect[2] / 2), (comp_rect[1] + comp_rect[3] / 2))
            cv.Circle(dst, center, cv.Round(magnitude*1.2), color, 3, cv.CV_AA, 0)
            cv.Line(dst,
                    center,
                    (cv.Round(center[0] + magnitude * cos(angle * cv.CV_PI / 180)),
                     cv.Round(center[1] - magnitude * sin(angle * cv.CV_PI / 180))),
                    color,
                    3,
                    cv.CV_AA,
                    0)
Exemple #13
0
    def compare_imgs(self, img1, img2):
        back_proj_img, hist1 = self.back_project_hs(img1)
        back_proj_img2, hist2 = self.back_project_hs(img2)

        scratch = cv.CreateImage(cv.GetSize(back_proj_img2), 8, 1)
        scratch2 = cv.CreateImage(cv.GetSize(back_proj_img2), 8, 1)
        cv.Zero(scratch)
        cv.Zero(scratch2)

        #cv.Sub(back_proj_img, back_proj_img2, scratch2) #opposite noise, but excludes object 
        cv.Sub(back_proj_img2, back_proj_img, scratch2) #noise, but includes object if failed, 
        cv.Sub(scratch2, ha.avg_noise, scratch)
        
        return scratch
    def process_image(self, slider_pos):
        """
        This function finds contours, draws them and their approximation by ellipses.
        """
        stor = cv.CreateMemStorage()

        # Create the destination images
        image02 = cv.CloneImage(self.source_image)
        cv.Zero(image02)
        image04 = cv.CreateImage(cv.GetSize(self.source_image),
                                 cv.IPL_DEPTH_8U, 3)
        cv.Zero(image04)

        # Threshold the source image. This needful for cv.FindContours().
        cv.Threshold(self.source_image, image02, slider_pos, 255,
                     cv.CV_THRESH_BINARY)

        # Find all contours.
        cont = cv.FindContours(image02, stor, cv.CV_RETR_LIST,
                               cv.CV_CHAIN_APPROX_NONE, (0, 0))

        for c in contour_iterator(cont):
            # Number of points must be more than or equal to 6 for cv.FitEllipse2
            if len(c) >= 6:
                # Copy the contour into an array of (x,y)s
                PointArray2D32f = cv.CreateMat(1, len(c), cv.CV_32FC2)
                for (i, (x, y)) in enumerate(c):
                    PointArray2D32f[0, i] = (x, y)

                # Draw the current contour in gray
                gray = cv.CV_RGB(100, 100, 100)
                cv.DrawContours(image04, c, gray, gray, 0, 1, 8, (0, 0))

                # Fits ellipse to current contour.
                (center, size, angle) = cv.FitEllipse2(PointArray2D32f)

                # Convert ellipse data from float to integer representation.
                center = (cv.Round(center[0]), cv.Round(center[1]))
                size = (cv.Round(size[0] * 0.5), cv.Round(size[1] * 0.5))
                angle = -angle

                # Draw ellipse in random color
                color = cv.CV_RGB(random.randrange(256), random.randrange(256),
                                  random.randrange(256))
                cv.Ellipse(image04, center, size, angle, 0, 360, color, 2,
                           cv.CV_AA, 0)

        # Show image. HighGUI use.
        cv.ShowImage("Result", image04)
Exemple #15
0
def get_hand(img):
    global h_lower, h_upper, s_lower, s_upper
    storage = cv.CreateMemStorage(0)
    contours, hull, max_contours, max_hull = (0, 0, 0, 0)
    max_rect = (1, 1, 100, 100)
    dst = cv.CreateImage((img.width, img.height), 8, 3)
    hsv = cv.CreateImage((img.width, img.height), 8, 3)
    frame = cv.CreateImage((img.width, img.height), 8, 1)
    con = cv.CreateImage((img.width, img.height), 8, 1)
    cv.Zero(con)
    cv.Smooth(img, dst, cv.CV_GAUSSIAN, 5, 5)
    for i in range(3): cv.Smooth(dst, dst, cv.CV_GAUSSIAN, 5, 5)
    cv.CvtColor(dst, hsv, cv.CV_RGB2HSV)
    cv.InRangeS(hsv, (h_lower, s_lower, 0), (h_upper, s_upper, 256), frame) 
    kernel = cv.CreateStructuringElementEx(3, 3, 0, 0, cv.CV_SHAPE_RECT)
    #cv.MorphologyEx(frame, frame, None, kernel, cv.CV_MOP_CLOSE , 7)
#    cv.MorphologyEx(frame, frame, None, kernel, cv.CV_MOP_OPEN , 3)
    #contours = im.find_contours(frame)
    #hull = im.find_convex_hull(contours)
    print contours

    #max_hull_area, max_contour_area = (0, 0)
    #print "xxxxxxxx"
    #contour = contours.h_next()
    #print "........"
    #while (contour != 0):
    #    hull = cv.ConvexHull2(contour, storage, cv.CV_CLOCKWISE, 1);
    #    maxv = cv.ContourArea(hull)
    #    contour = contour.h_next()
    #cv.DrawContours(con, contours, red, blue, 1, 3, 8)
    

    cv.ShowImage("result", con)
Exemple #16
0
def dummy_window():
    cv.NamedWindow('keyboard')
    cv.MoveWindow('keyboard', 0, 0)
    D.size = (100, 100)
    D.dummy = cv.CreateImage(D.size, 8, 1)
    D.created_images = True
    cv.ShowImage('keyboard', cv.Zero(D.dummy))
Exemple #17
0
    def backgroundDiff(self, img, Imask):
        #cv.Zero(self.Imaskt)
        cv.Zero(Imask)
        cv.CvtScale(img, self.Iscratch, 1, 0)
        cv.InRange(self.Iscratch, self.IlowF, self.IhiF, Imask)
        cv.SubRS(Imask, 255, Imask)
        cv.MorphologyEx(Imask, Imask, None, None, cv.CV_MOP_OPEN, 1)
        cv.MorphologyEx(Imask, Imask, None, None, cv.CV_MOP_CLOSE, 2)

        #######This stuff was for when it was 3 channel model, now only 1
        #######for backprojection
        #cv.Split(self.Iscratch, self.Igray1, self.Igray2, self.Igray3, None)
        #cv.InRange(self.Igray1, self.Ilow1, self.Ihi1, Imask)

        # cv.InRange(self.Igray2, self.Ilow2, self.Ihi2, self.Imaskt)
        # cv.Or(Imask, self.Imaskt, Imask)

        # cv.InRange(self.Igray3, self.Ilow3, self.Ihi3, self.Imaskt)
        # cv.Or(Imask, self.Imaskt, Imask)

        # cv.SubRS(Imask, 255, Imask)
        #cv.SaveImage('/home/mkillpack/Desktop/mask.png', Imask)
        #cv.Erode(Imask, Imask)

        return Imask
Exemple #18
0
        def get_dirmarker(img, angle, Dist, radius):
            X, Y = entCenter(robot)
            Len, _ = entSize(robot)
            point = (X + (Dist + Len / 2.0) * cos(angle),
                     Y - (Dist + Len / 2.0) * sin(angle))
            point = intPoint(point)

            #For visualisation:
            # cv.Circle( frame, point, radius, (0,200,200), 1 )

            point2 = point[0] - nhood[0], point[1] - nhood[1]
            out = cv.CloneImage(img)
            cv.Zero(out)
            cv.Circle(out, point2, radius, (255, 255, 255), -1)

            cv.And(out, img2, out)
            center1 = self.centralMoment(out)
            count1 = cv.CountNonZero(out)

            cv.Erode(out, out)
            center2 = self.centralMoment(out)
            count2 = cv.CountNonZero(out)

            if count2 == 0 and count1 > 10:
                return center1
            else:
                return center2
Exemple #19
0
def plot_1dhisto(histogram,
                 scale=2,
                 gap=0,
                 histh=200,
                 histimg=None,
                 origin='bottom'):
    """
	Given histogram data, make a visual represntation in the form of a 
	IplImage, helper function for calc_1dhisto().

	@param scale Width of each bar (pix)
	@param gap Gap between bars (pix)
	@param histh Bar plot height (pix)
	@param origin Origin of histogram bars, 'bottom' (for positive values) or 'center' (for any value)
	@return Graphical cv.iplimage representation of the histogram
	"""

    nbin = len(histogram)
    histh, scale, gap = int(histh), int(scale), int(gap)

    if (not histimg):
        histimg = cv.CreateImage((nbin * scale, histh), cv.IPL_DEPTH_8U, 1)

    cv.Zero(histimg)
    for i, bin_h in enumerate(histogram):
        if (origin == 'bottom'):
            cv.Rectangle(histimg, (i * scale, histh),
                         ((i + 1) * scale - 1 - gap, histh - int(bin_h)), 256,
                         cv.CV_FILLED)
        else:
            cv.Rectangle(histimg, (i * scale, histh / 2),
                         ((i + 1) * scale - 1 - gap, histh / 2 - int(bin_h)),
                         256, cv.CV_FILLED)

    return histimg
Exemple #20
0
def get_normalized_rgb_planes(r, g, b):
    size = cv.GetSize(r)
    #    r,g,b = get_three_planes(img)

    nr_plane = cv.CreateImage(size, 8, 1)
    ng_plane = cv.CreateImage(size, 8, 1)
    nb_plane = cv.CreateImage(size, 8, 1)

    r32 = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
    g32 = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
    b32 = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
    sum = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
    cv.Zero(sum)
    cv.Convert(r, r32)
    cv.Convert(g, g32)
    cv.Convert(b, b32)

    cv.Add(r32, g32, sum)
    cv.Add(b32, sum, sum)

    tmp = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
    cv.Div(r32, sum, tmp)
    cv.ConvertScale(tmp, nr_plane, scale=255)
    cv.Div(g32, sum, tmp)
    cv.ConvertScale(tmp, ng_plane, scale=255)
    cv.Div(b32, sum, tmp)
    cv.ConvertScale(tmp, nb_plane, scale=255)

    #    res = image_empty_clone(img)
    #    cv.Merge(nr_plane,ng_plane,nb_plane,None,res)
    return nr_plane, ng_plane, nb_plane
Exemple #21
0
    def redraw_monocular(self, drawable):
        width, height = cv.GetSize(drawable.scrib)

        display = cv.CreateMat(max(480, height), width + 100, cv.CV_8UC3)
        cv.Zero(display)
        cv.Copy(drawable.scrib, cv.GetSubRect(display, (0, 0, width, height)))
        cv.Set(cv.GetSubRect(display, (width, 0, 100, height)),
               (255, 255, 255))

        self.buttons(display)
        if not self.c.calibrated:
            if drawable.params:
                for i, (label, lo, hi, progress) in enumerate(drawable.params):
                    (w, _), _ = cv.GetTextSize(label, self.font)
                    cv.PutText(display, label,
                               (width + (100 - w) / 2, self.y(i)), self.font,
                               (0, 0, 0))
                    color = (0, 255, 0)
                    if progress < 1.0:
                        color = (0, int(progress * 255.), 255)
                    cv.Line(display, (int(width + lo * 100), self.y(i) + 20),
                            (int(width + hi * 100), self.y(i) + 20), color, 4)

        else:
            cv.PutText(display, "lin.", (width, self.y(0)), self.font,
                       (0, 0, 0))
            linerror = drawable.linear_error
            if linerror < 0:
                msg = "?"
            else:
                msg = "%.2f" % linerror
                #print "linear", linerror
            cv.PutText(display, msg, (width, self.y(1)), self.font, (0, 0, 0))

        self.show(display)
Exemple #22
0
def publish_debug(img, results):
    imgsize = cv.GetSize(img)
    sizelist = [cv.GetSize(tmp[1]) for tmp in templates]
    width = max(imgsize[0], sum([s[0] for s in sizelist]))
    height = imgsize[1] + max([s[1] for s in sizelist])
    output = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 1)
    cv.Zero(output)
    cur_x = 0

    view_rect = (0, height-imgsize[1], imgsize[0], imgsize[1])
    cv.SetImageROI(output, view_rect)
    cv.Copy(img, output)
    for template in templates:
        size = cv.GetSize(template[1])
        cv.SetImageROI(output, (cur_x, 0, size[0], size[1]))
        cv.Copy(template[1], output)
        cur_x += size[0]

        #cv.PutText(output, tempname, (0,size[1]-16), font, cv.CV_RGB(255,255,255))
        #cv.PutText(output, str(tempthre)+'<'+str(status[1]), (0,size[1]-8), font, cv.CV_RGB(255,255,255))
        for _,status in [s for s in results if s[0] == template[3]]:
            print status
            cv.PutText(output, template[3], (0,size[1]-42), font, cv.CV_RGB(255,255,255))
            cv.PutText(output, "%7.5f"%(status[0]), (0,size[1]-24), font, cv.CV_RGB(255,255,255))
            cv.PutText(output, "%7.5f"%(status[2]), (0,size[1]-8), font, cv.CV_RGB(255,255,255))
            if status[3] : 
                cv.Rectangle(output, (0, 0), size, cv.RGB(255,255,255), 9)
        cv.SetImageROI(output, view_rect)
        for _,status in [s for s in results if s[0] == template[3]]:
            pt2 = (status[1][0]+size[0], status[1][1]+size[1])
            if status[3] : 
                cv.Rectangle(output, status[1], pt2, cv.RGB(255,255,255), 5)

    cv.ResetImageROI(output)
    debug_pub.publish(bridge.cv_to_imgmsg(output, encoding="passthrough"))
Exemple #23
0
    def calc_stats(self):
        cv.NamedWindow("noise", cv.CV_WINDOW_AUTOSIZE)
        cv.NamedWindow("img1_back", cv.CV_WINDOW_AUTOSIZE)
        cv.NamedWindow("img2_back", cv.CV_WINDOW_AUTOSIZE)
        self.check_for_hist()
        self.avg_noise = cv.CreateImage(cv.GetSize(self.background_noise[0]),
                                        8, 1)
        cv.Zero(self.avg_noise)

        for i in xrange(len(self.background_noise) - 1):
            cv.ShowImage("noise", self.avg_noise)
            back_proj_img1, hist1 = self.back_project_hs(
                self.background_noise[i])
            back_proj_img2, hist2 = self.back_project_hs(
                self.background_noise[i + 1])

            self.accumulateBackground(back_proj_img1)

            cv.ShowImage("img1_back", back_proj_img1)
            cv.ShowImage("img2_back", back_proj_img2)
            scratch = cv.CreateImage(cv.GetSize(back_proj_img2), 8, 1)
            scratch2 = cv.CreateImage(cv.GetSize(back_proj_img2), 8, 1)

            # do something clever with ands ors and diffs
            cv.Zero(scratch)
            cv.Zero(scratch2)
            cv.Sub(back_proj_img2, back_proj_img1,
                   scratch2)  #noise, but includes object if failed,

            #cv.Sub(scratch2, self.avg_noise, scratch)
            #cv.Or(self.avg_noise, scratch2, self.avg_noise)

            cv.Or(self.avg_noise, scratch2, self.avg_noise)

            cv.ShowImage("diff_back", scratch2)
            cv.ShowImage("diff_noise_scratch", scratch)

            cv.WaitKey(-1)
        self.createModelsfromStats()
        print self.Icount
        cv.NamedWindow("Ilow", cv.CV_WINDOW_AUTOSIZE)
        cv.NamedWindow("Ihi", cv.CV_WINDOW_AUTOSIZE)
        cv.NamedWindow("IavgF", cv.CV_WINDOW_AUTOSIZE)

        cv.ShowImage("Ihi", self.IhiF)
        cv.ShowImage("Ilow", self.IlowF)
        cv.ShowImage("IavgF", self.IavgF)
Exemple #24
0
def edge_threshold(image, roi=None, debug=0):
    thresholded = cv.CloneImage(image)
    horizontal = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_16S, 1)
    magnitude32f = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1)
    vertical = cv.CloneImage(horizontal)
    v_edge = cv.CloneImage(image)
    magnitude = cv.CloneImage(horizontal)

    storage = cv.CreateMemStorage(0)
    mag = cv.CloneImage(image)
    cv.Sobel(image, horizontal, 0, 1, 1)
    cv.Sobel(image, vertical, 1, 0, 1)
    cv.Pow(horizontal, horizontal, 2)
    cv.Pow(vertical, vertical, 2)

    cv.Add(vertical, horizontal, magnitude)
    cv.Convert(magnitude, magnitude32f)
    cv.Pow(magnitude32f, magnitude32f, 0.5)
    cv.Convert(magnitude32f, mag)
    if roi:
        cv.And(mag, roi, mag)
    cv.Normalize(mag, mag, 0, 255, cv.CV_MINMAX, None)
    cv.Threshold(mag, mag, 122, 255, cv.CV_THRESH_BINARY)
    draw_image = cv.CloneImage(image)
    and_image = cv.CloneImage(image)
    results = []

    threshold_start = 17
    for window_size in range(threshold_start, threshold_start + 1, 1):
        r = 20
        for threshold in range(0, r):
            cv.AdaptiveThreshold(image, thresholded, 255, \
                cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, window_size, threshold)
            contour_image = cv.CloneImage(thresholded)
            contours = cv.FindContours(contour_image, storage, cv.CV_RETR_LIST)
            cv.Zero(draw_image)
            cv.DrawContours(draw_image, contours, (255, 255, 255),
                            (255, 255, 255), 1, 1)
            if roi:
                cv.And(draw_image, roi, draw_image)
            cv.And(draw_image, mag, and_image)
            m1 = np.asarray(cv.GetMat(draw_image))
            m2 = np.asarray(cv.GetMat(mag))
            total = mag.width * mag.height  #cv.Sum(draw_image)[0]

            coverage = cv.Sum(and_image)[0] / (mag.width * mag.height)
            if debug:
                print threshold, coverage
                cv.ShowImage("main", draw_image)
                cv.ShowImage("main2", thresholded)
                cv.WaitKey(0)
            results.append((coverage, threshold, window_size))

    results.sort(lambda x, y: cmp(y, x))
    _, threshold, window_size = results[0]
    cv.AdaptiveThreshold(image, thresholded, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, \
        cv.CV_THRESH_BINARY, window_size, threshold)

    return thresholded
Exemple #25
0
 def test_2686307(self):
     lena = cv.LoadImage(find_sample("lena.jpg"), 1)
     dst = cv.CreateImage((512, 512), 8, 3)
     cv.Set(dst, (128, 192, 255))
     mask = cv.CreateImage((512, 512), 8, 1)
     cv.Zero(mask)
     cv.Rectangle(mask, (10, 10), (300, 100), 255, -1)
     cv.Copy(lena, dst, mask)
     self.snapL([lena, dst, mask])
def get_mask(w, h):
    img = cv.CreateImage((w, h), 8, 1)
    cv.Zero(img)
    t = int(w / 5)
    k = int(w / 15)
    p = w - k - 1
    poly = ((k, t), (t + k, 0), (p - t, 0), (p, t), (p, h - t), (p - t, h),
            (t + k, h), (k, h - t))
    cv.FillPoly(img, (poly, ), 255)
    return img
Exemple #27
0
def rects_to_mask(regions, mask, inverted=False, value=255):
    if inverted:
        cv.Set(mask, cv.ScalarAll(value))
        colour = cv.ScalarAll(0)
    else:
        cv.Zero(mask)
        colour = cv.ScalarAll(value)
    for rect in regions:
        cv.Rectangle(mask, (rect[0], rect[1]),
                     (rect[0] + rect[2], rect[1] + rect[3]), colour, -1)
Exemple #28
0
def initialize_interface():
    """Initializes all windows needed by our program."""

    #Create window to monitor robot status
    cv.NamedWindow('Monitor')
    cv.MoveWindow('Monitor', 0, 0)
    D.size = (100, 100)
    D.dummy = cv.CreateImage(D.size, 8, 1)
    D.image = cv.Zero(D.dummy)
    cv.ShowImage('Monitor', D.image)
Exemple #29
0
    def add_features(self, cv_image, face):
        """ Look for any new features around the current feature cloud """
        """ Create the ROI mask"""
        roi = cv.CreateImage(cv.GetSize(cv_image), 8, 1)
        """ Begin with all black pixels """
        cv.Zero(roi)
        """ Get the coordinates and dimensions of the current track box """
        try:
            ((x, y), (w, h), a) = face.track_box
        except:
            logger.info("Track box has shrunk to zero...")
            return
        """ Expand the track box to look for new features """
        w = int(face.expand_roi * w)
        h = int(face.expand_roi * h)

        roi_box = ((x, y), (w, h), a)
        """ Create a filled white ellipse within the track_box to define the ROI. """
        cv.EllipseBox(roi, roi_box, cv.CV_RGB(255, 255, 255), cv.CV_FILLED)
        """ Create the temporary scratchpad images """
        eig = cv.CreateImage(cv.GetSize(self.grey), 32, 1)
        temp = cv.CreateImage(cv.GetSize(self.grey), 32, 1)

        if self.feature_type == 0:
            """ Get the new features using Good Features to Track """
            features = cv.GoodFeaturesToTrack(self.grey,
                                              eig,
                                              temp,
                                              self.max_count,
                                              self.quality,
                                              self.good_feature_distance,
                                              mask=roi,
                                              blockSize=3,
                                              useHarris=0,
                                              k=0.04)

        elif self.feature_type == 1:
            """ Get the new features using SURF """
            features = []
            (surf_features, descriptors) = cv.ExtractSURF(
                self.grey, roi, cv.CreateMemStorage(0),
                (0, self.surf_hessian_quality, 3, 1))
            for feature in surf_features:
                features.append(feature[0])
        """ Append new features to the current list if they are not too
            far from the current cluster """
        for new_feature in features:
            try:
                distance = self.distance_to_cluster(new_feature, face.features)
                if distance > self.add_feature_distance:
                    face.features.append(new_feature)
            except:
                pass
        """ Remove duplicate features """
        face.features = list(set(face.features))
Exemple #30
0
def cv_convolution(image, b):
    #cv.ShowImage('image', cv.fromarray(image))
    dft_m = cv.GetOptimalDFTSize(image.shape[0] + b.shape[0] - 1)
    dft_n = cv.GetOptimalDFTSize(image.shape[1] + b.shape[1] - 1)
    print dft_m, dft_n
    #
    c = cv.CreateMat(image.shape[0] + d - 1, image.shape[1] + d - 1, cv.CV_8U)
    # getting gaussian dft
    dft_b = cv.CreateMat(dft_m, dft_n, cv.CV_64F)
    #
    tmp = cv.GetSubRect(dft_b, (0, 0, b.shape[1], b.shape[0]))
    cv.Copy(cv.fromarray(b), tmp)
    tmp = cv.GetSubRect(dft_b,
                        (b.shape[1], 0, dft_b.cols - b.shape[1], b.shape[0]))
    cv.Zero(tmp)
    #
    cv.DFT(dft_b, dft_b, cv.CV_DXT_FORWARD, b.shape[0])
    # getting layers dft
    dfts = []
    new_channels = []
    channels = cv2.split(image)
    for i, channel in enumerate(channels):
        #cv2.imshow('channel %d'%i, channel)
        a = np.array(channel, dtype='float')
        dft_a = cv.CreateMat(dft_m, dft_n, cv.CV_64F)
        #
        tmp = cv.GetSubRect(dft_a, (0, 0, a.shape[1], a.shape[0]))
        cv.Copy(cv.fromarray(a), tmp)
        tmp = cv.GetSubRect(
            dft_a, (a.shape[1], 0, dft_a.cols - a.shape[1], a.shape[0]))
        cv.Zero(tmp)
        #
        cv.DFT(dft_a, dft_a, cv.CV_DXT_FORWARD, a.shape[0])
        cv.MulSpectrums(dft_a, dft_b, dft_a, 0)
        cv.DFT(dft_a, dft_a, cv.CV_DXT_INV_SCALE, c.rows)
        tmp = cv.GetSubRect(dft_a, (0, 0, c.cols, c.rows))
        #cv.Copy(tmp, c)
        channel = np.array(tmp, dtype='uint8')
        cv.ShowImage('new channel %d' % i, cv.fromarray(channel))
        new_channels.append(channel)
    result = cv2.merge(new_channels)
    return result