elerac / polanalyser

Polarization image analysis tool. Demosaicing, Stokes vector, Mueller matrix.
MIT License
135 stars 25 forks source link

About the kernel of cv2.filter2D #3

Closed vinvin0430 closed 3 years ago

vinvin0430 commented 3 years ago

I noticed that you used a kernel in demosaicing_mono_float:

kernel = np.array([[1/4, 1/2, 1/4], [1/2, 1.0, 1/2], [1/4, 1/2, 1/4]], dtype=np.float64) img_polarization = cv2.filter2D(img_subsampled, -1, kernel)

What is the role of this kernel?

elerac commented 3 years ago

Hi @vinvin0430!

The kernel can treat the demosaicing process as the 2D convolution. Here I show the figure that describes an overview of __demosaicing_mono_float process.

demosaicing_conv

The main process can be divided into two steps, Subsampling and 2D convolution.

  1. Subsampling step Generates img_subsampled by subsampling the values of each channel (angle of polarization filter, 0-45-90-135) of img_mpfa. For img_subsampled, the lattice area shown in the figure contains the same values as in img_mpfa and the white area is filled with zeros.
  2. 2D convolution step After subsampling step, apply 2D convolution to img_subsampled. The filter kernel is designed to calculate the equivalent of demosaicing using bilinear interpolation. As you can see in the figure, the white area is zero, so only a part of the filter (shown in red color) is used for calculation, and the result is the same as general bilinear interpolation demosaicing.

The reason for using 2D convolutional (cv2.filter2D) is that it is easy and fast, thanks to the well-tuned OpenCV code.

vinvin0430 commented 3 years ago

Hi elerac,

Thank you for your patient reply. It helped me a lot. : )