Esempio n. 1
0
def main(pixCount, srcDir, destDir):
	srcNames = os.listdir(srcDir)
	nr = 0
	for srcName in srcNames:
		nr += 1
		countLog = str(nr) + '/' + str(len(srcNames)) + ' >'
		srcPath = os.path.join(srcDir, srcName)
		destName = srcName.split('.')[0] + '_slice' + str(pixCount) + '.tif'
		destPath = os.path.join(destDir, destName)
		if os.path.isfile(destPath):
			#print(countLog, destName, 'already processed')
			continue
		srcExt = srcName.split('.')[-1]
		
		if srcExt.lower() in ['png', 'jpg']:
			img = Image.open(srcPath)
			rgb = numpy.asarray(img)
			sliver(pixCount, rgb, destPath)
			print(countLog, srcName, 'slivered')

		elif srcExt.lower() in ['tif', 'tiff']:
			rgb = tifffile.imread(srcPath)
			sliver(pixCount, rgb, destPath)
			print(countLog, srcName, 'slivered')

		elif srcExt.lower() in ['nef', 'cr2', 'dng']:
			try:
				raw = rawpy.imread(srcPath)
			except:
				raw = None
			if raw:
				rawpyPars = rawpy.Params(output_bps=16)
				rgb = raw.postprocess(params=rawpyPars)
				sliver(pixCount, rgb, destPath)
				print(countLog, srcName, 'slivered')
			else:	
				corruptDir = os.path.join(srcDir, '../' + os.path.basename(srcDir) + '__corrupt')
				if not os.path.isdir(corruptDir):
					os.makedirs(corruptDir)
				corruptPath = os.path.join(corruptDir, srcName)
				if not os.path.exists(corruptPath):
					copyfile(srcPath, corruptPath)
					print(countLog, srcName, 'rawpy import faild, copied to "corrupt" dir')
				else:
					convertedName = srcName.split('.')[0] + '.dng'
					convertedPath = os.path.join(corruptDir, convertedName)
					if not os.path.exists(convertedPath):
						print(countLog, srcName, 'is corrupt, convert to .dng, leave in "corrupt" dir')
					else:
						try:
							raw = rawpy.imread(convertedPath)
						except:
							raw = None
						if raw:
							print(countLog, convertedName)
							sliver(pixCount, rawConverted, destPath)
						else:
							print(countLog, convertedName, 'found converted .dng, rawpy import faild, you`re screwed')
	print('finished!')
Esempio n. 2
0
def testBayerPattern():
    expected_desc = b'RGBG' # libraw hard-codes this and varies the color indices only
    
    for path in [rawTestPath, raw2TestPath]:
        raw = rawpy.imread(path)
        assert_equal(raw.color_desc, expected_desc)
        assert_array_equal(raw.raw_pattern, [[0,1],[3,2]])

    raw = rawpy.imread(raw3TestPath)
    assert_equal(raw.color_desc, expected_desc)
    assert_array_equal(raw.raw_pattern, [[3,2],[0,1]])
Esempio n. 3
0
 def take_picture_thread(self, stop):
    self.go_button.setEnabled(False)
    self.stop_button.setEnabled(True)
    if self.get_parameter('shutterspeed')[1] != 'bulb':
         self.text_line.setText('Capturing...')
         error, self.file_path = gphoto2.gp_camera_capture(self.camera, gphoto2.GP_CAPTURE_IMAGE, self.context)
         camera_file = gphoto2.check_result(gphoto2.gp_camera_file_get(self.camera, self.file_path.folder, self.file_path.name, gphoto2.GP_FILE_TYPE_NORMAL, self.context))
         error = gphoto2.gp_file_save(camera_file, self.file_path.name)
         img_num = 1
         while os.path.isfile(self.current_picture):
             self.current_picture = 'IMG_'+str(img_num).zfill(4)+'.CR2'
             img_num = img_num + 1
         if hasattr(self, 'file_path'):
             os.rename(self.file_path.name, self.current_picture)
         raw = rawpy.imread(self.current_picture)
         image = raw.postprocess(user_flip=False, output_bps=8)
         image = cv2.resize(image,(768, 512))
         self.myImage = QtGui.QImage(image.astype(numpy.uint8), 768, 512, QtGui.QImage.Format_RGB888)
         self.text_line.setText('Done')
         self.image_event = 1
         self.update()
    else:
        secs = int(self.bulb_time.text())
        i = 0
        while i < int(self.bulb_pics.text()) and not stop.isSet():
           self.file_path = self.get_bulb_picture(secs, stop)
           img_num = 1
           while os.path.isfile(self.current_picture):
               self.current_picture = 'IMG_'+str(img_num).zfill(4)+'.CR2'
               img_num = img_num + 1
           if hasattr(self, 'file_path'):
               os.rename(self.file_path.name, self.current_picture)
           raw = rawpy.imread(self.current_picture)
           image = raw.postprocess(user_flip=False, output_bps=8)
           image = cv2.resize(image,(768, 512))
           self.myImage = QtGui.QImage(image.astype(numpy.uint8), 768, 512, QtGui.QImage.Format_RGB888)
           self.text_line.setText('Image '+str(i+1)+" of "+self.bulb_pics.text()+' done')
           self.image_event = 1
           self.update()
           i = i + 1
        if stop.isSet():
           self.text_line.setText('Interruped by user!')
        stop.clear()
    if hasattr(self, 'file_path'):
        self.solve_button.setEnabled(True)
        self.solve_scale.setEnabled(True)
    self.go_button.setEnabled(True)
    self.stop_button.setEnabled(False)
Esempio n. 4
0
def testFileOpenAndPostProcess():
    raw = rawpy.imread(rawTestPath)
    assert_array_equal(raw.raw_image.shape, [2844, 4288])
        
    rgb = raw.postprocess(no_auto_bright=True, user_wb=raw.daylight_whitebalance)
    assert_array_equal(rgb.shape, [2844, 4284, 3])
    print_stats(rgb)
    save('test_8daylight.tiff', rgb)
 
    print('daylight white balance multipliers:', raw.daylight_whitebalance)
     
    rgb = raw.postprocess(no_auto_bright=True, user_wb=raw.daylight_whitebalance)
    print_stats(rgb)
    save('test_8daylight2.tiff', rgb)
 
    rgb = raw.postprocess(no_auto_bright=True, user_wb=raw.daylight_whitebalance,
                          output_bps=16)
    print_stats(rgb)
    save('test_16daylight.tiff', rgb)
     
    # linear images are more useful for science (=no gamma correction)
    # see http://www.mit.edu/~kimo/blog/linear.html
    rgb = raw.postprocess(no_auto_bright=True, user_wb=raw.daylight_whitebalance,
                          gamma=(1,1), output_bps=16)
    print_stats(rgb)
    save('test_16daylight_linear.tiff', rgb)
Esempio n. 5
0
def testBufferOpen():
    with open(rawTestPath, 'rb') as rawfile:
        with rawpy.imread(rawfile) as raw:
            assert_array_equal(raw.raw_image.shape, [2844, 4288])
            rgb = raw.postprocess()
    print_stats(rgb)
    save('test_buffer.tiff', rgb)
Esempio n. 6
0
 def _open(self):
     try:
         img = rawpy.imread(self.fp)
         self.mode = "RGB"
         self.size = (img.sizes.width, img.sizes.height)
     except:
         raise (SyntaxError, "Cannot process file.")
Esempio n. 7
0
    def _create_proxy_rawpy(self, source, dest, mode):
        # maybe Pillow supports this file type directly?
        if not self.image:
            try:
                self.image = Image.open(source)
            except IOError:
                pass
            except Exception as e:
                logging.error('cannot read {}: {}'.format(source, e.args[0]))
                raise e

        # obviously not, try decoding as Raw
        if not self.image:
            try:
                raw = rawpy.imread(source)
                rgb = raw.postprocess(use_camera_wb=True, no_auto_bright=True)
                self.image = Image.fromarray(rgb)
            except Exception as e:
                logging.error('cannot read {}: {}'.format(source, e.args[0]))
                raise e

        image = self.image.copy()
        if mode == self.PROXY_FULLSIZE:
            pass
        elif mode == self.PROXY_THUMBNAIL:
            image.thumbnail(settings.THUMBNAILSIZE)
        elif mode == self.PROXY_WEBSIZED:
            image.thumbnail(settings.WEBSIZE)

        try:
            image.save(dest)
        except Exception as e:
            logging.error('cannot write {}: {}'.format(dest, e.args[0]))
Esempio n. 8
0
def convertRawFileToArray(filename, batchSideLength):
    with rawpy.imread(filename) as raw:
        rgb = raw.postprocess()

    arr = rgb.astype(float)
    batchedArr = batchAndDifferentiate(arr, [(batchSideLength, False), \
        (batchSideLength, False), (1, False)])

    return batchedArr
Esempio n. 9
0
 def publishCR2(self, file):
     print "publishCR2", file
     raw = rawpy.imread(file)
     #rgb = raw.postprocess(gamma=(1,1), no_auto_bright=True, output_bps=16)
     rgb = raw.postprocess()
     imageio.imsave(file+'.tiff', rgb)
     print "save"
     bayer = raw.raw_image
     print "bayer"
     self.pub_image.publish(self.bridge.cv2_to_imgmsg(cv2.fromarray(bayer), "bgr8"))
Esempio n. 10
0
def testNikonD4Size():
    if rawpy.libraw_version < (0,15):
        # older libraw/dcraw versions don't support D4 fully
        return
    raw = rawpy.imread(raw2TestPath)
    s = raw.sizes
    assert_equal(s.width, 4940)
    assert_equal(s.height, 3292)
    assert_equal(s.top_margin, 0)
    assert_equal(s.left_margin, 2)
 def __init__(self, path):
     """ Convert RAW images to 3-color array (ndarray) """
     # Load the RAW file
     self._file = rawpy.imread(path)
     # Save the size of the image
     raw_height, raw_width, height, width, top_margin, left_margin, iheight, iwidth, pixel_aspect, flip= (self._file).sizes
     size = (height,width)
     # Postprocess this file to obtain a numpy ndarray of shape (h,w,c) : 16 bits => la palette de couleur peut contenir 2^16 = 65536 couleurs
     ndarray = self._file.postprocess(output_bps=16, output_color=ColorSpace.raw, demosaic_algorithm=DemosaicAlgorithm.VNG, use_camera_wb=True, no_auto_bright=True)
     # SuperClass Constructor
     super(ImageRaw,self).__init__(path,ndarray,size)
Esempio n. 12
0
def testWindowsFileLockRelease():
    # see https://github.com/neothemachine/rawpy/issues/10
    # we make a copy of the raw file which we will later remove
    copyPath = rawTestPath + '-copy'
    shutil.copyfile(rawTestPath, copyPath)
    with rawpy.imread(copyPath) as raw:
        rgb = raw.postprocess()
    assert_array_equal(rgb.shape, [2844, 4284, 3])
    print_stats(rgb)
    # if the following does not throw an exception on Windows,
    # then the file is not locked anymore, which is how it should be
    os.remove(copyPath)
    
    # we test the same using .close() instead of a context manager
    shutil.copyfile(rawTestPath, copyPath)
    raw = rawpy.imread(copyPath)
    rgb = raw.postprocess()
    raw.close()
    os.remove(copyPath)
    assert_array_equal(rgb.shape, [2844, 4284, 3])
