Debian Codegraph @

src/imageutils.f90

raw
1 !-----------------------------------------------------------------
2 !
3 ! This file is (or was) part of SPLASH, a visualisation tool
4 ! for Smoothed Particle Hydrodynamics written by Daniel Price:
5 !
6 ! http://users.monash.edu.au/~dprice/splash
7 !
8 ! SPLASH comes with ABSOLUTELY NO WARRANTY.
9 ! This is free software; and you are welcome to redistribute
10 ! it under the terms of the GNU General Public License
11 ! (see LICENSE file for details) and the provision that
12 ! this notice remains intact. If you modify this file, please
13 ! note section 2a) of the GPLv2 states that:
14 !
15 ! a) You must cause the modified files to carry prominent notices
16 ! stating that you changed the files and the date of any change.
17 !
18 ! Copyright (C) 2020- Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22 !----------------------------------------------------------------------
23 !
24 ! Some utilities for image manipulation using SPH algorithms
25 !
26 !----------------------------------------------------------------------
27 module imageutils
28 use kernels, only:select_kernel,wfunc
29 implicit none
30 real, parameter :: pi = 4.*atan(1.)
31
32 public :: image_denoise, image_denoise3D, image_rotate
33 private
34
35 contains
36
37 !----------------------------------------------------------------------
38 !
39 ! denoise an image using adaptive kernel convolution
40 ! i.e. variable smoothing lengths, where the smoothing scale
41 ! is inversely proportional to the image intensity
42 !
43 !----------------------------------------------------------------------
44 subroutine image_denoise(naxes,image,hh,iterations,imax,beam,skip,err)
45 use interpolations2D, only:interpolate2D
46 integer, intent(in) :: naxes(2)
47 real, intent(inout) :: image(naxes(1),naxes(2))
48 real, intent(inout) :: hh(naxes(1)*naxes(2))
49 integer, intent(in), optional :: iterations
50 real, intent(in), optional :: beam,imax
51 integer, intent(out), optional :: err
52 logical, intent(in), optional :: skip
53 real, allocatable :: x(:),y(:),weight(:),dat(:)
54 integer, allocatable :: mask(:)
55 integer :: npixels,its,niter,i,j,n,ierr,iskip
56 real :: imagemax,dx,dy,hfac,xmin,ymin
57 logical :: do_skip
58
59 ! choose default kernel if not already set
60 if (.not.associated(wfunc)) call select_kernel(0)
61
62 ! allocate memory for temporary arrays
63 npixels = product(naxes)
64 allocate(x(npixels),y(npixels),weight(npixels),dat(npixels),mask(npixels),stat=ierr)
65 if (present(err)) err = ierr
66 if (ierr /= 0) return
67
68 dy = 1.
69 dx = 1.
70 xmin = 0.0
71 ymin = 0.0
72 mask(:) = 1 ! do not mask any pixels
73 if (present(imax)) then
74 imagemax = imax
75 !print*,' imagemax = ',imagemax,' was ',maxval(image)
76 else
77 imagemax = maxval(image)
78 endif
79 do_skip = .true.
80 if (present(skip)) do_skip = skip
81 !fluxold = sum(image)
82 !print*,' total intensity =',fluxold
83
84 niter = 4
85 if (present(iterations)) niter = iterations
86
87 hfac = 1.0
88 !
89 ! decide whether to subsample pixels
90 ! no point using all if beam is large
91 !
92 iskip = 1 ! as many particles as pixels
93 if (present(beam)) then
94 hfac = beam
95 if (do_skip) then
96 iskip = max(nint(0.5*hfac),1)
97 if (iskip > 1) then
98 print "(a,i2,a)",' beam is large compared to pixel scale, subsampling by factor of ',iskip,' for efficiency'
99 print "(a)",' use --nosubsample to switch off'
100 endif
101 endif
102 endif
103
104 ! find the convolution length by iteration
105 h_iterations: do its=1,niter
106 n = 0
107 weight = 0.
108 do j=1,naxes(2),iskip
109 do i=1,naxes(1),iskip
110 n = n + 1
111 x(n) = i - 0.5*dx
112 y(n) = j - 0.5*dy
113 if (niter > 0) then
114 if (abs(image(i,j)) > 0.) then
115 hh(n) = min(max(0.4*hfac*sqrt(imagemax/abs(image(i,j))),1.),5.*(its+1)*sqrt(hfac))
116 else
117 hh(n) = 5.*(its+1)*sqrt(hfac)
118 endif
119 endif
120 if (its==1) dat(n) = image(i,j)
121 enddo
122 enddo
123 !print*,' sampling ',n,' pixels of ',naxes(1)*naxes(2)
124 if (niter > 0) print "(' Iteration: ',i2,' of ',i2,': ',3(a,1g8.3))",its,niter,&
125 'beam: min = ',minval(hh(1:n)),' max = ',maxval(hh(1:n)),' mean = ',sum(hh(1:n))/n
126
127 ! set weights for interpolation
128 weight(1:n) = dx*dy*iskip**2/hh(1:n)**2
129
130 ! perform interpolation of dat array to new set of pixels (image)
131 call interpolate2D(x,y,hh,weight,dat,mask,n, &
132 xmin,ymin,image,naxes(1),naxes(2),dx,dy,&
133 normalise=.false.,exact=.true.,periodicx=.false.,periodicy=.false.,iverbose=0)
134
135 enddo h_iterations
136
137 ! deallocate memory
138 deallocate(x,y,mask,weight,dat)
139
140 end subroutine image_denoise
141
142 !----------------------------------------------------------------------
143 !
144 ! denoise an image using adaptive kernel convolution in 3D
145 !
146 !----------------------------------------------------------------------
147 subroutine image_denoise3D(naxes,image,hh,iterations,imax,beam,err)
148 use interpolations3D, only:interpolate3D
149 integer, intent(in) :: naxes(3)
150 real, intent(inout) :: image(naxes(1),naxes(2),naxes(3))
151 real, intent(inout) :: hh(naxes(1)*naxes(2)*naxes(3))
152 integer, intent(in), optional :: iterations
153 real, intent(in), optional :: imax,beam
154 integer, intent(out), optional :: err
155 real, allocatable :: x(:),y(:),z(:),weight(:),dat(:)
156 integer, allocatable :: mask(:)
157 integer :: npixels,its,niter,i,j,k,n,ierr
158 real :: imagemax,dx,dy,dz,xmin,ymin,zmin,hfac
159 real(8), allocatable :: image_dp(:,:,:)
160
161 ! choose default kernel if not already set
162 if (.not.associated(wfunc)) call select_kernel(0)
163
164 ! allocate memory for temporary arrays
165 npixels = product(naxes)
166 allocate(x(npixels),y(npixels),z(npixels),weight(npixels),dat(npixels),mask(npixels),stat=ierr)
167 if (present(err)) err = ierr
168 if (ierr /= 0) return
169
170 if (size(hh) /= size(x)) then
171 print*,' error: smoothing length array too small'
172 ierr = 3
173 return
174 endif
175
176 dz = 1.
177 dy = 1.
178 dx = 1.
179 xmin = 0.0
180 ymin = 0.0
181 zmin = 0.0
182 mask(:) = 1 ! do not mask any pixels
183 if (present(imax)) then
184 imagemax = imax
185 else
186 imagemax = maxval(image)
187 endif
188
189 niter = 4
190 if (present(iterations)) niter = iterations
191
192 hfac = 1.0
193 if (present(beam)) hfac = beam
194
195 allocate(image_dp(naxes(1),naxes(2),naxes(3)))
196
197 ! find the convolution length by iteration
198 h_iterations: do its=1,max(niter,1)
199 n = 0
200
201 do k=1,naxes(3)
202 do j=1,naxes(2)
203 do i=1,naxes(1)
204 n = n + 1
205 !print*,n,size(z),size(hh),size(dat),i,j,k
206 x(n) = i - 0.5*dx
207 y(n) = j - 0.5*dy
208 z(n) = k - 0.5*dz
209 if (niter > 0) then
210 if (abs(image(i,j,k)) > 0.) then
211 hh(n) = min(max(0.4*hfac*sqrt(imagemax/abs(image(i,j,k))),1.),5.*(its+1)*sqrt(hfac))
212 else
213 hh(n) = 5.*(its+1)*sqrt(hfac)
214 endif
215 endif
216 if (its==1) dat(n) = image(i,j,k)
217 enddo
218 enddo
219 enddo
220 if (niter > 0) print "(' Iteration: ',i2,' of ',i2,': ',3(a,1g8.3))",its,niter,&
221 'beam: min = ',minval(hh),' max = ',maxval(hh),' mean = ',sum(hh(1:npixels))/npixels
222
223 weight(1:npixels) = dx*dy*dz/hh(1:npixels)**3
224 call interpolate3D(x,y,z,hh,weight,dat,mask,npixels, &
225 xmin,ymin,zmin,image_dp,naxes(1),naxes(2),naxes(3),dx,dy,dz,&
226 normalise=.false.,periodicx=.false.,periodicy=.false.,periodicz=.false.)
227
228 image = real(image_dp)
229 enddo h_iterations
230
231 ! deallocate memory
232 deallocate(x,y,z,mask,weight,dat)
233 if (allocated(image_dp)) deallocate(image_dp)
234
235 end subroutine image_denoise3D
236
237 !----------------------------------------------------------------------
238 !
239 ! rotate an image by a specified angle using kernel interpolation
240 !
241 !----------------------------------------------------------------------
242 subroutine image_rotate(naxes,image,angle,err)
243 use interpolations2D, only:interpolate2D
244 integer, intent(in) :: naxes(2)
245 real, intent(inout) :: image(naxes(1),naxes(2))
246 real, intent(in) :: angle
247 integer, intent(out), optional :: err
248 real, allocatable :: x(:),y(:),weight(:),dat(:),hh(:)
249 integer, allocatable :: mask(:)
250 integer :: npixels,i,j,n,ierr
251 real :: dx,dy,xmin,ymin,anglerad,xpos(2),x0,y0
252
253 ! choose default kernel if not already set
254 if (.not.associated(wfunc)) call select_kernel(0)
255
256 ! allocate memory for temporary arrays
257 npixels = product(naxes)
258 allocate(x(npixels),y(npixels),weight(npixels),dat(npixels),mask(npixels),hh(npixels),stat=ierr)
259 if (present(err)) err = ierr
260 if (ierr /= 0) return
261
262 x0 = 0.5*naxes(1)
263 y0 = 0.5*naxes(2)
264 dy = 1.
265 dx = 1.
266 xmin = -0.5*naxes(1)
267 ymin = -0.5*naxes(2)
268 mask(:) = 1 ! do not mask any pixels
269 hh(:) = 1.0 ! do not blur image
270 n = 0
271 do j=1,naxes(2)
272 do i=1,naxes(1)
273 n = n + 1
274 x(n) = xmin + (i-0.5)*dx
275 y(n) = ymin + (j-0.5)*dy
276 dat(n) = image(i,j)
277 enddo
278 enddo
279
280 ! convert angle to radians
281 anglerad = angle*pi/180.
282
283 ! rotate particles
284 do i=1,n
285 call rotatez(x(i),y(i),anglerad)
286 enddo
287
288 ! set weights for interpolation
289 weight(:) = 1.0 !dx*dy/hh(1:n)**2
290 ! perform interpolation of dat array to new set of pixels (image)
291 call interpolate2D(x,y,hh,weight,dat,mask,n, &
292 xmin,ymin,image,naxes(1),naxes(2),dx,dy,&
293 normalise=.false.,exact=.true.,periodicx=.false.,periodicy=.false.,iverbose=0)
294
295 ! deallocate memory
296 deallocate(x,y,mask,weight,dat)
297
298 end subroutine image_rotate
299
300 subroutine rotatez(x,y,anglez)
301 real, intent(inout) :: x,y
302 real, intent(in) :: anglez
303 real :: r, phi
304 !
305 !--rotate about z
306 !
307 r = sqrt(x**2 + y**2)
308 phi = atan2(y,x)
309 phi = phi - anglez
310 x = r*cos(phi)
311 y = r*sin(phi)
312
313 end subroutine rotatez
314
315 end module imageutils