IVALab Python Libraries
Collection of code for computer vision and robotics with specific API.
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
MatrixDefect Class Reference

Instance of Matrix that can also generate defective puzzles by introducing erroenous puzzle pieces based on the configuration parameters and random sampling. More...

Inheritance diagram for MatrixDefect:
Inheritance graph
[legend]
Collaboration diagram for MatrixDefect:
Collaboration graph
[legend]

Public Member Functions

def __init__ (self, theBoard=[], imSource=[], theParams=CfgMatrix)
 Constructor. More...
 
def annotateDetectionsWithCircles (self, theImage, piCoords, blockOutGrid)
 Uses trial settings to generate robot detection annotation on image. More...
 
def annotatePuzzleImage (self, theImage, spCoords, fpCoords)
 Given bad piece and false positive locations, mark them in image. More...
 
def applyCorruptionToPiecesByMatrix (self, pgCoords)
 Uses trial settings to generate corruption vectors for pieces. More...
 
def applyDetectionToPuzzleImage (self, theImage, pgCoords, blockOutGrid)
 Apply detection process to system, if robot specified to exist. More...
 
def buildFrom_ImageAndRegions (theImage, theRegions, theParams=CfgMatrix())
 
def corruptPieces (self, pcInds, pcErrs)
 Corrupt the pieces indicated according to specified defect vector. More...
 
def corruptPiecesByCoords (self, pcCoords, pcErrs)
 Corrupt pieces at specified coordinates according to specified defect vector. More...
 
def reset (self)
 Reset puzzle to the original version. More...
 
def samplePieces (self, nSamp, doBlockOut=True, blockOutMat=None)
 Sample a small set of pieces based on block-out (or not). More...
 
- Public Member Functions inherited from Matrix
def __init__ (self, theBoard=[], theParams=CfgMatrix)
 Constructor for the puzzle.builder.adjacent class. More...
 
def coord2ind (self, theCoords)
 Uses puzzle shape to convert puzzle piece coordinate to linear index/key. More...
 
def explodedPuzzle (self, dx=100, dy=50, bgColor=(0, 0, 0))
 Create an exploded version of the puzzle. More...
 
def getCoordinates (self)
 Obtain solution board coordinates for the pieces. More...
 
def retile (self, dx=150, dy=150, inOrder=True)
 Organize puzzle pieces according to gridding. More...
 
def shuffle (self, numPieces=None, reorient=False)
 Randomly shuffle location of puzzle pieces of the puzzle. More...
 
def sub2ind (self, theSubs)
 Uses puzzle shape to convert puzzle piece subscripts to linear index/key. More...
 
def swap (self, theswap=None, reorient=False)
 Randomly shuffle location of puzzle pieces of the puzzle. More...
 
def swapByCoords (self, swapCoords)
 Specify puzzle piece coordinates that should be swapped. More...
 
- Public Member Functions inherited from Adjacent
def processAdjacency (self)
 Process the solution board and determine what pieces are adjacent or "close enough." It will determine the adjacency matrix. More...
 
- Public Member Functions inherited from Arrangement
def corrections (self, pLoc)
 Given an array of locations that correspond to the puzzle board (e.g., in the same order as puzzle board list), provide the correction vector that would move them to the calibrated locations. More...
 
def distances (self, pLoc)
 Given an array of locations that correspond to the puzzle board (e.g., in same order as puzzle board list), provide the distances between the locations and the calibrated locations. More...
 
def piecesInPlace (self, pLoc, tauDist=None)
 Return boolean array indicating whether the piece is correctly in place or not. More...
 
def scoreBoard (self, theBoard)
 Given a puzzle board with in ordered correspondence with the calibrated puzzle board, in the same order as puzzle board list), provide a score for the distance between the puzzle piece locations and the calibrated locations. More...
 
def scoreByLocation (self, pLoc)
 Given an array of locations that correspond to the puzzle board (e.g., in same order as puzzle board list), provide a score for the distance between the locations and the calibrated locations. More...
 
- Public Member Functions inherited from Board
def __init__ (self, *argv)
 Constructor for puzzle board. More...
 
def addPiece (self, piece, ORIGINAL_ID=False)
 Add puzzle piece instance to the board. More...
 
def addPieceFromImageAndMask (self, theImage, theMask, centroidLoc=None, cLoc=None)
 Given a mask and an image of same base dimensions, use to instantiate a puzzle piece template. More...
 
