however it's not easy to run or determine what he's doing.
I've written similar code, without the assistance of ChatGPT , previously in FreeBasic.
The inverse radon transform is [ was ] used for computerised axial tomography .
Therefore ChatGPT, and I, have produced a more basic version that many Basic programmers can
comprehend; documentation is on the agenda .
As typical, I'm using my own coding style .
I did have to wrestle with the code from ChatGPT , it didn't run as expected first time and manual
debugging was necessary .
The left side image is the original, the right hand side image has been reconstructed.
Anyway here's the code :
Code: Select all
'
' sciwiseg@gmail.com , 2024 ; lgpl , if applicable
'
' 74% help from ChatGPT
'
' Define the size of the image
const IMG_WIDTH = 256 '128
const IMG_HEIGHT = 256 '128
const N_ANGLES = 180
'
' -------------------------- declarations -----------------
'
Declare sub GenerateTestImage(image() as single)
Declare sub RadonTransform(image() as single, radon() as single)
Declare sub InverseRadonTransform(radon() as single, image1() as single)
Declare sub plot2img(image1() as single,image2() as single)
'
' ---------------------------------------------------------------
'
' Main program
'
redim as single image( IMG_WIDTH - 1,IMG_HEIGHT - 1)
redim as single radon(0 to 0, 0 to 0)
redim as single reconstructed( IMG_WIDTH - 1,IMG_HEIGHT - 1)
'
' ---------------------------------------------------------------
'
' Generate a test image
GenerateTestImage(image())
' Perform the Radon transform
RadonTransform(image(), radon())
' Perform the inverse Radon transform
InverseRadonTransform(radon(), reconstructed())
' Plot the original and reconstructed images .
plot2img(image(), reconstructed())
sleep
end
'
' ===========================
'
'
' Function to generate a simple test image (a filled circle)
sub GenerateTestImage(image() as single)
dim as integer x, y
dim as single radius = 30
dim as single cx = IMG_WIDTH / 2
dim as single cy =IMG_HEIGHT / 2
dim as single dist, rd2
rd2=radius^2
redim image( IMG_WIDTH - 1,IMG_HEIGHT - 1)
' Initialize the image with zeros
For y = 0 To IMG_HEIGHT - 1
For x = 0 To IMG_WIDTH - 1
image(x, y) = 0
Next
Next
for y = 0 to IMG_HEIGHT - 1
for x = 0 to IMG_WIDTH - 1
dist = ((x - cx) * (x - cx) + (y - cy) * (y - cy))
if dist <= rd2 then image(x, y) = 1 end if
next x
next y
'
end sub
'
' ---------------------------------------------------------------
'
' Function to perform Radon Transform
sub RadonTransform(image() as single, radon() as single)
dim as integer x, y, t
dim as single theta, rho
dim as integer r_index
dim as single pi = atn(1) * 4
dim as integer max_rho = sqr(IMG_WIDTH * IMG_WIDTH + IMG_HEIGHT * IMG_HEIGHT)
redim radon(0 to N_ANGLES - 1, 0 to 2 * max_rho - 1)
for t = 0 to N_ANGLES - 1
theta = t * pi / N_ANGLES
for y = 0 to IMG_HEIGHT - 1
for x = 0 to IMG_WIDTH - 1
if image(x, y) > 0 then
rho = x * cos(theta) + y * sin(theta)
r_index = max_rho + int(rho)
radon(t, r_index) += image(x, y)
end if
next
next
next
'
end sub
'
' ---------------------------------------------------------------
'
sub InverseRadonTransform(radon() as single, image1() as single)
dim as integer x, y, t
dim as single theta, rho
dim as integer r_index
dim as single pi = atn(1) * 4
dim as integer max_rho = sqr(ubound(image1, 1) * ubound(image1, 1) + ubound(image1, 2) * ubound(image1, 2))
redim image1( IMG_WIDTH - 1,IMG_HEIGHT - 1)
' Clear the image
for y = 0 to ubound(image1, 2)
for x = 0 to ubound(image1, 1)
image1(x, y) = 0
next
next
for t = 0 to N_ANGLES - 1
theta = t * pi / N_ANGLES
for y = 0 to ubound(image1, 2)
for x = 0 to ubound(image1, 1)
rho = x * cos(theta) + y * sin(theta)
r_index = max_rho + int(rho)
image1(x, y) += radon(t, r_index)
next
next
next
'
end sub
'
' ---------------------------------------------------------------
'
sub plot2img(image1() as single,image2() as single)
'
' Plot 2 images, alongside each other .
'
dim as integer x, y
dim as single max_val1, min_val1
dim as single max_val2, min_val2
'
max_val1 = 0
min_val1=100
' Find the maximum and minimum value in the array .
for y = 0 to ubound(image1, 2)
for x = 0 to ubound(image1, 1)
if image1(x, y) > max_val1 then
max_val1 = image1(x, y)
end if
if image1(x, y) < min_val1 then
min_val1 = image1(x, y)
end if
next
next
'
'min_val1=0
max_val1=abs(max_val1-min_val1)
if max_val1=0 then max_val1=1 end if
'
' ......................................................................
'
'
max_val2 = 0
min_val2=100
' Find the maximum and minimum value in the array .
for y = 0 to ubound(image2, 2)
for x = 0 to ubound(image2, 1)
if image2(x, y) > max_val2 then
max_val2 = image2(x, y)
end if
if image2(x, y) < min_val2 then
min_val2 = image2(x, y)
end if
next
next
'
'min_val1=0
max_val2=abs(max_val2-min_val2)
if max_val2=0 then max_val2=1 end if
'
' ---------------------- plot images -----------------------
'
x=2*IMG_WIDTH
y=IMG_HEIGHT
screenres x, y, 32
'screenres 256, 128, 32
'
' Plot the radon array values as grayscale intensities
dim as integer intensity
'
for y = 0 to ubound(image1, 2)
for x = 0 to ubound(image1, 1)
intensity = int(255 * (image1(x, y)-min_val1) / max_val1)
pset (x, y), rgb(intensity, intensity, intensity)
next
next
'
' ......................................................................
'
for y = 0 to ubound(image2, 2)
for x = 0 to ubound(image2, 1)
intensity = int(255 * (image2(x, y)-min_val2) / max_val2)
pset (x+ IMG_WIDTH, y), rgb(intensity, intensity, intensity)
next
next
'
'
end sub
'
' ---------------------------------------------------------------
'