Using LibRaw to correctly decode CR2 image? - image

My eventual goal is to decode CR2 images from multiple cameras for display in a desktop gui.
Using the LibRaw image decoding library, I've used the sample project to attempt to decode a CR2 image into a .TIFF file.
The original file as a jpg thumbnail is as follows:
Image 1
And the original CR2 after decoding and saving into a .TIFF is as follows:
Image 2
As you can see, the outcome is slightly brighter and yellowish.
The sample project was contains the following parameters for decoding images:
"-c float-num Set adjust maximum threshold (default 0.75)\n"
"-v Verbose: print progress messages (repeated -v will add verbosity)\n"
"-w Use camera white balance, if possible\n"
"-a Average the whole image for white balance\n"
"-A <x y w h> Average a grey box for white balance\n"
"-r <r g b g> Set custom white balance\n"
"+M/-M Use/don't use an embedded color matrix\n"
"-C <r b> Correct chromatic aberration\n"
"-P <file> Fix the dead pixels listed in this file\n"
"-K <file> Subtract dark frame (16-bit raw PGM)\n"
"-k <num> Set the darkness level\n"
"-S <num> Set the saturation level\n"
"-n <num> Set threshold for wavelet denoising\n"
"-H [0-9] Highlight mode (0=clip, 1=unclip, 2=blend, 3+=rebuild)\n"
"-t [0-7] Flip image (0=none, 3=180, 5=90CCW, 6=90CW)\n"
"-o [0-5] Output colorspace (raw,sRGB,Adobe,Wide,ProPhoto,XYZ)\n"
#ifndef NO_LCMS
"-o file Output ICC profile\n"
"-p file Camera input profile (use \'embed\' for embedded profile)\n"
#endif
"-j Don't stretch or rotate raw pixels\n"
"-W Don't automatically brighten the image\n"
"-b <num> Adjust brightness (default = 1.0)\n"
"-q N Set the interpolation quality:\n"
" 0 - linear, 1 - VNG, 2 - PPG, 3 - AHD, 4 - DCB\n"
#ifdef LIBRAW_DEMOSAIC_PACK_GPL2
" 5 - modified AHD,6 - AFD (5pass), 7 - VCD, 8 - VCD+AHD, 9 - LMMSE\n"
#endif
#ifdef LIBRAW_DEMOSAIC_PACK_GPL3
" 10-AMaZE\n"
#endif
"-h Half-size color image (twice as fast as \"-q 0\")\n"
"-f Interpolate RGGB as four colors\n"
"-m <num> Apply a 3x3 median filter to R-G and B-G\n"
"-s [0..N-1] Select one raw image from input file\n"
"-4 Linear 16-bit, same as \"-6 -W -g 1 1\n"
"-6 Write 16-bit linear instead of 8-bit with gamma\n"
"-g pow ts Set gamma curve to gamma pow and toe slope ts (default = 2.222 4.5)\n"
"-T Write TIFF instead of PPM\n"
"-G Use green_matching() filter\n"
"-B <x y w h> use cropbox\n"
"-F Use FILE I/O instead of streambuf API\n"
"-timing Detailed timing report\n"
"-fbdd N 0 - disable FBDD noise reduction (default), 1 - light FBDD, 2 - full\n"
"-dcbi N Number of extra DCD iterations (default - 0)\n"
"-dcbe DCB color enhance\n"
#ifdef LIBRAW_DEMOSAIC_PACK_GPL2
"-eeci EECI refine for mixed VCD/AHD (q=8)\n"
"-esmed N Number of edge-sensitive median filter passes (only if q=8)\n"
#endif
#ifdef LIBRAW_DEMOSAIC_PACK_GPL3
//"-amazeca Use AMaZE chromatic aberrations refine (only if q=10)\n"
"-acae <r b>Use chromatic aberrations correction\n" //modifJD
"-aline <l> reduction of line noise\n"
"-aclean <l c> clean CFA\n"
"-agreen <g> equilibrate green\n"
#endif
"-aexpo <e p> exposure correction\n"
// WF
"-dbnd <r g b g> debanding\n"
#ifndef WIN32
"-mmap Use mmap()-ed buffer instead of plain FILE I/O\n"
#endif
"-mem Use memory buffer instead of FILE I/O\n"
"-disars Do not use RawSpeed library\n"
"-disinterp Do not run interpolation step\n"
"-dsrawrgb1 Disable YCbCr to RGB conversion for sRAW (Cb/Cr interpolation enabled)\n"
"-dsrawrgb2 Disable YCbCr to RGB conversion for sRAW (Cb/Cr interpolation disabled)\n"
"-disadcf Do not use dcraw Foveon code either if compiled with demosaic-pack-GPL2\n"
I've tried various options to replicate the image in the thumbnail, such as white balancing (-w), interpolation quality (-q N), and embedded color matrix (+M). When I used white balancing, it removed the yellowish tint but produced a bright image. I then went on to disable automatic brightening (-W) and it produced a non-yellow image but much darker than the thumbnail.
What image decoding parameters will help me to decode the CR2 into the a high-quality image that looks like the thumbnail (in terms of color, brightness, etc.)?