def addPieces (self, pieces)
 Add puzzle piece to board. More...
 
def boundingBox (self)
 Iterate through pieces to get tight bounding box. More...
 
def clear (self)
 
def display_cv (self, theImage=None, fh=None, ID_DISPLAY=False, CONTOUR_DISPLAY=False, BOUNDING_BOX=False, window_name='Puzzle')
 Display the puzzle board as an image using matplot library. More...
 
def display_mp (self, theImage=None, ax=None, fh=None, ID_DISPLAY=False, CONTOUR_DISPLAY=False, BOUNDING_BOX=False)
 Display the puzzle board as an image using matplot library. More...
 
def extents (self)
 Iterate through puzzle pieces to get tight bounding box extents of the board. More...
 
def fromImageAndLabels (self, theImage, theLabels)
 
Template getPiece (self, id)
 Get puzzle piece instance based on id. More...
 
def markMissing (self, indSetMeasured)
 Given set of indices to measured pieces, mark remaining as unmeasured. More...
 
def offset (self, dr)
 Offset the location of the entire puzzle in the board. More...
 
def pieceLocations (self, isCenter=False)
 Returns list/array of puzzle piece locations. More...
 
def relabel (self, newLabels, idContinue)
 Relabel the puzzle piece IDs in the board using new label reassignments and adjust IDs for those without reassignment. More...
 
def rmPiece (self, id)
 
def size (self)
 Number of pieces on the board. More...
 
def testAdjacent (self, id_A, id_B, tauAdj)
 Check if two puzzle pieces are adjacent or not. More...
 
def toImage (self, theImage=None, ID_DISPLAY=False, COLOR=(0, 0, 0), ID_COLOR=(255, 255, 255), CONTOUR_DISPLAY=True, BOUNDING_BOX=True)
 Uses puzzle piece locations to create an image for visualizing them. More...
 

Static Public Member Functions

def buildFrom_ImageAndMask (theImage, theMask, theParams=CfgMatrix(), show_grid=False)
 Given an image and an image mask, parse both to recover the puzzle calibration/solution. More...
 
def buildFrom_ImageAndSpecs (theImage, theParams)
 Given a fully specific Matrix puzzle and an image, instantiate a MatrixDefect puzzle. More...
 
def buildFrom_ImageProcessing (theImage, theProcessor=None, theDetector=None, theParams=None)
 
def buildFrom_Sketch (theImage, theMask, theProcessor=None, theDetector=None, theParams=None)
 
def buildFromFile_ImageAndMask (fileName, theParams=None)
 Load a saved arrangement calibration/solution stored as an image and a mask. More...
 
def buildFromFile_Puzzle (fileName, theParams=None)
 Load a saved arrangement calibration/solution puzzle board. More...
 
def buildFromFiles_ImageAndMask (imFile, maskFile, theParams=None)
 Load a saved arrangement calibration/solution stored as separate image and mask files. More...
 
def generateStudy (theTrials, theSettings, theImage, filePre)
 Generate all of the images associated to a study. More...
 
def generateTrainingImages (theTrials, theSettings, theImage, filePre)
 Generate all of the images associated to a study. More...
 
def generateTrainingImages_Pilot01 (puzzleImage, theSettings, filePre, randSeed=3454564)
 Generate trial matrix associated to training trials across all conditions and trial classes. More...
 
def generateTrial (trialInfo, trialStgs, theImage, filePrefix)
 
- Static Public Member Functions inherited from Adjacent
def buildFrom_ImageAndMask (theImage, theMask, theParams=None)
 Given an image and an image mask, process both to recover and instantiate a puzzle calibration/solution. More...
 

Public Attributes

 conditions
 
 image
 
 original
 
 pieces
 
- Public Attributes inherited from Matrix
 gc
 
- Public Attributes inherited from Adjacent
 adjMat
 
- Public Attributes inherited from Arrangement
 params
 
- Public Attributes inherited from Board
 id_count
 
 pieces
 

Static Public Attributes

tuple cent
 
 dx = self.xcoords[1]-self.xcoords[0]
 
 dy = self.ycoords[1]-self.ycoords[0]
 
int li = li+1
 
 markImage = cv2.circle(markImage, cent, r, (0, 0, 0) , 2)
 
 modPiece = self.pieces[inds]
 
list piShift = [];
 
 r = self.params.rho * np.linalg.norm(np.array([ self.xcoords[-1], self.ycoords[-1] ]))
 
