def gen_sweep_plane(src_Hs, src_frames): max_poly = None # the bound of the blended image bldIm = None # the blended plane image cost_info = None # An array structure to collect pixel colors of # blended frames nframes = len(src_frames) o = np.array([[0], [0], [1]]) # origin = zero # Get max polygon for k in range(nframes): cpoly = Rect(Point(0, 0), Point(src_frames[k].shape[1], src_frames[k].shape[0])).get_trans_regpoly( src_Hs[k], 10) if (k == 0): max_poly = cpoly else: max_poly = max_poly | cpoly #end if #end for (xmin, xmax, ymin, ymax) = max_poly.boundingBox(0) max_rec = Rect(Point(xmin, ymin), Point(xmax, ymax)) #print "Max: " , max_rec, "W: ", max_rec.width(), " H: ", max_rec.height() #alocate costs and count matrixes cost_info = np.zeros( [int(max_rec.height()), int(max_rec.width()), nframes, 3], dtype=np.int) counts = np.zeros( [int(max_rec.height()), int(max_rec.width())], dtype=np.int) bldIm = cv.CreateMat(int(np.round(max_rec.height())), int(np.round(max_rec.width())), cv.CV_32FC3) cv.Zero(bldIm) for k in range(nframes): cur_H = src_Hs[k] cur_o = np.dot(cur_H, o) # translate the warped frame to origin from topleft corner disp = np.array([[1, 0, (0 - cur_o[0, 0]) / cur_o[2, 0]], [0, 1, (0 - cur_o[1, 0]) / cur_o[2, 0]], [0, 0, 1]]) o_H = np.dot(disp, cur_H) tpoly = Rect(Point(0, 0), Point(src_frames[k].shape[1], src_frames[k].shape[0])).get_trans_poly(cur_H) cpoly = Rect(Point(0, 0), Point(src_frames[k].shape[1], src_frames[k].shape[0])).get_trans_regpoly( cur_H, 10) (xmin, xmax, ymin, ymax) = cpoly.boundingBox(0) frec = Rect(Point(xmin, ymin), Point(xmax, ymax)) mask = gen_mask(frec, tpoly) #print "Rec: ", frec, "W: ", frec.width(), " H: ", frec.height() if k == 0: fwarp = cv2.warpPerspective( src_frames[k], o_H, (int(frec.width()), int(frec.height()))) # get blended image blend_views(bldIm, fwarp, mask, frec, max_rec) # determine costs collect_costs_info(cost_info, counts, fwarp, mask, frec, max_rec, k) else: fwarp = cv2.warpPerspective( src_frames[k], o_H, (int(frec.width()), int(frec.height()))) # get blended image blend_views(bldIm, fwarp, mask, frec, max_rec) # determine costs collect_costs_info(cost_info, counts, fwarp, mask, frec, max_rec, k) #end if #end for bldIm = np.asarray(bldIm) # Scale blended image to 8U bldIm8U = scaleTo8U(bldIm, counts) ## Measure Cost costs = None costs = calculate_costs(cost_info, counts) ##return blended image and costs return (bldIm8U, costs, max_rec)
def gen_sweep_plane(src_Hs, src_frames): max_poly = None # the bound of the blended image bldIm = None # the blended plane image cost_info = None # An array structure to collect pixel colors of # blended frames nframes = len(src_frames) o = np.array([[0], [0], [1]]) # origin = zero # Get max polygon for k in range(nframes): cpoly = Rect(Point(0, 0), Point(src_frames[k].shape[1], src_frames[k].shape[0])).get_trans_regpoly(src_Hs[k], 10) if(k == 0): max_poly = cpoly else: max_poly = max_poly | cpoly #end if #end for (xmin, xmax, ymin, ymax) = max_poly.boundingBox(0) max_rec = Rect(Point(xmin, ymin), Point(xmax, ymax)) #print "Max: " , max_rec, "W: ", max_rec.width(), " H: ", max_rec.height() #alocate costs and count matrixes cost_info = np.zeros([int(max_rec.height()), int(max_rec.width()), nframes, 3], dtype=np.int) counts = np.zeros([int(max_rec.height()), int(max_rec.width())], dtype=np.int) bldIm = cv.CreateMat(int(np.round(max_rec.height())), int(np.round(max_rec.width())), cv.CV_32FC3) cv.Zero(bldIm) for k in range(nframes): cur_H = src_Hs[k] cur_o = np.dot(cur_H, o) # translate the warped frame to origin from topleft corner disp = np.array([[1, 0, (0 - cur_o[0, 0]) / cur_o[2, 0]], [0, 1, (0 - cur_o[1, 0]) / cur_o[2, 0]], [0, 0, 1]]) o_H = np.dot(disp, cur_H) tpoly = Rect(Point(0, 0), Point(src_frames[k].shape[1], src_frames[k].shape[0])).get_trans_poly(cur_H) cpoly = Rect(Point(0, 0), Point(src_frames[k].shape[1], src_frames[k].shape[0])).get_trans_regpoly(cur_H, 10) (xmin, xmax, ymin, ymax) = cpoly.boundingBox(0) frec = Rect(Point(xmin, ymin), Point(xmax, ymax)) mask = gen_mask(frec, tpoly) #print "Rec: ", frec, "W: ", frec.width(), " H: ", frec.height() if k == 0: fwarp = cv2.warpPerspective(src_frames[k], o_H, (int(frec.width()), int(frec.height()))) # get blended image blend_views(bldIm, fwarp, mask, frec, max_rec) # determine costs collect_costs_info(cost_info, counts, fwarp, mask, frec, max_rec, k) else: fwarp = cv2.warpPerspective(src_frames[k], o_H, (int(frec.width()), int(frec.height()))) # get blended image blend_views(bldIm, fwarp, mask, frec, max_rec) # determine costs collect_costs_info(cost_info, counts, fwarp, mask, frec, max_rec, k) #end if #end for bldIm = np.asarray(bldIm) # Scale blended image to 8U bldIm8U = scaleTo8U(bldIm, counts) ## Measure Cost costs = None costs = calculate_costs(cost_info, counts) ##return blended image and costs return (bldIm8U, costs, max_rec)