Commit cc08ca2d authored by Daniel Bourgault's avatar Daniel Bourgault

Addition of the function g_jpg_jpw.m created by Tom Shand to make .jpg and worldfile.

parent 56902974
......@@ -98,11 +98,12 @@ precision = 'double';
%% GROUND CONTROL POINTS (GCPs).
% The GCP data must come right after the 'gcpData = true' instruction
% Column 1: horizontal image index of GCPs
% Column 2: vertical image index of GCPs
% Column 3: longitude (Geodetic) or x-position (Cartesian) of GCPs
% Column 4: latitude (Geodetic) or y-position (Cartesian) of GCPs
% Column 5 (optional): elevation (in m) of GCPs.
% Column 1: horizontal image index of GCPs
% Column 2: vertical image index of GCPs
% Column 3: longitude (Geodetic) or x-position (Cartesian) of GCPs
% Column 4: latitude (Geodetic) or y-position (Cartesian) of GCPs
% Column 5: elevation (in m) of GCPs (optional)
%
gcpData = true;
360 829 -70.561367 47.303783 0
54 719 -70.54500 47.335 0
......
......@@ -103,11 +103,11 @@ precision = 'double';
%% GROUND CONTROL POINTS (GCPs).
% The GCP data must come right after the 'gcpData = true' instruction
% Column 1: horizontal image index of GCPs
% Column 2: vertical image index of GCPs
% Column 3: longitude (Geodetic) or x-position (Cartesian) of GCPs
% Column 4: latitude (Geodetic) or y-position (Cartesian) of GCPs
% Column 5 (optional): elevation (in m) of GCPs.
% Column 1: horizontal image index of GCPs
% Column 2: vertical image index of GCPs
% Column 3: longitude (Geodetic) or x-position (Cartesian) of GCPs
% Column 4: latitude (Geodetic) or y-position (Cartesian) of GCPs
% Column 5: elevation (in m) of GCPs (optional)
%
gcpData = true;
1818 490 1661903.38 5316012.52 6.28
......
% I/O information
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% The image found in this folder ('IMG_8368.jpg') as well as the data found below in
% this parameter file are only to provide an example of the g_rect package applied
% to a laboratory situation. These data are not associated with any publication.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% INPUT/OUTPUT INFORMATION
% imgFname: This is the only image that will actually be treated
%
% firstImgFname: This is really just a comment not actually used by the algorithm
% that indicates the name of the first image of a sequence to which
% the georectification of image 'imgFname' could also be applied to.
%
% lastImgFname: Same as 'firstImgFname'but for the last sequential image.
%
% outputFname: This is the name of the output fil that will contain, among other
% variables, the matrices LON and LAT that will give the ground
% coordinate of every pixel of the image that are below the horizon.
%
imgFname = 'IMG_8368.jpg';
firstImgFname = 'IMG_8368.jpg';
lastImgFname = 'IMG_8368.jpg';
outputFname = 'IMG_8368_grect.mat';
% Field or lab case situation.
% Set field = true for field situation and field = false for lab situation.
frameRef = 'Cartesian';
% Camera position
% lat/lon for field situation
% meter for lab situation
%% FRAME OF REFERENCE
% The frame of reference could be either 'Geodetic' or 'Cartesian'.
% If 'Geodetic' is used, longitudes and latitudes are expected for positions.
% If 'Cartesian' is used, x-y positions are expected (in m)
%
frameRef = 'Cartesian';
%% CAMERA POSITION
% Set the longitude and latitude for ‘Geodetic’ frame of reference
% Set the x-y coordinate for ‘Cartesian’ frame of reference.
%
LON0 = 1.01;
LAT0 = 2.36;
% Offset from center of the principal point (generally zero)
%% OFFSET
% Offset from center of the principal point (generally zero)
%
ic = 0;
jc = 0;
% Parameters
%% CAMERA PARAMETERS
% These are either the initial guesses for the uncertain parameters, i.e. those
% with an uncertainty > 0 (see next section) or the exact values for the parameters
% with an uncertainty set to 0 below.
%
% hfov: Field of view of the camera (degree)
%
% lambda: Dip angle (degree) below horizontal (e.g. straight down = 90, horizontal = 0)
%
% phi: Tilt angle (degree), generally close to 0.
%
% theta: View angle (degree) clockwise from North (e.g. straight East = 90)
%
% H: Camera altitude relative to the local water level (m)
%
hfov = 62.00; % Field of view of the camera
lambda = 53.0; % Dip angle below horizontal (e.g. straight down = 90, horizontal = 0)
phi = 1.0; % Tilt angle (generally close to 0).
H = 1.755; % Camera altitude (m)
theta = 180.0; % View angle clockwise from North (e.g. straight East = 90)
% Uncertainty in parameters. Set the uncertainty to 0.0 for fixed parameters.
%% UNCERTAINTIES
% Set here the uncertainties of the associated camera parameters.
% Set the uncertainty to 0 for fixed parameters.
%
dhfov = 5.0;
dlambda = 10.0;
dphi = 5.0;
dH = 0.5;
dtheta = 20.0;
% Order of the polynomial correction (0, 1 or 2)
%% POLYNOMIAL CORRECTION
% After the geometrical correction, a polynomial correction of degree 1 or 2
% could be applied. This could correct for some unknown distortions that cannot be
% corrected on geometrical grounds. Play carefully with this option as it is a purely
% mathematical fit which may lead to unphysical corrections or may hide other
% prior problems with the actual geometrical correction. Always first set this option
% to '0' in which case there will be no polynomial correction applied. In principle,
% the geometrical fit without this option should already be pretty good. You could then
% fine tune the image with this option. Be extra careful when using the second order
% polynomial fit, especially outside the region of the GCPs as the image could there
% be completely distorted.
%
polyOrder = 2;
% To save memory calculation can be done in single precision.
% For higher precision set the variable 'precision' to 'double';
%% PRECISION
% To save memory, calculations can be done in single precision.
% For higher precision set the variable 'precision' to 'double';
%
precision = 'double';
% Ground Control Points (GCP).
%% GROUND CONTROL POINTS (GCPs).
% The GCP data must come right after the 'gcpData = true' instruction
% Column 1: horizontal image index of GCPs
% Column 2: vertical image index of GCPs
% Column 3: longitude (Geodetic) or x-position (Cartesian) of GCPs
% Column 4: latitude (Geodetic) or y-position (Cartesian) of GCPs
% Column 5: elevation (in m) of GCPs (optional)
%
gcpData = true;
2999 226 0.500 0.00 0
1694 220 1.500 0.00 0
......@@ -56,12 +115,4 @@ gcpData = true;
4131 1580 0.000 1.23 0
521 2548 1.910 1.82 0
248 2328 2.060 1.71 0
14 1868 2.290 1.46 0
14 1868 2.290 1.46 0
\ No newline at end of file
function [] = g_jpg_jpw(LON,LAT,dx,lon_min,lon_max,lat_min,lat_max,grey)
%
% This function creates geotiff images given the longitudes (LON)
% and latitudes (LAT) of every pixels of the associated jpeg images found
% in the current folder. The matrices LON and LAT typically come from the
% g_rect package.
%
% The geotiff images are constructed by interpolating on a regular grid of
% size dx (in m) the irregularly-spaced pixel intensities defined by the
% LON-LAT pair.
%
% INPUT PARAMETERS:
%
% LON: A matrix of size identical to the associated images in the current
% folder that gives the longitude of every pixel of those images.
% This matrix would typically come from the g_rect package.
%
% LAT: Same as LON but for the latitude.
%
% dx: The grid size (in m) of the regularly-spaced interpolated
% geotif image
%
% lon_min: The longitude of the southwest corner of the desired
% geotif image.
%
% lon_max: The longitude of the northeastcorner of the desired
% geotif image.
%
% lat_min: The latitude of the southwest corner of the desired
% geotif image.
%
% lat_max: The latitude of the northeastcorner of the desired
% geotif image.
%
% grey: greyscale (1) or rgb (2)
%
% OUTPUT:
%
% This function writes geotiff images that have the same name as the
% jpeg images but with the extension .tif
%
% LAST REVISION: 5 May 2020
%
% AUTHOR:
% - Daniel Bourgault (daniel_bourgault@uqar.ca)
%
% Hacked around by Tom Shand 8/5/20 to output as JPG + worldfile and grey
% or RGB options.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Construct the vectors 'lon' and 'lat' that define the interpolating
% grid of constant resolution dx (in m)
%TDS 8/5/20 swapped this
% lat0 = (lat_min + lat_max)/2;
% dlat = dx/(1852*60);
% dlon = dlat/cosd(lat0);
% lon = [lon_min:dlon:lon_max];
% lat = [lat_min:dlat:lat_max];
%For this
lon = [lon_min:dx:lon_max];
lat = [lat_min:dx:lat_max];
% Find the indices of only the finite values in the matrix LON
% so that the interpolation is only done on finite values and does
% not spend time interpolating NaNs.
ifinite = find(isfinite(LON) == 1);
% Find all .jpg images in the current folder
img_fname = dir('*.jpg');
N_img = length(img_fname);
% Loop over all images
for i = 1:N_img
image = imread(img_fname(i).name);
if grey == 1
% Convert to gray scale
img = rgb2gray(image);
img = double(img);
img = img';
% Interpolate on the regular grid defined above by the vector
% lon and lat
img_interp = griddata(LON(ifinite),LAT(ifinite),img(ifinite),lon,lat');
% Convert real numbers to unsigned integers
img_interp = uint8(img_interp);
else
imgrgb=double(image);
imgr=imgrgb(:,:,1)';
imgg=imgrgb(:,:,2)';
imgb=imgrgb(:,:,3)';
img_interpr = griddata(LON(ifinite),LAT(ifinite),imgr(ifinite),lon,lat');
img_interpg = griddata(LON(ifinite),LAT(ifinite),imgg(ifinite),lon,lat');
img_interpb = griddata(LON(ifinite),LAT(ifinite),imgb(ifinite),lon,lat');
img2=zeros(size(img_interpb));
img2(:,:,1)=img_interpr;
img2(:,:,2)=img_interpg;
img2(:,:,3)=img_interpb;
img_interp = uint8(img2);
end
% Write the jpg + worldfile
jpg_img_fname = [img_fname(i).name(1:end-4),'rect.jpg']
imwrite(flipud(img_interp),jpg_img_fname,'Quality',100)
% Setting worldfile values
xpixel=dx;
yrot=0;
xrot=0;
ypixel=-dx;
xcoord=lon_min;
ycoord=lat_max;
% Export csv with gps coordinates of the line.
% worldfilenme_name = strcat(photo,'_rectified','.jpw');
worldfilenme_name = [img_fname(i).name(1:end-4),'rect.jpw']
wldmatrix = [xpixel; yrot; xrot; ypixel; xcoord; ycoord];
dlmwrite(worldfilenme_name,wldmatrix,'precision', 10)
% Write the geotiff file
% geotif_img_fname = [img_fname(i).name(1:end-3),'tif']
% bbox = [lon_min, lat_min; lon_max, lat_max];
% geotiffwrite(geotif_img_fname, bbox, flipud(img_interp),8);
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment