# Denoising function for a sliding DCT filter

4 views (last 30 days)
Deepika Sundresh on 15 Dec 2021
Edited: Abhimenyu on 14 Apr 2024
I am trying to build a function that denoises every channel of an image using DCT, and outputs the clean channel.
function denoised_ch = sliding_dct(channel, variance)
% choose window size as 8x8
fun1 = @(block_struct) dct2(block_struct.data(:));
dct_ch = blockproc(channel, [8 8], fun1);
[r, c] = size(dct_ch);
% threshold the DCT coeff with threshold = 3xvariance of the channel
fun2 = @(block_struct) idct2(block_struct.data(:));
denoised_ch = blockproc(channel, [8 8], fun2);
How can I implemement the part about thresholding? (The commented part)
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Answers (1)

Abhimenyu on 14 Apr 2024
Edited: Abhimenyu on 14 Apr 2024
Hello,
From the information shared, I could infer that you are trying to implement thresholding in your function to modify the "DCT coefficients" based on the threshold, which is the "3 * variance" of the channel. The idea is to set the "DCT coefficients" that are below this threshold to zero, as they are likely to represent noise rather than signal. Please follow the below given example MATLAB code for the implementation of thresholding:
function denoised_ch = sliding_dct(channel, variance)
% Choose window size as 8x8
% Threshold Calculation: The threshold is calculated outside
% the block processing function as "3 * variance".
threshold = 3 * variance;
% Function to apply DCT and then threshold the coefficients
% thresholdDCT Function: This helper function applies the "2D DCT" to a block,
% then thresholds the "DCTthresholdDCT Function" coefficients by setting those that are below the
% absolute value of the threshold to zero.
fun1 = @(block_struct) thresholdDCT(block_struct.data, threshold);
% Apply DCT and thresholding within the block processing
% Block Processing for DCT and Thresholding: The blockproc function is used
% with fun1, which now points to the thresholdDCT function.
% This processes each 8x8 block of the channel, applying DCT and then thresholding.
dct_ch = blockproc(channel, [8 8], fun1);
% Function to apply inverse DCT
fun2 = @(block_struct) idct2(block_struct.data);
% Apply inverse DCT to get the denoised channel
denoised_ch = blockproc(dct_ch, [8 8], fun2);
end
% thresholdDCT Function
function dct_block = thresholdDCT(block, threshold)
% Apply DCT
dct_block = dct2(block);
% Apply thresholding
dct_block(abs(dct_block) < threshold) = 0;
end
The above-mentioned MATLAB code applies a threshold to each "DCT coefficient" with the help of the helper function "thresholdDCT" which applies the "dct2" MATLAB function for computing "DCT" and then thresholding to remove noise.
I hope this helps!
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Categories

Find more on Smoothing and Denoising in Help Center and File Exchange

R2020b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!