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()
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()
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
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