Problem solved.
While I don't have the specifics just yet, my senior said windows didn't support the original formatting option, and he also moved memory row-by-by.

Related

Using "gdal_translate" from .TIF to .JPG how to set background to white?

I tried using the following line:
gdal_translate -of jpeg -a_nodata 0 -b 1 -b 2 -b 3 c:\myfolder\mytif.tif c:\myfolder\myNewtif.jpg
This produces the image with wanted specs, but turns the background to black (transparency?), eventhough the original has white. Can I accomplish a white background with just gdal_translate?
File dump of a file with mask:
https://filebin.net/f15v63to2x3cc4z0
File dump of a file with no a mask: https://filebin.net/kc940hqotcoeny0w
gdalinfo output of a Tif that produces white background as expected:
Driver: GTiff/GeoTIFF
Files: test.tif
Size is 4799, 3196
Metadata:
TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
TIFFTAG_XRESOLUTION=300
TIFFTAG_YRESOLUTION=300
Image Structure Metadata:
COMPRESSION=LZW
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 3196.0)
Upper Right ( 4799.0, 0.0)
Lower Right ( 4799.0, 3196.0)
Center ( 2399.5, 1598.0)
Band 1 Block=4799x1 Type=Byte, ColorInterp=Red
Band 2 Block=4799x1 Type=Byte, ColorInterp=Green
Band 3 Block=4799x1 Type=Byte, ColorInterp=Blue
Tif that produces black background:
Warning 1: TIFFFetchNormalTag:Incompatible type for "RichTIFFIPTC"; tag ignored
Driver: GTiff/GeoTIFF
Files: 100011_1.tif
Size is 1640, 2401
Metadata:
TIFFTAG_DATETIME=2020:01:13 12:29:55
TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
TIFFTAG_SOFTWARE=Adobe Photoshop 21.0 (Windows)
TIFFTAG_XRESOLUTION=300
TIFFTAG_YRESOLUTION=300
Image Structure Metadata:
COMPRESSION=LZW
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 2401.0)
Upper Right ( 1640.0, 0.0)
Lower Right ( 1640.0, 2401.0)
Center ( 820.0, 1200.5)
Band 1 Block=1640x39 Type=Byte, ColorInterp=Red
Mask Flags: PER_DATASET ALPHA
Band 2 Block=1640x39 Type=Byte, ColorInterp=Green
Mask Flags: PER_DATASET ALPHA
Band 3 Block=1640x39 Type=Byte, ColorInterp=Blue
Mask Flags: PER_DATASET ALPHA
Band 4 Block=1640x39 Type=Byte, ColorInterp=Alpha
Also of the following images that have black background after translate, gdal produces this warning "Warning 1: TIFFFetchNormalTag: Incompatible type for "RichTIFFIPTC"; tag ignored"
The file that you have shared on filebin contains an "alpha" mask, as you can see in the output of gdalinfo. The mask of this file says that the background is masked, while the rest of the image is not.
If you display the tiff with the default Ubuntu viewer, for example, you can see that the background pixels are masked out (they appear as a checker board)
If you inspect the raster data, you also see that the underlying pixels from the background are black, not white. That is why gdal_translate generates a jpeg with black pixels in the background, it is because they really are black (but masked out) in the original tiff file.
If you absolutely want the background to be white, you can do so with a few lines of Python using the rasterio library for example, by explicitly setting the masked pixels to white:
import rasterio
with rasterio.open("101679_1.tif") as src:
arr = src.read(masked=True)
# Convert all masked values to white
arr[arr.mask] = 255
# Write to jpeg file
profile = src.profile
profile["count"] = 3
profile["driver"] = "jpeg"
with rasterio.open("test.jpeg", "w", **profile) as dst:
dst.write(arr[:3])
This should give the following jpeg file:
The code snippet I have included above will also work on TIF files which already have a white background, because the arr[arr.mask] = 255 line will not do anything if the file contains no mask.
To process a directory full of .tif files, you can do:
from glob import glob
import rasterio
for tif in glob("*.tif"):
with rasterio.open(tif) as src:
arr = src.read(masked=True)
# Convert all masked values to white
arr[arr.mask] = 255
# Write to jpeg file
profile = src.profile
profile["count"] = 3
profile["driver"] = "jpeg"
with rasterio.open(tif.replace(".tif", ".jpeg"), "w", **profile) as dst:
dst.write(arr[:3])

