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 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 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 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()) plist = [{"x": p[1], "y": p[0]} for p in plist] pairs = marker.makePairsFromPoints(plist, colSlice=colSlice) return pairs[0]
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_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 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 generateInferior2MeanMatrix(self): if self.GEN_MEAN_INF_ZONE_MAT: vietImcp = self.loadImageCol() vietslice = vietImcp[:, 550:600] carver = SeamMarker(img=vietImcp) emap = carver.calc_energy(vietslice) normalized = normalizeImageVals(emap.copy()) # vietEmap = ImageOps.grayscale(Image.fromarray(emap)) funcs = SeamFuncsAI() zones = funcs.getInferior2MeanZones(normalized) np.save(self.mean_inf_zone_mat_path, zones)
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 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_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_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_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 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_matchMarkCoordPairLength_down_colSliceTrue(self): colSlice = True isUpTo = False pair = getCoordPair(self.coords_down_path, colSlice) coords = getCoordsDict(self.coords_down_path) point1 = pair[0] point2 = pair[1] marker = SeamMarker(img=np.zeros((2, 2), dtype=np.uint8)) coord1 = coords[(point1['y'], point1['x'])] coord2 = coords[(point2['y'], point2['x'])] retval1, retval2 = marker.matchMarkCoordPairLength( coord1, coord2, colSlice, isUpTo) # pdb.set_trace() self.assertEqual(retval1[-1][0], retval2[-1][0])
def test_seammarker_markPointSeam_down(self): viet = self.loadImageCol() points = loadJfile(self.points_down_path) point = points["0"] marker = SeamMarker(viet) markedImage = marker.markPointSeam(img=viet.copy(), point=point, 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 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, 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 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 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 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_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")
def test_seammarker_markSeam4Point_colSliceTrue_isUpToFalse(self): 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, isUpTo, point['threshold'], colSlice, mark_color=[0, 255, 0]) 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 test_seammarker_markSeam4Point_colSliceFalse_isUpToTrue(self): 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, isUpTo, point['threshold'], colSlice, mark_color=[0, 255, 0]) # 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_getPointSeamCoordinate_down(self): viet = self.loadImageCol() points = getPointListFromPointPath(self.points_down_path) point = points[0] marker = SeamMarker(viet) jfile = loadJfile(self.coords_down_path) # pdb.set_trace() pointData = getCoordWithPoint(self.coords_down_path, point) thresh_val = pointData['threshold'] direction = pointData['direction'] coords = marker.getPointSeamCoordinate(viet.copy(), point, direction=direction, thresh=thresh_val) coords = shapeCoordinate(coords) ccoord = np.array(pointData["seamCoordinates"], dtype=np.int) # pdb.set_trace() self.compareArrays(coords, ccoord, "Computed coordinates are not equal")
def generateRowSlice(self): if self.GEN_ROW_SLICE: demot = self.loadImageRow() points = getPointListFromPointPath(self.points_left_path) point = points[0] carver = SeamMarker(demot) slicer = PointSlicer(point, demot) rowslice = slicer.getRowSliceOnPoint(point, demot.copy(), isUpTo=True, thresh=5)[0] Image.fromarray(rowslice).save(self.rowslicePath)
def test_seammarker_sliceImageWithMarkCoordPair_down_colSliceTrue(self): "test seam marker slice Image with mark coordinate pair" viet = self.loadImageCol() marker = SeamMarker(img=np.zeros((2, 2), dtype=np.uint8)) coords = getCoordsDict(self.coords_down_path) colSlice = True isUpTo = False pair = getCoordPair(self.coords_down_path, colSlice) p1 = pair[0] p2 = pair[1] coord1 = coords[(p1['y'], p1['x'])] coord2 = coords[(p2['y'], p2['x'])] coord1_2d = coord1[:, :2] coord2_2d = coord2[:, :2] uni1 = np.unique(coord1_2d, axis=0) uni2 = np.unique(coord2_2d, axis=0) segment = marker.sliceImageWithMarkCoordPair(viet.copy(), uni1, uni2, colSlice, isUpTo) # pdb.set_trace() compimg = os.path.join(self.imagedir, 'slicedImage.png') compimg = np.array(Image.open(compimg)) self.compareArrays(compimg, segment, "Image slicing with mark coordinate has failed")