tuple r = (self.params.rhoDetect * rad).astype('int')
 
int rad = np.linalg.norm(np.array([dx, dy]))/2
 
 selCoord = sc[selInd,:]
 
int selInd = random.randint(1,sc.shape[0])-1
 
list shiftVec = []
 
int theta = 2 * math.pi * random.random()
 
int ti = ti+1
 
list xtake = [selCoord[0]-self.params.blockOut, 1+selCoord[0]+self.params.blockOut]
 
list ytake = [selCoord[1]-self.params.blockOut, 1+selCoord[1]+self.params.blockOut]
 
- Static Public Attributes inherited from Matrix
 colTarg = int((imcols*theParams.isize[0])/imrows)
 
 dc = int(imsize[1] / theParams.psize[0])
 
 dr = int(imsize[0] / theParams.psize[1])
 
 imcols = imsize[0]
 
 imFact = theParams.isize[0] / imcols
 
 imRatio = imcols / imrows
 
 imrows = imsize[1]
 
 imsize = theImage.shape
 
 isize
 
 order
 
 partition_num
 
 rowTarg = int((imrows*theParams.isize[1])/imcols)
 
 shape
 
 theImage = cv2.resize(theImage, None, fx=imFact, fy=imFact)
 
 theMask = np.ones([imrows, imcols])
 
 thePuzzle = Matrix.buildFrom_ImageAndMask(theImage, theMask, theParams)
 
 ticols = dc * theParams.psize[0]
 
 tirows = dr * theParams.psize[1]
 
 x_labels
 
 x_list
 
 x_parts
 
 xcoords
 
 y_labels
 
 y_list
 
 y_parts
 
 ycoords
 

Detailed Description

Instance of Matrix that can also generate defective puzzles by introducing erroenous puzzle pieces based on the configuration parameters and random sampling.

Provides an API to the Matrix puzzle for creating faulty or defective puzzles.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  theBoard = [],
  imSource = [],
  theParams = CfgMatrix 
)

Constructor.

Parameters
[in]theBoardInput board instance that serves as the non-defective board.
[in]imSourceThe source image for generating defects with.
[in]theParamsMatrix puzzle configuration instance.

Member Function Documentation

◆ annotateDetectionsWithCircles()

def annotateDetectionsWithCircles (   self,
  theImage,
  piCoords,
  blockOutGrid 
)

Uses trial settings to generate robot detection annotation on image.

Applies any annotation configuration specifications.

Note
Not used. The applyDetectionToPuzzleImage member function does it all.
Parameters
[in]theImageThe image to annotate.
[in]pcCoordsThe puzzle piece image coordinates to annotate.

◆ annotatePuzzleImage()

def annotatePuzzleImage (   self,
  theImage,
  spCoords,
  fpCoords 
)

Given bad piece and false positive locations, mark them in image.

The purpose is to provide a quality control human with the true quality control state of the puzzle board. Presumably for training purposes; to learn how to differentiate pieces.

◆ applyCorruptionToPiecesByMatrix()

def applyCorruptionToPiecesByMatrix (   self,
  pgCoords 
)

Uses trial settings to generate corruption vectors for pieces.

Applies the corruption.

Parameters
[in]pgCoordsPuzzle pieces to corrupt in (x,y) matrix grid coordinates.

◆ applyDetectionToPuzzleImage()

def applyDetectionToPuzzleImage (   self,
  theImage,
  pgCoords,
  blockOutGrid 
)

Apply detection process to system, if robot specified to exist.

Parameters
[in]theImageImage to annotate with detections (if at all).
[in]pgCoordsPuzzle grid coordinates (ground truth).
[in]blockOutGridFalse positive block-out grid.
[out]theImageAnnotated image.
[out]fpcFalse positive (matrix) coordinate.
[ou]wasDetected Boolean array indicating if defect was detected or not.

◆ buildFrom_ImageAndMask()

def buildFrom_ImageAndMask (   theImage,
  theMask,
  theParams = CfgMatrix(),
  show_grid = False 
)
static

Given an image and an image mask, parse both to recover the puzzle calibration/solution.

Todo:
NOT UPDATED!!!!

Instantiates a puzzle parser that gets applied to the submitted data to create a puzzle board instance. That instance is the calibration/solution.