gnuplot: how to plot one 2D array element per pixel with no margins

I am trying to use gnuplot 5.0 to plot a 2D array of data with no margins or borders or axes... just a 2D image (.png or .jpg) representing some data. I would like to have each array element to correspond to exactly one pixel in the image with no scaling / interpolation etc and no extra white pixels at the edges.
So far, when I try to set the margins to 0 and even using the pixels flag, I am still left with a row of white pixels on the right and top borders of the image.
How can I get just an image file with pixel-by-pixel representation of a data array and nothing extra?
gnuplot script:
#!/usr/bin/gnuplot --persist
set terminal png size 400, 200
set size ratio -1
set lmargin at screen 0
set rmargin at screen 1
set tmargin at screen 0
set bmargin at screen 1
unset colorbox
unset tics
unset xtics
unset ytics
unset border
unset key
set output "pic.png"
plot "T.dat" binary array=400x200 format="%f" with image pixels notitle
Example data from Fortran 90:
program main
implicit none
integer, parameter :: nx = 400
integer, parameter :: ny = 200
real, dimension (:,:), allocatable :: T
allocate (T(nx,ny))
T(:,:)=0.500
T(2,2)=5.
T(nx-1,ny-1)=5.
T(2,ny-1)=5.
T(nx-1,2)=5.
open(3, file="T.dat", access="stream")
write(3) T(:,:)
close(3)
end program main
Some gnuplot terminals implement "with image" by creating a separate png file containing the image and then linking to it inside the resulting plot. Using that separate png image file directly will avoid any issues of page layout, margins, etc. Here I use the canvas terminal. The plot itself is thrown away; all we keep is the png file created with the desired content.
gnuplot> set term canvas name 'myplot'
Terminal type is now 'canvas'
Options are ' rounded size 600,400 enhanced fsize 10 lw 1 fontscale 1 standalone'
gnuplot> set output '/dev/null'
gnuplot> plot "T.dat" binary array=400x200 format="%f" with image
linking image 1 to external file myplot_image_01.png
gnuplot> quit
$identify myplot_image_01.png
myplot_image_01.png PNG 400x200 400x200+0+0 8-bit sRGB 348B 0.000u 0:00.000
Don't use gnuplot.
Instead, write a script that reads your data and converts it into one of the Portable Anymap formats. Here's an example in Python:
#!/usr/bin/env python3
import math
import struct
width = 400
height = 200
levels = 255
raw_datum_fmt = '=d' # native, binary double-precision float
raw_datum_size = struct.calcsize(raw_datum_fmt)
with open('T.dat', 'rb') as f:
print("P2")
print("{} {}".format(width, height))
print("{}".format(levels))
raw_data = f.read(width * height * raw_datum_size)
for y in range(height):
for x in range(width):
raw_datum, = struct.unpack_from(raw_datum_fmt, raw_data, (y * width + x) * raw_datum_size)
datum = math.floor(raw_datum * levels) # assume a number in the range [0, 1]
print("{:>3} ".format(datum), end='')
print()
If you can modify the program which generates the data file, you can even skip the above step and instead generate the data directly in a PNM format.
Either way, you can then use ImageMagick to convert the image to a format of your choice:
./convert.py | convert - pic.png
This should be an easy task, however, apparently it's not.
The following might be a (cumbersome) solution because all other attempts failed. My suspicion is that some graphics library has an issue which you probably cannot solve as a gnuplot user.
You mentioned that ASCII matrix data is also ok. The "trick" here is to plot data with lines where the data is "interrupted" by empty lines, basically drawing single points. Check this in case you need to get your datafile 1:1 into a datablock.
However, if it is not already strange enough, it seems to work for png and gif terminal but not for pngcairo or wxt.
I guess the workaround is probably slow and inefficient but at least it creates the desired output. I'm not sure if there is a limit on size. Tested with 100x100 pixels with Win7, gnuplot 5.2.6. Comments and improvements are welcome.
Code:
### pixel image from matrix data without strange white border
reset session
SizeX = 100
SizeY = 100
set terminal png size SizeX,SizeY
set output "tbPixelImage.png"
# generate some random matrix data
set print $Data2
do for [y=1:SizeY] {
Line = ''
do for [x=1:SizeX] {
Line = Line.sprintf(" %9d",int(rand(0)*0x01000000)) # random color
}
print Line
}
set print
# print $Data2
# convert matrix data into x y z data with empty lines inbetween
set print $Data3
do for [y=1:SizeY] {
do for [x=1:SizeX] {
print sprintf("%g %g %s", x, y, word($Data2[y],x))
print ""
}
}
set print
# print $Data3
set margins 0,0,0,0
unset colorbox
unset border
unset key
unset tics
set xrange[1:SizeX]
set yrange[1:SizeY]
plot $Data3 u 1:2:3 w l lw 1 lc rgb var notitle
set output
### end of code
Result: (100x100 pixels)
(enlarged with black background):
Image with 400x200 pixels (takes about 22 sec on my 8 year old laptop).
What I ended up actually using to get what I needed even though the question / bounty asks for a gnuplot solution:
matplotlib has a function matplotlib.pyplot.imsave which does what I was looking for... i.e. plotting 'just data pixels' and no extras like borders, margins, axes, etc. Originally I only knew about matplotlib.pyplot.imshow and had to pull a lot of tricks to eliminate all the extras from the image file and prevent any interpolation/smoothing etc (and therefore turned to gnuplot at a certain point). With imsave it's fairly easy, so I'm back to using matplotlib for an easy yet still flexible (in terms of colormap, scaling, etc) solution for 'pixel exact' plots. Here's an example:
#!/usr/bin/env python3
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
nx = 400
ny = 200
data = np.fromfile('T.dat', dtype=np.float32, count=nx*ny)
data = data.reshape((nx,ny), order='F')
matplotlib.image.imsave('T.png', np.transpose(data), origin='lower', format='png')
OK, here is another possible solution (I separated it from my first cumbersome approach). It creates the plot immediately, less than a second. No renaming necessary or creation of a useless file.
I guess key is to use term png and ps 0.1.
I don't have a proof but I think ps 1 would be ca. 6 pixels large and would create some overlap and/or white pixels at the corner. Again, for whatever reason it seems to work with term png but not with term pngcairo.
What I tested (Win7, gnuplot 5.2.6) is a binary file having the pattern 00 00 FF repeated all over (I can't display null bytes here). Since gnuplot apparently reads 4 bytes per array item (format="%d"), this leads to an alternating RGB pattern if I am plotting with lc rgb var.
In the same way (hopefully) we can figure out how to read format="%f" and use it together with a color palette. I guess that's what you are looking for, right?
Further test results, comments, improvements and explanations are welcome.
Code:
### pixel image from matrix data without strange white border
reset session
SizeX = 400
SizeY = 200
set terminal png size SizeX,SizeY
set output "tbPixelImage.png"
set margins 0,0,0,0
unset colorbox
unset border
unset key
unset tics
set xrange[0:SizeX-1]
set yrange[0:SizeY-1]
plot "tbBinary.dat" binary array=(SizeX,SizeY) format="%d" w p pt 5 ps 0.1 lc rgb var
### end of code
Result:

Auto-brightening images

I found this code for auto-brightening images to an optimum level.
% AUTOBRIGHTNESS
% -->Automatically adjusts brightness of images to optimum level.
% e.g. autobrightness('Sunset.jpg','Output.jpg')
function autobrightness(input_img,output_img)
my_limit = 0.5;
input_image=imread(input_img);
if size(input_image,3)==3
a=rgb2ntsc(input_image);
else
a=double(input_image)./255;
end
mean_adjustment = my_limit-mean(mean(a(:,:,1)));
a(:,:,1) = a(:,:,1) + mean_adjustment*(1-a(:,:,1));
if size(input_image,3)==3
a=ntsc2rgb(a);
end
imwrite(uint8(a.*255),output_img);
I want to ask, why the value of my_limit is 0.5?
How we determine that value?
Why use the 'ntsc' colorspace instead of another colorspace like hsv, lab or yCbCr?
I want to ask, why the value of my_limit is 0.5? How we determine that
value?
The color space NTSC ranges from 0 to 1 for each of its channel. So essentially 0.5 is the center. This is equivalent of choosing 127 for RGB space
Why use the 'ntsc' colorspace instead of another colorspace like hsv,
lab or yCbCr?
I believe ntsc provides 100% coverage of the color space and so the author of the code choose it. However most modern systems wont display in this color space and hence we use standard RGB for display. I used this website to come to this conclusion NTSC color space
Also, as pointed by Cris in this wikipedia page. NTSC stores Luminance and Chrominance and the author of the code is adjusting the Lumiance(brightness). I am including a modified script I used to come to these conclusions
input_img='lena_std.tif'
output_img='lena2.tif'
my_limit = 0.5;
input_image=imread(input_img);
if size(input_image,3)==3
a=rgb2ntsc(input_image);
k=rgb2ntsc(input_image);
else
a=double(input_image)./255;
end
mean_adjustment = my_limit-mean(mean(a(:,:,1)));
a(:,:,1) = a(:,:,1) + mean_adjustment*(1-a(:,:,1));
if size(input_image,3)==3
a=ntsc2rgb(a);
end
imwrite(uint8(a.*255),output_img);
output=uint8(a.*255);
imwrite(uint8(k.*255),'test.tif');
ntscoutput=uint8(k.*255);

Detect if video is black and white in bash

I have a folder with hundreds of films, and I'd like to separate the color ones from black and white. Is there a bash command to do this for general video files?
I already extract a frame:
ffmpeg -ss 00:15:00 -i vid.mp4 -t 1 -r 1/1 image.bmp
How can I check if the image has a color component?
I never found out why video processing questions are answered on SO but as they typically are not closed, i'll do my best... As this is a developer board, i cannot recommend any ready commandline tool to use for your bash command, nor do i know any. Also i cannot give a bash only solution because i do not know how to process binary data in bash.
To get out if an image is grey or not, you'll need to check each pixel for it's color and "guess" if it is kind of grey. As others say in the comments, you will need to analyze multiple pictures of each video to get a more accurete result. For this you could possibly use the scene change detection filter of ffmpeg but thats another topic.
I'd start by resizing the image to save processing power, e.g. to 4x4 pixels. Also make sure you guarantee the colorspace or better pix_format is known so you know what a pixel looks like.
Using this ffmpeg line, you would extract one frame in 4x4 pixels to raw RGB24:
ffmpeg -i D:\smpte.mxf -pix_fmt rgb24 -t 1 -r 1/1 -vf scale=4:4 -f rawvideo d:\out_color.raw
The resulting file contains exactly 48 bytes, 16 pixels each 3 bytes, representing R,G,B color. To check if all pixels are gray, you need to compare the difference between R G and B. Typically R G and B have the same value when they are gray, but in reality you will need to allow some more fuzzy matching, e.g. if all values are the same +-10.
Some example perl code:
use strict;
use warnings;
my $fuzz = 10;
my $inputfile ="d:\\out_grey.raw";
die "input file is not an RGB24 raw picture." if ( (-s $inputfile) %3 != 0);
open (my $fh,$inputfile);
binmode $fh;
my $colordetected = 0;
for (my $i=0;$i< -s $inputfile;$i+=3){
my ($R,$G,$B);
read ($fh,$R,1);
$R = ord($R);
read ($fh,$B,1);
$B = ord($B);
read ($fh,$G,1);
$G = ord($G);
if ( $R >= $B-$fuzz and $R <= $B+$fuzz and $B >= $G-$fuzz and $B <= $G+$fuzz ) {
#this pixel seems gray
}else{
$colordetected ++,
}
}
if ($colordetected != 0){
print "There seem to be colors in this image"
}

convert hex(png) file to rgb values

I converted a PNG image to hex and am wondering if it is possible to decompress the hex into this type of format for each pixel of the image:
Opacity(0-255)-Red(0-255)-Green(0-255)-Blue(0-255)
I'm using a site/program that has heavy restrictions on images you can upload(quality, size, amount, etc,) but I can create images pixel by pixel. I was hoping to decompress the hex that I converted from the original PNG file to the format above so that I can create a simple function to build it on the screen. Come to think of it, is there a way to pull the RGB and transparency from the hex PNG file without any need for reformatting?
It is very easy you have to cut your hexvalue,
and applicate an HEX to DECIMAL CONVERSION.
Ex : FFAACC00
FF = R = 255
AA = G = 170
CC = B = 204
00 = Transparency Full (FF = Transparency OFF)
If you need a program to do that you can use PNG2HEX.exe :
Download
(just drop your PNG File on it and it will create 2 files :
1- fileHex.txt with the value of each pixel in Hexa
2- FileRGB.txt with the value of each pixel in R G B
You can then modify values in these files and rebuild the png with the modification using
HEX2PNG.exe

Resources