Example #1
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
def is_grayscale(im):
    if im.nChannels == 1:
        return True
    h,s,v = split(rgb2hsv(im))
    if cv.CountNonZero(h) == 0 == cv.CountNonZero(s):
        return True
    return False
Example #3
0
def estimaBG(frames):

    npixels = cv.GetSize(frames[0])[1] * cv.GetSize(frames[0])[0]

    gray = cv.CreateImage(cv.GetSize(frames[0]), cv.IPL_DEPTH_8U, 1)
    frAbsDif = cv.CloneImage(gray)
    frThrDif = cv.CloneImage(gray)
    frAnt = cv.CloneImage(gray)

    # converte cada quadro para cinza
    cinzas = []
    for fr in frames:
        gray = cv.CreateImage(cv.GetSize(frames[0]), cv.IPL_DEPTH_8U, 1)
        cv.CvtColor(fr, gray, cv.CV_RGB2GRAY)
        cinzas.append(gray)

    iframe = 1
    fundos = []
    for fr in cinzas[1:]:
        frAnt = cinzas[iframe - 1]
        cv.AbsDiff(frAnt, fr, frAbsDif)

        pixelsDiferentesAbs = cv.CountNonZero(frAbsDif)

        # limiariza para evitar diferencas muito pequenas.
        cv.Threshold(frAbsDif, frThrDif, 120, 255, cv.CV_THRESH_TOZERO)
        # se a direrença for zero, considera a imagem para a mediana dos fundos
        pixelsDiferentesThr = cv.CountNonZero(frThrDif)
        print("Pixels diferentes do threeshold: " + str(pixelsDiferentesThr))

        if pixelsDiferentesThr < 30:
            #print 'Imagem adicionada para determinacao do background...'
            fundos.append((frames[iframe], cv.CloneImage(frThrDif),
                           cv.CloneImage(frThrDif), pixelsDiferentesAbs,
                           pixelsDiferentesThr))
        iframe += 1

    if not fundos:
        print 'Não foi possivel determinar o fundo usando os primeiros %d frames da imagem' % len(
            frames)
        sys.exit(1)

    # pega o menor threshold absoluto, ordenando e pegando o primeiro.
    def porThr(a, b):
        return cmp(a[3], b[3])

    fundos.sort(porThr)

    # retorna o que apresentou melhor diferença com relação ao anterior
    return fundos[0][0]

    return fr
def prepare_negatives():
    path = root_folder+"negative_source/new2"
    negatives_path=root_folder+"negative"
    for fullpath, name in directory_files(path):
        try:
            big_img = cv.LoadImage(fullpath)
        except IOError:
            continue
        print "Getting samples from %s" % name
        no_suffix_name = name.split(".")[0]
        negatives_new_path = os.path.join(negatives_path, no_suffix_name)
        if not os.path.exists(negatives_new_path):
            os.mkdir(negatives_new_path)
        sample_gen = samples_generator(big_img,32,32,3,2,bw_from_v_plane=False)
        k=0
#        random_from_generator()
        for sample, _ in sample_gen:
            prepared = normalize_plane(sample)
            if cv.CountNonZero(prepared) > 0:
                new_name=os.path.join(negatives_new_path, "%d.png" % k)
                cv.SaveImage(new_name, prepared)
                k+=1
#            if k > 1500:
#                break
        print k
Example #5
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
Example #6
0
def get_contours(frame, approx=True):
    """Get contours from an image
    :: iplimage -> CvSeq
    """
    # A workaround for OpenCV 2.0 crash on receiving a (nearly) black image
    nonzero = cv.CountNonZero(frame)
    logging.debug("Segmentation got an image with %d nonzero pixels", nonzero)
    if nonzero < 20 or nonzero > 10000:
        return []

    storage = cv.CreateMemStorage(0)
    # find the contours
    contours = cv.FindContours(frame, storage, cv.CV_RETR_LIST,
                               cv.CV_CHAIN_APPROX_SIMPLE)
    if contours is None:
        return []

    res = []
    while contours:
        if not approx:
            result = contours
        else:
            result = cv.ApproxPoly(contours, storage, cv.CV_POLY_APPROX_DP,
                                   cv.ArcLength(contours) * 0.02, 1)
        res.append(result)
        contours = contours.h_next()

    return res
Example #7
0
def noise_per_grid(images):
    size = cv.GetSize(images[0])
    total = float(size[0] * size[1])
    scores = []
    for im in images:
        scores.append(cv.CountNonZero(im) / total)
    return scores
Example #8
0
def count(img, upper_thresh, lower_thresh):
	img = cv.LoadImage(img, 0)
	assert img.depth == cv.IPL_DEPTH_8U

	cv.Threshold(img, img, upper_thresh, 0, cv.CV_THRESH_TOZERO_INV)
	cv.Threshold(img, img, lower_thresh, 0, cv.CV_THRESH_TOZERO)

	return cv.CountNonZero(img)
Example #9
0
def count_neighbors(im):
    size = cv.GetSize(im)
    mx, my = int(size[0] / 2), int(size[1] / 2)
    pixel = im[mx, my]
    if pixel == 0:
        return 0
    c = cv.CountNonZero(im)
    return c
