def adjust_aspect(img, a=1.5, show=False, binarize=False): """Resizes img such that the object defined by ON pixels within has aspect ratio (height / width) a.""" if show: showimg(img) min_rect = cv.BoundingRect(img) wr, hr = min_rect[2:] w, h = cv.GetSize(img) orig_aspect = float(hr) / wr if orig_aspect < a: new_w = w new_h = int(round(a * h / orig_aspect)) else: new_w = int(round(orig_aspect * w / a)) new_h = h # print w, h, new_w, new_h result = cv.CreateMat(new_h, new_w, cv.CV_8U) cv.Resize(img, result) if binarize: b = cv.CreateImage((result.width, result.height), 8, 1) # Threshold of 128 should work if the original image was # binary. cv.Threshold(result, b, 128, 255, cv.CV_THRESH_BINARY | cv.CV_THRESH_OTSU) result = b if show: print "Original: %.2f" % orig_aspect showimg(result) return result
def lines(self, show=False): result = [] line = [] line_no = self.words[0].pos[0] if show: cpimg = cv.CloneMat(self.img) for w in self.words: if w.pos[0] != line_no: line_no = w.pos[0] result.append(line) line = [] if show: print '\n' line.append(w) if show: print w xmin = min([c.bounding_box[0] for c in w.chars]) xmax = max([c.bounding_box[2] for c in w.chars]) ymin = min([c.bounding_box[1] for c in w.chars]) ymax = max([c.bounding_box[3] for c in w.chars]) cv.Rectangle(cpimg, (xmin, ymin), (xmax, ymax), color=128, thickness=2) showimg(cpimg) result.append(line) return result
def make_mask(image, width, height, show=False): x, y, w, h = cv.BoundingRect(image) image = image[y:y + h, x:x + w] result = cv.CreateMat(height, width, cv.CV_8U) cv.Resize(image, result) b = cv.CreateMat(height, width, cv.CV_8U) cv.Threshold(result, b, 128, 1, cv.CV_THRESH_BINARY | cv.CV_THRESH_OTSU) result = b if show: showimg(result) return numpy.asarray(result)
com.xy_sym /= (h / 2) * w return com def __init__(self, width, height, intensity_grid_rows=3, intensity_grid_cols=3): # Cache a copy of the mask with a border around it so we can # find contours. self.border_mask = cv.CreateMat(height + 2, width + 2, cv.CV_8U) self.mask = self.border_mask[1:-1, 1:-1] cv.Set(self.border_mask, 0) self.offset = (-1, -1) self.intensity = -1 self.x_sym = -1 self.y_sym = -1 self.xy_sym = -1 self.intensity_grid = np.zeros((intensity_grid_rows, intensity_grid_cols)) def xnor(x, y): return bool(x) == bool(y) if __name__ == '__main__': import sys from common.show import show as showimg i = cv.LoadImageM(sys.argv[1], cv.CV_LOAD_IMAGE_GRAYSCALE) coms = connected_components(i) print len(coms) for c in sorted(coms, key=lambda c: c.mask.rows * c.mask.cols, reverse=True)[:15]: print c.intensity, c.x_sym, c.y_sym, c.xy_sym showimg(c.mask)