mardi 7 mai 2024

Making your own linear filters with Rebol-OpenCV

 In his Rebol-OpenCV extension, Oldes has implemented a filter2D function which is very useful for constructing various types of linear filters, such as a Sobel filter, a Kurawhara filter, etc.

In this code, I've just used the example given in the OpenCV documentation.

You will find here a complete documentation about the use of filter2D : https://docs.opencv.org/3.4/d4/dbd/tutorial_filter_2d.html


#!/usr/local/bin/r3
Rebol [
]
;-- see https://docs.opencv.org/3.4/d4/dbd/tutorial_filter_2d.html 
cv: import opencv
with cv [
    src: imread "../../image/lena.jpg" ;--use your own image
    dst: Matrix :src ;--create matrices from image
    anchor: -1x-1 ;--kernel center
    delta: 0.0 ;--default value
    ddepth: -1 ;-- output image depth as same as the input image
    ind: 0 ;--counter for forever loop
    print "Press any key to stop the animation!"
    forever [
    kernelSize: 3 + (2 * (ind % 5)) ;--odd values in the range [3..11]
    kSize2: to-integer kernelSize ** 2 ;--update kernel size for a normalized filter
    vec: make vector! reduce ['float! kSize2] ;--create Rebol vector 
    vec: vec + (1.0 / kSize2) ;--update vector with normalized values
    kSize: as-pair kernelSize kernelSize ;--use a pair! for matrix creation
    kernel: Matrix [CV_32FC1 :kSize :vec] ;--create kernel 
    filter2D src dst ddepth kernel anchor delta ;--call OpenCV filter2D 
    imshow/name dst "Normalized Kernel" ;--show result
    if 0 <= waitkey 500 [break] ;--exit if any key
    ++ ind ;--increment counter
    ]
    destroyAllWindows ;--close window
]

This linear filter creates a blurring effect on the source image according to the kernel size.
You can also have a look to gaussianBlur and medianBlur in Rebol-Opencv extension.

Aucun commentaire:

Enregistrer un commentaire