def auto_edges(im, starting_threshold=50, percentage=0.2):
    size = cv.GetSize(im)
    total = size[0] * size[1] * percentage
    e = edges(im, starting_threshold, starting_threshold*3)
    while cv.CountNonZero(e) > total:
        starting_threshold += 10
        e = edges(im, starting_threshold, starting_threshold*3)
    return e
Example #11
0
def cutNonBlack(comp, thresh, colthresh=0):
    image = comp[3]
    rect = comp[2]
    newRect = findNonBlackRect(image, thresh, colthresh)
    if newRect[2] == 0 or newRect[3] == 0:
        return None
    region = cvext.getSubImage(image, newRect)
    newRect = shiftRect(newRect, rect[0], rect[1])
    return (cv.CountNonZero(region), 255.0, newRect, region)
Example #12
0
 def draw(self, im, threshold):
     new_im = image.new_from(im)
     edges = image.edges(im, threshold, threshold * 3, 3)
     cv.SetZero(new_im)
     cv.Copy(im, new_im, edges)
     size = cv.GetSize(im)
     print cv.CountNonZero(image.threshold(edges)) / float(
         size[0] * size[1])
     #cv.Dilate(new_im, new_im)
     #cv.Erode(new_im, new_im)
     return new_im
Example #13
0
def threshhold_amount(image, image_hsv, lower, upper):
    """
    determine how many pixels of an image are within a HSV threshhold
    """
    # create the placeholder for thresholded image
    channels = 1
    image_threshed = cv.CreateImage(cv.GetSize(image), image.depth, channels)

    # do the actual thresholding
    cv.InRangeS(image_hsv, lower, upper, image_threshed)

    return cv.CountNonZero(image_threshed)
def detect_skin(im, debug=False):
    hsv = image.rgb2hsv(im)
    
    if debug:
        image.show(hsv, 'hsv')
    h,s,v = image.split(hsv)
    
    if cv.CountNonZero(h) == cv.CountNonZero(s) == 0:
        white = image.new_from(im)
        cv.Set(white, 255)
        return white
    
    if debug:
        image.show(h, "Hue")
        image.show(s,"sat1")
    
    h_rng = 0, 46
    s_rng = 48, 178
    
    h = image.threshold(image.gaussian(h, 5), threshold=h_rng[1], type=cv.CV_THRESH_TOZERO_INV)
    h = image.threshold(h, threshold=h_rng[0], type=cv.CV_THRESH_TOZERO)
    h = image.threshold(h, threshold=1)
    
    s = image.threshold(image.gaussian(s, 5), threshold=s_rng[1], type=cv.CV_THRESH_TOZERO_INV)
    s = image.threshold(s, threshold=s_rng[0], type=cv.CV_THRESH_TOZERO)
    if debug:
        image.show(s,"sat2")
    s = image.threshold(s, threshold=1)
    
    v = image.dilate(image.erode(image.And(s, h)))
    
    
    #im = image.hsv2rgb(image.merge(h,s,v))
    if debug:
        image.show(v, "Human")
    return image.threshold(v, threshold=1)
Example #15
0
def non_zero_in_conv_table(image, x, y):
    conv_table_length = 3
    shift = (conv_table_length - 1) / 2

    if x < shift:
        x_orig = 0
    else:
        x_orig = x - shift

    if y < shift:
        y_orig = 0
    else:
        y_orig = y - shift

    return cv.CountNonZero(
        cv.GetSubRect(image,
                      (x_orig, y_orig, conv_table_length, conv_table_length)))
Example #16
0
def evaluatePolygonLabelRaw(polygons, gtImage, maskImage, foundZeros, image):
    #splat the results to an image
    labelImage = getBlankLabelImage(image)
    visualizePolygons(labelImage, polygons)

    #get the label image and rescale it to the correct size, if necessary
    labelImageRescale = cv.CreateImage(cv.GetSize(gtImage), cv.IPL_DEPTH_8U, 1)
    cv.Resize(labelImage, labelImageRescale)
    labelImage = labelImageRescale

    #compute the difference -- this is 0 if correct
    cv.AbsDiff(labelImage, gtImage, labelImage)
    w, h = cv.GetSize(gtImage)

    #mask out unknown values
    cv.Set(labelImage, 1, maskImage)

    #return (Total Pixels - incorrect) / (total pixels)
    nPixValid = w * h - foundZeros
    return float(w * h - cv.CountNonZero(labelImage)) / nPixValid
Example #17
0
    def motiondetect(self, img):
        gray = cv.CreateImage(self.size, 8, 1)
        cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

        if not self.oldimg or (time.time() - self.lasttime) > 1:
            self.oldimg = cv.CreateImage(self.size, 8, 1)
            cv.Copy(gray, self.oldimg)
            self.lasttime = time.time()
            return 0

        self.lasttime = time.time()

        diff = cv.CreateImage(self.size, 8, 1)
        cv.AbsDiff(gray, self.oldimg, diff)

        thr = cv.CreateImage(self.size, 8, 1)
        movement = cv.CreateImage(self.size, 8, 1)
        cv.SetZero(movement)

        cv.Threshold(diff, thr, 30, 1, cv.CV_THRESH_BINARY)
        movecount = cv.CountNonZero(thr)

        color = cv.CreateImage(self.size, 8, 3)
        cv.Set(color, (255, 0, 0))

        cv.Copy(color, img, thr)

        cv.Copy(diff, movement, thr)
        cv.ShowImage("movement" + str(self.channel), movement)

        writetext(
            img, 'c' + str(self.channel) + ' a' +
            str(int(self.core.bucket.value)) + ' m' + str(movecount))

        cv.ShowImage("camera" + str(self.channel), img)

        cv.Copy(gray, self.oldimg)
        self.lasttime = time.time()

        return movecount
Example #18
0
def find_connected_components(frame):
    """Find connected components from an image.
    :: iplimage -> [ dict(box<CvBox2D>, rect<CvRect>) ]

    Takes as input a grayscale image that should have some blobs in
    it. Outputs a data structure containing the 'centers' of the blobs
    and minimal rectangles enclosing them. A maximum of 3 blobs are
    returned.

    The input frame is modified in place.
    """

    # A workaround for OpenCV 2.0 crash on receiving a (nearly) black image
    nonzero = cv.CountNonZero(frame)
    if nonzero < 20:
        return []
    logging.debug("Segmentation got an image with %d nonzero pixels", nonzero)

    contours = get_contours(frame)
    #out = draw_contours(frame, contours)

    if contours is None:
        return []

    candidates = []
    while contours:
        storage = cv.CreateMemStorage(0)
        minBox = cv.MinAreaRect2(contours, storage)

        boundingRect = cv.BoundingRect(contours, 0)
        candidates.append({ 'box' : minBox, 'rect' : boundingRect })

        contours = contours.h_next()

    candidates = sorted( candidates,
                         key=lambda x:getArea(x['box']),
                         reverse=True )
    return candidates
Example #19
0
def measure(im, debug=False):

    gray = image.rgb2gray(im)
    size = cv.GetSize(im)
    total = float(size[0] * size[1])
    edges = image.auto_edges(im)

    _, sat, val = image.split(image.rgb2hsv(im))
    edges = image.auto_edges(im)
    l, u, v = tuple(map(image.equalize_hist, image.split(image.rgb2luv(im))))
    u, v = tuple(map(image.gaussian, (u, v)))
    if debug:
        image.show(l, "1. L")
        image.show(u, "1. U")
        image.show(v, "1. V")
    la, ua, va, uva = tuple(map(image.cv2array, (l, u, v, image.And(l, u, v))))
    test = image.new_from(gray)
    test2 = image.new_from(gray)
    cv.Xor(u, v, test)
    #cv.AbsDiff(u,v, test2)
    if debug:
        #cv.Threshold(test, test, 32, 255, cv.CV_THRESH_BINARY)
        image.show(test, "2. U Xor V")
        #image.show(test2, "TEST 2")
        #test = image.dilate(test)
    cv.Set(test, 0, image.dilate(edges))
    #cv.Set(test, 0, image.invert(image.threshold(sat, threshold=8)))
    uv_score = cv.CountNonZero(test) / total
    if debug:
        image.show(
            test, "3. U Xor V - dilate(Edges) - invert(threshold(Saturation))")

    arr = image.cv2array(test)
    avg_mean, avg_std = arr.mean(), arr.std()

    score = uv_score, avg_std

    return test, score
Example #20
0
    def getOrientation(self, frame, robot, name, thresh):
        cv.SetImageROI(frame, robot['rect'])
        img = thresh(frame)
        cv.ResetImageROI(frame)
        thresholded_T = cv.CloneImage(img)

        offset = 40
        c = entCenter(robot)
        nhood = (max(0,
                     int(round(c[0])) - offset),
                 max(0,
                     int(round(c[1])) - offset), 2 * offset, 2 * offset)
        cv.SetImageROI(frame, nhood)
        img2 = self.threshold.dirmarker(frame)
        cv.ResetImageROI(frame)

        def mask_end(img, angle):
            """Mask out one end of the T given an angle of orientation

            Returns a new image.
            """
            X, Y = entCenter(robot)
            Len, _ = entSize(robot)
            Dist = 5
            point = (X + (Dist + Len / 2.0) * cos(angle),
                     Y - (Dist + Len / 2.0) * sin(angle))
            point = intPoint(point)
            radius = int(round(Dist + Len / 2.0))

            #For visualisation:
            #cv.Circle( frame, point, radius, (0,0,255), 2 )

            rect = entRect(robot)
            point2 = point[0] - rect[0], point[1] - rect[1]
            out = cv.CloneImage(img)
            cv.Circle(out, point2, radius, (0, 0, 0), -1)
            return out

        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

        box_direction = None
        max_count = 0
        for angle in entOrientations(robot):
            masked = mask_end(thresholded_T, angle)
            count = cv.CountNonZero(masked)
            if count > max_count:
                max_count = count
                box_direction = angle

        if box_direction is None:
            return

        # Not the cleanest way to deal with this angle...
        robot['orient'] = -box_direction

        if self.overlay:
            cv.SetImageROI(frame, robot['rect'])
            tmp = cv.CloneImage(frame)
            cv.CvtColor(img, tmp, cv.CV_GRAY2BGR)
            cv.Add(frame, tmp, frame)
            cv.ResetImageROI(frame)

            "Draw the corners of the bounding box"
            for i in getBoxCorners(robot['box']):
                col2 = (0, 255, 255)
                col2 = (255, 0, 0)
                #cv.Circle( frame, intPoint(i), 3, col2, -1 )

        dCenter = get_dirmarker(img2, pi - robot['orient'], 3, 4)
        if dCenter is None:
            dCenter = get_dirmarker(img2, pi - robot['orient'], 8, 10)
        if dCenter is None:
            dCenter = get_dirmarker(img2, pi - robot['orient'], 10, 4)
        if dCenter is None:
            dCenter = get_dirmarker(img2, pi - robot['orient'], 14, 8)
        if dCenter is None:
            dCenter = get_dirmarker(img2, pi - robot['orient'], 10, 12)
        if dCenter is None:
            return

        dCenter = intPoint(dCenter + nhood[:2])

        if self.overlay:
            cv.Circle(frame, dCenter, 6, (0, 0, 255), 2)

        Tcenter = self.centralMoment(img)
        if Tcenter is not None:
            Tcenter += entRect(robot)[:2]
            robot['center'] = Tcenter
        else:
            robot['center'] = entCenter(robot)

        cx, cy = robot['center']
        dx, dy = dCenter
        robot['orient'] = atan2(cy - dy, cx - dx)