Parameters
[in]theImagePuzzle image data.
[in]theMaskPuzzle mask data.
[in]theParamsMatrix configuration instance.
Returns
thePuzzle Matrix puzzle board instance.

Reimplemented from Matrix.

◆ buildFrom_ImageAndRegions()

def buildFrom_ImageAndRegions (   theImage,
  theRegions,
  theParams = CfgMatrix() 
)

Reimplemented from Matrix.

◆ buildFrom_ImageAndSpecs()

def buildFrom_ImageAndSpecs (   theImage,
  theParams 
)
static

Given a fully specific Matrix puzzle and an image, instantiate a MatrixDefect puzzle.

The image and the puzzle specification need not agree. The image will be reshaped to the nearest possible size compatible with the Matrix specifications. It may be that the puzzle sizing has to change to match that aspect ratio of the original image, or the image has to be cropped. This routine tries to minimize how much the specifications are adjusted to make a puzzle from the source image.

What is important about the puzzle specification is that the number of columns and rows (width and height) of the puzzle matrix should be given. The target image size should be given too. If not, then the source image will be used for the initial target image size, which may be adjusted or cropped for compatiblity with the puzzle sizing.

Parameters
[in]theImageThe source image.
[in]theParamsThe Matrix puzzle configuration (should be complete).
Returns
thePuzzle A Matrix puzzle board instance.
theImage The modified source image.

Reimplemented from Matrix.

◆ buildFrom_ImageProcessing()

def buildFrom_ImageProcessing (   theImage,
  theProcessor = None,
  theDetector = None,
  theParams = None 
)
static
@todo   NOT UPDATED!!!!

@brief  Given an image with regions clearly separated by some color or threshold,
    parse it to recover the puzzle calibration/solution. Can source alternative
    detector.

Instantiates a puzzle parser that gets applied to the submitted data to create a
puzzle board instance. That instance is the calibration/solution.

@param[in]  theImage: The puzzle image data.
@param[in]  theProcessor: The processing scheme.
@param[in]  theDetector: The detector scheme.
@param[in]  theParams   Matrix configuration instance.

@return   thePuzzle   Matrix puzzle board instance.

Reimplemented from Matrix.

◆ buildFrom_Sketch()

def buildFrom_Sketch (   theImage,
  theMask,
  theProcessor = None,
  theDetector = None,
  theParams = None 
)
static
@todo   NOT UPDATED!!!!

@brief  Given an image with regions clearly separated by some color or threshold,
    parse it to recover the puzzle calibration/solution. Can source alternative
    detector.

Instantiates a puzzle parser that gets applied to the submitted data to create a
puzzle board instance. That instance is the calibration/solution.

@param[in]  theImage:       Puzzle image data.
@param[in]  theMask:        Puzzle mask data.
@param[in]  theProcessor    Processing scheme.
@param[in]  theDetector     Detector scheme.
@param[in]  theParams       Matrix configuration instance.

@return     thePuzzle   Matrix puzzle board instance.

Reimplemented from Matrix.

◆ buildFromFile_ImageAndMask()

def buildFromFile_ImageAndMask (   fileName,
  theParams = None 
)
static

Load a saved arrangement calibration/solution stored as an image and a mask.

Todo:
NOT UPDATED!!!!

The python file contains the puzzle board mask and image source data. It gets processed into an arrangement instance. If a threshold variable tauDist is found, then it is applied to the arrangement instance.

Parameters
[in]fileNamePython file to load.
[in]theParamsMatrix configuration instance.
Returns
thePuzzle Matrix puzzle board instance.

Reimplemented from Matrix.

◆ buildFromFile_Puzzle()

def buildFromFile_Puzzle (   fileName,
  theParams = None 
)
static

Load a saved arrangement calibration/solution puzzle board.

Todo:
NOT UPDATED!!!!
Parameters
[in]fileNamePython file to load.
[in]theParamsMatrix configuration instance.
Returns
thePuzzle Matrix puzzle board instance.

Reimplemented from Matrix.

◆ buildFromFiles_ImageAndMask()

def buildFromFiles_ImageAndMask (   imFile,
  maskFile,
  theParams = None 
)
static

Load a saved arrangement calibration/solution stored as separate image and mask files.

Todo:
NOT UPDATED!!!!

The source file contain the puzzle board image and mask data. It gets processed into an arrangement instance. If a threshold variable tauDist is found, then it is applied to the arrangement instance.

