google / ffcc

Fast Fourier Color Constancy: an auto white balance solution with machine learning in Fourier space
https://jonbarron.info/
Apache License 2.0
203 stars 72 forks source link

How to understand the meaning of the conv operations in the function "RenderHistorgramGaussian"? #14

Open lixiaohui2020 opened 3 years ago

lixiaohui2020 commented 3 years ago

hi, @jonbarron, thanks for sharing the codes of the ffcc. I can't understand the meaning of the conv operations in the function "RenderHistorgramGaussian". The code is as follow,

% Threshold the mahalanobis distance at 3 to make a binary mask, which is
% dilated to produce an ellipse with a dot in the center.
mask = mahal_dist <= 3;
prediction = (conv2(double(mask), ones(3,3), 'same') > 0) & ~mask;
prediction = prediction | (conv2(double(mahal_dist == min(mahal_dist(:))), [0, 1, 0; 1, 1, 1; 0, 1, 0], 'same') > 0);

% Optionally create a mask with the ground-truth white point rendered as a dot.
if ~isempty(mu_true)
  D = (us - mu_true(1)).^2 + (vs - mu_true(2)).^2;
  truth = D == min(D(:));
  truth = (conv2(double(truth), [0, 1, 0; 1, 1, 1; 0, 1, 0], 'same') > 0);
  truth = (conv2(double(truth), [0, 1, 0; 1, 1, 1; 0, 1, 0], 'same') > 0);

I want to ask the questions,

  1. What's the meaning of the function conv2(double(mask), ones(3,3), 'same') > 0) ?
  2. I can understand the formula about _mahal_dist == min(mahal_dist(:) to get the ellipse of the minmum distance. However, i can't understand why to run the conv operation of the formula conv2(double(mahal_dist == min(mahal_dist(:))), [0, 1, 0; 1, 1, 1; 0, 1, 0], 'same') and why to chose the matrix [0, 1, 0; 1, 1, 1; 0, 1, 0]_ .
  3. Why are the two same conv operations with conv2(double(truth), [0, 1, 0; 1, 1, 1; 0, 1, 0], 'same') ?

Look forward to your favourable reply ! @jonbarron

jonbarron commented 3 years ago
  1. This just dilates the mask, https://en.wikipedia.org/wiki/Dilation_(morphology). By dilating the mask and then removing the original mask, you get a boundary around the ellipse.
  2. mahal_dist == min(mahal_dist(:) just makes a single pixel at the argmin, and then it's convolved by a plus shape to make a plus. This is just because I wanted a plus shape at the center.
  3. Convolving by [0, 1, 0; 1, 1, 1; 0, 1, 0] twice just gives a nice diamond shape. All of this code is just for visualizing the results, and isn't critical to the algorithm.
lixiaohui2020 commented 3 years ago

@jonbarron I am very glad to receive your prompt reply! Thanks!