Example #21
0
def measure(im, debug=False):
    gray = image.rgb2gray(im)
    size = cv.GetSize(im)
    total = float(size[0] * size[1])
    l = image.sub(gray, image.gaussian(gray, 5))
    l2 = image.sub(gray, image.gaussian(gray, 9))
    edges = image.dilate(image.auto_edges(im, percentage=0.2))
    if debug:
        image.show(image.threshold(l, threshold=1),
                   "Before Edge Removal (kernel=5)")
        image.show(image.threshold(l2, threshold=1),
                   "Before Edge Removal (kernel=9)")
    cv.Set(l, 0, image.threshold(edges, threshold=1))
    cv.Set(l2, 0, image.threshold(edges, threshold=1))

    l = image.threshold(l, threshold=1)
    l2 = image.threshold(l2, threshold=1)

    if debug:
        image.show(image.threshold(edges, threshold=1), "Edges")
        image.show(l, "After Edge Removal (kernel=5)")
        image.show(l2, "After Edge Removal (kernel=9)")

    noise2 = image.new_from(gray)
    cv.EqualizeHist(gray, noise2)
    cv.AbsDiff(noise2, gray, noise2)
    cv.Set(noise2, 0,
           image.threshold(image.sobel(im, xorder=2, yorder=2), threshold=4))
    diff = image.cv2array(noise2)
    if debug:
        image.show(noise2, "DIFF")
        print "M", diff.mean(), "S", diff.std()
    diff_stat = (diff.mean(), diff.std())
    percent_noise = cv.CountNonZero(noise2) / total
    if debug:
        image.show(noise2, "NOISE2")

    # magical, I don't understand how this works
    _, sat, _ = image.split(image.rgb2hsv(im))
    edges = image.auto_edges(im)
    l, u, v = tuple(map(image.equalize_hist, image.split(image.rgb2luv(im))))
    u, v = tuple(map(image.gaussian, (u, v)))
    if debug:
        image.show(l, "1. L")
        image.show(u, "1. U")
        image.show(v, "1. V")
    la, ua, va, uva = tuple(map(image.cv2array, (l, u, v, image.And(l, u, v))))
    test = image.new_from(gray)
    test2 = image.new_from(gray)
    cv.Xor(u, v, test)
    if debug:
        image.show(test, "2. U Xor V")
    cv.Set(test, 0, image.dilate(edges))
    #cv.Set(test, 0, image.invert(image.threshold(sat, threshold=8)))
    uv_score = cv.CountNonZero(test) / total
    if debug:
        image.show(
            test, "3. U Xor V - dilate(Edges) - invert(threshold(Saturation))")

    g = Grid(size)
    images = map(image.cv2array, g.split_into(test, 6))
    arr = image.cv2array(test)
    avg_mean, avg_std = arr.mean(), arr.std()

    #ms = [(a.mean(), a.std()) for a in images]
    #min_mean = min_std = 255
    #max_mean = max_std = 0
    #for m,s in ms:
    #    min_mean = min(min_mean, m)
    #    min_std = min(min_std, s)
    #    max_mean = max(max_mean, m)
    #    max_std = max(max_std, s)
    #if debug:
    #    print min_mean, min_std
    #    print avg_mean, avg_std
    #    print max_mean, max_std
    #
    #score = uv_score, min_mean, avg_mean, avg_std, max_mean
    uv_score = uv_score, avg_std

    score = cv.CountNonZero(l) / total,  cv.CountNonZero(l2) / total, \
        diff_stat[0], diff_stat[1], uv_score

    return l, score
Example #22
0
def hit_value(resim, hedef):
    roi = python_opencv_modulu.GetSubRect(resim, hedef.getDimensions())
    return python_opencv_modulu.CountNonZero(roi)
