def generateSliceRowMark(self): if self.GEN_SLICEROW_MARK: demot = self.loadImageRow() carver = SeamMarker(img=demot) clip = demot[150:250, :] clip, mask = carver.mark_row(clip) Image.fromarray(clip).save(self.sliceRowMarkPath)
def generateMarkedRowsImage(self): if self.GEN_MARK_ROWS: demot = self.loadImageRow() marker = SeamMarker(demot) points = loadJfile(self.points_left_path) markedImage = marker.markPointListSeam(img=demot, plist=points) Image.fromarray(markedImage).save(self.markRowsImagePath)
def test_seammarker_addPointSlice2Image_colSliceTrue_isUpToFalse(self): isUpTo = False colSlice = True viet = self.loadImageCol() points = getPointListFromPointPath(self.points_down_path) point = points[0] marker = SeamMarker(viet) before = 138 after = 180 compcolslice = os.path.join(self.imagedir, 'colslice.png') compimg = np.array(Image.open(compcolslice)) compcp = compimg.copy() compcphalf = compcp.shape[1] // 2 compcp[:, compcphalf:compcphalf + 3] = 255 addedimg = marker.addPointSlice2Image(viet, point, beforeAfterCoord=(before, after), colSlice=colSlice, imgSlice=compcp, isUpTo=isUpTo) compimg = os.path.join(self.imagedir, 'columnAddedImage.png') compimg = np.array(Image.open(compimg)) self.compareArrays( addedimg, compimg, "Added column does not give the expected added image")
def generateMarkedColumnsImage(self): if self.GEN_MARK_COLS: viet = self.loadImageCol() marker = SeamMarker(viet) points = loadJfile(self.points_down_path) markedImage = marker.markPointListSeam(img=viet, plist=points) Image.fromarray(markedImage).save(self.markColumnsImagePath)
def generateBacktrackSliceMatrix(self): if self.GEN_SLICE_BACKTRACK_MAT: vietcp = self.loadImageCol() vietslice = vietcp[:, 550:600] carver = SeamMarker(img=vietcp) emap = carver.calc_energy(vietslice.copy()) mat, backtrack = carver.minimum_seam(img=vietslice, emap=emap) np.save(self.slicemat_backtrack_path, backtrack)
def getCoordPair(coordpath, colSlice: bool): "Get coordinate pair from coordpath and image path" img = np.zeros((2, 2), dtype=np.uint8) marker = SeamMarker(img) coords = getCoordsDict(coordpath) plist = list(coords.keys()) pairs = marker.makePairsFromPoints(plist, colSlice=colSlice) return pairs[0]
def generateSliceMark(self): if self.GEN_SLICE_MARK: viet = self.loadImageCol() vietcp = viet.copy() vietslice = vietcp[:, 550:600] carver = SeamMarker(img=vietcp) slicp = vietslice.copy() imcp, mask = carver.mark_column(slicp) Image.fromarray(imcp).save(self.slicemarkPath)
def generateSliceMatrix(self): if self.GEN_SLICE_MAT: img = self.loadImageCol() viet = np.array(img.copy(), dtype=np.uint8) vietslice = viet[:, 550:600] carver = SeamMarker(img=None) emap = carver.calc_energy(viet.copy()) mat, backtrack = carver.minimum_seam(img=vietslice.copy(), emap=emap) np.save(self.slicemat_path, mat)
def test_seammarker_markPointListSeam_down(self): viet = self.loadImageCol() marker = SeamMarker(viet) points = loadJfile(self.points_down_path) markedImage = marker.markPointListSeam(img=viet, plist=points) # pdb.set_trace() compimg = os.path.join(self.imagedir, 'markedColumnsImage.png') compimg = np.array(Image.open(compimg)) self.compareArrays(markedImage, compimg, "Marked image is not equivalent to expected image")
def test_seammarker_markPointListSeam_left(self): demot = self.loadImageRow() marker = SeamMarker(demot) points = loadJfile(self.points_left_path) markedImage = marker.markPointListSeam(img=demot, plist=points) # pdb.set_trace() compimg = os.path.join(self.imagedir, 'markedRowsImage.png') compimg = np.array(Image.open(compimg)) self.compareArrays(markedImage, compimg, "Marked image is not equivalent to expected image")
def generateRowSlice(self): if self.GEN_ROW_SLICE: demot = self.loadImageRow() points = getPointListFromPointPath(self.points_left_path) point = points[0] carver = SeamMarker(demot) rowslice = carver.getRowSliceOnPoint(point, demot.copy(), isUpTo=True, thresh=5)[0] Image.fromarray(rowslice).save(self.rowslicePath)
def test_seammarkerk_mark_row(self): demot = self.loadImageRow() compimg = os.path.join(self.imagedir, 'slicerowmark.png') compimg = np.array(Image.open(compimg)) carver = SeamMarker(img=demot) clip = demot[150:250, :] clip, mask = carver.mark_row(clip) # pdb.set_trace() self.compareArrays( clip, compimg, "Point carver mark row function emap not given, " "checking if function produces same marks on same slice")
def generateColSlice(self): if self.GEN_COL_SLICE: viet = self.loadImageCol() points = getPointListFromPointPath(self.points_down_path) point = points[0] marker = SeamMarker(viet) colslice = marker.getColumnSliceOnPoint(point, viet, thresh=5, isUpTo=False)[0] Image.fromarray(colslice).save(self.colslicePath)
def test_seammarker_minimum_seam_emap_matrix(self): "tests the minimum seam function of pointcarver" matrixPath = os.path.join(self.npdir, "vietSliceMatrix.npy") compmatrix = np.load(matrixPath) vietImcp = self.loadImageCol() vietslice = vietImcp[:, 550:600] carver = SeamMarker(img=vietImcp) emap = carver.calc_energy(vietslice) mat, backtrack = carver.minimum_seam(img=vietslice, emap=emap) self.compareArrays( mat, compmatrix, "Point carver minimum seam function emap given, checking matrix")
def generateSegments(self): if self.GEN_SEGMENTS: img = self.loadImageCol() marker = SeamMarker(img=np.zeros((2, 2), dtype=np.uint8)) coords = prepPointCoord(self.coords_down_path) segments = marker.segmentImageWithPointListSeamCoordinate( image=img, coords=coords) paths = self.segmentPaths [[ Image.fromarray(segs[i]).save(paths[i]) for i in range(len(segs)) ] for direction, segs in segments.items()]
def test_seammarker_minimum_seam_backtrack(self): backtrackPath = os.path.join(self.npdir, 'vietSliceBacktrack.npy') compBacktrack = np.load(backtrackPath) vietcp = self.loadImageCol() vietslice = vietcp[:, 550:600] carver = SeamMarker(img=vietcp) mat, backtrack = carver.minimum_seam(img=vietslice) self.compareArrays( backtrack, compBacktrack, "Point carver minimum seam function emap not given, " "checking backtrack")
def test_seammarker_getRowSliceOnPointIsUpToTrue(self): demot = self.loadImageRow() points = getPointListFromPointPath(self.points_left_path) point = points[0] carver = SeamMarker(demot) rowslice = carver.getRowSliceOnPoint(point, demot, isUpTo=True, thresh=5)[0] compimg = os.path.join(self.imagedir, 'rowslice.png') compimg = np.array(Image.open(compimg)) self.compareArrays(rowslice, compimg, "rowslice is not the same for sliced image")
def generateSlicedImage(self): if self.GEN_SLICE_IMAGE: viet = self.loadImageCol() m1 = os.path.join(self.npdir, 'matchedMarkCoordPair1ColSliceTrueDown.npy') m1 = np.load(m1) m2 = os.path.join(self.npdir, 'matchedMarkCoordPair2ColSliceTrueDown.npy') m2 = np.load(m2) marker = SeamMarker(viet) colSlice = True swaped = marker.swapAndSliceMarkCoordPair(m1, m2, viet, colSlice) Image.fromarray(swaped).save(self.sliceImagePath)
def test_seammarker_segmentImageWithPointListSeamCoordinate(self): img = self.loadImageCol() marker = SeamMarker(img=np.zeros((2, 2), dtype=np.uint8)) coords = prepPointCoord(self.coords_down_path) segments = marker.segmentImageWithPointListSeamCoordinate( image=img, coords=coords) compsegs = self.loadSegments() message = "Segment {0} failed" # pdb.set_trace() compvals = [[ self.compareArrays(segs[i], compsegs[i], message.format(str(i))) for i in range(len(segs)) ] for direction, segs in segments.items()]
def test_seammarker_mark_column(self): compimpath = os.path.join(self.imagedir, 'slicemark.png') viet = self.loadImageCol() sliceImage = np.array(Image.open(compimpath), dtype=np.uint8) vietcp = viet.copy() vietslice = vietcp[:, 550:600] carver = SeamMarker(img=vietcp) slicp = vietslice.copy() imcp, mask = carver.mark_column(slicp) self.compareArrays( imcp, sliceImage, "Point carver mark column function emap not given, " "checking if function produces same marks on same slice")
def test_seammarker_matchMarkCoordPairLength_left_colSliceFalse(self): colSlice = False isUpTo = True pair = getCoordPair(self.coords_left_path, colSlice) coords = getCoordsDict(self.coords_left_path) point1 = pair[0] point2 = pair[1] marker = SeamMarker(img=np.zeros((2, 2), dtype=np.uint8)) coord1 = coords[point1] coord2 = coords[point2] # pdb.set_trace() retval1, retval2 = marker.matchMarkCoordPairLength( coord1, coord2, colSlice, isUpTo) self.assertEqual(retval1[-1][1], retval2[-1][1])
def test_seammarker_getColSliceOnPointIsUpToFalse(self): viet = self.loadImageCol() points = getPointListFromPointPath(self.points_down_path) point = points[0] marker = SeamMarker(viet) colslice = marker.getColumnSliceOnPoint(point, viet, thresh=5, isUpTo=False)[0] # pdb.set_trace() compimg = os.path.join(self.imagedir, 'colslice.png') compimg = np.array(Image.open(compimg)) self.compareArrays(colslice, compimg, "column slice is not the same for sliced image")
def test_seammarker_markPointSeam_left(self): demot = self.loadImageRow() points = loadJfile(self.points_left_path) point = points["2"] point_coord = (point['y'], point['x']) marker = SeamMarker(demot) markedImage = marker.markPointSeam(img=demot, point=point_coord, direction=point['direction'], thresh=point['threshold']) # pdb.set_trace() compimg = os.path.join(self.imagedir, 'rowMarkedImage.png') compimg = np.array(Image.open(compimg)) self.compareArrays(markedImage, compimg, "Marked image is not equivalent to expected image")
def test_seammarker_markPointSeam_down(self): viet = self.loadImageCol() points = loadJfile(self.points_down_path) point = points["0"] point_coord = (point['y'], point['x']) marker = SeamMarker(viet) markedImage = marker.markPointSeam(img=viet.copy(), point=point_coord, direction=point['direction'], thresh=point['threshold']) # pdb.set_trace() compimg = os.path.join(self.imagedir, 'colMarkedImage.png') compimg = np.array(Image.open(compimg)) self.compareArrays(markedImage, compimg, "Marked image is not equivalent to expected image")
def generateMatchCoordinateMatrix(self, colSlice: bool, isUpTo: bool): if self.GEN_MATCH_COORD_PAIRS: firstPath = self.match_coord_path.format(str(1)) secondPath = self.match_coord_path.format(str(2)) coords = getCoordsDict(self.coords_down_path) pair = getCoordPair(self.coords_down_path, colSlice) p1 = pair[0] p2 = pair[1] coord1 = coords[p1] coord2 = coords[p2] marker = SeamMarker(img=np.zeros((2, 2), dtype=np.uint8)) markCoord1, markCoord2 = marker.matchMarkCoordPairLength( coord1, coord2, colSlice, isUpTo) np.save(firstPath, markCoord1) np.save(secondPath, markCoord2)
def generateColMarkedImage(self): if self.GEN_COL_MARKED: isUpTo = False colSlice = True viet = self.loadImageCol() points = loadJfile(self.points_down_path) point = points["0"] point_coord = (point['y'], point['x']) marker = SeamMarker(viet) markedImage = marker.markSeam4Point(viet, point_coord, isUpTo, point['threshold'], colSlice, mark_color=[0, 255, 0]) Image.fromarray(markedImage).save(self.colMarkedImagePath)
def generateRowMarkedImage(self): if self.GEN_ROW_MARKED: isUpTo = True colSlice = False demot = self.loadImageRow() points = loadJfile(self.points_left_path) point = points["2"] point_coord = (point['y'], point['x']) marker = SeamMarker(demot) markedImage = marker.markSeam4Point(demot, point_coord, isUpTo, point['threshold'], colSlice, mark_color=[0, 255, 0]) Image.fromarray(markedImage).save(self.rowMarkedImagePath)
def test_seammarker_calc_energy(self): "tests the calc energy function of pointcarver" vietEmap = Image.open(self.emap_path) vietEmap = ImageOps.grayscale(vietEmap) vietImg = self.loadImageCol() vietEmap = np.array(vietEmap, dtype=np.uint8) # vietImcp = vietImg.copy() vietEmapcp = vietEmap.copy() # carver = SeamMarker(img=vietImcp) emap = carver.calc_energy(vietImcp) emap = np.interp(emap, (emap.min(), emap.max()), (0, 256)) emap = np.uint8(emap) self.compareArrays(emap, vietEmapcp, "Point carver energy calculation function")
def test_seammarker_expandPointCoordinate_colBeforeIsZero(self): "Coord after is above ubound" viet = self.loadImageCol() vietcp = viet.copy() carver = SeamMarker(img=vietcp) colBefore, colAfter = carver.expandPointCoordinate(80, coord=1, thresh=5) colbef_comp, col_after_comp = 0, 3 message = "Point column coordinate is expanded to {0} " message1 = "minimum column before is normally lower than 0" message2 = "maximum column after is not normally" self.assertEqual(colBefore, colbef_comp, message.format('left') + message1) self.assertEqual(colAfter, col_after_comp, message.format('right') + message2)
def test_seammarker_swapAndSliceMarkCoordPair_down_colSliceTrue(self): viet = self.loadImageCol() # pdb.set_trace() m1 = os.path.join(self.npdir, 'matchedMarkCoordPair1ColSliceTrueDown.npy') m1 = np.load(m1) m2 = os.path.join(self.npdir, 'matchedMarkCoordPair2ColSliceTrueDown.npy') m2 = np.load(m2) marker = SeamMarker(viet) colSlice = True swaped = marker.swapAndSliceMarkCoordPair(m1, m2, viet, colSlice) # pdb.set_trace() compimg = os.path.join(self.imagedir, 'slicedImage.png') compimg = np.array(Image.open(compimg)) self.compareArrays(compimg, swaped, "Image slicing with mark coordinate has failed")