Esempio n. 13
0
def testVisibleSize():
    for path in [rawTestPath, raw2TestPath]:
        print('testing', path)
        raw = rawpy.imread(path)
        s = raw.sizes
        print(s)
        h,w = raw.raw_image_visible.shape
        assert_equal(h, s.height)
        assert_equal(w, s.width)
        h,w = raw.raw_colors_visible.shape
        assert_equal(h, s.height)
        assert_equal(w, s.width)
Esempio n. 14
0
def load_image(path):
    if(path[-3:] == 'dng'):
        import rawpy
        with rawpy.imread(path) as raw:
            img = raw.postprocess()
        # img = plt.imread(path)
    elif(path[-3:]=='bmp' or path[-3:]=='jpg' or path[-3:]=='png'):
        import cv2
        return cv2.imread(path)[:,:,::-1]
    else:
        img = (255*plt.imread(path)[:,:,:3]).astype('uint8')

    return img
Esempio n. 15
0
def testProperties():
    raw = rawpy.imread(rawTestPath)
    
    print('black_level_per_channel:', raw.black_level_per_channel)
    print('color_matrix:', raw.color_matrix)
    print('rgb_xyz_matrix:', raw.rgb_xyz_matrix)
    print('tone_curve:', raw.tone_curve)
    
    assert_array_equal(raw.black_level_per_channel, [0,0,0,0])
    
    # older versions have zeros at the end, was probably a bug
    if rawpy.libraw_version >= (0,16):
        assert_array_equal(raw.tone_curve, np.arange(65536))
Esempio n. 16
0
def scandirs(path):
    for currentFile in glob.glob( os.path.join(path, '*') ):
        if os.path.isdir(currentFile):
            print 'got a directory: ' + currentFile
            scandirs(currentFile)

        filename, file_extension = os.path.splitext(currentFile)
        file_extension = file_extension.lower()
        if  file_extension in rawextens:
            print "processing file: " + currentFile
            imageFilename = "{0}.jpg".format(filename)
            raw = rawpy.imread(currentFile)
            rgb = raw.postprocess()
            imageio.imsave(imageFilename, rgb)
Esempio n. 17
0
def proxify(file_path, out_path, orientation):
    # create proxies and output to hashdir.
    
    try:
        os.makedirs(os.path.dirname(out_path))
    except:
        print logger('error creating dir - exists')

    try:
        ext = os.path.splitext(file_path)[-1].lower()
        # create image object for raw files through rawpy module
        if ext in RAW_EXTENSIONS:
            print logger( 'processing raw image')
            raw = rawpy.imread(file_path)
            rgb = raw.postprocess()
            im = Image.fromarray(rgb)
            filetype = 'raw'

        elif ext in MOVIE_EXTENSIONS:
            print logger('found a movie!')
            ffmpeg(file_path, out_path)
	    print logger('not making a proxy!')
            filetype = 'mov'
            return filetype

        else:
            # otherwise every other image type (not raw)
            filetype = 'img'
            im = Image.open(file_path)

        # rotate image as necessary 90, 180, 270 from metadata.
        if orientation in ROTATEMAP:
            print logger( 'ROTATING! ' + orientation)
            im = im.rotate(ROTATEMAP[orientation], expand=True)

        for s in THUMB_SIZE:
            im2 = im.copy() # create copy to allow for different resolution images to be created from full res. Just trust me...
            im2.thumbnail(s, Image.ANTIALIAS)
            im2.save(out_path + '_' + str(THUMB_SIZE.index(s)) + OUTEXT, "JPEG")
            im2.close()
        im.close()

        return filetype

    except:
        print logger( str(traceback.print_exc()))
        print logger( 'error creating image: ' + file_path )
        IM_ERRORS.append(file_path)

        return filetype
Esempio n. 18
0
 def _processedImage(self, key):
     if key in self.processedImagePaths:
         return self.processedImagePaths[key]
     assert self.useRaw
     rawPath = self.rawImagePaths[key]
    
     raw = rawpy.imread(rawPath)           
     rawpy.enhance.repair_bad_pixels(raw, self.badPixels)
     if self.raw_white_balance:
         if not (raw.color_desc == 'RGBG' and raw.num_colors == 3):
             raise NotImplementedError
         wb = [self.raw_white_balance[0], self.raw_white_balance[1], 
               self.raw_white_balance[2], self.raw_white_balance[1]]
     else:
         wb = self.apiData['raw_white_balance']
     rgb = raw.postprocess(user_wb=wb, output_bps=self.raw_bps, 
                           no_auto_bright=self.raw_no_auto_bright,
                           gamma=self.raw_gamma, user_flip=False)
     
     if self.apiData['raw_is_upside_down']:
         # rotate 180deg
         rgb = rgb[::-1,::-1]
     
     # correct lens distortion
     dist_corr = self.apiData['distortion_correction']
     if dist_corr:
         # TODO undistCoords could be cached to avoid recomputing them each time
         mod = getLensfunModifierFromParams(dist_corr['model'], dist_corr['params'], 
                                            rgb.shape[1], rgb.shape[0])
         undistCoords = mod.apply_geometry_distortion()
         rgb = lensfunpy.util.remap(rgb, undistCoords)
     
     # WCS solutions from the ISS dataset are based on cropped images,
     # therefore we crop here as well to make it match.
     rgb = croppedImage(rgb, divisible_by=16)
     
     if self.noRawPostprocessCaching:
         return rgb
     else:       
         processedImagePath = os.path.join(self.cacheFolder, key + '.tiff')
         saveImage(processedImagePath, rgb)
         self.processedImagePaths[key] = processedImagePath
         return processedImagePath
Esempio n. 19
0
def read_raw(path, tmp=None):
  import rawpy
  tmp_ = tmp or tempfile.mkdtemp(suffix='tmp', prefix='crayimage')

  _, filename = osp.split(path)
  tmppath = osp.join(tmp_, filename)

  with gzip.open(path, "r") as gf, open(tmppath, 'w') as f:
    shutil.copyfileobj(gf, f, length=128 * 1024)

  with rawpy.imread(tmppath) as img_f:
    img = img_f.raw_image.copy()

  os.remove(tmppath)

  if tmp is None:
    shutil.rmtree(tmp_)

  img = img.reshape((1, ) + img.shape)

  return img
Esempio n. 20
0
def generate_thumbnail(source, outname, box, skip, fit=False, options=None):
    """Create a thumbnail image."""

    logger = logging.getLogger(__name__)

    extension = os.path.splitext(source)[1]
    if extension in ('.cr2'):
        raw = rawpy.imread(source)
        img = PILImage.fromarray(raw.postprocess())
        original_format = "JPEG"
    else:
        img = PILImage.open(source)
        original_format = img.format

    if fit:
        img = ImageOps.fit(img, box, PILImage.ANTIALIAS)
    else:
        img.thumbnail(box, PILImage.ANTIALIAS)

    outformat = img.format or original_format or 'JPEG'
    logger.debug(u'Save thumnail image: {0} ({1})'.format(outname, outformat))
    save_image(img, outname, outformat, options=options, autoconvert=True)
Esempio n. 21
0
def testBadPixelRepair():
    def getColorNeighbors(raw, y, x):
        # 5x5 area around coordinate masked by color of coordinate
        raw_colors = raw.raw_colors_visible
        raw_color = raw_colors[y, x]
        masked = ma.masked_array(raw.raw_image_visible, raw_colors!=raw_color)
        return masked[y-2:y+3,x-2:x+3].copy()
    
    bad_pixels = np.loadtxt(badPixelsTestPath, int)
    i = 60
    y, x = bad_pixels[i,0], bad_pixels[i,1]
    
    for useOpenCV in [False,True]:
        if useOpenCV:
            if rawpy.enhance.cv2 is None:
                print('OpenCV not available, skipping subtest')
                continue
            print('testing with OpenCV')
        else:
            print('testing without OpenCV')
            oldCv = rawpy.enhance.cv2
            rawpy.enhance.cv2 = None
            
        for repair in [_repair_bad_pixels_generic, _repair_bad_pixels_bayer2x2]:
            print('testing ' + repair.__name__)
            raw = rawpy.imread(rawTestPath)
            
            before = getColorNeighbors(raw, y, x)
            repair(raw, bad_pixels, method='median')
            after = getColorNeighbors(raw, y, x)
        
            print(before)
            print(after)
        
            # check that the repaired value is the median of the 5x5 neighbors
            assert_equal(int(ma.median(before)), raw.raw_image_visible[y,x],
                         'median wrong for ' + repair.__name__)
        if not useOpenCV:
            rawpy.enhance.cv2 = oldCv
        train_id = train_ids[ind]
        in_files = glob.glob(input_dir + '%05d_00*.ARW' % train_id)
        in_path = in_files[np.random.random_integers(0, len(in_files) - 1)]
        _, in_fn = os.path.split(in_path)

        gt_files = glob.glob(gt_dir + '%05d_00*.hdr' % train_id)
        gt_path = gt_files[0]
        _, gt_fn = os.path.split(gt_path)
        in_exposure = float(in_fn[9:-5])
        gt_exposure = float(gt_fn[9:-5])
        ratio = min(gt_exposure / in_exposure, 300)  #calculate exposure ratio
        st = time.time()
        cnt += 1

        if input_images[str(ratio)[0:2]][ind] is None:
            raw = rawpy.imread(in_path)
            input_images[str(ratio)[0:2]][ind] = np.expand_dims(pack_raw(raw),
                                                                axis=0) * ratio

            print(gt_path)
            '''
			# method 1: read raw hdr file in rgbe format
			gt_file = open(gt_path, 'rb')
			header = gt_file.read(78)
			gt_bytes = gt_file.read()
			gt_file.close()
			gt_images[ind] = np.expand_dims(np.float32(pack_hdr(gt_bytes)/255.0),axis = 0)# 8-bit, 4 channels
			'''
            # method 2: use Reinhard tonemapped image as gt
            hdr = cv2.imread(gt_path, -1)
            tonemapReinhard = cv2.createTonemapReinhard(3.0, -2.5, 0, 0)
import tifffile as tiff
import math

paths = [ ]
tk = []
for k in range(1,17):
	paths.append('exposure%d.nef' % k )
	tk.append(2**(k-12))
print(k)
print(paths)
print(tk)
i = 0;
########## section 3.a <processing raw images and resizing the images >#######################
for path in paths:
	i= i +1	
	with rawpy.imread(path) as raw:
   		 rgb = raw.postprocess(gamma=(1,1), no_auto_bright=True, output_bps=16)
	xnew,ynew=rgb.shape[1]/10,rgb.shape[0]/10
	xnew = int(xnew)
	ynew = int(ynew)
	#print (xnew)
	rgb=cv2.resize(rgb,(xnew,ynew))
	imageio.imsave('processed_exposure%d.tiff' % i,rgb)