Example #23
0
def measure(im, debug=False):
    size = cv.GetSize(im)
    npixels = size[0] * size[1]
    #print 'np', npixels

    focused = get_focus_points(im, debug)
    points = convert_to_points(focused)

    if debug:
        print "\t" + str(
            len(points)), '/', npixels, '=', len(points) / float(npixels)
        print "\tlen(points) =", len(points)
        image.show(focused, "4. Focused Points")

    saturation_score = 0
    if not image.is_grayscale(im):
        edges = image.auto_edges(im)
        _, saturation, _ = image.split(image.rgb2hsv(im))
        if debug:
            image.show(saturation, "5. Saturation")
        #saturation = image.laplace(image.gaussian(saturation, 3))
        saturation = image.invert(saturation)
        mask = image.invert(image.threshold(im, threshold=16))
        if debug:
            image.show(saturation, "5.3. Laplace of Saturation")
        cv.Set(saturation, 0, mask)
        cv.Set(saturation, 0, focused)
        if debug:
            image.show(mask,
                       "5.6. Mask(focused AND invert(threshold(im, 16)))")
            image.show(saturation, "6. Set(<5.3>, 0, <5.6>)")

        saturation_score = cv.Sum(saturation)[0] / float(npixels * 255)
        print "\tSaturation Score:", saturation_score

    # light exposure
    h, s, v = image.split(image.rgb2hsv(im))
    if debug:
        image.show(h, "7. Hue")
        image.show(s, "7. Saturation")
        image.show(v, "7. Value")
    diff = cv.CloneImage(v)
    cv.Set(diff, 0, image.threshold(s, threshold=16))
    diff = image.dilate(diff, iterations=10)
    if debug:
        thres_s = image.threshold(s, threshold=16)
        image.show(thres_s, "8.3. Mask(threshold(<7.Saturation>, 16))")
        image.show(diff, "8.6. Dilate(Set(<7.Value>, 0, <8.3>), 10)")

    cdiff = cv.CountNonZero(diff)
    if cdiff > 0 and cdiff / float(npixels) > 0.01:
        test = cv.CloneImage(v)
        cv.Set(test, 0, image.invert(diff))
        s = cv.Sum(test)[0] / float(cdiff * 255)
        if debug:
            print '\tLight Exposure Score:', s
    else:
        s = 0

    if image.is_grayscale(im):
        return focused, (1, 1, 1, saturation_score, s)

    # we want to short circuit ASAP to avoid doing KMeans 50% of the image's pixels
    if len(points) > npixels / 2:
        return focused, (1, 1, 1, saturation_score, s)

    # we're so blurry we don't have any points!
    if len(points) < 1:
        return focused, (0, 0, 0, saturation_score, s)

    if debug:
        im2 = cv.CloneImage(im)
    focused_regions = image.new_from(im)
    cv.Set(focused_regions, 0)

    r = lambda x: random.randrange(1, x)
    groups = form_groups(points,
                         estimated_size=min(max(int(len(points) / 1000), 2),
                                            15))
    #groups = form_groups(points, threshold=max(cv.GetSize(im))/16)
    #print 'groups', len(groups)
    hulls = draw_groups(groups, focused_regions)
    focused_regions = image.threshold(focused_regions,
                                      threshold=32,
                                      type=cv.CV_THRESH_TOZERO)
    min_area = npixels * 0.0005
    densities = [h.points_per_pixel() for h in hulls if h.area() >= min_area]

    if debug:
        #image.show(focused, "Focused Points")
        image.show(focused_regions, "9. Focused Regions from <4>")
        cv.Sub(
            im2,
            image.gray2rgb(
                image.invert(image.threshold(focused_regions, threshold=1))),
            im2)
        image.show(im2, "10. threshold(<9>)")

    focused_regions = image.rgb2gray(focused_regions)

    densities = array(densities)
    c = cv.CountNonZero(focused_regions)
    c /= float(npixels)

    score = (c, densities.mean(), densities.std(), saturation_score, s)

    return focused, score
Example #24
0
for x in range(256):
    for y in range(size[1]):
        cv.Set2D(u, y, x, x)
        cv.Set2D(v, 255 - x, min(y, 255), x)

image.show(u, "U")
image.show(v, "V")

rgb = image.luv2rgb(image.merge(l, u, v))
r, g, b = image.split(rgb)
#xor = image.threshold(image.Xor(u,v), 0, cv.CV_THRESH_BINARY)
xor = image.Xor(u, v)
cv.Threshold(xor, xor, 16, 255, cv.CV_THRESH_TOZERO)
image.show(rgb, "RGB")
image.show(xor, "Xor")

#cv.Sub(rgb, image.gray2rgb(image.invert(xor)), rgb)
_, sat, _ = image.split(image.rgb2hsv(rgb))
image.show(sat, 'Saturation')
#cv.Set(xor, 0, image.invert(image.threshold(sat, threshold=4)))

cv.Sub(rgb, image.invert(image.gray2rgb(xor)), rgb)

image.show(rgb, "Rgb - Xor")
arr = image.cv2array(xor)
avg_mean, avg_std = arr.mean(), arr.std()
print cv.CountNonZero(xor) / float(size[0] * size[1]), avg_mean, avg_std

cv.WaitKey()
cv.DestroyAllWindows()
#!/usr/bin/env python2.7

import cv

# get orginal image
orig = cv.LoadImage('cake.png')

# show original
cv.ShowImage("orig", orig)

# convert to hsv and get just hue
hsv = cv.CreateImage(cv.GetSize(orig), 8, 3)
hue = cv.CreateImage(cv.GetSize(orig), 8, 1)
sat = cv.CreateImage(cv.GetSize(orig), 8, 1)
val = cv.CreateImage(cv.GetSize(orig), 8, 1)
cv.CvtColor(orig, hsv, cv.CV_RGB2HSV)
cv.Split(hsv, hue, sat, val, None)
#cv.ShowImage("hue", hue)

