def __call__(self, context, *args): LOG.info(self.mess) tsi = args[0] self.image = tsi.pixels meanIntensity = np.mean(self.image) if meanIntensity < self.minIntensity: # FIXME: this should be handled with an error. LOG.warn('Image is too dark, skiping colorcard detection!') return([self.image, [None, None, None]]) if not self.useWhiteBackground: self.imagePyramid = cd.createImagePyramid(self.image) ccdImg = cv2.imread(self.ccf)[:, :, ::-1] if ccdImg is None: raise ValueError("Failed to read %s" % self.ccf) self.colorcardPyramid = cd.createImagePyramid(ccdImg) # create image pyramid for multiscale matching SearchRange = [self.colorcardPyramid[0].shape[1], self.colorcardPyramid[0].shape[0]] score, loc, angle = cd.matchTemplatePyramid( self.imagePyramid, self.colorcardPyramid, 0, EstimatedLocation=self.colorcardPosition, SearchRange=SearchRange ) if score > 0.3: # extract color information self.foundCard = self.image[ loc[1] - ccdImg.shape[0] // 2:loc[1] + ccdImg.shape[0] // 2, loc[0] - ccdImg.shape[1] // 2:loc[0] + ccdImg.shape[1] // 2] self.ccdColors, _ = cd.getColorcardColors(self.foundCard, GridSize=[6, 4]) self.colorcardParams = cd.estimateColorParameters( self.colorcardTrueColors, self.colorcardColors ) # Save colourcard image to instance self.colorcardImage = ccdImg # for displaying self.loc = loc else: # FIXME: this should be handled with an error. LOG.warn('Cannot find color card') self.colorcardParams = [None, None, None] else: self.colorcardParams = cd.estimateColorParametersFromWhiteBackground( self.image, self.backgroundWindow, self.maxIntensity) return([tsi, self.colorcardParams])
def correctColor(self): if self.colorcardParams == None: if len(self.colorcardList) > 0: medianSize = cd.getMedianRectSize(self.colorcardList) capturedColorcards = cd.rectifyRectImages(self.image, self.colorcardList, medianSize) self.colorcardColors, _ = cd.getColorcardColors(capturedColorcards[0], GridSize = [6, 4]) self.colorcardParams = cd.estimateColorParameters(cd.CameraTrax_24ColorCard, self.colorcardColors) else: self.status.append('Need to select a color card first.') return colorMatrix, colorConstant, colorGamma = self.colorcardParams self.imageCorrected = cd.correctColorVectorised(self.image.astype(np.float), colorMatrix, colorConstant, colorGamma) self.imageCorrected[np.where(self.imageCorrected < 0)] = 0 self.imageCorrected[np.where(self.imageCorrected > 255)] = 255 self.imageCorrected = self.imageCorrected.astype(np.uint8) self.updateFigure(self.imageCorrected)
def __exec__(self, context, *args): tsi = args[0] self.image = tsi.pixels meanIntensity = np.mean(self.image) if meanIntensity < self.minIntensity: raise PCExImageTooDark(tsi.path) if not self.useWhiteBackground: self.imagePyramid = cd.createImagePyramid(self.image) ccdImg = read_image(self.ccf)[:, :, 0:3] if ccdImg is None: raise PCExBadImage(self.ccf) self.ccdPyramid = cd.createImagePyramid(ccdImg) # create image pyramid for multiscale matching SearchRange = [self.ccdPyramid[0].shape[1] * 1.5, self.ccdPyramid[0].shape[0] * 1.5] score, loc, angle = cd.matchTemplatePyramid( self.imagePyramid, self.ccdPyramid, 0, EstimatedLocation=self.colorcardPosition, SearchRange=SearchRange) if score > 0.3: # extract color information self.foundCard = self.image[ loc[1] - ccdImg.shape[0] // 2:loc[1] + ccdImg.shape[0] // 2, loc[0] - ccdImg.shape[1] // 2:loc[0] + ccdImg.shape[1] // 2] self.ccdColors, _ = cd.getColorcardColors(self.foundCard, GridSize=[6, 4]) self.ccdParams = cd.estimateColorParameters( self.colorcardTrueColors, self.ccdColors) # Save colourcard image to instance self.colorcardImage = ccdImg # for displaying self.loc = loc else: raise PCExCannotFindColorCard(tsi.path) else: self.ccdParams = cd.estimateColorParametersFromWhiteBackground( self.image, self.backgroundWindow, self.maxIntensity) return([tsi, self.ccdParams])
def savePipelineSettings(self): ''' save to pipeline setting file''' self.settingFileName = str(QtGui.QFileDialog.getSaveFileName(self, \ 'Save selection (default in ./_data)', self.timestreamRootPath)) if len(self.settingFileName) == 0: return self.settingPath = os.path.relpath(os.path.dirname(self.settingFileName), self.timestreamRootPath) self.settings = {} if self.ImageSize != None and self.CameraMatrix != None and self.DistCoefs != None: undistortDic = {'cameraMatrix': self.CameraMatrix.tolist(), \ 'distortCoefs': self.DistCoefs.tolist(), \ 'imageSize': list(self.ImageSize), 'rotationAngle': self.rotationAngle + self.smaleRotationAngle } else: undistortDic = {} self.settings['undistort'] = undistortDic if len(self.colorcardList) > 0: medianSize = cd.getMedianRectSize(self.colorcardList) capturedColorcards = cd.rectifyRectImages(self.image, self.colorcardList, medianSize) colorCardFile = 'CapturedColorcard.png' cv2.imwrite(os.path.join(self.timestreamRootPath, self.settingPath, colorCardFile), capturedColorcards[0][:,:,::-1].astype(np.uint8)) colorcardColors, colorStd = cd.getColorcardColors(capturedColorcards[0], GridSize = [6,4]) colorcardPosition, Width, Height, Angle = cd.getRectangleParamters(self.colorcardList[0]) colorcardDic = {'colorcardFile': colorCardFile,\ 'colorcardPosition': colorcardPosition.tolist(),\ 'colorcardTrueColors': cd.CameraTrax_24ColorCard,\ 'settingPath': '_data' } else: colorcardDic = {} self.settings['colorcarddetect'] = colorcardDic self.settings['colorcorrect'] = {'minIntensity': 15} if len(self.trayList) > 0: trayMedianSize = cd.getMedianRectSize(self.trayList) trayImages = cd.rectifyRectImages(self.image, self.trayList, trayMedianSize) trayDict = {} trayDict['settingPath'] = '_data' trayDict['trayNumber'] = len(self.trayList) trayDict['trayFiles'] = 'Tray_%02d.png' trayPositions = [] for i,tray in enumerate(trayImages): cv2.imwrite(os.path.join(self.timestreamRootPath, self.settingPath, trayDict['trayFiles'] %i), tray[:,:,::-1].astype(np.uint8)) trayPosition, Width, Height, Angle = cd.getRectangleParamters(self.trayList[i]) trayPositions.append(trayPosition.tolist()) trayDict['trayPositions'] = trayPositions else: trayDict = {} self.settings['traydetect'] = trayDict if len(self.potList) > 0: trayMedianSize = cd.getMedianRectSize(self.trayList) potPosition, Width, Height, Angle = cd.getRectangleParamters(self.potList[0]) Width, Height = int(Width), int(Height) topLeft = [int(self.potList[0][0][0]), int(self.potList[0][0][1])] self.potImage = self.image[topLeft[1]:topLeft[1]+Height, topLeft[0]:topLeft[0]+Width, :] potFile = 'Pot.png' cv2.imwrite(os.path.join(self.timestreamRootPath, self.settingPath, potFile), self.potImage[:,:,::-1].astype(np.uint8)) potDict = {} potDict['potPositions'] = potPosition.tolist() potDict['potSize'] = [int(Width), int(Height)] potDict['traySize'] = [int(trayMedianSize[0]), int(trayMedianSize[1])] potDict['potFile'] = potFile potDict['potTemplateFile'] = 'PotTemplate.png' potDict['settingPath'] = '_data' potTemplatePathIn = os.path.join(self.scriptPath, './data/PotTemplate.png') potTemplatePathOut = os.path.join(self.timestreamRootPath, self.settingPath, potDict['potTemplateFile']) shutil.copyfile(potTemplatePathIn, potTemplatePathOut) if self.potTemplate != None: potTemplateFile = 'potTemplate.png' cv2.imwrite(os.path.join(self.timestreamRootPath, self.settingPath, potTemplateFile), self.potTemplate[:,:,::-1]) potDict['potTemplateFile'] = potTemplateFile else: potDict = {} self.settings['potdetect'] = potDict self.settings['plantextract'] = {'meth': 'method1', 'methargs': {'threshold' : 0.6, 'kSize' : 5, 'blobMinSize' : 50} } with open(self.settingFileName, 'w') as outfile: outfile.write( yaml.dump(self.settings, default_flow_style=None) ) self.status.append('Saved initial data to ' + self.settingFileName)