####################################

########## section 3.b <calculating hdr image using the formula >#######################

Ihdr=np.zeros((400,600,3))
Ihdr0=0
Ihdr1=0
Esempio n. 24
0
def main(args):
  setproctitle.setproctitle('hdrnet_run')

  inputs = get_input_list(args.input)

  # -------- Load params ----------------------------------------------------
  config = tf.ConfigProto()
  config.gpu_options.allow_growth = True
  with tf.Session(config=config) as sess:
    checkpoint_path = tf.train.latest_checkpoint(args.checkpoint_dir)
    if checkpoint_path is None:
      log.error('Could not find a checkpoint in {}'.format(args.checkpoint_dir))
      return

    metapath = ".".join([checkpoint_path, "meta"])
    log.info('Loading graph from {}'.format(metapath))
    tf.train.import_meta_graph(metapath)

    model_params = utils.get_model_params(sess)

  # -------- Setup graph ----------------------------------------------------
  if not hasattr(models, model_params['model_name']):
    log.error("Model {} does not exist".format(params.model_name))
    return
  mdl = getattr(models, model_params['model_name'])

  tf.reset_default_graph()
  net_shape = model_params['net_input_size']
  t_fullres_input = tf.placeholder(tf.float32, (1, None, None, 3))
  t_lowres_input = tf.placeholder(tf.float32, (1, net_shape, net_shape, 3))

  with tf.variable_scope('inference'):
    prediction = mdl.inference(
        t_lowres_input, t_fullres_input, model_params, is_training=False)
  output = tf.cast(255.0*tf.squeeze(tf.clip_by_value(prediction, 0, 1)), tf.uint8)
  saver = tf.train.Saver()

  if args.debug:
    coeffs = tf.get_collection('bilateral_coefficients')[0]
    if len(coeffs.get_shape().as_list()) == 6:
      bs, gh, gw, gd, no, ni = coeffs.get_shape().as_list()
      coeffs = tf.transpose(coeffs, [0, 3, 1, 4, 5, 2])
      coeffs = tf.reshape(coeffs, [bs, gh*gd, gw*ni*no, 1])
      coeffs = tf.squeeze(coeffs)
      m = tf.reduce_max(tf.abs(coeffs))
      coeffs = tf.clip_by_value((coeffs+m)/(2*m), 0, 1)

    ms = tf.get_collection('multiscale')
    if len(ms) > 0:
      for i, m in enumerate(ms):
        maxi = tf.reduce_max(tf.abs(m))
        m = tf.clip_by_value((m+maxi)/(2*maxi), 0, 1)
        sz = tf.shape(m)
        m = tf.transpose(m, [0, 1, 3, 2])
        m = tf.reshape(m, [sz[0], sz[1], sz[2]*sz[3]])
        ms[i] = tf.squeeze(m)

    fr = tf.get_collection('fullres_features')
    if len(fr) > 0:
      for i, m in enumerate(fr):
        maxi = tf.reduce_max(tf.abs(m))
        m = tf.clip_by_value((m+maxi)/(2*maxi), 0, 1)
        sz = tf.shape(m)
        m = tf.transpose(m, [0, 1, 3, 2])
        m = tf.reshape(m, [sz[0], sz[1], sz[2]*sz[3]])
        fr[i] = tf.squeeze(m)

    guide = tf.get_collection('guide')
    if len(guide) > 0:
      for i, g in enumerate(guide):
        maxi = tf.reduce_max(tf.abs(g))
        g = tf.clip_by_value((g+maxi)/(2*maxi), 0, 1)
        guide[i] = tf.squeeze(g)

  with tf.Session(config=config) as sess:
    log.info('Restoring weights from {}'.format(checkpoint_path))
    saver.restore(sess, checkpoint_path)

    for idx, input_path in enumerate(inputs):
      if args.limit is not None and idx >= args.limit:
        log.info("Stopping at limit {}".format(args.limit))
        break

      log.info("Processing {}".format(input_path))

      im_ext = os.path.splitext(input_path)[1].lower()
      if im_ext == '.cr2' or im_ext == '.nef':
        raw = rawpy.imread(input_path)
        im_input = raw.postprocess()
      else:
        raw = cv2.imread(input_path)                  # opencv reads into a BGR format
        im_input = cv2.cvtColor(raw, cv2.COLOR_BGR2RGB)

      if im_input.shape[2] == 4:
        log.info("Input {} has 4 channels, dropping alpha".format(input_path))
        im_input = im_input[:, :, :3]

      log.info("Max level: {}".format(np.amax(im_input[:, :, 0])))
      log.info("Max level: {}".format(np.amax(im_input[:, :, 1])))
      log.info("Max level: {}".format(np.amax(im_input[:, :, 2])))

      # HACK for HDR+.
      if im_input.dtype == np.uint16 and args.hdrp:
        log.info("Using HDR+ hack for uint16 input. Assuming input white level is 32767.")
        # im_input = im_input / 32767.0
        # im_input = im_input / 32767.0 /2
        # im_input = im_input / (1.0*2**16)
        im_input = skimage.img_as_float(im_input)
      else:
        im_input = skimage.img_as_float(im_input)

      # Make or Load lowres image
      if args.lowres_input is None:
        lowres_input = skimage.transform.resize(
            im_input, [net_shape, net_shape], order = 0)
      else:
        raise NotImplemented

      fname = os.path.splitext(os.path.basename(input_path))[0]
      output_path = os.path.join(args.output, fname+".png")
      basedir = os.path.dirname(output_path)

      im_input = im_input[np.newaxis, :, :, :]
      lowres_input = lowres_input[np.newaxis, :, :, :]

      feed_dict = {
          t_fullres_input: im_input,
          t_lowres_input: lowres_input
      }

      out_ = sess.run(output, feed_dict=feed_dict)

      if not os.path.exists(basedir):
        os.makedirs(basedir)

      Image.fromarray(out_, mode='RGB').save(output_path)

      if args.debug:
        output_path = os.path.join(args.output, fname+"_input.png")
        Image.fromarray(np.squeeze(im_input), mode='RGB').save(output_path)

        coeffs_ = sess.run(coeffs, feed_dict=feed_dict)
        output_path = os.path.join(args.output, fname+"_coeffs.png")
        Image.fromarray(coeffs_, mode='RGB').save(output_path)
        if len(ms) > 0:
          ms_ = sess.run(ms, feed_dict=feed_dict)
          for i, m in enumerate(ms_):
            output_path = os.path.join(args.output, fname+"_ms_{}.png".format(i))
            Image.fromarray(m, mode='RGB').save(output_path)

        if len(fr) > 0:
          fr_ = sess.run(fr, feed_dict=feed_dict)
          for i, m in enumerate(fr_):
            output_path = os.path.join(args.output, fname+"_fr_{}.png".format(i))
            Image.fromarray(m, mode='RGB').save(output_path)

        if len(guide) > 0:
          guide_ = sess.run(guide, feed_dict=feed_dict)
          for i, g in enumerate(guide_):
            output_path = os.path.join(args.output, fname+"_guide_{}.png".format(i))
            Image.fromarray(g, mode='RGB').save(output_path)
PARAMS_CONVERSION = {
    'demosaic_algorithm': rawpy.DemosaicAlgorithm(3),
    'no_auto_bright': True,
    'output_bps': 16,
    'gamma': (1, 1),
    'output_color': rawpy.ColorSpace(0),
    'user_wb': [1, 1, 1, 1],
    'four_color_rgb': True,
    # 'median_filter_passes': 3
}

# Processing for loop
i = 0
for path_red, path_green, path_blue in zip(filenames[0::3], filenames[1::3],
                                           filenames[2::3]):
    with rawpy.imread(path_red) as raw:
        red = raw.postprocess(**PARAMS_CONVERSION)

    with rawpy.imread(path_green) as raw:
        green = raw.postprocess(**PARAMS_CONVERSION)

    with rawpy.imread(path_blue) as raw:
        blue = raw.postprocess(**PARAMS_CONVERSION)

    merged_frames = np.zeros(red.shape)
    merged_frames[:, :, 0] = red[:, :, 0]
    merged_frames[:, :, 1] = green[:, :, 1]
    merged_frames[:, :, 2] = blue[:, :, 2]

    i += 1
    print(i)
Esempio n. 26
0
        os.makedirs('/home/stephan/Images/.memories/thumbnails')

    for picture in pictures:
        try:
            im = Image.open('/home/stephan/Images/%s' % picture.get('filename'))
            im.thumbnail((640, 480))

            if not os.path.exists(os.path.dirname('/home/stephan/Images/.memories/thumbnails/%s' % picture.get('filename'))):
                os.makedirs(os.path.dirname('/home/stephan/Images/.memories/thumbnails/%s' % picture.get('filename')))

            try:
                filename = picture.get('filename')
                im.save('/home/stephan/Images/.memories/thumbnails/%s' % filename)
            except KeyError as nef:
                print 'raw file %s' % picture.get('filename')
                raw = rawpy.imread('/home/stephan/Images/%s' % picture.get('filename'))
                rbg = raw.postprocess()
                img = Image.fromarray(rbg)
                filename = '%s.jpg' % os.path.splitext(picture.get('filename'))[0]
                img.save('%s.jpg' % (os.path.splitext('/home/stephan/Images/.memories/thumbnails/%s' % picture.get('filename'))[0]))

            print filename
            db.pictures.update_one({'_id': picture['_id']}, {"$set": {'thumbnail': filename}})

        except IOError as e:
            print e
            print picture.get('filename')
            print '---------------------'
            print '---------------------'
            print '---------------------'
            print '---------------------'
Esempio n. 27
0
'''
@ author Bean, Fangte, Yike,
'''
import cv2
import numpy as np
from matplotlib import pyplot as plt
import rawpy
from patchmatch import *
from tau import *
from hdr_fusion import *

# Read raw images. Here we use 1_320 as the reference image. Typically, an image under moderate
# exposure time has the most well exposued pixels.
raw = rawpy.imread('1_320.NEF')
imgR = raw.postprocess()
imgR = cv2.cvtColor(imgR, cv2.COLOR_RGB2BGR)
imgR = cv2.resize(imgR, None, fx=0.1, fy=0.1)
cv2.imwrite('imgR.png', imgR)

# Read two other images as source images
raw1 = rawpy.imread('2_800.NEF')
imgS1 = raw1.postprocess()
imgS1 = cv2.cvtColor(imgS1, cv2.COLOR_RGB2BGR)
imgS1 = cv2.resize(imgS1, None, fx=0.1, fy=0.1)
cv2.imwrite('imgS1.png', imgS1)