# loop to find how many different hues are present...
query = cv.CreateImage(cv.GetSize(orig), 8, 1)
result = cv.CreateImage(cv.GetSize(orig), 8, 1)
for i in range(0, 255):
    cv.Set(query, i)
    cv.Cmp(query, hue, result, cv.CV_CMP_EQ)
    # if a number of pixels are equal - show where they are
    if (cv.CountNonZero(result) > 1000):  # <-what is signficant?
        cv.ShowImage(str(i), result)
        cv.SaveImage(str(i) + ".png", result)
        cv.WaitKey(-1)
#print x

    subparte_largura = regiao_de_interesse.width / 3
    subparte_altura = regiao_de_interesse.height / 3
    area_subparte = subparte_largura * subparte_altura
    for i in range(0, 3):
        for j in range(0, 3):
            l = i * 3
            retangulo = (subparte_largura * i, subparte_altura * j,
                         subparte_largura, subparte_altura)
            cv.SetImageROI(regiao_de_interesse, retangulo)
            quadrante.append(
                cv.CreateImage((subparte_largura, subparte_altura),
                               regiao_de_interesse.depth,
                               regiao_de_interesse.channels))
            pixelsbrancos.append(cv.CountNonZero(regiao_de_interesse))
            porcentagem.append(
                float(pixelsbrancos[l + j]) / float(area_subparte))
            cv.Copy(regiao_de_interesse, quadrante[l + j])
            cv.ResetImageROI(regiao_de_interesse)

    os.system("clear")
    #print porcentagem
    print pixelsbrancos

    cv.ShowImage("Mascara", mascara)
    cv.ShowImage("Binario", cinza)
    cv.ShowImage("Webcam", imagem)
    cv.ShowImage('Regiao de Interesse', regiao_de_interesse)

    if cv.WaitKey(7) % 0x100 == 27:
Example #27
0
            framecnt = 0
            stime = ctime

    # Get original image
    img = cv.QueryFrame(cam)

    # Adaptive diff
    #cv.Smooth(img, col, cv.CV_GAUSSIAN, 3, 0)
    cv.AddWeighted(img, 1 - ADAPT, avg, ADAPT, 0, avg)
    cv.AbsDiff(img, avg, col)
    cv.CvtColor(col, col, cv.CV_RGB2HSV)
    cv.Split(col, None, None, val, None)
    cv.CmpS(val, TH, val, cv.CV_CMP_GE)
    #cv.Dilate(val, val, None, 18)
    #cv.Erode(val, val, None, 10)
    dif = cv.CountNonZero(val)

    # Show image if it's radically new
    if (dif < PIXCOUNT):
        if act < 0:
            act = ACTLOW
        else:
            act -= 1
    else:
        if act > 0:
            act = ACTHIGH
        else:
            act += 1
            if act > 0:
                cv.SaveImage('/tmp/tmp.png', img)
                shutil.move('/tmp/tmp.png', "/shared/%s.png" % (time()))
