jeudi 16 avril 2026

Heat diffusion

From https://en.wikipedia.org/wiki/Heat_equation

"In mathematics and physics (more specifically thermodynamics), the heat equation is a parabolic partial differential equation. The theory of the heat equation was first developed by Joseph Fourier in 1822 for the purpose of modeling how a quantity such as heat diffuses through a given region. Since then, the heat equation and its variants have been found to be fundamental in many parts of both pure and applied mathematics". 

The idea is to see how Rebol or Red can solve this problem. Let’s imagine a coin heated at its center at T0.0 and calculate the temperature distribution toward the edges of the coin. We get a nice Gaussian curve.
Now, let’s look at how the phenomenon changes over time (from 0.0 to 0.10). We still see nice Gaussian curves whose mean decreases over time. We can also model the cooling of the metal part. 
Due to vector datatype  (https://github.com/Oldes/Rebol3) these equations are very easy to solve.

#!/usr/local/bin/r3
REBOL [
]

b2d: import 'blend2d ;--use blend2d (draw module)
cv2: import 'opencv ;--use OpenCV for visualisation

;--Global Physical Parameters
k: 0.5 ;--Diffusion coefficient (Thermal diffusivity coefficient of material)
l: 1.0     ;--Domain size
itime: 0.1   ;--Integration time

;--Global Numerical Parameters
nx: 100     ;--Number of grid points
nt: 1000   ;--Number of time steps
dx: l / (nx - 1)   ;--Grid step (Spatial step size: 0.0101010101010101)
dt: itime / nt   ;--Grid step (Time step size :0.0001)

;--Based on Oldes's code
linSpace: func [
    "Generates n linearly spaced numbers from a to b (inclusive)."
    a [number!]  "Start"
    b [number!]  "End"
    n [integer!] "Number of samples"
    /no-end      "Don't include end value in the result"
    /local vec div step i ;--local values
][
    vec: make vector! [f64! :n]
    div: either no-end [n][n - 1]
    step: (b - a) / div
    repeat i n [vec/:i: a + (step * (i - 1))]
    vec
]

;--Initialisation (using global variables)
init: does [
x: linSpace 0.0 1.0 nx ;--linear spacing between 0.0 and 1.0
t: (x * 2.0 * pi) ;--t is a vector
repeat i nx [t/:i: sin t/:i] ;--in radians
;--use blend2d commands in plot block
plot: compose [
font %/System/Library/Fonts/Geneva.ttf
fill black
text 0x10  10 "+1.0"
text 0x204 10 "0"
text 0x390 10 "-1.0"
pen black
line 5x200 410x200 
]
]

;--Heat diffusion equation (using global variables)
diffusion: does [
vect: make vector! [f64! :nx]
posy: 0
n: 0
while [n <= nt] [
j: 2
while [j < nx] [
vect/:j: dt * k * (t/(j - 1) - (2.0 * t/:j) + t/(j + 1)) / (dx ** 2)
++ j
]
repeat j nx [t/:j: t/:j + vect/:j]
;--Plot every 100 time steps
if zero? n % 100 [ 
acolor: random white
posy: posy + 15
either n = 0 [tt: 0.0] [tt: round/to n % (n * dt) 0.001]
tt: ajoin ["time: " tt]  
pos: as-pair 340 posy
append plot reduce ['fill (acolor) 'text (pos) 12 (tt) 'pen (acolor) 'line]
xx: 2
foreach val t [
append plot as-pair xx * 4 200 - (val * 195) 
++ xx 
]
]
++ n
]
]
;************************ Main Program ************************
init 
diffusion
img: b2d/draw 410x400 :plot
img: cv2/resize img 150% 
cv2/imshow/name :img "Heat Diffusion"
print "Any key to close"
cv2/waitKey 0






vendredi 3 avril 2026

Using Filter2D with Rebol-OpenCV

 Rebol-OpenCV (https://github.com/Oldes/Rebol-OpenCV) Filter2D is really fantastic for creating various filters on image.

filter2D: [
"Convolves an image with the kernel."
src    [handle!] "cvMat"
dst    [handle! none!] "cvMat"
ddepth [integer!] "desired depth of the destination image"
kernel [handle!] "convolution kernel (or rather a correlation kernel), a single-channel floating point matrix"
anchor [pair! integer!] "position of the anchor within the element"
delta  [number!] "value added to the filtered pixels before storing them in dst"
/border "border mode used to extrapolate pixels outside of the image"
type [integer!] "one of: [0 1 2 4 5 16]"
]

This is an example of creating a Kuwahara_filter

#!/usr/local/bin/r3
Rebol [
]
;--https://en.wikipedia.org/wiki/Kuwahara_filter
cv: import opencv
with cv [
    src: imread/with "../images/mandrill.jpg" -1 ;--(IMREAD_UNCHANGED)
    dst: Matrix :src ;--create a matrix
    ;--using Rebol vector type for a 3x3 kernel
    vec: #(f32! [-0.5 1.5 -0.5 1.5 -3.0 1.5 -0.5 1.5 -0.5]) ;--sum = 1
    filter: Matrix [CV_32FC1 3x3 :vec] ;--a 3x3 kernel
    filter2D :src :dst -1 :filter -1x1 0 ;-1 same image depth
    ;--show result
    imshow/name src "Source"
    imshow/name dst "Kuwahara"
    moveWindow "Source"  0x0
    moveWindow "Kuwahara" 260x0
    waitKey 0
    destroyAllWindows

]

The trick is to use vector datatype implemented by Oldes to create the kernel used by the filter2D function.
The result for the Kuwahara_filter:


You can find here https://github.com/ldci/R3_OpenCV_Samples/tree/main/image_filtering a lot of examples for generating filters on image.

vendredi 19 décembre 2025

A simple GUI for Rebol3

Rebol3, developed by Oldes (https://github.com/Oldes/Rebol3), is improving day by day. Unfortunately, Rebol3 does not yet have a VID (except for Windows as a proof of concept), which means that attractive interfaces cannot yet be created. 
Fortunately, Oldes added a nice modules management to Rebol3, which allows this problem to be overcome. 
In this code, we use two modules:
The idea is as follows: we use the Blend2D module to create the interface for our application and the OpenCV module to display the result. 
With Blend2D module, I created five buttons (simple blend2D boxes). Each button is associated with a Rebol3 function that will execute when the button is clicked. Button 1 calls the loadImage function, which allows you to select an image and display it. Buttons 2 and 3 call the convertTo function, which displays the image in grayscale or in HSV color space. Button 4 displays the original image and button 5 exits the application.
Now, the question is how to associate mouse movements with different buttons. Blend2D does not have a mouse manager, which is where the OpenCV module comes in.

Oldes has introduced a mouse event handler into the OpenCV module for these events:
  • MOUSEMOVE
  • LBUTTONDOWN
  • RBUTTONDOWN
  • MBUTTONDOWN
  • LBUTTONUP
  • RBUTTONUP
  • MBUTTONUP
  • LBUTTONDBLCLK
  • RBUTTONDBLCLK
  • MBUTTONDBLCLK
  • MOUSEWHEEL
  • MOUSEHWHEEL
With the cv/setMouseCallback function, we can retrieve the event type, the x and y position of the mouse, and the event flag. We also obtain the mouse position as a pair! x y.

;--OpenCV mouse callback in context
ctx: context [
    on-mouse-click: func [
        type  [integer!]
        x     [integer!]
        y     [integer!]
        flags [integer!]
    ][
    pos: mcb/pos ;--mouse position as a pair!
        if type == cv/EVENT_LBUTTONDOWN [
        if pos/y < 20 [
        case [
        all [pos > 0x0 pos < 80x20]   [loadImage]         ;--button 1
        all [pos > 80x0 pos < 160x20] [if isLoaded? [convertTo/GS fileName]]        ;--button 2
        all [pos > 160x0 pos < 240x20][if isLoaded? [convertTo/HSV fileName]]    ;--button 3
        all [pos > 240x0 pos < 320x20][showSource]    ;--button 4
        all [pos > 320x0 pos < 400x20][quit] ;--button 5
        ]
        ]
        ]
    ]
]

In the on-mouse-click function, we first check that the mouse is within the button area. Then we check that the mouse is on the selected button. If so, we execute the function associated with the button. In this example button 3 is clicked and convertTo function is executed.
Added new faces for Rebol3 GUI
CheckBox

RadioBox

Horizontal Scroller
Toggle On
Progress









vendredi 12 décembre 2025

Virginia Project: A first paper from R2P2 Lab

https://www.frontiersin.org/journals/pediatrics/articles/10.3389/fped.2025.1636667/full

First, the Virginia software, written with Red language, isolated neonatal anatomy through PyTorch-based PointRend segmentation combined with morphological filtering.

Second, radiometric decoding via ExifTool and ImageMagick extracted pixel-level temperature values mapped to anatomical regions of interest (chest, extremities). Finally, quantitative thermal metrics were derived, including median body surface, temperature and spatial thermal variability (interquartile range).

A key advantage of this automated pipeline is its low operator dependence; once the image is acquired, the entire segmentation and feature extraction process is software-driven, minimizing human interpretation bias.





jeudi 23 octobre 2025

BlurHash with r3

 

What is BlurHash? (from https://uploadcare.com/blog/blurhash-images/ and https://github.com/woltapp/blurhash )

BlurHash is a lightweight way to represent a blurred version of an image that was invented by the Wolt team. It is represented by a short piece of text that when decoded can produce a low quality version of an image which can be shown to the user while the actual image is being loaded.

The idea behind BlurHash is to design a placeholder that is close to the original image but has a smaller size. This makes it possible to send the placeholder to the client side of your application, which reduces the time that the user spends waiting for the page to load.

Oldes wrote an extension for Rebol 3 that makes it very easy to use BlurHash.

See https://github.com/Siskin-framework/Rebol-BlurHash


#!/usr/local/bin/r3
Rebol [
    title: "Rebol/BlurHash test"
]
blurhash: import 'blurhash
cv:         import 'opencv

image: load %../pictures/test1.tiff ;--use your own image
print ["Encoding image of size" as-yellow image/size]
hash: blurhash/encode image
print ["String: " hash]
print ["Decoding hash into image"]
blured: resize blurhash/decode hash 32x32 image/size
;--use OpenCV extension for visualisation
with cv [
print "Source Image"
imshow/name image "Source"
waitkey 0
print "Blured Image"
imshow/name blured "Blured"
waitkey 0
print "Any key to close"
]

Thanks to Oldes!😀




jeudi 2 octobre 2025

SEDFormer

Exciting news! Our paper "SEDformer: Path Signatures and Transformers to Predict Newborns Movement Symmetry" has been accepted at the International Joint Workshop of Artificial Intelligence for Healthcare (HC@AIxIA) and HYbrid Models for Coupling Deductive and Inductive ReAsoning (HYDRA), ECAI 2025.


This research introduces SEDformer, a FEDformer variant that integrates path signatures to enhance newborn movement symmetry prediction - a crucial step toward automated early screening tools for motor development disorders.

Thank you to the entire team for this interdisciplinary collaboration between applied mathematics, AI, and pediatric medicine.

 Rambaud, P., Rimmel, A., Trabelsi, I., Zini, J., Wodecki, A., Motte Signoret, E., Jouen, F., Tomasik, J., Bergounioux, J. (2025). SEDformer: Path Signatures and Transformers to Predict Newborns Movement Symmetry. International Joint Workshop of Artificial Intelligence for Healthcare (HC@AIxIA) and HYbrid Models for Coupling Deductive and Inductive ReAsoning (HYDRA)" ECAI 2025.