def main(opts): out = [opts['-o'],] args = izip(ts_iter_images(opts['-i']), cycle(out)) count = 0 for _ in imap(process_image, args): if count % 10 == 0: print("Renamed {} images!".format(count), end="\r") count += 1 print("Renamed {} images!\n\n".format(count))
def ts_to_tsnc(ts_path, tsnc_path): log = logging.getLogger("timestreamlib") # Get timestream images imgs = list(ts_iter_images(ts_path)) # Make netcdf4 file root = ncdf.Dataset(tsnc_path, 'w', format="NETCDF4") root.createGroup('timestream') # Make dimensions # Kludge time!!! root.createDimension('y', mat0.shape[0]) root.createDimension('x', mat0.shape[1]) root.createDimension('z', mat0.shape[2]) root.createDimension('t', None) mat0 = None mat_n = 0 while mat0 is None: try: mat0 = read_image(imgs[mat_n]) mat_n += 1 except IndexError: raise ValueError("Didn't find a valid image in {}".format(ts_path)) # setup variables times = root.createVariable("time", 'f8', ('t',)) times.units = "seconds since 1970-01-01 00:00:00.0" times.calendar = "standard" root.createVariable("z", 'u1', ('z',)) root.createVariable("y", 'u4', ('y',)) root.createVariable("x", 'u4', ('x',)) # create actual pixel array px_type = 'u{:d}'.format(mat0.dtype.itemsize) pixels = root.createVariable("pixel", px_type, ('t', 'y', 'x', 'z'), zlib=True) log.info("Created netcdf4 file {} with pixel array dimensions {!r}".format( tsnc_path, pixels.shape)) # iteratively add images count = 0 for img_n, mat in enumerate(imap(read_image, imgs)): img = imgs[img_n] if mat is None: continue n_dates = len(root.dimensions['t']) time = ts_parse_date_path(img) times[n_dates] = date2num([time, ], units=times.units, calendar=times.calendar) pixels[n_dates, :, :, :] = mat count += 1 log.debug("Processed {}. Matrix shape is {!r}".format(img, pixels.shape)) log.log(NOEOL, "Processed {: 5d} images.\r".format(count)) log.info("Processed {: 5d} images. ts_to_tsnc finished!".format(count)) root.close()
def main(opts): pool = mp.Pool() if opts['-t']: pool = mp.Pool(opts['-t']) xy = tuple(map(int, opts['-s'].replace('x', ',').split(','))) if len(xy) == 1: xy = (xy[0], 0) xy = [xy, ] out = [opts['-o'],] args = izip(ts_iter_images(opts['-i']), cycle(out), cycle(xy)) count = 0 print("Resizing {} to {}".format(opts['-i'], opts['-s'])) for _ in pool.imap(process_image, args): print("Resized {: 5d} images!".format(count), end="\r") count += 1 print("Resized {: 5d} images! Done!".format(count)) pool.close() pool.join()
def ts_to_tsnc(ts_path, tsnc_path): log = logging.getLogger("CONSOLE") # Get timestream images imgs = list(ts_iter_images(ts_path)) mats = ts_iter_numpy(imgs) # Make netcdf4 file root = ncdf.Dataset(tsnc_path, 'w', format="NETCDF4") root.createGroup('timestream') # Make dimensions # Kludge time!!! mat0 = next(ts_iter_numpy([imgs[0], ]))[1] root.createDimension('y', mat0.shape[0]) root.createDimension('x', mat0.shape[1]) root.createDimension('z', mat0.shape[2]) root.createDimension('t', None) # setup variables times = root.createVariable("time", 'f8', ('t',)) times.units = "seconds since 1970-01-01 00:00:00.0" times.calendar = "standard" root.createVariable("z", 'u1', ('z',)) root.createVariable("y", 'u4', ('y',)) root.createVariable("x", 'u4', ('x',)) # create actual pixel array px_type = 'u{d}'.format(mat0.dtype.itemsize) pixels = root.createVariable("pixel", px_type, ('t', 'y', 'x', 'z'), zlib=True) log.info("Created netcdf4 file {} with pixel array dimensions {!r}".format( tsnc_path, pixels.shape)) # iteratively add images count = 0 for img, mat in mats: n_dates = len(root.dimensions['t']) time = ts_parse_date_path(img) times[n_dates] = date2num([time, ], units=times.units, calendar=times.calendar) pixels[n_dates, :, :, :] = mat count += 1 log.debug("Processed {}. Matrix shape is {!r}".format(img, pixels.shape)) if count % 2 == 0: log.log(NOEOL, "Processed {: 5d} images.\r".format(count)) log.info("Processed {: 5d} images. ts_to_tsnc finished!".format(count)) root.close()
def test_good_timestream_manifold(self): """Test ts_iter_images with a timestream with a manifold""" res = ts_iter_images(helpers.FILES["timestream_manifold"]) self.assertTrue(isgenerator(res)) self.assertListEqual(sorted(list(res)), helpers.TS_MANIFOLD_FILES_JPG)
def __init__(self, rootPath): self.img_iter = ts_iter_images(rootPath) self.counter = -1
def main(argv): HelpString = 'correctDistortionAndColor.py -i <image file> ' + \ '-f <root image folder> '+ \ '-o <output file>\n' + \ 'Example:\n' + \ "$ ./correctDistortionAndColor.py -f /mnt/phenocam/a_data/TimeStreams/BorevitzTest/BVZ0018/BVZ0018-GC04L~fullres-orig/ -k /mnt/phenocam/a_data/TimeStreams/BorevitzTest/BVZ0018/BVZ0018-GC04L~fullres-corr/calib_param_700Dcam.yml -g /mnt/phenocam/a_data/TimeStreams/BorevitzTest/BVZ0018/BVZ0018-GC04L~fullres-corr/CameraTrax_24ColorCard_2x3in.png -c /mnt/phenocam/a_data/TimeStreams/BorevitzTest/BVZ0018/BVZ0018-GC04L~fullres-corr/CameraTrax_24ColorCard_2x3inCaptured.png -o /mnt/phenocam/a_data/TimeStreams/BorevitzTest/BVZ0018/BVZ0018-GC04L~fullres-corr/ -j 16" try: opts, args = getopt.getopt(argv,"hi:f:c:k:b:g:t:o:j:",\ ["ifile=","ifolder=","--configfolder","calibfile=","captured-colorcard=",\ "groundtruth-colorcard=","--tray-image-pattern","ofolder=","jobs="]) except getopt.GetoptError: print(HelpString) sys.exit(2) if len(opts) == 0: print(HelpString) sys.exit() ImageFile = '' InputRootFolder = '' OutputFolder = '' InitialGeometryFile = 'ColorcardTrayPotSelections.yml' ColorCardTrueFile = 'CameraTrax_24ColorCard_2x3in.png' # ColorCardTrueFile = 'CameraTrax_24ColorCard_2x3in180deg.png' # ColorCardCapturedFile = 'CameraTrax_24ColorCard_2x3inCaptured.png' ColorCardCapturedFile = 'Card_%d.png' TrayCapturedFile = 'Tray_%d.png' PotCapturedFile = 'PotCaptured2.png' #'PotCaptured.png' #'PotCaptured4.png' #'PotCaptured3.png' # CalibFile = 'Canon700D_18mm_CalibParam.yml' ConfigFolder = '' RotationAngle = None NoJobs = 1 for opt, arg in opts: if opt == '-h': print(HelpString) sys.exit() elif opt in ("-i", "--ifile"): ImageFile = arg elif opt in ("-f", "--ifolder"): InputRootFolder = arg elif opt in ("-c", "--configfolder"): ConfigFolder = arg elif opt in ("-b", "--captured-colorcard"): ColorCardCapturedFile = arg elif opt in ("-g", "--groundtruth-colorcard"): ColorCardTrueFile = arg elif opt in ("-t", "--tray-image-pattern"): TrayCapturedFile = arg elif opt in ("-k", "--calibfile"): CalibFile = arg elif opt in ("-o", "--ofolder"): OutputFolder = arg elif opt in ("-j", "--jobs"): NoJobs = int(arg) if len(ConfigFolder) > 0: ColorCardTrueFile = os.path.join(ConfigFolder, os.path.basename(ColorCardTrueFile)) ColorCardCapturedFile = os.path.join(ConfigFolder, os.path.basename(ColorCardCapturedFile)) TrayCapturedFile = os.path.join(ConfigFolder, os.path.basename(TrayCapturedFile)) PotCapturedFile = os.path.join(ConfigFolder, os.path.basename(PotCapturedFile)) CalibFile = os.path.join(ConfigFolder, os.path.basename(CalibFile)) InitialGeometryFile = os.path.join(ConfigFolder, os.path.basename(InitialGeometryFile)) if len(OutputFolder) > 0 and not os.path.exists(OutputFolder): os.makedirs(OutputFolder) if len(CalibFile) > 0: ImageSize, SquareSize, CameraMatrix, DistCoefs, RVecs, TVecs = utils.readCalibration(CalibFile) print('CameraMatrix =', CameraMatrix) print('DistCoefs =', DistCoefs) UndistMapX, UndistMapY = cv2.initUndistortRectifyMap(CameraMatrix, DistCoefs, \ None, CameraMatrix, ImageSize, cv2.CV_32FC1) if len(InitialGeometryFile): rotationAngle, distortionCorrected, colorcardList, trayList, potList = utils.readGeometries(InitialGeometryFile) print('trayList =', trayList) RotationAngle = rotationAngle colorcardCentre, colorcardWidth, colorcardHeight, colorcardAngle = utils.getRectangleParamters(colorcardList[0]) colorcardPosition = [int(colorcardCentre[0]), int(colorcardCentre[1])] print('colorcardPosition =', colorcardPosition) potCentre, potWidth, potHeight, potAngle = utils.getRectangleParamters(potList[0]) potSize = (int(potWidth), int(potHeight)) print('potSize =', potSize) trayPositions = [] for tray in trayList: trayCentre, trayWidth, trayHeight, trayAngle = utils.getRectangleParamters(tray) trayPositions.append([int(trayCentre[0]), int(trayCentre[1])]) P24ColorCardTrueImage = cv2.imread(ColorCardTrueFile)[:,:,::-1] # read and convert to R-G-B image SquareSize = int(P24ColorCardTrueImage.shape[0]/4) HalfSquareSize = int(SquareSize/2) P24ColorCardCapturedImage = cv2.imread(ColorCardCapturedFile %0)[:,:,::-1] # read and convert to R-G-B image P24ColorCardCaptured_PyramidImages = utils.createImagePyramid(P24ColorCardCapturedImage) Tray_PyramidImagesList = [] for i in range(8): TrayFilename = TrayCapturedFile %(i) TrayImage = cv2.imread(TrayFilename) if TrayImage == None: print('Unable to read', TrayFilename) # Tray_PyramidImages = None continue else: TrayImage = TrayImage[:,:,::-1] Tray_PyramidImages = utils.createImagePyramid(TrayImage) Tray_PyramidImagesList.append(Tray_PyramidImages) PotCapturedImage = cv2.imread(PotCapturedFile)[:,:,::-1] # read and convert to R-G-B image # supress green channel PotCapturedImage[:,:,1] = 0 if PotCapturedImage == None: print('Unable to read', TrayFilename) scaleFactor = potSize[1]/PotCapturedImage.shape[0] print('scaleFactor', scaleFactor) PotCapturedImage = cv2.resize(PotCapturedImage, potSize, interpolation = cv2.INTER_CUBIC) print('PotCapturedImage.shape') Pot_PyramidImages = utils.createImagePyramid(PotCapturedImage) # collect 24 colours from the captured color card: Colors = np.zeros([3,24]) for i in range(24): Row = int(i/6) Col = i - Row*6 rr = Row*SquareSize + HalfSquareSize cc = Col*SquareSize + HalfSquareSize Colors[0,i] = P24ColorCardTrueImage[rr,cc,0] Colors[1,i] = P24ColorCardTrueImage[rr,cc,1] Colors[2,i] = P24ColorCardTrueImage[rr,cc,2] print('Colors = \n', Colors) if len(ImageFile): img_iter = [sorted(glob.glob(ImageFile))] elif len(InputRootFolder): img_iter = ts_iter_images(InputRootFolder) else: print('Need imput image for image folder') return ArgList = [] for i,ImageFile_ in enumerate(img_iter): if i <= 830: continue if len(OutputFolder) > 0 and len(InputRootFolder) == 0: OutputFile = os.path.join(OutputFolder, os.path.basename(ImageFile_)) elif len(OutputFolder) > 0 and len(InputRootFolder) > 0: ImagePath = os.path.dirname(ImageFile_) ImageName = os.path.basename(ImageFile_) OutputPath = os.path.join(OutputFolder, ImagePath[len(InputRootFolder):]) OutputFile = os.path.join(OutputPath, ImageName) ArgList.append([ImageFile_, RotationAngle, UndistMapX, UndistMapY, P24ColorCardCaptured_PyramidImages, colorcardPosition, Colors, Tray_PyramidImagesList, trayPositions, Pot_PyramidImages, OutputFile]) # if i == 50: # break Process = Pool(processes = NoJobs) import time time1 = time.time() Results = Process.map(correctDistortionAndColor, ArgList) # for Arg in ArgList: # correctDistortionAndColor(Arg) time2 = time.time() json.dump(Results, open(os.path.join(OutputFolder, 'Result.json'))) InfoFile = os.path.join(OutputFolder, 'ColorCorrectionInfo.txt') with open(InfoFile, 'w') as myfile: myfile.write('It took %0.3f seconds to process %d files using %d processes\n' % (time2-time1, len(Results), NoJobs)) myfile.write('ImageFileName; MatchingScore; ColorPosition-X(-1.0 for undetected colorbar); ColorbarPosition-Y(-1.0 for undetected colorbar); CorrectionError(-1.0 for undetected colorbar)\n') print('Finished. Saved color correction info to', InfoFile)
RotationAngle = RectData['RotationAngle'] Rect = RectData['Colorbar'].tolist() print('Rect =', Rect) Centre, Width, Height, Angle = getRectangleParamters(Rect) print(Centre, Width, Height, Angle) ColCardMapX, ColCardMapY = createMap(Centre, Width, Height, Angle) P24ColorCard = cv2.imread('/home/chuong/Data/ColorbarPositions/CameraTrax_24ColorCard_2x3in.png')[:,:,::-1] # read and convert to R-G-B image SquareSize = int(P24ColorCard.shape[0]/4) HalfSquareSize = int(SquareSize/2) P24ColorCardCaptured = cv2.imread('/home/chuong/Data/ColorbarPositions/CameraTrax_24ColorCard_2x3inCaptured.png')[:,:,::-1] # read and convert to R-G-B image SquareSizeCaptured = int(P24ColorCardCaptured.shape[0]/4) HalfSquareSizeCaptured = int(SquareSizeCaptured/2) img_iter = ts_iter_images('/home/chuong/Data/ColorbarPositions') #img_iter = ts_iter_images('/home/chuong/Data/BVZ0012-GC02L-CN650D-Cam01') for ImageFile in img_iter: Image = cv2.imread(ImageFile)[:,:,::-1] print(ImageFile) if Image.shape[0] > Image.shape[1]: RotationAngle = 90 Image = rotateImage(Image, RotationAngle) # maxVal, maxLoc = findColorbarPyramid(Image, P24ColorCardCaptured) maxVal, maxLoc, RotationAngle2 = findColorbarPyramid(Image, P24ColorCardCaptured, NoLevels = 5, FinalLevel = 3) if maxVal == None: continue RotationAngle = RotationAngle + RotationAngle2 # if maxVal < maxVal180: # RotationAngle = RotationAngle + 180 # maxVal, maxLoc, maxVal180, maxLoc180 = maxVal180, maxLoc180, maxVal, maxLoc