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])