Difference between revisions of "Function:ImageFilter"

From CUVI Wiki
 
(4 intermediate revisions by the same user not shown)
Line 3: Line 3:
===Function===
===Function===
{|
{|
|style="font-size:150%;"|
|style="font-size:100%;"|
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
CuviStatus imageFilter(CuviImage* srcImage,
 
                       CuviImage* dstImage,
CuviStatus imageFilter(const CuviImage& src,
                       CuviROI roi,
                      CuviImage& dst,
                       CuviFilter* filter,
                      const CuviFilter& filter,
                       CuviStream* stream = NULL);
                      const CuviStream& stream = CuviStream());
 
CuviStatus imageFilter(const CuviImage& src,
                       CuviImage& dst,
                       const CuviRect& roi,
                       const CuviFilter& filter,
                       const CuviStream& stream = CuviStream());
</syntaxhighlight>
</syntaxhighlight>
|}
|}
===Parameters===
===Parameters===


Line 20: Line 27:
! Description
! Description
|-
|-
| srcImage
| src
| CuviImage*
| const CuviImage&
| Input Image
| Input Image
|-
|-
| dstImage
| dst
| CuviImage*
| CuviImage&
| Output Image
| Output Image
|-
|-
| roi
| roi
| CuviROI
| const CuviRect&
| Region of Interest
| Region of Interest
|-
|-
| filter
| filter
| CuviFilter*
| const CuviFilter&
| This structure contains filter parameters including dimensions of the filter kernel, coefficients and anchor position
| This structure contains filter parameters including dimensions of the filter kernel, coefficients and anchor position
|-
|-
| stream  
| stream  
| CuviStream*
| const CuviStream&
| GPU stream ID for execution
| GPU stream ID for execution


Line 65: Line 72:
|-
|-
| 32fC3
| 32fC3
| 32fC3
|-
| 8uC1
| 32fC1
|-
| 8uC3
| 32fC3
|-
| 16uC1
| 32fC1
|-
| 16uC3
| 32fC3
| 32fC3
|}
|}
Line 94: Line 89:
|[[File:Filtergs.jpg|frame|Gaussian 5x5 with sigma=0.35]]
|[[File:Filtergs.jpg|frame|Gaussian 5x5 with sigma=0.35]]
|[[File:Filteru.jpg|frame|Unsharp with alpha=0.2]]
|[[File:Filteru.jpg|frame|Unsharp with alpha=0.2]]
|-
|[[File:Filterinb.jpg‎|frame|Input]]
|[[File:Filterxd.jpg|frame|X Derivative]]
|[[File:Filteryd.jpg|frame|Y Derivative]]
|}
|}


Line 102: Line 102:
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">


//Input image
CuviImage gimg = cuvi::io::loadImage(path);
//Output image
CuviImage gout;
//Choosing ROI
CuviRect roi(0,0,gimg.width(),gimg.height());
//Creating a 3x3 Averaging Filter. User can also enter custom coefficients
Cuvi32f coeffs[9] = {
                      1/9.0f, 1/9.0f, 1/9.0f,
                      1/9.0f, 1/9.0f, 1/9.0f,
                      1/9.0f, 1/9.0f, 1/9.0f,
                    };


CuviImage* gimg = new CuviImage(size,img->depth,img->nChannels);
CuviFilter f(3,3,coeffs);
CuviImage* gout = new CuviImage(size,img->depth,img->nChannels);


//Populate input image with pixel data
//Applying filter 'f' on the image
gimg->upload(img->imageData,img->widthStep);
cuvi::imageFiltering::imageFilter(gimg,gout,roi,f);


//Mask borders with pixel value of 0
cuvi::colorOperations::borderMask(gimg,gout,10,10,5,3,0);
</syntaxhighlight>
</syntaxhighlight>
|}
|}

Latest revision as of 12:14, 23 October 2022

Linear filters are an important part of Image Processing Applications. CUVI provides a single generic filter function to apply any filter of any size to an image. We have in-build flags for various famous filters and programmer can input his custom filter elements as well.

Function

CuviStatus imageFilter(const CuviImage& src,
                       CuviImage& dst,
                       const CuviFilter& filter,
                       const CuviStream& stream = CuviStream());

CuviStatus imageFilter(const CuviImage& src,
                       CuviImage& dst,
                       const CuviRect& roi,
                       const CuviFilter& filter,
                       const CuviStream& stream = CuviStream());

Parameters

Name Type Description
src const CuviImage& Input Image
dst CuviImage& Output Image
roi const CuviRect& Region of Interest
filter const CuviFilter& This structure contains filter parameters including dimensions of the filter kernel, coefficients and anchor position
stream const CuviStream& GPU stream ID for execution

Image Type Support

Input Output
8uC1 8uC1
8uC3 8uC3
16uC1 16uC1
16uC3 16uC3
32fC1 32fC1
32fC3 32fC3


Anchor Position

For creating custom square or rectangular filter kernel, you need to specify the taps as 1D row-major 32bit floating-point array. By default, the center of the 2D filter kernelis placed on the image pixel that corresponds to the current output location. This is known as the filter kernel anchor and it’s position is given by a 2D point. For a 3x3 filter kernel, the default anchor position is -1,-1 which corresponds to the center of the filter kernel, -2,-2 for 5x5 filter and so on. You can change this to another location within the filter kernel through the anchorPositionargument of the imFilter function. Here’s how you create a custom filter kernel and populate it with taps:

Anchor.png

Sample

Input
Average 5x5
Gaussian 5x5 with sigma=0.35
Unsharp with alpha=0.2
Input
X Derivative
Y Derivative


Example

//Input image
CuviImage gimg = cuvi::io::loadImage(path);

//Output image
CuviImage gout;

//Choosing ROI
CuviRect roi(0,0,gimg.width(),gimg.height());

//Creating a 3x3 Averaging Filter. User can also enter custom coefficients 

Cuvi32f coeffs[9] = {
                      1/9.0f, 1/9.0f, 1/9.0f,
                      1/9.0f, 1/9.0f, 1/9.0f,
                      1/9.0f, 1/9.0f, 1/9.0f,
                    };

CuviFilter f(3,3,coeffs);

//Applying filter 'f' on the image
cuvi::imageFiltering::imageFilter(gimg,gout,roi,f);