Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new parameter "wref" for the final estimation #25

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ The input clip is processed in 3-step stages, For each reference block:<br />
This final estimate can be realized as a refinement. It can significantly improve the denoising quality, keeping more details and fine structures that were removed in basic estimate.

```python
bm3d.Final(clip input, clip ref[, string profile="fast", float[] sigma=[10,10,10], int block_size, int block_step, int group_size, int bm_range, int bm_step, float th_mse, int matrix=2])
bm3d.Final(clip input, clip ref[, clip wref=ref, string profile="fast", float[] sigma=[10,10,10], int block_size, int block_step, int group_size, int bm_range, int bm_step, float th_mse, int matrix=2])
```

- input:<br />
Expand All @@ -195,6 +195,21 @@ bm3d.Final(clip input, clip ref[, string profile="fast", float[] sigma=[10,10,10
It must be specified. In original BM3D algorithm, it is the basic estimate.<br />
Alternatively, you can choose any other decent denoising filter as basic estimate, and take this final estimate as a refinement.

- wref:<br />
The reference clip for empirical Wiener filtering. If specified, wref will replace ref as the empirical estimate for Wiener filtering.<br />
You should specify this parameter if input and ref are sampled from different domains, e.g. input might be a difference image while ref could be a natural image.<br />
In such case you should assign something sampled from the input’s domain to wref.<br />
A common use case of wref would be to refine the denoising result of an alternative denoiser using BM3D.

```python
flt = some_filter(src)
dif = core.std.MakeDiff(src, flt)
ref_dif = core.bm3d.Basic(dif, flt)
ref = core.std.MergeDiff(flt, ref_dif)
dif = core.bm3d.Final(dif, ref, wref=ref_dif)
flt = core.std.MergeDiff(flt, dif)
```

- profile, sigma, block_size, block_step, group_size, bm_range, bm_step, th_mse, matrix:<br />
Same as those in bm3d.Basic.

Expand Down Expand Up @@ -252,10 +267,10 @@ bm3d.VBasic(clip input[, clip ref=input, string profile="fast", float[] sigma=[1
#### final estimate of V-BM3D denoising filter

```python
bm3d.VFinal(clip input, clip ref[, string profile="fast", float[] sigma=[10,10,10], int radius, int block_size, int block_step, int group_size, int bm_range, int bm_step, int ps_num, int ps_range, int ps_step, float th_mse, int matrix=2])
bm3d.VFinal(clip input, clip ref[, clip wref=ref, string profile="fast", float[] sigma=[10,10,10], int radius, int block_size, int block_step, int group_size, int bm_range, int bm_step, int ps_num, int ps_range, int ps_step, float th_mse, int matrix=2])
```

- input, ref:<br />
- input, ref, wref:<br />
Same as those in bm3d.Final.

- profile, sigma, block_size, block_step, group_size, bm_range, bm_step, th_mse, matrix:<br />
Expand Down
17 changes: 15 additions & 2 deletions include/BM3D_Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class BM3D_Data_Base
VSNodeRef *rnode = nullptr;
const VSVideoInfo *rvi = nullptr;

bool wdef = false;
VSNodeRef* wnode = nullptr;
const VSVideoInfo* wvi = nullptr;

bool wiener;
ColorMatrix matrix;

Expand Down Expand Up @@ -126,11 +130,19 @@ class BM3D_Process_Base
const VSFrameRef *ref = nullptr;
const VSFormat *rfi = nullptr;

const VSFrameRef* wref = nullptr;
const VSFormat* wfi = nullptr;

PCType ref_height[VSMaxPlaneCount];
PCType ref_width[VSMaxPlaneCount];
PCType ref_stride[VSMaxPlaneCount];
PCType ref_pcount[VSMaxPlaneCount];

PCType wref_height[VSMaxPlaneCount];
PCType wref_width[VSMaxPlaneCount];
PCType wref_stride[VSMaxPlaneCount];
PCType wref_pcount[VSMaxPlaneCount];

bool full = true;

private:
Expand Down Expand Up @@ -220,11 +232,12 @@ class BM3D_Process_Base
_NewFrame(width, height, dfi == fi);
}

void Kernel(FLType *dst, const FLType *src, const FLType *ref) const;
void Kernel(FLType *dst, const FLType *src, const FLType *ref, const FLType *wref) const;

void Kernel(FLType *dstY, FLType *dstU, FLType *dstV,
const FLType *srcY, const FLType *srcU, const FLType *srcV,
const FLType *refY, const FLType *refU, const FLType *refV) const;
const FLType *refY,
const FLType *wrefY, const FLType *wrefU, const FLType *wrefV) const;

PosPairCode BlockMatching(const FLType *ref, PCType j, PCType i) const;

Expand Down
32 changes: 26 additions & 6 deletions include/BM3D_Final.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class BM3D_Final_Data
_Myt &operator=(const _Myt &right) = delete;
_Myt &operator=(_Myt &&right) = delete;

virtual ~BM3D_Final_Data() override {}
virtual ~BM3D_Final_Data() override {
if (wdef && wnode) vsapi->freeNode(wnode);
}

virtual int arguments_process(const VSMap *in, VSMap *out) override;
};
Expand All @@ -70,11 +72,29 @@ class BM3D_Final_Process
const _Mydata &d;

public:
BM3D_Final_Process(_Mydata &_d, int _n, VSFrameContext *_frameCtx, VSCore *_core, const VSAPI *_vsapi)
: _Mybase(_d, _n, _frameCtx, _core, _vsapi), d(_d)
{}

virtual ~BM3D_Final_Process() override {}
BM3D_Final_Process(_Mydata& _d, int _n, VSFrameContext* _frameCtx, VSCore* _core, const VSAPI* _vsapi)
: _Mybase(_d, _n, _frameCtx, _core, _vsapi), d(_d) {
if (d.wdef) {
wref = vsapi->getFrameFilter(n, d.wnode, frameCtx);
wfi = vsapi->getFrameFormat(wref);
}
else {
wref = ref;
wfi = rfi;
}

if (!skip)
for (int i = 0; i < PlaneCount; ++i) {
wref_height[i] = vsapi->getFrameHeight(wref, i);
wref_width[i] = vsapi->getFrameWidth(wref, i);
wref_stride[i] = vsapi->getStride(wref, i) / wfi->bytesPerSample;
wref_pcount[i] = wref_height[i] * wref_stride[i];
}
}

virtual ~BM3D_Final_Process() override {
if (d.wdef) vsapi->freeFrame(wref);
}

protected:
virtual void CollaborativeFilter(int plane,
Expand Down
18 changes: 15 additions & 3 deletions include/VBM3D_Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class VBM3D_Data_Base
VSNodeRef *rnode = nullptr;
const VSVideoInfo *rvi = nullptr;

bool wdef = false;
VSNodeRef* wnode = nullptr;
const VSVideoInfo* wvi = nullptr;

bool wiener;
ColorMatrix matrix;

Expand Down Expand Up @@ -135,8 +139,10 @@ class VBM3D_Process_Base

std::vector<const VSFrameRef *> v_src;
std::vector<const VSFrameRef *> v_ref;

std::vector<const VSFrameRef *> v_wref;

const VSFormat *rfi = nullptr;
const VSFormat* wfi = nullptr;

PCType dst_height[VSMaxPlaneCount];
PCType dst_pcount[VSMaxPlaneCount];
Expand All @@ -146,6 +152,11 @@ class VBM3D_Process_Base
PCType ref_stride[VSMaxPlaneCount];
PCType ref_pcount[VSMaxPlaneCount];

PCType wref_height[VSMaxPlaneCount];
PCType wref_width[VSMaxPlaneCount];
PCType wref_stride[VSMaxPlaneCount];
PCType wref_pcount[VSMaxPlaneCount];

bool full = true;

private:
Expand Down Expand Up @@ -310,11 +321,12 @@ class VBM3D_Process_Base
vsapi->propSetIntArray(dst_map, "BM3D_V_process", process, VSMaxPlaneCount);
}

void Kernel(const std::vector<FLType *> &dst, const std::vector<const FLType *> &src, const std::vector<const FLType *> &ref) const;
void Kernel(const std::vector<FLType *> &dst, const std::vector<const FLType *> &src, const std::vector<const FLType *> &ref, const std::vector<const FLType *> &wref) const;

void Kernel(const std::vector<FLType *> &dstY, const std::vector<FLType *> &dstU, const std::vector<FLType *> &dstV,
const std::vector<const FLType *> &srcY, const std::vector<const FLType *> &srcU, const std::vector<const FLType *> &srcV,
const std::vector<const FLType *> &refY, const std::vector<const FLType *> &refU, const std::vector<const FLType *> &refV) const;
const std::vector<const FLType *> &refY,
const std::vector<const FLType *> &wrefY, const std::vector<const FLType *> &wrefU, const std::vector<const FLType *> &wrefV) const;

Pos3PairCode BlockMatching(const std::vector<const FLType *> &ref, PCType j, PCType i) const;

Expand Down
34 changes: 29 additions & 5 deletions include/VBM3D_Final.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class VBM3D_Final_Data
_Myt &operator=(const _Myt &right) = delete;
_Myt &operator=(_Myt &&right) = delete;

virtual ~VBM3D_Final_Data() override {}
virtual ~VBM3D_Final_Data() override {
if (wdef && wnode) vsapi->freeNode(wnode);
}

virtual int arguments_process(const VSMap *in, VSMap *out) override;
};
Expand All @@ -71,10 +73,32 @@ class VBM3D_Final_Process

public:
VBM3D_Final_Process(const _Mydata &_d, int _n, VSFrameContext *_frameCtx, VSCore *_core, const VSAPI *_vsapi)
: _Mybase(_d, _n, _frameCtx, _core, _vsapi), d(_d)
{}

virtual ~VBM3D_Final_Process() override {}
: _Mybase(_d, _n, _frameCtx, _core, _vsapi), d(_d) {
if (d.wdef) {
for (int o = b_offset; o <= f_offset; ++o)
v_wref.push_back(vsapi->getFrameFilter(n + o, d.wnode, frameCtx));

wfi = vsapi->getFrameFormat(v_wref[cur]);
}
else {
v_wref = v_ref;
wfi = rfi;
}

if (!skip)
for (int i = 0; i < PlaneCount; ++i) {
wref_height[i] = vsapi->getFrameHeight(v_wref[cur], i);
wref_width[i] = vsapi->getFrameWidth(v_wref[cur], i);
wref_stride[i] = vsapi->getStride(v_wref[cur], i) / wfi->bytesPerSample;
wref_pcount[i] = wref_height[i] * wref_stride[i];
}
}

virtual ~VBM3D_Final_Process() override {
if (d.wdef)
for (int i = 0; i < frames; ++i)
vsapi->freeFrame(v_wref[i]);
}

protected:
virtual void CollaborativeFilter(int plane,
Expand Down
11 changes: 6 additions & 5 deletions msvc/BM3D.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@
<ProjectGuid>{E93BF469-E2BA-4C9A-9E5A-C08D085FE469}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>BM3D</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
Expand Down Expand Up @@ -125,6 +125,7 @@
<BufferSecurityCheck>false</BufferSecurityCheck>
<ConformanceMode>true</ConformanceMode>
<EnforceTypeConversionRules>true</EnforceTypeConversionRules>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand Down
Loading