def test_Transform(self): trapez = ChessTrapezoid([(20, 40), (44, 38), (60, 8), (10, 10)]) #if debug: # plt.figure() # plt.plot(trapez.pts_normedSquare[[0,1,2,3,0], 0],trapez.pts_normedSquare[[0,1,2,3,0], 1], '-') squarePatches = [] tSquarePatches = [] # https://stackoverflow.com/questions/26935701/ploting-filled-polygons-in-python for tsquare in trapez.genSquares(): color = [1, 1, 1, 1] if tsquare.fieldColor else [0.1, 0.1, 0.1, 1] polygon = Polygon(tsquare.rpolygon, color=color) tpolygon = Polygon(tsquare.polygon, color=color) squarePatches.append(polygon) tSquarePatches.append(tpolygon) # https://stackoverflow.com/questions/44725201/set-polygon-colors-matplotlib squareP = PatchCollection(squarePatches, match_original=True) tSquareP = PatchCollection(tSquarePatches, match_original=True) if debug: fig, ax = plt.subplots(2) ax1 = ax[0] ax1.set_title("relative normed square") ax1.add_collection(squareP) ax1.set_ylim(ax1.get_ylim()[::-1]) ax2 = ax[1] ax2.set_title("trapezoidal warp") ax2.add_collection(tSquareP) ax2.autoscale() plt.show()
def setup(self): self.trapezoid=ChessTrapezoid(self.points,rotation=self.rotation,idealSize=self.idealSize) if self.ans is None: self.ans=[] for tsquare in self.trapezoid.genSquares(): self.ans.append(tsquare.an) return self
def test_SortedTSquares(self): trapezoid=ChessTrapezoid([(140,5),(506,10),(507,377),(137,374)]) trapezoid.updatePieces(chess.STARTING_FEN) sortedTSquares=trapezoid.byFieldState() expected=[16,8,8,16,8,8] for fieldState,tsquareList in sortedTSquares.items(): l=len(tsquareList) if debug: print(fieldState.title(),l) assert expected[fieldState]==l
def test_RelativeToTrapezXY(self): trapez=ChessTrapezoid([(20,40),(40,40),(60,10),(10,10)]) rxrys=[(0,0),(1,0),(1,1),(0,1),(0,0.5),(0.5,0.5),(1,0.5)] expected=[(20,40),(40,40),(60,10),(10,10),(17.1,31.4),(31.4,31.4),(45.7,31.4)] for index,rxry in enumerate(rxrys): rx,ry=rxry x,y=trapez.relativeToTrapezXY(rx,ry) ex,ey=expected[index] print ("%d r:(%.1f,%.1f) e:(%.1f,%.1f) == (%.1f,%.1f)?" % (index,rx,ry,ex,ey,x,y)) assert x ==pytest.approx(ex,0.1) assert y ==pytest.approx(ey,0.1)
def test_Histogram(self): for imageInfo in testEnv.imageInfos: fen=imageInfo.fen if not fen==chess.STARTING_BOARD_FEN: continue image,video,warp=testEnv.prepareFromImageInfo(imageInfo) title=imageInfo.title #cbImage=ChessBoardImage(image,"chessboard") trapez=ChessTrapezoid(warp.pointList,rotation=warp.rotation,idealSize=800) cbWarped=trapez.warpedBoardImage(image) histogram=Histogram(cbWarped.image) histogram.showDebug() Environment.checkDir(Environment.debugImagePath) histogram.save(Environment.debugImagePath+title+"-histogram.jpg")
class ExampleVideo: """ an example video to be used in tests""" def __init__(self, frames, totalFrames, path, points, rotation=270, idealSize=800, ans=None): self.path = path self.title = Video.title(path) self.frames = frames self.totalFrames = totalFrames, self.points = points self.rotation = rotation self.idealSize = idealSize self.ans = ans def setup(self): self.trapezoid = ChessTrapezoid(self.points, rotation=self.rotation, idealSize=self.idealSize) if self.ans is None: self.ans = [] for tsquare in self.trapezoid.genSquares(): self.ans.append(tsquare.an) return self
def test_Rotation(self): csquare = ChessTrapezoid([(0, 0), (100, 0), (100, 100), (0, 100)]) rotations = [0, 90, 180, 270] indices = [(0, 0), (7, 0), (7, 7), (0, 7)] expected = [(0, 0), (7, 0), (7, 7), (0, 7), (7, 0), (7, 7), (0, 7), (0, 0), (7, 7), (0, 7), (0, 0), (7, 0), (0, 7), (0, 0), (7, 0), (7, 7)] index = 0 for rotation in rotations: csquare.rotation = rotation anstr = "" for row in range(ChessTrapezoid.rows): for col in range(ChessTrapezoid.cols): tsquare = csquare.tSquareAt(row, col) anstr = anstr + tsquare.an anstr = anstr + '\n' print(anstr) for rowcol in indices: row, col = rowcol rotated = csquare.rotateIndices(row, col, rotation) print(rotation, rowcol, rotated, expected[index]) assert rotated == expected[index] index = index + 1
def test_RankAndFile(self): csquare = ChessTrapezoid([(0, 0), (100, 0), (100, 100), (0, 100)]) for tsquare in csquare.genSquares(): assert tsquare.an == chess.FILE_NAMES[ tsquare.col] + chess.RANK_NAMES[7 - tsquare.row]
def test_ColorDistribution(self): imgPath = "/tmp/" for imageInfo in testEnv.imageInfos: fen = imageInfo.fen if not fen == chess.STARTING_BOARD_FEN: continue start = timer() image, video, warp = testEnv.prepareFromImageInfo(imageInfo) title = imageInfo.title trapez = ChessTrapezoid(warp.pointList, rotation=warp.rotation, idealSize=800) warped = trapez.warpedBoardImage(image) end = timer() video.writeImage(warped.image, imgPath + title + "-warped.jpg") #startd = timer() #denoised=video.getEmptyImage(warped, 3) #cv2.fastNlMeansDenoisingColored(warped,denoised) #endd = timer() #print("%.3fs for loading %.3fs for denoising image %s: %4d x %4d" % ((end-start),(endd-startd),title,width,height)) #video.writeImage(denoised,imgPath+title+"-denoised.jpg") print("%.3fs for loading image %s: %4d x %4d" % ((end - start), title, warped.width, warped.height)) trapez.updatePieces(fen) #ChessTrapezoid.colorDebug=True averageColors = trapez.analyzeColors(warped) startc = timer() fcs = trapez.optimizeColorCheck(warped, averageColors) endc = timer() fcs.showStatsDebug(endc - startc) idealImage = trapez.idealColoredBoard(warped.width, warped.height) diffImage = warped.diffBoardImage(idealImage) for tSquare in trapez.genSquares(): percent = "%.0f" % (fcs.colorPercent[tSquare.an]) trapez.drawRCenteredText(diffImage.image, percent, tSquare.rcx, tSquare.rcy, (0, 255, 0)) #video.showImage(warped,title,keyWait=15000) video.writeImage(diffImage.image, imgPath + title + "-colors.jpg")