Example #28
0
def main():
    os.chdir(sys.argv[1])
    try:
        os.mkdir(OUTPUT_DIR_NAME)
    except OSError:
        pass

    tree = et.parse("project.xml")

    movie = tree.getroot()
    file_path = movie.attrib["path"]

    if DEBUG:
        cv.NamedWindow("win", cv.CV_WINDOW_AUTOSIZE)
        cv.MoveWindow("win", 200, 200)

    cap = cv.CreateFileCapture(file_path)
    skip_frames(cap, movie)

    pixel_count = None
    prev_img = None

    global_frame_counter = 0
    file_counter = 0

    w = None
    h = None

    output_img = cv.CreateImage((WIDTH, MAX_FRAMES), cv.IPL_DEPTH_8U, 3)

    f = open("shots.txt", "r")
    lines = [line for line in f if line]  # (start_frame, end_frame, duration)
    f.close()

    f_frm = open("motion.txt", "w")
    f_avg = open("motion_shot-avg.txt", "w")
    motion = []

    t = time.time()

    for nr, line in enumerate(lines):
        print(nr + 1), "/", len(lines)

        duration = int(line.split("\t")[2])

        for frame_counter in range(duration):
            img = cv.QueryFrame(cap)
            if not img:
                print "error?"
                print nr, frame_counter
                #break
                return

            if DEBUG:
                cv.ShowImage("win", img)

            global_frame_counter += 1

            if nr == 0 and frame_counter == 0:  # first shot, first frame
                w = img.width
                h = img.height
                pixel_count = float(img.width * img.height)
                prev_img = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3)
                cv.Zero(prev_img)

            diff = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3)
            cv.AbsDiff(img, prev_img, diff)
            cv.Threshold(diff, diff, 10, 255, cv.CV_THRESH_BINARY)
            d_color = 0
            for i in range(1, 4):
                cv.SetImageCOI(diff, i)
                d_color += cv.CountNonZero(diff) / pixel_count
            d_color = d_color / 3  # 0..1
            #print "%.1f" % (d_color*100), "%"

            motion.append(d_color)
            cv.Copy(img, prev_img)

            # WRITE TEXT FILE
            f_frm.write("%f\n" % (d_color))
            if frame_counter == duration - 1:  # last frame of current shot
                motion_value = sum(motion) / len(motion)
                print "average motion:", motion_value
                f_avg.write("%f\t%d\n" % (motion_value, duration))
                motion = []

            # WRITE IMAGE
            if frame_counter == 0:  # ignore each first frame -- the diff after a hard cut is meaningless
                global_frame_counter -= 1
                continue
            else:
                for i in range(WIDTH):
                    value = d_color * 255
                    cv.Set2D(output_img,
                             (global_frame_counter - 1) % MAX_FRAMES, i,
                             cv.RGB(value, value, value))

            if global_frame_counter % MAX_FRAMES == 0:
                cv.SaveImage(
                    os.path.join(OUTPUT_DIR_NAME,
                                 "motion_%03d.png" % (file_counter)),
                    output_img)
                file_counter += 1

            if DEBUG:
                if cv.WaitKey(1) == 27:
                    break

    if global_frame_counter % MAX_FRAMES != 0:
        #cv.SetImageROI(output_img, (0, 0, WIDTH-1, (global_frame_counter % MAX_FRAMES)-1))
        cv.SetImageROI(output_img, (0, 0, WIDTH - 1,
                                    (global_frame_counter - 1) % MAX_FRAMES))
        cv.SaveImage(
            os.path.join(OUTPUT_DIR_NAME, "motion_%03d.png" % (file_counter)),
            output_img)

    f_frm.close()
    f_avg.close()

    if DEBUG:
        cv.DestroyWindow("win")

    print "%.2f min" % ((time.time() - t) / 60)
    #raw_input("- done -")
    return
    def get_point_cloud(self):

        # Scan the scene
        col_associations = self.get_projector_line_associations()

        # Project white light onto the scene to get the intensities of each pixel (for coloring our point cloud)
        illumination_projection = cv.CreateMat(self.projector_info.height,
                                               self.projector_info.width,
                                               cv.CV_8UC1)
        cv.Set(illumination_projection, 255)
        intensities_image = self.get_picture_of_projection(
            illumination_projection)

        # Turn projector off when done with it
        off_projection = cv.CreateMat(self.projector_info.height,
                                      self.projector_info.width, cv.CV_8UC1)
        cv.SetZero(off_projection)
        off_image_message = self.bridge.cv_to_imgmsg(off_projection,
                                                     encoding="mono8")
        self.set_projection(off_image_message)

        camera_model = PinholeCameraModel()
        camera_model.fromCameraInfo(self.camera_info)

        valid_points_mask = cv.CreateMat(self.camera_info.height,
                                         self.camera_info.width, cv.CV_8UC1)
        cv.CmpS(col_associations, -1, valid_points_mask, cv.CV_CMP_NE)

        number_of_valid_points = cv.CountNonZero(valid_points_mask)

        rectified_camera_coordinates = cv.CreateMat(1, number_of_valid_points,
                                                    cv.CV_32FC2)
        scanline_associations = cv.CreateMat(1, number_of_valid_points,
                                             cv.CV_32FC1)
        intensities = cv.CreateMat(1, number_of_valid_points, cv.CV_8UC1)
        i = 0
        for row in range(self.camera_info.height):
            for col in range(self.camera_info.width):
                if valid_points_mask[row, col] != 0:
                    rectified_camera_coordinates[0, i] = (col, row)
                    scanline_associations[0, i] = col_associations[row, col]
                    intensities[0, i] = intensities_image[row, col]
                    i += 1

        cv.UndistortPoints(rectified_camera_coordinates,
                           rectified_camera_coordinates,
                           camera_model.intrinsicMatrix(),
                           camera_model.distortionCoeffs())

        # Convert scanline numbers to plane normal vectors
        planes = self.scanline_numbers_to_planes(scanline_associations)

        camera_rays = project_pixels_to_3d_rays(rectified_camera_coordinates,
                                                camera_model)

        intersection_points = ray_plane_intersections(camera_rays, planes)

        self.point_cloud_msg = sensor_msgs.msg.PointCloud()
        self.point_cloud_msg.header.stamp = rospy.Time.now()
        self.point_cloud_msg.header.frame_id = 'base_link'
        channels = []
        channel = sensor_msgs.msg.ChannelFloat32()
        channel.name = "intensity"
        points = []
        intensities_list = []

        for i in range(number_of_valid_points):
            point = geometry_msgs.msg.Point32()
            point.x = intersection_points[0, i][0]
            point.y = intersection_points[0, i][1]
            point.z = intersection_points[0, i][2]
            if point.z < 0:
                print scanline_associations[0, i]
                print rectified_camera_coordinates[0, i]

            points.append(point)
            intensity = intensities[0, i]
            intensities_list.append(intensity)

        channel.values = intensities_list
        channels.append(channel)
        self.point_cloud_msg.channels = channels
        self.point_cloud_msg.points = points

        return self.point_cloud_msg