raw2 = rawpy.imread('3_160.NEF')
imgS2 = raw2.postprocess()
imgS2 = cv2.cvtColor(imgS2, cv2.COLOR_RGB2BGR)
imgS2 = cv2.resize(imgS2, None, fx=0.1, fy=0.1)
cv2.imwrite('imgS2.png', imgS2)
Esempio n. 28
0
def testHalfSizeParameter():
    raw = rawpy.imread(rawTestPath)
    s = raw.sizes
    rgb = raw.postprocess(half_size=True)
    assert_equal(rgb.shape[0], s.height // 2)
    assert_equal(rgb.shape[1], s.width // 2)
Esempio n. 29
0
def testThumbExtractBitmap():
    with rawpy.imread(raw4TestPath) as raw:
        thumb = raw.extract_thumb()
    assert thumb.format == rawpy.ThumbFormat.BITMAP
    assert_array_equal(thumb.data.shape, [378, 567, 3])
Esempio n. 30
0
def testThumbExtractJPEG():
    with rawpy.imread(rawTestPath) as raw:
        thumb = raw.extract_thumb()
    assert thumb.format == rawpy.ThumbFormat.JPEG
    img = imageio.imread(thumb.data)
    assert_array_equal(img.shape, [2832, 4256, 3])
def raw2fits(inputfile, crop="",  dark="", flat='', inverseGrayscale=False):
    """
    Create FITS object from raw file (doesn't write)
    parameters:
        - inputfile: input raw file path
        - crop: use this option if you want to crop the image. string like "x0,y0,size_x,size_y"
        - dark: path to pgm file containing frame to subtract
        - inverseGrayscale
    returns:
        - pyfits object
    """
    
    logger = logging.getLogger()

    # Read RAW meta
    try:
        # Getting the EXIF of raw file with dcraw
        p = subprocess.Popen(["dcraw", "-i", "-v", inputfile], stdout=subprocess.PIPE)
        dcraw_exif = p.communicate()[0].decode('utf-8')
        
        # Catching the Shutter Speed
        m = re.search('(?<=Shutter:).*(?=sec)', dcraw_exif)
        shutterstr = m.group(0).strip()
        if "/" in shutterstr:
            shutterplit = m.group(0).strip().split('/')
            shutter = float(shutterplit[0]) / float(shutterplit[1])
        else:
            shutter = float(shutterstr)

        m = re.search('(?<=Aperture: f/).*', dcraw_exif)
        aperture = m.group(0).strip()

        m = re.search('(?<=ISO speed:).*', dcraw_exif)
        iso = m.group(0).strip()

        m = re.search('(?<=Filename:).*', dcraw_exif)
        original_file = m.group(0).strip()

        m = re.search('(?<=Focal length: ).*(?=mm)', dcraw_exif)
        focal = m.group(0).strip()

        m = re.search('(?<=Camera:).*', dcraw_exif)
        camera = m.group(0).strip()
        
        m = re.search('(?<=Filter pattern:).*', dcraw_exif)
        filter_pattern = m.group(0).strip()
        
        # Catching the true Image Size
        # Image size:  7362 x 4920
        m = re.search('(?<=Image size: ).*', dcraw_exif)
        dim_list = m.group(0).strip().split('x')
        NAXIS1 = int(dim_list[0])
        NAXIS2 = int(dim_list[1])


        # Catching the Timestamp
        m = re.search('(?<=Timestamp:).*', dcraw_exif)
        date1 = m.group(0).split()
        months = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
        date = datetime.datetime(int(date1[4]), months[date1[1]], int(date1[2]), int(date1[3].split(':')[0]), int(date1[3].split(':')[1]), int(date1[3].split(':')[2]))

        # To use for timestamp/timezone correction FIXME
        #print('WARNING WARNING WARNING WARNING WARNING WARNING WARNING')
        #logger.critical('This is a very special mode for timing correction, do not use in PRODUCTION')
        #logger.critical("DATETIME: " + str(date))
        #from datetime import timedelta
        #utc_delta = timedelta(days=0,hours=11,minutes=0, seconds=6)
        #logger.critical(utc_delta)
        #logger.critical(date - utc_delta)
        #date = date - utc_delta
        #logger.critical("DATETIME: " + str(date))

        # formating date
        date = '{0:%Y-%m-%d %H:%M:%S}'.format(date)
        
        exposure_recorded_time = Time(date)
        
        try:
            # to to match the timestamp to actual trigger time + trigger delay
            try:
                # D810 56ms
                interval_log_file = dfn_utils.find_log_file(os.path.dirname(inputfile), suffix='_log_interval', extension='txt')
                logger.debug('Found interval log file: {}'.format(interval_log_file))
                fw_string = dfn_utils.search_dfn_operation_log(interval_log_file, key='leostick_version', module='interval_control_lin')
                ###    core_1_temperature_list = re.findall('(?:^Core 1:\s+)([\+-]\d+\.\d+)', sensors, re.MULTILINE)
                # 2017-08-23 18:17:51,447, INFO, interval_control_lin, leostick_version, built:Jun  7 2017 10:12:58 note:kit, small and ext compatible, bulb and non-bulb, pulse frequency and pulse width encoding, ext heating and cooling, single exposure and video camera triggering, exposure:5.0/15s, element period:1000ms, short 0 dash length:200ms, long 1 dash length:600ms, target:unifi^Ad
                #print(re.findall('(?:exposure:\d+\.\d+/)(\d+)', fw_string))
                cadence = int(re.findall('(?:exposure:\d+\.\d+/)(\d+)', fw_string)[0])
                logger.info('Found cadence {} in interval log {}'.format(cadence, os.path.basename(interval_log_file)))
            except Exception as e:
                logger.error(e)
                # default to 5 seconds (avoid messing up data too much)
                cadence = 5
                
            
            exposure_triggered_time = dfn_utils.round_to_nearest_n_seconds(exposure_recorded_time, cadence)
            
            logger.info('Image timestamp has been rounded off from {} to {}'.format(exposure_recorded_time.isot, exposure_triggered_time.isot))
            
            try:
                trigger_delay = TimeDelta(DSLR_TRIGGER_DELAY_CATALOG[camera], format='sec')
            except Exception as e:
                logger.error(e)
                trigger_delay = TimeDelta(0.0, format='sec')
                
            exposure_corrected_time = exposure_triggered_time + trigger_delay
            
            logger.info('Image timestamp has been offset from {} to {}'.format(exposure_triggered_time.isot, exposure_corrected_time.isot))
        
        except Exception as e:
            logger.error(e)
            exposure_corrected_time = exposure_recorded_time
        

    except:
        logger.error('Failed during DCRAW meta read attempt')
        raise


    
    # Dark subtraction if needed
    if dark != "":
        dark_com = ["-K ", dark]
    else:
        dark_com = []
        
    # Cropping if needed
    if crop == "":
        needsCropping = False
        cropprofile = "None"
        crop_command = []
    else:
        needsCropping = True
        # regex for finding 4 numbers separated by commas
        match = re.search("[0-9]+(,[0-9]+){3}", crop)
        # Crop centered presets
        if 'centered_' in crop and crop.split('_')[1].isnumeric():
            crop_size = int(crop.split('_')[1])
            # top left pixel must have even coordinates, otherwise bayer pattern will be shifted
            x0 = int((NAXIS1 - crop_size) / 2)
            y0 = int((NAXIS2 - crop_size) / 2)
            y_size = crop_size
            x_size = crop_size
            cropprofile = crop
        elif match:
            cropstr = match.group().split(',')
            x0 = int(cropstr[0])
            y0 = int(cropstr[1])
            x_size = int(cropstr[2])
            y_size = int(cropstr[3])
            cropprofile = "Custom"
        else:
            needsCropping = False
            cropprofile = "None"
            logger.error('Crop input is not valid. Crop aborted')
            raise KeyError('Invalid crop parameters')

        # top left pixel must have even coordinates, otherwise bayer pattern will be shifted
        if x0 % 2 == 1:
            x0 += 1
        if y0 % 2 == 1:
            y0 += 1

        # check the requested crop falls into the picture bounds
        if (x0 >= NAXIS1) or (y0 >= NAXIS2) or ((x0 + x_size) >= NAXIS1) or ((y0 + y_size) >= NAXIS2) or (x0 < 0) or (y0 < 0):
            needsCropping = False
            cropprofile = "None"
            OOB_msg = 'Crop input does NOT fall into picture bounds. Crop aborted'
            logger.error(OOB_msg)
            raise IndexError(OOB_msg)

        # use netpbm's pamcut to crop
        crop_command = ['pamcut', '-left', str(x0), '-top', str(y0), '-width', str(x_size), '-height', str(y_size)]
    
    
    if RAW_HANDLING_LIB == 'RAWPY':
        logger.info("Using RAWPY for decoding")
        # .raw_image contains 32 extra columns (on Sony) filled with nonsense
        raw_data = rawpy.imread(inputfile).raw_image_visible
        if dark != "":
            dark_data = pyfits.open(dark)[0].data
            raw_data = (raw_data - dark_data).astype(np.uint16)
        if flat != "":
            flat_data = pyfits.open(flat)[0].data
            raw_data = (raw_data / flat_data * np.mean(flat_data)).astype(np.uint16)
        if needsCropping:
            im_ppm = raw_data[y0:y0+y_size, x0:x0+x_size]
        else:
            im_ppm = raw_data
    
    elif RAW_HANDLING_LIB == 'DCRAW':
        logger.info("Using DCRAW for decoding")

        # DCRAW extract command
        # -c option send dcraw output to stdout
        base_command = ["dcraw", "-D", "-4", "-t", "0", "-c"]
        
        # Prepare command for converting the RAW to PPM
        the_command = base_command
        # dark subtraction option
        the_command += dark_com
        # target raw file
        the_command += [inputfile]
                    
        # Read RAW data
        try:
            conv = subprocess.Popen(the_command,
                                    stdout=subprocess.PIPE)
            
            # optional pipe to cropping command
            if len(crop_command) >= 1:
                crop = subprocess.Popen(crop_command,
                                        stdin=conv.stdout,
                                        stdout=subprocess.PIPE)
                end_of_pipe = crop.stdout
            else:
                end_of_pipe = conv.stdout
            
            # pipe output into netPbm object
            im_ppm = netPbm.NetpbmFile(end_of_pipe).asarray()
            
        except Exception as e:
            logger.critical('Failed during DCRAW data read attempt. Can be dcraw, pamcut, netPbm... :')
            logger.error(e)
            raise e
    
    else:
        logger.critical('shit went wrong')
        raise NotImplementedError('shit went wrong')

    try:
        if inverseGrayscale:
            maxBitPix = np.ndarray.max(im_ppm)
            im_ppm = maxBitPix * np.ones_like(im_ppm) - im_ppm

        # Create the FITS object
        hdu = pyfits.PrimaryHDU(im_ppm)

        # set basic header values
        hdu.header.set('OBSTIME', exposure_corrected_time.iso, 'ISO timestamp of start of exposure')
        # comment next line to bypass shutter fraction problem
        hdu.header.set('EXPTIME', float(shutter), 'Exposure time in s')
        hdu.header.set('APERTUR', float(aperture), 'Lens aperture, as in f/APERTUR')
        hdu.header.set('ISO', int(iso), 'ISO speed')
        hdu.header.set('FOCAL', float(focal), 'Focal length in mm')
        hdu.header.set('FILT_PAT', filter_pattern, 'Shape of the Color array (usually Bayer)')
        hdu.header.set('ORIGIN', original_file)
        hdu.header.set('CAMERA', camera, 'Camera model')
        hdu.header.add_comment('FITS File Created with ' + __pipeline_task__)
        if needsCropping:
            hdu.header.set('CROPPED', "TRUE", 'If picture has been cropped from original')
            hdu.header.set('CROP', cropprofile, 'Crop preset (for further use)')
            hdu.header.set('CROPX0', x0, 'LEFT boundary of the crop')
            hdu.header.set('CROPX1', x0 + x_size, 'RIGHT boundary of the crop')
            hdu.header.set('CROPY0', y0, 'BOTTOM boundary of the crop')
            hdu.header.set('CROPY1', y0 + y_size, 'TOP boundary of the crop')
            hdu.header.set('ONAXIS1', NAXIS1, 'Original NAXIS1 before cropping')
            hdu.header.set('ONAXIS2', NAXIS2, 'Original NAXIS2 before cropping')
        else:
            hdu.header.set('CROPPED', "FALSE", 'If picture has been cropped from original')
    except:
        logger.error('Something went wrong while creating the FITS object')
        raise

    logger.info("RAW extraction into FITS successful")

    return hdu
Esempio n. 32
0
def testManualClose():
    raw = rawpy.imread(rawTestPath)
    assert_array_equal(raw.raw_image.shape, [2844, 4288])
    raw.close()
Esempio n. 33
0
import rawpy, matplotlib.pyplot as plt, numpy as np

path='tree.exif.dng'
raw=rawpy.imread(path)
print 'Sizes of the image:',raw.sizes
print 'Bayer pattern:\n',raw.raw_pattern
print 'Indices 0,1,2,3 in above are, in order: ',raw.color_desc
print 'camera_whitebalance',raw.camera_whitebalance
print 'daylight_whitebalance',raw.daylight_whitebalance
##note the two following methods are (y,x) - annoying!
print 'Colour of bayer pixel at 101,100:',raw.raw_color(101,100)
print 'Value of bayer pixel at 101,100:',raw.raw_value(101,100)

nx=raw.raw_image.shape[1]
ny=raw.raw_image.shape[0]
ris=raw.raw_image.astype(float)
rismax=ris.max()

#Make an rgb bayer image
rgb=np.zeros((ny,nx,3), 'float')
rgb[1::2,0::2,0]=ris[1::2,0::2]/rismax
rgb[0::2,0::2,1]=ris[0::2,0::2]/rismax
rgb[1::2,1::2,1]=ris[1::2,1::2]/rismax
rgb[0::2,1::2,2]=ris[0::2,1::2]/rismax

plt.imshow(rgb, interpolation='none')
plt.show()

#Perform a half-resolution demosaic
rgbi=np.zeros((ny/2,nx/2,3), 'float')
rgbi[::,::,0]=ris[1::2,0::2]/rismax
    def _inference_image(self, model, transform, filename):
        '''
            Loads the image with given filename from disk, splits it up into
            regular patches, performs inference and then re-splits the image
            into patches that fit the predicted boxes tightly
            ('WindowCropping' strategy), if there are any boxes.
            Commits the resulting patch names to the database and returns the
            identified bounding boxes under the patch names as a dict.
        '''

        device = self.get_device()

        # load image
        filePath = os.path.join(self.baseFolder_unlabeled, filename)

        _, fileExt = os.path.splitext(filePath)
        if fileExt.lower() in ['.nef', '.cr2']:
            img = Image.fromarray(rawpy.imread(filePath).postprocess())
        else:
            img = Image.open(filePath).convert('RGB')

        # transform
        tensor = transform(img).to(device)

        # evaluate in a grid fashion
        gridX, gridY = tensorSharding.createSplitLocations_auto(
            img.size, [self.patchSize[1], self.patchSize[0]],
            stride=self.stride,
            tight=True)
        tensors = tensorSharding.splitTensor(
            tensor, [self.patchSize[1], self.patchSize[0]], gridY, gridX)
        gridX, gridY = gridX.view(-1).float(), gridY.view(-1).float()

        bboxes = torch.empty(size=(
            0,
            4,
        ), dtype=torch.float32)
        labels = torch.empty(size=(0, ), dtype=torch.long)
        confs = torch.empty(size=(
            0,
            model.numClasses,
        ), dtype=torch.float32)
        scores = torch.empty(size=(0, ), dtype=torch.float32)

        numPatches = tensors.size(0)
        numBatches = int(np.ceil(numPatches / float(self.batchSize)))
        for t in range(numBatches):
            startIdx = t * self.batchSize
            endIdx = min((t + 1) * self.batchSize, numPatches)

            batch = tensors[startIdx:endIdx, :, :, :]

            if len(batch.size()) == 3:
                batch = batch.unsqueeze(0)

            with torch.no_grad():
                bboxes_pred_img, labels_pred_img = model(batch)

            bboxes_pred_img, labels_pred_img, confs_pred_img = self.encoder.decode(
                bboxes_pred_img.squeeze(0).cpu(),
                labels_pred_img.squeeze(0).cpu(),
                self.patchSize,
                cls_thresh=0.1,
                nms_thresh=0,  #TODO
                return_conf=True)

            # incorporate patch offsets and append to list of predictions
            for b in range(len(bboxes_pred_img)):
                if len(bboxes_pred_img[b]):
                    bboxes_pred_img[b][:, 0] += gridX[startIdx + b]
                    bboxes_pred_img[b][:, 1] += gridY[startIdx + b]
                    bboxes_pred_img[b][:, 2] += gridX[startIdx + b]
                    bboxes_pred_img[b][:, 3] += gridY[startIdx + b]

                    scores_pred, _ = torch.max(confs_pred_img[b], 1)

                    bboxes = torch.cat((bboxes, bboxes_pred_img[b]), dim=0)
                    labels = torch.cat((labels, labels_pred_img[b]), dim=0)
                    confs = torch.cat((confs, confs_pred_img[b]), dim=0)
                    scores = torch.cat((scores, scores_pred), dim=0)

        # do NMS on entire set
        keep = box_nms(bboxes, scores, threshold=0.1)  #TODO
        bboxes = bboxes[keep, :]
        labels = labels[keep]
        confs = confs[keep, :]
        scores = scores[keep]

        # re-split into patches (WindowCropping)
        patchData = self.windowCropper.splitImageIntoPatches(
            img, bboxes, labels, confs)

        # #TODO
        # import matplotlib.pyplot as plt
        # from matplotlib.patches import Rectangle
        # plt.figure(1)
        # plt.clf()
        # plt.imshow(img)
        # ax = plt.gca()
        # for b in range(bboxes.size(0)):
        #     ax.add_patch(Rectangle(
        #         (bboxes[b,0], bboxes[b,1]),
        #         (bboxes[b,2] - bboxes[b,0]), (bboxes[b,3] - bboxes[b,1]),
        #         fill=False,
        #         ec='r'
        #     ))
        # plt.draw()
        # plt.waitforbuttonpress()

        # iterate over patches
        result = {}
        for key in patchData.keys():

            if not self.options['contrib']['export_empty_patches'] and not len(
                    patchData[key]['predictions']):
                continue

            # patch name
            patchName = re.sub('\..*$', '', filename) + '_' + key + '.JPG'

            patchDir = os.path.join(
                self.config.getProperty('FileServer', 'staticfiles_dir'),
                patchName)
            parentFolder, _ = os.path.split(patchDir)
            os.makedirs(parentFolder, exist_ok=True)

            # save patch
            patchData[key]['patch'].save(patchDir)

            # append metadata
            result[patchName] = {'predictions': patchData[key]['predictions']}

            # #TODO
            # plt.figure(2)
            # plt.clf()
            # plt.imshow(patchData[key]['patch'])
            # psz = patchData[key]['patch'].size
            # ax = plt.gca()
            # for b in range(len(patchData[key]['predictions'])):
            #     bbox = patchData[key]['predictions'][b]
            #     ax.add_patch(Rectangle(
            #         (psz[0] * (bbox['x']-bbox['width']/2), psz[1] * (bbox['y']-bbox['height']/2),),
            #         psz[0]*bbox['width'], psz[0]*bbox['height'],
            #         fill=False,
            #         ec='r'
            #     ))
            # plt.draw()
            # plt.waitforbuttonpress()

        # return metadata
        return result
Esempio n. 35
0
def predict_Ltsitd (filename) :
    
    if request.method == 'POST':
        file_path1 = os.path.join(APP_ROOT, 'static/hasilfitur1')
        if not os.path.isdir(file_path1):
            os.mkdir(file_path1)

        input_dir = './static/RAW/'
        checkpoint_dir = './checkpoint/Sony/'
	

        DEBUG = 0
        if DEBUG == 1:
            save_freq = 2
            test_ids = test_ids[0:5]

        def lrelu(x):
            return tf.maximum(x * 0.2, x)


        def upsample_and_concat(x1, x2, output_channels, in_channels):
            pool_size = 2
            deconv_filter = tf.Variable(tf.truncated_normal([pool_size, pool_size, output_channels, in_channels], stddev=0.02))
            deconv = tf.nn.conv2d_transpose(x1, deconv_filter, tf.shape(x2), strides=[1, pool_size, pool_size, 1])

            deconv_output = tf.concat([deconv, x2], 3)
            deconv_output.set_shape([None, None, None, output_channels * 2])

            return deconv_output


        def network(input):
            conv1 = slim.conv2d(input, 32, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv1_1')
            conv1 = slim.conv2d(conv1, 32, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv1_2')
            pool1 = slim.max_pool2d(conv1, [2, 2], padding='SAME')

            conv2 = slim.conv2d(pool1, 64, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv2_1')
            conv2 = slim.conv2d(conv2, 64, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv2_2')
            pool2 = slim.max_pool2d(conv2, [2, 2], padding='SAME')

            conv3 = slim.conv2d(pool2, 128, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv3_1')
            conv3 = slim.conv2d(conv3, 128, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv3_2')
            pool3 = slim.max_pool2d(conv3, [2, 2], padding='SAME')

            conv4 = slim.conv2d(pool3, 256, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv4_1')
            conv4 = slim.conv2d(conv4, 256, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv4_2')
            pool4 = slim.max_pool2d(conv4, [2, 2], padding='SAME')

            conv5 = slim.conv2d(pool4, 512, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv5_1')
            conv5 = slim.conv2d(conv5, 512, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv5_2')

            up6 = upsample_and_concat(conv5, conv4, 256, 512)
            conv6 = slim.conv2d(up6, 256, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv6_1')
            conv6 = slim.conv2d(conv6, 256, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv6_2')

            up7 = upsample_and_concat(conv6, conv3, 128, 256)
            conv7 = slim.conv2d(up7, 128, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv7_1')
            conv7 = slim.conv2d(conv7, 128, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv7_2')

            up8 = upsample_and_concat(conv7, conv2, 64, 128)
            conv8 = slim.conv2d(up8, 64, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv8_1')
            conv8 = slim.conv2d(conv8, 64, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv8_2')

            up9 = upsample_and_concat(conv8, conv1, 32, 64)
            conv9 = slim.conv2d(up9, 32, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv9_1')
            conv9 = slim.conv2d(conv9, 32, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv9_2')

            conv10 = slim.conv2d(conv9, 12, [1, 1], rate=1, activation_fn=None, scope='g_conv10')
            out = tf.depth_to_space(conv10, 2)
            return out


        def pack_raw(raw):
            # read image black level
            bl = raw.black_level_per_channel[0]

            # pack Bayer image to 4 channels
            im = raw.raw_image_visible.astype(np.float32)
            im = np.maximum(im - bl, 0) / (16383 - bl)  # subtract the black level

            im = np.expand_dims(im, axis=2)
            img_shape = im.shape
            H = img_shape[0]
            W = img_shape[1]

            out = np.concatenate((im[0:H:2, 0:W:2, :],
                                      im[0:H:2, 1:W:2, :],
                                      im[1:H:2, 1:W:2, :],
                                      im[1:H:2, 0:W:2, :]), axis=2)
            return out


        #manggil model
        sess = tf.Session()
        in_image = tf.placeholder(tf.float32, [None, None, None, 4])
        gt_image = tf.placeholder(tf.float32, [None, None, None, 3])
        out_image = network(in_image)

        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
        if ckpt:
            print('loaded ' + ckpt.model_checkpoint_path)
            saver.restore(sess, ckpt.model_checkpoint_path)

        #proses gambar input
        in_files = glob.glob(input_dir + filename)
        in_path = in_files[0]
            
        #in_fn = os.path.basename(in_path)
        #print(in_fn)

        ratio = 200
        raw = rawpy.imread(in_path)

        input_full = np.expand_dims(pack_raw(raw), axis=0) * ratio
        im = raw.postprocess(use_camera_wb=True, half_size=False, no_auto_bright=True, output_bps=16)

        # scale_full = np.expand_dims(np.float32(im/65535.0),axis = 0)*ratio
        scale_full = np.expand_dims(np.float32(im / 65535.0), axis=0)
                    
        input_full = np.minimum(input_full, 1.0)

        output = sess.run(out_image, feed_dict={in_image: input_full})
        output = np.minimum(np.maximum(output, 0), 1)

        output = output[0, :, :, :]
        origin_full = scale_full[0, :, :, :]

        #save image
        fileoutput = os.path.splitext(filename)[0] + '.png'
		#fileoutput = namafile + '.png'
        fileinput = os.path.splitext(filename)[0] + '.png'
        namainput = fileinput	
        scipy.misc.toimage(output * 255, high=255, low=0, cmin=0, cmax=255).save(file_path1 + '/' + fileoutput)
        #scipy.misc.toimage(origin_full * 255, high=255, low=0, cmin=0, cmax=255).save(input_dir + '/' + fileoutput)
    	#out = scipy.misc.toimage(output * 255, high=255, low=0, cmin=0, cmax=255)
        # out.save(os.path.join(file_path1, 'out.png'))
	
    keluar =  Image.open(os.path.join(file_path1, fileoutput))
	
    return render_template("resultem.html", keluar=fileoutput, namainput=fileinput) #belom tau bisa gini apa engga wkwkw
Esempio n. 36
0
def testHighlightModeParameter():
    raw = rawpy.imread(rawTestPath)
    raw.postprocess(highlight_mode=1)
    raw.postprocess(highlight_mode=rawpy.HighlightMode.Blend)
    raw.postprocess(highlight_mode=rawpy.HighlightMode.Reconstruct(3))
Esempio n. 37
0
    def __getitem__(self, idx):

        raw = rawpy.imread(self.gt_files[idx])

        img_gt = raw.postprocess(use_camera_wb=True,
                                 half_size=False,
                                 no_auto_bright=True,
                                 output_bps=16).copy()
        img_gtt = np.float32(img_gt / 65535.0)

        raw.close()

        raw = rawpy.imread(self.train_files[idx])
        img = raw.raw_image_visible.astype(np.float32).copy()
        raw.close()

        img_loww = (np.maximum(img - 512, 0) / (16383 - 512))
        H, W = img_loww.shape

        ##############################################################################

        r_low = []
        g1_low = []
        g2_low = []
        b_low = []

        gt = []

        for_amplifier = []

        for gener in range(4):

            if random.randint(0, 100) > 50:
                flip_flag = True
            else:
                flip_flag = False

            if random.randint(0, 100) < 20:
                v_flag = True
            else:
                v_flag = False

        #     print(H)

            i = random.randint(0, (H - self.opt['patch'] - 2) // 2) * 2
            j = random.randint(0, (W - self.opt['patch'] - 2) // 2) * 2

            img_low = img_loww[i:i + self.opt['patch'],
                               j:j + self.opt['patch']]
            img_gt = img_gtt[i:i + self.opt['patch'],
                             j:j + self.opt['patch'], :]

            if flip_flag:
                img_gt = np.flip(img_gt, 0).copy()
                img_low = np.flip(img_low, 0).copy()

            if v_flag:
                img_gt = np.flip(img_gt, 1).copy()
                img_low = np.flip(img_low, 1).copy()

            values, edges = np.histogram(img_low,
                                         bins=self.a,
                                         range=(0, 1),
                                         normed=None,
                                         weights=None,
                                         density=None)
            values = values / (img_low.shape[0] * img_low.shape[1])

            img_low_r = img_low[0:self.opt['patch']:2, 0:self.opt['patch']:2]
            img_low_g1 = img_low[0:self.opt['patch']:2, 1:self.opt['patch']:2]
            img_low_g2 = img_low[1:self.opt['patch']:2, 0:self.opt['patch']:2]
            img_low_b = img_low[1:self.opt['patch']:2, 1:self.opt['patch']:2]

            for_amplifier.append(torch.from_numpy(values).float())

            img_gt_avg = np.zeros((opt['patch'] // 8, opt['patch'] // 8,
                                   int(64 * 3))).astype(np.float32)

            r_avg = np.zeros((opt['patch'] // 16, opt['patch'] // 16,
                              64)).astype(np.float32)
            g1_avg = np.zeros((opt['patch'] // 16, opt['patch'] // 16,
                               64)).astype(np.float32)
            g2_avg = np.zeros((opt['patch'] // 16, opt['patch'] // 16,
                               64)).astype(np.float32)
            b_avg = np.zeros((opt['patch'] // 16, opt['patch'] // 16,
                              64)).astype(np.float32)

            count_gt = 0  # We now begin the Pack 8x operation
            count_raw = 0
            for ii in range(8):
                for jj in range(8):

                    img_gt_avg[:, :, count_gt:count_gt +
                               3] = img_gt[ii:opt['patch']:8,
                                           jj:opt['patch']:8, :]
                    count_gt = count_gt + 3
                    #             print(count_gt)

                    r_avg[:, :, count_raw] = img_low_r[ii:opt['patch'] // 2:8,
                                                       jj:opt['patch'] // 2:8]
                    g1_avg[:, :,
                           count_raw] = img_low_g1[ii:opt['patch'] // 2:8,
                                                   jj:opt['patch'] // 2:8]
                    g2_avg[:, :,
                           count_raw] = img_low_g2[ii:opt['patch'] // 2:8,
                                                   jj:opt['patch'] // 2:8]
                    b_avg[:, :, count_raw] = img_low_b[ii:opt['patch'] // 2:8,
                                                       jj:opt['patch'] // 2:8]
                    count_raw = count_raw + 1
        #             print('{},{},{}'.format(count_raw,ii,jj))

            gt.append(
                torch.from_numpy((np.transpose(img_gt_avg,
                                               [2, 0, 1]))).float())
            r_low.append(
                torch.from_numpy((np.transpose(r_avg, [2, 0, 1]))).float())
            g1_low.append(
                torch.from_numpy((np.transpose(g1_avg, [2, 0, 1]))).float())
            g2_low.append(
                torch.from_numpy((np.transpose(g2_avg, [2, 0, 1]))).float())
            b_low.append(
                torch.from_numpy((np.transpose(b_avg, [2, 0, 1]))).float())

        return gt, r_low, g1_low, g2_low, b_low, for_amplifier
Esempio n. 38
0
def testSegfaultBug():
    # https://github.com/neothemachine/rawpy/issues/7
    im = rawpy.imread(rawTestPath).raw_image
    assert_array_equal(im.shape, [2844, 4288])
    print(im)
Esempio n. 39
0
def testSegfaultBug():
    # https://github.com/neothemachine/rawpy/issues/7
    im = rawpy.imread(rawTestPath).raw_image
    assert_array_equal(im.shape, [2844, 4288])
    print(im)
Esempio n. 40
0
def testLibRawFileUnsupportedError():
    rawpy.imread(os.path.join(thisDir, 'README.txt'))
Esempio n. 41
0
def read_raw_image(filename):
    with rawpy.imread(filename) as raw:
        rgb = raw.postprocess()
    # Changing to BGR
    rgb[:, :, 0], rgb[:, :, 2] = rgb[:, :, 2], rgb[:, :, 0]
    return rgb
Esempio n. 42
0
def testContextManager():
    with rawpy.imread(rawTestPath) as raw:
        assert_array_equal(raw.raw_image.shape, [2844, 4288])
Esempio n. 43
0
        srgb = np.clip(srgb, 0, 1)
        return (srgb*255).astype(np.uint8)


def convertColor(img, ccm):

    out = np.zeros_like(img)
    out[:, :, 0] = ccm[0, 0] * img[:, :, 0] + ccm[0, 1] * img[:, :, 1] + ccm[0, 2] * img[:, :, 2]
    out[:, :, 1] = ccm[1, 0] * img[:, :, 0] + ccm[1, 1] * img[:, :, 1] + ccm[1, 2] * img[:, :, 2]
    out[:, :, 2] = ccm[2, 0] * img[:, :, 0] + ccm[2, 1] * img[:, :, 1] + ccm[2, 2] * img[:, :, 2]
    return out

if __name__ == '__main__':
    raw = RAW()
    raw = raw.fromFile('00002_00_10s.ARW')
    srgb = raw.get_sRGB()
    Image.fromarray(srgb).show()

    raw = rawpy.imread('00002_00_10s.ARW')
    srgb = raw.postprocess(
        demosaic_algorithm=rawpy.DemosaicAlgorithm.AHD,
        half_size=False,
        use_camera_wb=True,
        output_color=rawpy.ColorSpace.sRGB,
        output_bps=8,
        no_auto_bright=False,
        no_auto_scale=True
    )
    Image.fromarray(srgb).show()

    print('Over!')
Esempio n. 44
0
def load(file, name=None):
    img = rawpy.imread(file)
    if not name:
        name = file

    return image(image=img, name=name)
Esempio n. 45
0
def test_color(path_im, color_name, ppg_data, wb_roi, patch_roi, path_jpeg = None):
    #Full DNG detail
    # with exiftool.ExifTool() as et:
    #     metadata = et.get_metadata(path_im)
    #     for key in metadata.keys():
    #         print(key, ' ', metadata[key])

    with rp.imread(path_im) as raw:
        h, w = np.shape(raw.raw_image)
        print(h, ' ', w)


        #print(raw.raw_colors)
        m_raw_rgb = np.array([0.0, 0.0, 0.0, 0.0])
        m_raw_cnt = np.array([0, 0, 0, 0])
        for i in range(wb_roi[1], wb_roi[3]):
            for j in range(wb_roi[0], wb_roi[2]):
                m_raw_rgb[raw.raw_color(i, j)] += raw.raw_value(i, j)
                m_raw_cnt[raw.raw_color(i, j)] += 1
        m_raw_rgb /= m_raw_cnt
        # print(m_raw_rgb)
        wp = np.array([m_raw_rgb[0], m_raw_rgb[1], m_raw_rgb[2]])
        wp /= wp[1]
        print('white point: ' + str(wp))
        user_whitebalance = (1.0 / wp).tolist() + [0]
        print('user white balance: ' + str(user_whitebalance))
        print('camera white balance: ' + str(raw.camera_whitebalance))

        print('Custom processing:')
        custom_xyz = raw.postprocess(user_wb=user_whitebalance,
                                     output_bps=16,
                                     output_color=rp.ColorSpace.XYZ,
                                     gamma = (1, 1)).astype(float) / (2.0**16 - 1)

        sample = custom_xyz[patch_roi[1]:patch_roi[3], patch_roi[0]:patch_roi[2]]
        c_xyz = [np.mean(sample[:, :, x]) for x in range(0, 3)]
        c_rgb = cam2xyz.XYZ2sRGB(c_xyz)

        print('Default processing:')
        default_xyz = raw.postprocess(use_camera_wb=True,
                                     output_bps=16,
                                     output_color=rp.ColorSpace.XYZ,
                                     gamma = (1, 1)).astype(float) / (2.0**16 - 1)
        sample = default_xyz[patch_roi[1]:patch_roi[3], patch_roi[0]:patch_roi[2]]
        d_xyz = [np.mean(sample[:, :, x]) for x in range(0, 3)]
        d_rgb = cam2xyz.XYZ2sRGB(d_xyz)
        j_rgb = [0, 0, 0]
        if path_jpeg:
            jpeg_rgb = cv2.cvtColor(cv2.imread(path_jpeg), cv2.COLOR_BGR2RGB)
            sample = jpeg_rgb[patch_roi[1]:patch_roi[3], patch_roi[0]:patch_roi[2]]
            j_rgb = [np.mean(sample[:, :, x]) for x in range(0, 3)]

        c_lab = cam2xyz.XYZ2LAB(c_xyz)
        d_lab = cam2xyz.XYZ2LAB(d_xyz)
        if path_jpeg:
            j_lab = cam2xyz.RGB2LAB(j_rgb)
        print('RGB after eliminate illuminant: ' + str(c_rgb))
        print('PPG RGB: ' + str(ppg_data[color_name]['RGB']))
        print('Lab after eliminate illuminant: ' + str(c_lab))
        print('PPG Lab: ' + str(ppg_data[color_name]['LAB']))

        # dE_c = deltaE(c_lab, np.array(ppg_data[color_name]['LAB']))
        # dE_d = deltaE(d_lab, np.array(ppg_data[color_name]['LAB']))
        dE_c = colour.delta_E(c_lab, np.array(ppg_data[color_name]['LAB']))
        dE_d = colour.delta_E(d_lab, np.array(ppg_data[color_name]['LAB']))
        # print('Custom wb dE: ', deltaE(c_lab, np.array(ppg_data[color_name]['LAB'])))
        # print('Default wb dE: ', deltaE(d_lab, np.array(ppg_data[color_name]['LAB'])))
        print('Custom wb dE: ', dE_c)
        print('Default wb dE: ', dE_d)


        dE_j = -1.0
        if path_jpeg:
            dE_j = colour.delta_E(j_lab, np.array(ppg_data[color_name]['LAB']))
            print('JPEG dE: ', dE_j)

        # vis_s = np.tile(c_rgb, [300, 300, 1])
        # vis_d = np.tile(d_rgb, [300, 300, 1])
        # vis_ppg = np.tile(ppg_data[color_name]['RGB'], [300, 300, 1])
        # if path_jpeg:
        #     vis_j = np.tile(j_rgb, [300, 300, 1])
        # if path_jpeg:
        #     fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4)
        #     ax1.imshow(vis_s)
        #     ax1.set_title('Custom wb')
        #     ax2.imshow(vis_ppg)
        #     ax2.set_title('Gt')
        #     ax3.imshow(vis_d)
        #     ax3.set_title('Default wb')
        #     ax4.imshow(vis_j)
        #     ax4.set_title('jpeg')
        #     plt.show()
        return dE_d, dE_c, dE_j, d_rgb, c_rgb, j_rgb, d_xyz, c_xyz
Esempio n. 46
0
import rawpy
import imageio

for i in range(33):
    raw = rawpy.imread('HG/'+str(i)+'.CR2')
    rgb = raw.postprocess()
    imageio.imsave('HG/'+str(i)+'.tiff', rgb)
Esempio n. 47
0
filedir = args.raw_image_folder

if not os.path.exists(filedir+'/jpg'):
    os.makedirs(filedir+'/jpg')
    
if not os.path.exists(filedir+'/jpg/cropped'):
    os.makedirs(filedir+'/jpg/cropped')
    
jpgdir = filedir+'/jpg'
croppeddir = jpgdir + '/cropped'

# Convert raw images CR2 in png
for full_path in glob.glob(filedir + "/*.CR2"): 
    filename = os.path.basename(full_path)
    print(full_path)
    raw = rawpy.imread(full_path)
    image = raw.postprocess()
    imageio.imsave(jpgdir + "/" + filename.split(".")[0] + ".jpg", image)   


for full_path in glob.glob(jpgdir + "/*.jpg"): 

    filename = os.path.basename(full_path)
    # Original image
    image = cv2.imread(full_path)
    print(full_path)
    # # Resize
    draw = np.zeros_like(image)
    # Convert to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # Get black and white image
Esempio n. 48
0
    bad_pixels = np.asarray(bad_pixels)
    # Format is: pixel column, pixel row, UNIX time of death
    dc = np.zeros((len(bad_pixels),3), dtype=int)
    dc[:,0] = bad_pixels[:,1]
    dc[:,1] = bad_pixels[:,0]
    np.savetxt(path, dc, fmt='%1i')

if __name__ == '__main__':
    prefix = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'test')
    testfiles = ['iss030e122639.NEF', 'iss030e122659.NEF', 'iss030e122679.NEF',
                 'iss030e122699.NEF', 'iss030e122719.NEF'][0:1]
    paths = [os.path.join(prefix, f) for f in testfiles]
    coords = find_bad_pixels(paths)
        
    import imageio
    raw = rawpy.imread(paths[0])
    if not os.path.exists('test_original.png'):
        rgb = raw.postprocess()
        imageio.imsave('test_original.png', rgb)
    
    # A. use dcraw repair
    # Note that this method fails when two bad pixels are direct neighbors.
    # This is because it corrects each bad pixel separately and uses the
    # mean of the surrounding pixels.
    t0 = time.time()
    bad_pixels_path = os.path.abspath('bad_pixels.txt')
    save_dcraw_bad_pixels(bad_pixels_path, coords)
    rgb = raw.postprocess(bad_pixels_path=bad_pixels_path)
    print('badpixel dcraw repair+postprocessing:', time.time()-t0, 's')
    imageio.imsave('test_hotpixels_repaired_dcraw.png', rgb)
    
Esempio n. 49
0
def load_frame(imgname, pbar):
    global args, master_dark, master_flat

    linearGamma = (1, 1)
    sRGBGamma = (2.4, 12.92)

    calibrate = master_dark is not None and master_flat is not None

    black = 0 if calibrate or args['--raw'] else None
    whitebalance = [0, 0, 0, 0] if calibrate or args['--raw'] else None

    params = rawpy.Params(
        gamma=linearGamma,
        no_auto_scale=False,
        no_auto_bright=True,
        output_bps=16,
        use_camera_wb=False,
        use_auto_wb=False,
        user_wb=whitebalance,
        output_color=rawpy.ColorSpace.sRGB,
        demosaic_algorithm=rawpy.DemosaicAlgorithm.AHD,
        fbdd_noise_reduction=rawpy.FBDDNoiseReductionMode.Off,
        dcb_enhance=False,
        dcb_iterations=0,
        half_size=False,
        median_filter_passes=0,
        user_black=black)

    try:
        pbar.set_postfix(step=f"loading")
        with rawpy.imread(imgname) as raw:
            if calibrate:
                if raw.raw_image.shape != master_dark.shape or raw.raw_image.shape != master_flat.shape:
                    raise RuntimeError(
                        "Flat and dark frames must be the same resolution as each stacked image"
                    )

                pbar.set_postfix(step=f"calibrating")

                dark_removed = (1.0 * raw.raw_image - master_dark)
                dark_removed[dark_removed < 0] = 0
                corrected = dark_removed / master_flat
                corrected[corrected < 0] = 0
                np.copyto(raw.raw_image, corrected.astype(np.uint16))

            if args['--raw']:
                img = np.float32(raw.raw_image.astype(np.uint16) / 65535.0)
            else:
                pbar.set_postfix(step=f"demosaicing")
                img = np.float32(
                    raw.postprocess(params).astype(np.uint16) / 65535.0)

            # crop margin
            if args['--visible']:
                s = raw.sizes
                img = img[s.top_margin * 2:s.raw_height - s.top_margin * 2,
                          s.left_margin * 2:s.raw_width - s.left_margin * 2]

        return img
    except rawpy.LibRawError as inst:
        return None
Esempio n. 50
0
def find_bad_pixels(paths, find_hot=True, find_dead=True, confirm_ratio=0.9):
    """
    Find and return coordinates of hot/dead pixels in the given RAW images.
    
    The probability that a detected bad pixel is really a bad pixel gets
    higher the more input images are given. The images should be taken around
    the same time, that is, each image must contain the same bad pixels.
    Also, there should be movement between the images to avoid the false detection
    of bad pixels in non-moving high-contrast areas.
    
    :param paths: paths to RAW images shot with the same camera
    :type paths: iterable of str
    :param bool find_hot: whether to find hot pixels
    :param bool find_dead: whether to find dead pixels
    :param float confirm_ratio: ratio of how many out of all given images
                          must contain a bad pixel to confirm it as such
    :return: coordinates of confirmed bad pixels
    :rtype: ndarray of shape (n,2) with y,x coordinates relative to visible RAW size
    """
    assert find_hot or find_dead
    coords = []
    width = None
    paths = list(paths)
    for path in paths:
        t0 = time.time()
        # TODO this is a bit slow, try RawSpeed
        raw = rawpy.imread(path)
        if width is None:
            if raw.raw_type != rawpy.RawType.Flat:
                raise NotImplementedError('Only Bayer-type images are currently supported')
            # we need the width later for counting
            width = raw.sizes.width
        print('imread:', time.time()-t0, 's')
            
        thresh = max(np.max(raw.raw_image_visible)//150, 20)
        print('threshold:', thresh)
        
        isCandidate = partial(_is_candidate, find_hot=find_hot, find_dead=find_dead, thresh=thresh)        
        coords.extend(_find_bad_pixel_candidates(raw, isCandidate))
    
    coords = np.vstack(coords)
    
    if len(paths) == 1:
        return coords
    
    # select candidates that appear on most input images
    # count how many times a coordinate appears
    
    # first we convert y,x to array offset such that we have an array of integers
    offset = coords[:,0]*width
    offset += coords[:,1]
    
    # now we count how many times each offset occurs
    t0 = time.time()
    counts = _groupcount(offset)
    print('groupcount:', time.time()-t0, 's')
    
    print('found', len(counts), 'bad pixel candidates, cross-checking images..')
    
    # we select the ones whose count is high
    is_bad = counts[:,1] >= confirm_ratio*len(paths)
        
    # and convert back to y,x
    bad_offsets = counts[is_bad,0]
    bad_coords = np.transpose([bad_offsets // width, bad_offsets % width])
    
    print(len(bad_coords), 'bad pixels remaining after cross-checking images')
    
    return bad_coords
Esempio n. 51
0
def render_raw(image, savename):
    im = rawpy.imread(image)
    rgb = im.postprocess(use_camera_wb=True, half_size=False, no_auto_bright=True, output_bps=8)
    imageio.imwrite(savename, rgb)
Esempio n. 52
0
import urllib.request as urllib
import pandas as pd
import rawpy
import cv2

df = pd.read_csv("RAISE_1k.csv")
urls = df['NEF'].values

num_images = 1
image_count = 1
for url in urls:
    image_str = 'raw/' + str(image_count) + '.nef'
    print(image_str)
    try:
        img = urllib.urlopen(url).read()
    except urllib.error.HTTPError:
        # add the os.environ code here
        img = urllib.urlopen(url).read()

    f = open(image_str, 'wb')
    f.write(img)
    f.close()
    rgb_img = rawpy.imread(image_str).postprocess(use_camera_wb=True,
                                                  output_bps=8)
    rgb_img = cv2.cvtColor(rgb_img, cv2.COLOR_RGB2BGR)
    cv2.imwrite('rgb/' + str(image_count) + '.png', rgb_img)
    image_count += 1
Esempio n. 53
0
def upload_pertama():
    # create image directory if not found
    target = os.path.join(APP_ROOT, 'static/RAW/')
    if not os.path.isdir(target):
        os.mkdir(target)
    targetdua = os.path.join(APP_ROOT, 'static/PNG/')
   # create image directory if not found
    if not os.path.isdir(targetdua):
        os.mkdir(targetdua)

    # retrieve file from html file-picker
    upload = request.files.getlist("file")[0]
    print("File name: {}".format(upload.filename))
    filename = upload.filename
    fileinput = os.path.splitext(filename)[0] + '.png'

    # save file
    destination = "/".join([target, filename])
    print("File saved to to:", destination)
    upload.save(destination)

    # save file png
    #destination = "/".join([targetdua, filename])
    #print("File saved to to:", destination)
    #upload.save(destination)

    input_dir = './static/RAW/'
    def pack_raw(raw):
        # read image black level
        bl = raw.black_level_per_channel[0]

        # pack Bayer image to 4 channels
        im = raw.raw_image_visible.astype(np.float32)
        im = np.maximum(im - bl, 0) / (16383 - bl)  # subtract the black level

        im = np.expand_dims(im, axis=2)
        img_shape = im.shape
        H = img_shape[0]
        W = img_shape[1]

        out = np.concatenate((im[0:H:2, 0:W:2, :],
                          im[0:H:2, 1:W:2, :],
                          im[1:H:2, 1:W:2, :],
                          im[1:H:2, 0:W:2, :]), axis=2)
        return out

    #proses gambar input
    in_files = glob.glob(input_dir + filename)
    in_path = in_files[0]

    raw = rawpy.imread(in_path)
    im = raw.postprocess(use_camera_wb=True, half_size=False, no_auto_bright=True, output_bps=16)
    scale_full = np.expand_dims(np.float32(im / 65535.0), axis=0)
    origin_full = scale_full[0, :, :, :]

    fileinput = os.path.splitext(filename)[0] + '.png'
    namainput=fileinput
    scipy.misc.toimage(origin_full * 255, high=255, low=0, cmin=0, cmax=255).save('./static/PNG/' + fileinput)

    # forward to processing page
    return render_template("processing1.html", image_name=filename, namainput=fileinput)
Esempio n. 54
0
#!/usr/bin/python
# OpenCV tutorial
import numpy as np
import rawpy, imageio
from skimage.exposure import rescale_intensity

raw4 = rawpy.imread('/home/simba/Downloads/Kitchen4.CR2')
raw3 = rawpy.imread('/home/simba/Downloads/Kitchen3.CR2')
raw2 = rawpy.imread('/home/simba/Downloads/Kitchen2.CR2')
raw1 = rawpy.imread('/home/simba/Downloads/Kitchen1.CR2')
raw0 = rawpy.imread('/home/simba/Downloads/Kitchen0.CR2')
rgb = raw.postprocess(no_auto_bright=True, use_auto_wb =False,gamma=None)
imageio.imwrite('/home/simba/Downloads/example.jpg', rgb)
Esempio n. 55
0
import matplotlib.pyplot as plt
from matplotlib import colors
from matplotlib import ticker
from matplotlib.colors import LinearSegmentedColormap


images=[]
for infile in os.listdir("./"):
    print( "file : " + infile)
    if infile[-3:] == "tif" or infile[-3:] == "DNG" :
       # print "is tif or DNG (RAW)"
       outfile = infile[:-3] + "jpg"
       
       print( "new filename : " + outfile)
       dim=(640,360)
       raw = rawpy.imread(infile)
       # Postprocessing, i.e demosaicing here, will always 
#change the original pixel values. Typically what you want
# is to get a linearly postprocessed image so that roughly 
#the number of photons are in linear relation to the pixel values. 
#You can do that with:

       rgb = raw.postprocess() # demosaicing
       #rgb = np.rot90(rgb, k=2) which could change RGGB pattern to BGGR, vice versa)
        
       rgb = cv2.resize(rgb,dim,interpolation = cv2.INTER_AREA)
       rgb = cv2.bitwise_not(~rgb)


       images.append(rgb)
       #Read the images from your directory
Esempio n. 56
0
import numpy
import rawpy
import imageio
import cv2
raw_image = rawpy.imread('IMG.CR2')
img = raw_image.postprocess()
rgb=cv2.cvtColor(img, cv2.COLOR_BGR2RGBA)
print(rgb.shape)
cv2.imwrite('1.jpg',rgb, [cv2.IMWRITE_PNG_COMPRESSION, 0])
Esempio n. 57
0
for test_id in test_ids:
    # test the first image in each sequence
    in_files = glob.glob(input_dir + '%05d_00*.ARW' % test_id)
    for k in range(len(in_files)):
        in_path = in_files[k]
        in_fn = os.path.basename(in_path)
        print(in_fn)
        gt_files = glob.glob(gt_dir + '%05d_00*.ARW' % test_id)
        gt_path = gt_files[0]
        gt_fn = os.path.basename(gt_path)
        in_exposure = float(in_fn[9:-5])
        gt_exposure = float(gt_fn[9:-5])
        ratio = min(gt_exposure / in_exposure, 300)

        raw = rawpy.imread(in_path)
        input_full = np.expand_dims(pack_raw(raw), axis=0) * ratio

        im = raw.postprocess(use_camera_wb=True,
                             half_size=False,
                             no_auto_bright=True,
                             output_bps=16)
        # scale_full = np.expand_dims(np.float32(im/65535.0),axis = 0)*ratio
        scale_full = np.expand_dims(np.float32(im / 65535.0), axis=0)

        gt_raw = rawpy.imread(gt_path)
        im = gt_raw.postprocess(use_camera_wb=True,
                                half_size=False,
                                no_auto_bright=True,
                                output_bps=16)
        gt_full = np.expand_dims(np.float32(im / 65535.0), axis=0)
Esempio n. 58
0
import numpy as np
import rawpy
from skimage import color, exposure
from matplotlib import pyplot as plt

file_path_long = r'E:\LSID\dataset\Sony\long\00100_00_30s.ARW'

gt_image = rawpy.imread(file_path_long)
gt_image = gt_image.postprocess(use_camera_wb=True,
                                half_size=False,
                                no_auto_bright=True,
                                output_bps=16)

# divide by 65535 after converting a uint16 intensity image to double
target = np.float32(
    gt_image / 65535.0)  # RGB image shape (2848, 4256, 3), channel order: RGB

# Convert image to HSV color space
img_hsv = color.rgb2hsv(target)

# Histogram normalization on V channel
img_v = img_hsv[:, :, 2].copy()
img_hsv[:, :, 2] = exposure.equalize_hist(img_hsv[:, :, 2])

# Convert back to RGB space
img_equalized = color.hsv2rgb(img_hsv.astype('float'))

plt.figure()
plt.subplot(2, 2, 1)
plt.title('Unmodified')
plt.imshow(target)
Esempio n. 59
0
path_im = 'E:/UIUC/Data_11_07_18/iOS'
im_name = 'RAW_2018_11_08_15_09_06_866_noflash.dng'
with open('E:/UIUC/Data_11_07_18/IOSNoFlashLabel.json', 'r') as fp:
    labels = json.load(fp)

test_key = [
    'HYDRANGEA FLORET', 'BLUE SHAMROCK', 'DARK GREEN VELVET', 'DOVER GRAY',
    'OREGANO', 'GINGER', 'FERRIS WHEEL'
]

fig, axs = plt.subplots(len(test_key), 5)
cnt = 0
for key in test_key:
    im_name = labels[key][0]['im_name']
    with rp.imread(path_im + '/' + im_name) as raw:
        patch_roi = labels[key][0]['patch_roi']

        #Raw values
        raw_values = np.array(raw.raw_image)
        h, w = np.shape(raw_values)
        rgb = np.zeros([int(h / 2), int(w / 2), 3], dtype=np.float)
        cwb = raw.camera_whitebalance
        print(cwb)
        for i in range(0, np.shape(raw_values)[0], 2):
            for j in range(0, np.shape(raw_values)[1], 2):
                #print(raw.raw_color(i, j), ' ', raw.raw_color(i, j + 1), ' ', raw.raw_color(i + 1, j))
                tmp = [
                    raw_values[i][j], raw_values[i][j + 1],
                    raw_values[i + 1][j + 1]
                ]
Esempio n. 60
0
def testThumbnail():
    raw = rawpy.imread(rawTestPath)
    thumb = raw.get_thumbnail()
    assert_equal(thumb["type"], 'jpeg')
    with open('thumb_test.jpg', 'wb') as f:
        f.write(thumb["data"])