Parameters
[in]imFileImage file to load.
[in]maskFileMask file to load.
[in]theParamsMatrix configuration instance.
Returns
thePuzzle Matrix puzzle board instance.

Reimplemented from Matrix.

◆ corruptPieces()

def corruptPieces (   self,
  pcInds,
  pcErrs 
)

Corrupt the pieces indicated according to specified defect vector.

Parameters
[in]pcIndIndex to puzzle piece(s) to corrupt.
[in]pcErrsVectors Parameter specifications for corruption process.

◆ corruptPiecesByCoords()

def corruptPiecesByCoords (   self,
  pcCoords,
  pcErrs 
)

Corrupt pieces at specified coordinates according to specified defect vector.

Autocorrects vector at boundaries.

Parameters
[in]pcCoordsCoordinates to puzzle pieces to corrupt. (x,y) coords.
[in]pcErrsVector shifts to apply for new puzzle patch images. (x,y) coords.

◆ generateStudy()

def generateStudy (   theTrials,
  theSettings,
  theImage,
  filePre 
)
static

Generate all of the images associated to a study.

Parameters
[in]theTrialsList of trial conditions.
[in]theSettingsStudy settings.
[in]repsNo. of replicates (whole number)
[in]filePrePrefix of saved files.

◆ generateTrainingImages()

def generateTrainingImages (   theTrials,
  theSettings,
  theImage,
  filePre 
)
static

Generate all of the images associated to a study.

Parameters
[in]theTrialsList of trial conditions.
[in]theSettingsStudy settings.
[in]repsNo. of replicates (whole number)
[in]filePrePrefix of saved files.

◆ generateTrainingImages_Pilot01()

def generateTrainingImages_Pilot01 (   puzzleImage,
  theSettings,
  filePre,
  randSeed = 3454564 
)
static

Generate trial matrix associated to training trials across all conditions and trial classes.

This particular pilot isolates certain conditions to a subject. Those are puzzle size and robot assistance, which are held constant for a subject. Meanwhile number of errors and magnitude of error vary.

◆ generateTrial()

def generateTrial (   trialInfo,
  trialStgs,
  theImage,
  filePrefix 
)
static

◆ reset()

def reset (   self)

Reset puzzle to the original version.

◆ samplePieces()

def samplePieces (   self,
  nSamp,
  doBlockOut = True,
  blockOutMat = None 
)

Sample a small set of pieces based on block-out (or not).

Randomly pick a small set of puzzle pieces from all possible puzzle pieces. Selection is without replacement and incorporate a block-out radius from the configuration parameters unless it is specified to be ignored.

Parameters
[in]nSampNumber of puzzle piece samples to return.
[in]blockOutOptional binary flag to disable blockOut [def: True]

Member Data Documentation

◆ cent

tuple cent
static
Initial value:
= ((np.mean(self.xcoords[fpCoords[0]:(fpCoords[0]+2)])).astype('int'),
(np.mean(self.ycoords[fpCoords[1]:(fpCoords[1]+2)])).astype('int'))

◆ conditions

conditions

◆ dx

dx = self.xcoords[1]-self.xcoords[0]
static

◆ dy

dy = self.ycoords[1]-self.ycoords[0]
static

◆ image

image

◆ li

int li = li+1
static

◆ markImage

markImage = cv2.circle(markImage, cent, r, (0, 0, 0) , 2)
static

◆ modPiece

modPiece = self.pieces[inds]
static

◆ original

original

◆ pieces

pieces

◆ piShift

list piShift = [];
static

◆ r [1/2]

r = self.params.rho * np.linalg.norm(np.array([ self.xcoords[-1], self.ycoords[-1] ]))
static

◆ r [2/2]

tuple r = (self.params.rhoDetect * rad).astype('int')
static

◆ rad

int rad = np.linalg.norm(np.array([dx, dy]))/2
static

◆ selCoord

selCoord = sc[selInd,:]
static

◆ selInd

int selInd = random.randint(1,sc.shape[0])-1
static

◆ shiftVec

list shiftVec = []
static

◆ theta

int theta = 2 * math.pi * random.random()
static

◆ ti

int ti = ti+1
static

◆ xtake

list xtake = [selCoord[0]-self.params.blockOut, 1+selCoord[0]+self.params.blockOut]
static

◆ ytake

list ytake = [selCoord[1]-self.params.blockOut, 1+selCoord[1]+self.params.blockOut]
static

The documentation for this class was generated from the following file: