function plotresiduals(ipr, tracks, c_range)
	% create a nice set of figures with residual images
	
	n_cameras		= ipr.n_cameras;
	camera			= ipr.setup.camera;

	if nargin < 3
		c_range		= 1 : n_cameras;
	end
	
	%% one figure for each camera
	for ic = c_range
		%% image space coordinates
		i_axis		= camera(ic).i_axis;
		j_axis		= camera(ic).j_axis;
		i_lim		= sort(i_axis([1 end]));
		j_lim		= sort(j_axis([1 end]));
		%% create the figure
		figure(100+ic);
		clf;
		layout		= tiledlayout(1,2);
		% measured image
		nexttile;
		img_rgb		= demosaic(uint8(ipr.img_proj{ic}), 'bggr');
		image(i_axis, j_axis, permute(img_rgb,[2 1 3]));
		hold on;
		axis equal tight xy;
		ax			= gca;
		xlim(sort(i_axis([1 end])));
		ylim(sort(j_axis([1 end])));
		title('reprojection', 'interpreter', 'latex', 'fontsize', 20);
		xlabel('$i$ [px]', 'interpreter', 'latex', 'fontsize', 20);
		ylabel('$j$ [px]', 'interpreter', 'latex', 'fontsize', 20);
		% projected image
		nexttile;
		img_rgb		= demosaic(uint8(abs(ipr.img_res{ic})), 'bggr');
		
		h_im = image(i_axis, j_axis, permute(img_rgb,[2 1 3]));
		h_im.AlphaData = 0.5 + 0.5*(ipr.img_res{ic}>-5).';
		set(gca,'color','r');
		hold on;
		axis equal tight xy;
		xlim(sort(i_axis([1 end])));
		ylim(sort(j_axis([1 end])));
		ax(2)		= gca;
		title('|error|', 'interpreter', 'latex', 'fontsize', 20);
		xlabel('$i$ [px]', 'interpreter', 'latex', 'fontsize', 20);
		ylabel('$j$ [px]', 'interpreter', 'latex', 'fontsize', 20);
		linkaxes(ax, 'xy');
		
		%% optionally plot tracks on residual images
		if nargin >= 2
			%%
			im_i = arrayfun(@(t) t.im_pred(1,:,ic), tracks, 'UniformOutput', false);
			im_j = arrayfun(@(t) t.im_pred(2,:,ic), tracks, 'UniformOutput', false);
			im_i = cell2mat(im_i(:)).';
			im_j = cell2mat(im_j(:)).';
			isbounded	= @(x,lim) all(x > lim(1), 1) & all(x < lim(2), 1); 
			ok			= isbounded(im_i,i_lim) & isbounded(im_j,j_lim);
			im_i		= im_i(:,ok);
			im_j		= im_j(:,ok);
			for iax=1:2
				plot(ax(iax), im_i(1,:), im_j(1,:), 'ro');
				plot(ax(iax), im_i(2,:), im_j(2,:), 'go');
				plot(ax(iax), im_i(3,:), im_j(3,:), 'bo');
			end
		end
	end
end
		
