Exemple #1
0
def measure(im, blur, noise=None, debug=False):
    focus_points = blur[0]
    #is_noisy = noise[2]

    size = cv.GetSize(im)
    npixels = size[0] * size[1]
    
    #if focused_regions is None:
    #    focused_regions = image.new_from(im)
    #    cv.Set(focused_regions, 0)
    #    groups = form_groups(focus_points,
    #        estimated_size=min(max(int(len(npixels) / 1000), 2), 15))
    #    #groups = form_groups(points, threshold=max(cv.GetSize(im))/16)
    #    #print 'groups', len(groups)
    #    draw_groups(groups, focused_regions)
    
    im2 = cv.CloneImage(im)
    g = Grid(cv.GetSize(im2))
    if debug:
        image.show(g.draw(im2), "Image with Grid + ROI")
    
    roi = image.new_from(im, nChannels=1)
    cv.Set(roi, 0)
    #g.draw_lines(roi, thickness=int(max(min((size[0] + size[1]) * 1/100.0, 255), 1)))
    g.draw_regions(roi)
    area = cv.Sum(roi)[0]
    
    (_, face_rects), face_score = faces.measure(im)
    face_block = image.new_from(im, nChannels=1)
    cv.Set(face_block, 0)
    for r in face_rects:
        r.draw(face_block, color=cv.RGB(255,255,255), thickness=cv.CV_FILLED)
    
    if debug:
        face_roi = cv.CloneImage(im)
        cv.Set(face_roi, 0, image.invert(roi))
        cv.Set(face_roi, 0, image.invert(image.threshold(face_block, threshold=1)))
        
        image.show(face_block, "Faces in Binary")
        image.show(g.draw(face_roi), "Face + ROI")
        
    return (im, (
         measure_focused_roi(im, roi, area, focus_points, debug),
         #measure_color_roi(im, roi, area, focus_points, debug),
         measure_contrast(im, debug),
         measure_saturation(im, debug),
         faces.measure(im, debug)[1],
    ))
def main(progname, *args):

    parser = OptionParser()
    parser.add_option("-f", "--file", dest="filename", default=None,
        help="analyze a given FILE ending in .jpg or .jpeg", metavar="FILE")
    parser.add_option("-i", "--imageset", dest="imageset", default=None,
        help="Runs on a predefined set of algorithms (li,chow,china,custom)")
    parser.add_option("-d", "--debug", dest="debug", action="store_true", default=False,
        help="Enable visual debugging.")
    parser.add_option("-t", "--type", dest="type", default="all",
        help="Specifies the type of feature to debug. Defaults to all.")

    (options, args) = parser.parse_args(list(args))
    
    if options.imageset:
        if options.imageset == 'li':
            process(li_dir)
            return 0
        elif options.imageset == 'chow':
            process(chow_dir)
            return 0
        elif options.imageset == 'china':
            process(china_dir)
            return 0
        elif options.imageset == 'custom':
            process()
            return 0
            
    if not options.filename:
        print "Please specify a file (--file) or image set (--imageset)."
        return 1
    
    if not options.debug:
        process([load_image(options.filename)])
        return 0
        
    if options.filename.startswith('data/'):
        options.filename = options.filename[len('data/'):]
    
    tdata = load_image(options.filename)
    kind = options.type.lower()
    
    size = None #(320,240,'crop') # (0.5, 0.5, 'resize-p')
    if size is None:
        im = tdata.load()
    elif size[-1] == 'crop':
        im = image.random_cropped_region(tdata.load(), size[:2])
    elif size[-1] == 'resize':
        im = tdata.load(size[:2])
    elif size[-1] == 'resize-p':
        im = image.resize(tdata.load(), by_percent=size[:2])
    else:
        raise TypeError, "Invalid image sizing type."
        
    image.show(im, "Image")
    #l,u,v = image.split(image.rgb2luv(im))
    ##cv.Set(l, 128)
    ##cv.EqualizeHist(l, l)
    ##cv.EqualizeHist(u, u)
    ##image.show(image.luv2rgb(image.merge(l,u,v)), "test")
    #s = cv.GetSize(im)
    #t = image.absDiff(u,v)
    #image.show(t, "test")
    #print "Test Score:", cv.CountNonZero(t) / float(s[0] * s[1])
    ##image.show(image.threshold(image.And(u,v), threshold=1), "LUV")

    # noise
    if kind in ('all','noise'):
        noise_img, score = noise.measure(im, debug=True)
        #image.show(noise_img, "Noise Result")
        print 'Noise Score:', score, noise.boolean(score)
    
    # contrast
    if kind in ('all','contrast'):
        contrast_img, score = contrast.measure(im, debug=True)
        #image.show(contrast_img, "Contrast Result")
        print 'Contrast Score:', score, contrast.boolean(score)
    
    # blur
    if kind in ('all','blur','composition'): 
        focused, score = blur.measure(im, debug=kind in ('all','blur'))
        #image.show(focused,  "Blur Result")
        print 'Blur Score:', score, blur.boolean(score)
    
    # composition
    if kind in ('all','composition'):
        composition_img, score = composition.measure(im,
            (focused,score, blur.boolean(score)), debug=True)
        print 'Composition Score:', score, composition.boolean(score)
        
    if kind in ('faces',):
        result, score = faces.measure(im,debug=True)
        print "Face Score:", score, faces.boolean(faces)
    
    #win = CornerTweaker(im)
    #win.show()
    
    #_, sat, _ = image.split(image.rgb2hsv(im))
    #arr = image.cv2array(sat)
    #print arr.mean(), arr.std()
    
    # faces
    #im, score = faces.measure(im, debug=True)
    #print score, faces.boolean(score)
    
    # composition
    #noise_img, score = noise.measure(im, debug=False)
    ##n = (noise_img, score, noise.boolean(score))
    #hulls, score = blur.measure(im, debug=False)
    #b = (hulls, score, blur.boolean(score))
    #cimg, score = composition.measure(im, b, debug=True)
    #print score, composition.boolean(score)
    
    # BLUR
    #from time import time
    #start = time()
    ##im2 = image.threshold(image.laplace(im), threshold=75, type=cv.CV_THRESH_TOZERO)
    #hulls, score = blur.measure(im, debug=True)
    ##blur_img, score = blur.measure(im, debug=True)
    #end = time()
    #print "Time:", (end - start), "seconds"
    #image.show(im,  "image")
    ##image.show(noise_img, "Noise Image")
    #print score, blur.boolean(score)
    
    
    #CONTRAST
    
    #_, score = contrast.measure(im, debug=True)
    #image.show(im, "Image")
    #print score, contrast.boolean(score)

    """
    
    #BLUR
    
    #im2 = image.threshold(image.laplace(im), threshold=75, type=cv.CV_THRESH_TOZERO)
    im3, score = blur.measure(im, debug=True)
    image.show(im,  "image")
    image.show(im3, "Focus Mask")
    print score, blur.boolean(score)
    #plt.show()
    """

    
    #NOISE
    
    #noise_img, score = noise.measure(im, debug=True)
    #image.show(noise_img, "Noise")
    #print score, noise.boolean(score)
    
    
    """
    #hwin = ColorHistograms(im)
    #hwin.show()
    hwin = HistogramWindow(image.rgb2gray(im))
    hwin.show()
    
    print cv.GetSize(im), cv.GetSize(im2)
    print 'blur', papers.blurry_histogram(im)
    #print papers.blurry_histogram(im2)
    
    wind = DerivativeTweaker(im, title="image derivative")
    wind.show()
    
    win = EdgeThresholdTweaker(im, title="image edges")
    win.show(50)#edge_threshold(im))
    
    #win2 = EdgeThresholdTweaker(im2, title="image resized edges")
    #win2.show(edge_threshold(im2))
    """
    cv.WaitKey()
    cv.DestroyAllWindows()
    return 0