Example #30
0
def main():
    BLACK_AND_WHITE = False
    THRESHOLD = 0.48
    BW_THRESHOLD = 0.4

    os.chdir(sys.argv[1])
    try:
        os.mkdir(OUTPUT_DIR_NAME)
    except:
        pass

    if len(sys.argv) > 2:
        if sys.argv[2] == "bw":
            BLACK_AND_WHITE = True
            THRESHOLD = BW_THRESHOLD
            print "##########"
            print " B/W MODE"
            print "##########"

    tree = et.parse("project.xml")
    movie = tree.getroot()
    file_path = movie.attrib["path"]
    cap = cv.CreateFileCapture(file_path)

    if DEBUG:
        cv.NamedWindow("win", cv.CV_WINDOW_AUTOSIZE)
        cv.MoveWindow("win", 200, 200)

    hist = None
    prev_hist = None
    prev_img = None

    pixel_count = None
    frame_counter = 0

    last_frame_black = False
    black_frame_start = -1

    t = time.time()

    while 1:
        img_orig = cv.QueryFrame(cap)

        if not img_orig:  # eof
            cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (frame_counter - 1),
                         prev_img)
            """movie.set("frames", str(frame_counter))
			tree.write("project.xml")"""
            break

        img = cv.CreateImage(
            (int(img_orig.width / 4), int(img_orig.height / 4)),
            cv.IPL_DEPTH_8U, 3)
        cv.Resize(img_orig, img, cv.CV_INTER_AREA)

        if frame_counter == 0:  # erster frame
            cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (0), img)
            pixel_count = img.width * img.height
            prev_img = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3)
            cv.Zero(prev_img)

        if DEBUG and frame_counter % 2 == 1:
            cv.ShowImage("win", img)

        img_hsv = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3)
        cv.CvtColor(img, img_hsv, cv.CV_BGR2HSV)

        # #####################
        # METHOD #1: find the number of pixels that have (significantly) changed since the last frame
        diff = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3)
        cv.AbsDiff(img_hsv, prev_img, diff)
        cv.Threshold(diff, diff, 10, 255, cv.CV_THRESH_BINARY)
        d_color = 0
        for i in range(1, 4):
            cv.SetImageCOI(diff, i)
            d_color += float(cv.CountNonZero(diff)) / float(pixel_count)

        if not BLACK_AND_WHITE:
            d_color = float(d_color / 3.0)  # 0..1

        # #####################
        # METHOD #2: calculate the amount of change in the histograms
        h_plane = cv.CreateMat(img.height, img.width, cv.CV_8UC1)
        s_plane = cv.CreateMat(img.height, img.width, cv.CV_8UC1)
        v_plane = cv.CreateMat(img.height, img.width, cv.CV_8UC1)
        cv.Split(img_hsv, h_plane, s_plane, v_plane, None)
        planes = [h_plane, s_plane, v_plane]

        hist_size = [50, 50, 50]
        hist_range = [[0, 360], [0, 255], [0, 255]]
        if not hist:
            hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, hist_range, 1)
        cv.CalcHist([cv.GetImage(i) for i in planes], hist)
        cv.NormalizeHist(hist, 1.0)

        if not prev_hist:
            prev_hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, hist_range,
                                      1)
            # wieso gibt es kein cv.CopyHist()?!
            cv.CalcHist([cv.GetImage(i) for i in planes], prev_hist)
            cv.NormalizeHist(prev_hist, 1.0)
            continue

        d_hist = cv.CompareHist(prev_hist, hist, cv.CV_COMP_INTERSECT)

        # combine both methods to make a decision
        if ((0.4 * d_color + 0.6 * (1 - d_hist))) >= THRESHOLD:
            if DEBUG:
                if frame_counter % 2 == 0:
                    cv.ShowImage("win", img)
                winsound.PlaySound(soundfile,
                                   winsound.SND_FILENAME | winsound.SND_ASYNC)
            print "%.3f" % ((0.4 * d_color + 0.6 * (1 - d_hist))), "%.3f" % (
                d_color), "%.3f" % (1 - d_hist), frame_counter
            if DEBUG and DEBUG_INTERACTIVE:
                if win32api.MessageBox(0, "cut?", "",
                                       win32con.MB_YESNO) == 6:  #yes
                    cv.SaveImage(
                        OUTPUT_DIR_NAME + "\\%06d.png" % (frame_counter), img)
            else:
                cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (frame_counter),
                             img)

        cv.CalcHist([cv.GetImage(i) for i in planes], prev_hist)
        cv.NormalizeHist(prev_hist, 1.0)

        # #####################
        # METHOD #3: detect series of (almost) black frames as an indicator for "fade to black"
        average = cv.Avg(v_plane)[0]
        if average <= 0.6:
            if not last_frame_black:  # possible the start
                print "start", frame_counter
                black_frame_start = frame_counter
            last_frame_black = True
        else:
            if last_frame_black:  # end of a series of black frames
                cut_at = black_frame_start + int(
                    (frame_counter - black_frame_start) / 2)
                print "end", frame_counter, "cut at", cut_at
                img_black = cv.CreateImage(
                    (img_orig.width / 4, img_orig.height / 4), cv.IPL_DEPTH_8U,
                    3)
                cv.Set(img_black, cv.RGB(0, 255, 0))
                cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (cut_at),
                             img_black)
            last_frame_black = False

        cv.Copy(img_hsv, prev_img)
        frame_counter += 1

        if DEBUG:
            if cv.WaitKey(1) == 27:
                break

    if DEBUG:
        cv.DestroyWindow("win")

    print "%.2f min" % ((time.time() - t) / 60)
    #raw_input("- done -")
    return