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 measure_focused_roi(im, roi, area, focus_points, debug=False): g = Grid(cv.GetSize(im)) canvas = image.new_from(im) cv.Set(canvas, 0) focus_in_roi = image.And(focus_points, roi) if debug: image.show(focus_in_roi, "ROI + Focused Points") densities = [] points = convert_to_points(focus_in_roi) groups = form_groups(points, estimated_size=24, iter=5) for group in groups: ch = ConvexHull(map(lambda x: (x[0], x[1]), group)) ppp = ch.points_per_pixel() a = int(ppp * 255) ch.draw_filled_hull(canvas, rgb=(a,a,a)) if debug: image.show(canvas, "Focused Regions in ROI") quadrants = g.split_in_four(canvas) sums = [] for i,quad in enumerate(quadrants): sums.append(cv.Sum(quad)[0] / float(area/4)) arr = array(sums) print arr.mean(), arr.std() diff = max(sums) - min(sums) return diff, arr.std()
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
def draw(self, images, layers): if layers == 0: gray, r,g,b = images im = cv.CloneImage(r) for i in (g,b): im = image.add(im, i) white = image.new_from(gray) cv.Set(white, (0,0,0)) cv.Set(white, (255,255,255), image.invert(image.threshold(im, threshold=1))) im = image.Or(image.blend(im, gray), white) else: im = cv.CloneImage(images[layers-1]) if layers != 1: white = image.new_from(images[0]) cv.Set(white, (0,0,0)) cv.Set(white, (255,255,255), image.invert(image.threshold(im, threshold=1))) im = image.Or(im, white) return im
def draw(self, images, layers): if layers == 0: gray, r, g, b = images im = cv.CloneImage(r) for i in (g, b): im = image.add(im, i) white = image.new_from(gray) cv.Set(white, (0, 0, 0)) cv.Set(white, (255, 255, 255), image.invert(image.threshold(im, threshold=1))) im = image.Or(image.blend(im, gray), white) else: im = cv.CloneImage(images[layers - 1]) if layers != 1: white = image.new_from(images[0]) cv.Set(white, (0, 0, 0)) cv.Set(white, (255, 255, 255), image.invert(image.threshold(im, threshold=1))) im = image.Or(im, white) return im
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
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
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
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)
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
import image import cv l = cv.CreateImage((256, 256), cv.IPL_DEPTH_8U, 1) cv.Set(l, 255) u = image.new_from(l) v = image.new_from(l) cv.Set(u, 0) cv.Set(v, 0) size = cv.GetSize(l) print size 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))
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
import image import cv l = cv.CreateImage((256,256), cv.IPL_DEPTH_8U, 1) cv.Set(l, 255) u = image.new_from(l) v = image.new_from(l) cv.Set(u, 0) cv.Set(v, 0) size = cv.GetSize(l) print size 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))
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): 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