def main(progname, *args):

    parser = OptionParser()
    parser.add_option("-f",
                      "--file",
                      dest="filename",
                      default=None,
                      help="analyze a given FILE ending in .jpg or .jpeg",
                      metavar="FILE")
    parser.add_option(
        "-i",
        "--imageset",
        dest="imageset",
        default=None,
        help="Runs on a predefined set of algorithms (li,chow,china,custom)")
    parser.add_option("-d",
                      "--debug",
                      dest="debug",
                      action="store_true",
                      default=False,
                      help="Enable visual debugging.")
    parser.add_option(
        "-t",
        "--type",
        dest="type",
        default="all",
        help="Specifies the type of feature to debug. Defaults to all.")

    (options, args) = parser.parse_args(list(args))

    if options.imageset:
        if options.imageset == 'li':
            process(li_dir)
            return 0
        elif options.imageset == 'chow':
            process(chow_dir)
            return 0
        elif options.imageset == 'china':
            process(china_dir)
            return 0
        elif options.imageset == 'custom':
            process()
            return 0

    if not options.filename:
        print "Please specify a file (--file) or image set (--imageset)."
        return 1

    if not options.debug:
        process([load_image(options.filename)])
        return 0

    if options.filename.startswith('data/'):
        options.filename = options.filename[len('data/'):]

    tdata = load_image(options.filename)
    kind = options.type.lower()

    size = None  #(320,240,'crop') # (0.5, 0.5, 'resize-p')
    if size is None:
        im = tdata.load()
    elif size[-1] == 'crop':
        im = image.random_cropped_region(tdata.load(), size[:2])
    elif size[-1] == 'resize':
        im = tdata.load(size[:2])
    elif size[-1] == 'resize-p':
        im = image.resize(tdata.load(), by_percent=size[:2])
    else:
        raise TypeError, "Invalid image sizing type."

    image.show(im, "Image")
    #l,u,v = image.split(image.rgb2luv(im))
    ##cv.Set(l, 128)
    ##cv.EqualizeHist(l, l)
    ##cv.EqualizeHist(u, u)
    ##image.show(image.luv2rgb(image.merge(l,u,v)), "test")
    #s = cv.GetSize(im)
    #t = image.absDiff(u,v)
    #image.show(t, "test")
    #print "Test Score:", cv.CountNonZero(t) / float(s[0] * s[1])
    ##image.show(image.threshold(image.And(u,v), threshold=1), "LUV")

    # noise
    if kind in ('all', 'noise'):
        noise_img, score = noise.measure(im, debug=True)
        #image.show(noise_img, "Noise Result")
        print 'Noise Score:', score, noise.boolean(score)

    # contrast
    if kind in ('all', 'contrast'):
        contrast_img, score = contrast.measure(im, debug=True)
        #image.show(contrast_img, "Contrast Result")
        print 'Contrast Score:', score, contrast.boolean(score)

    # blur
    if kind in ('all', 'blur', 'composition'):
        focused, score = blur.measure(im, debug=kind in ('all', 'blur'))
        #image.show(focused,  "Blur Result")
        print 'Blur Score:', score, blur.boolean(score)

    # composition
    if kind in ('all', 'composition'):
        composition_img, score = composition.measure(
            im, (focused, score, blur.boolean(score)), debug=True)
        print 'Composition Score:', score, composition.boolean(score)

    if kind in ('faces', ):
        result, score = faces.measure(im, debug=True)
        print "Face Score:", score, faces.boolean(faces)

    #win = CornerTweaker(im)
    #win.show()

    #_, sat, _ = image.split(image.rgb2hsv(im))
    #arr = image.cv2array(sat)
    #print arr.mean(), arr.std()

    # faces
    #im, score = faces.measure(im, debug=True)
    #print score, faces.boolean(score)

    # composition
    #noise_img, score = noise.measure(im, debug=False)
    ##n = (noise_img, score, noise.boolean(score))
    #hulls, score = blur.measure(im, debug=False)
    #b = (hulls, score, blur.boolean(score))
    #cimg, score = composition.measure(im, b, debug=True)
    #print score, composition.boolean(score)

    # BLUR
    #from time import time
    #start = time()
    ##im2 = image.threshold(image.laplace(im), threshold=75, type=cv.CV_THRESH_TOZERO)
    #hulls, score = blur.measure(im, debug=True)
    ##blur_img, score = blur.measure(im, debug=True)
    #end = time()
    #print "Time:", (end - start), "seconds"
    #image.show(im,  "image")
    ##image.show(noise_img, "Noise Image")
    #print score, blur.boolean(score)

    #CONTRAST

    #_, score = contrast.measure(im, debug=True)
    #image.show(im, "Image")
    #print score, contrast.boolean(score)
    """
    
    #BLUR
    
    #im2 = image.threshold(image.laplace(im), threshold=75, type=cv.CV_THRESH_TOZERO)
    im3, score = blur.measure(im, debug=True)
    image.show(im,  "image")
    image.show(im3, "Focus Mask")
    print score, blur.boolean(score)
    #plt.show()
    """

    #NOISE

    #noise_img, score = noise.measure(im, debug=True)
    #image.show(noise_img, "Noise")
    #print score, noise.boolean(score)
    """
    #hwin = ColorHistograms(im)
    #hwin.show()
    hwin = HistogramWindow(image.rgb2gray(im))
    hwin.show()
    
    print cv.GetSize(im), cv.GetSize(im2)
    print 'blur', papers.blurry_histogram(im)
    #print papers.blurry_histogram(im2)
    
    wind = DerivativeTweaker(im, title="image derivative")
    wind.show()
    
    win = EdgeThresholdTweaker(im, title="image edges")
    win.show(50)#edge_threshold(im))
    
    #win2 = EdgeThresholdTweaker(im2, title="image resized edges")
    #win2.show(edge_threshold(im2))
    """
    cv.WaitKey()
    cv.DestroyAllWindows()
    return 0