Skip to content

Commit 16883e5

Browse files
committed
integral_image/boxdiff -> IntegralArrays
1 parent cfe077d commit 16883e5

File tree

6 files changed

+64
-110
lines changed

6 files changed

+64
-110
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ ImageQualityIndexes = "2996bd0c-7a13-11e9-2da2-2f5ce47296a9"
2020
ImageShow = "4e3cecfd-b093-5904-9786-8bbb286a6a31"
2121
ImageTransformations = "02fcd773-0e25-5acc-982a-7f6622650795"
2222
IndirectArrays = "9b13fd28-a010-5f03-acff-a1bbcff69959"
23+
IntegralArrays = "1d092043-8f09-5a30-832f-7509e371ab51"
2324
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
2425
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2526
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
@@ -46,6 +47,7 @@ ImageQualityIndexes = "0.1.3, 0.2"
4647
ImageShow = "0.3"
4748
ImageTransformations = "0.9"
4849
IndirectArrays = "0.5"
50+
IntegralArrays = "0.1"
4951
OffsetArrays = "0.8, 0.9, 0.10, 0.11, 1.0.1"
5052
Reexport = "1.1"
5153
StaticArrays = "0.8, 0.9, 0.10, 0.11, 0.12, 1.0"

src/Images.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ using ImageTransformations.Interpolations
2525
@reexport using ImageDistances
2626
@reexport using ImageContrastAdjustment
2727
@reexport using ImageQualityIndexes
28+
@reexport using IntegralArrays
29+
@reexport using IntegralArrays.IntervalSets
2830

2931
# Non-exported symbol bindings to ImageShow so that we can use, e.g., `Images.gif`
3032
import ImageShow: play, explore, gif
@@ -106,8 +108,6 @@ export
106108
thin_edges_nonmaxsup,
107109
thin_edges_nonmaxsup_subpix,
108110
canny,
109-
integral_image,
110-
boxdiff,
111111
gaussian_pyramid,
112112

113113
# phantoms
@@ -126,7 +126,7 @@ Contrast/coloration:
126126
127127
Algorithms:
128128
129-
- Reductions: `maxfinite`, `maxabsfinite`, `minfinite`, `meanfinite`, `integral_image`, `boxdiff`, `gaussian_pyramid`
129+
- Reductions: `maxfinite`, `maxabsfinite`, `minfinite`, `meanfinite`, `IntegralArray`, `gaussian_pyramid`
130130
- Resizing: `restrict`, `imresize` (not yet exported)
131131
- Filtering: `imfilter`, `imfilter!`, `mapwindow`, `imROF`, `padarray`
132132
- Filtering kernels: `Kernel.` or `KernelFactors.`, followed by `ando[345]`, `guassian`, `Laplacian', `DoG`, `prewitt`, `sobel`, etc.

src/algorithms.jl

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -216,62 +216,6 @@ end
216216
dilate(img::ImageMeta, region=coords_spatial(img)) = shareproperties(img, dilate!(copy(arraydata(img)), region))
217217
erode(img::ImageMeta, region=coords_spatial(img)) = shareproperties(img, erode!(copy(arraydata(img)), region))
218218

219-
"""
220-
```
221-
integral_img = integral_image(img)
222-
```
223-
224-
Returns the integral image of an image. The integral image is calculated by assigning
225-
to each pixel the sum of all pixels above it and to its left, i.e. the rectangle from
226-
(1, 1) to the pixel. An integral image is a data structure which helps in efficient
227-
calculation of sum of pixels in a rectangular subset of an image. See `boxdiff` for more
228-
information.
229-
"""
230-
function integral_image(img::AbstractArray)
231-
integral_img = Array{accum(eltype(img))}(undef, size(img))
232-
sd = coords_spatial(img)
233-
cumsum!(integral_img, img, dims=sd[1])
234-
for i = 2:length(sd)
235-
cumsum!(integral_img, integral_img, dims=sd[i])
236-
end
237-
integral_img
238-
end
239-
240-
"""
241-
```
242-
sum = boxdiff(integral_image, ytop:ybot, xtop:xbot)
243-
sum = boxdiff(integral_image, CartesianIndex(tl_y, tl_x), CartesianIndex(br_y, br_x))
244-
sum = boxdiff(integral_image, tl_y, tl_x, br_y, br_x)
245-
```
246-
247-
An integral image is a data structure which helps in efficient calculation of sum of pixels in
248-
a rectangular subset of an image. It stores at each pixel the sum of all pixels above it and to
249-
its left. The sum of a window in an image can be directly calculated using four array
250-
references of the integral image, irrespective of the size of the window, given the `yrange` and
251-
`xrange` of the window. Given an integral image -
252-
253-
A - - - - - - B -
254-
- * * * * * * * -
255-
- * * * * * * * -
256-
- * * * * * * * -
257-
- * * * * * * * -
258-
- * * * * * * * -
259-
C * * * * * * D -
260-
- - - - - - - - -
261-
262-
The sum of pixels in the area denoted by * is given by S = D + A - B - C.
263-
"""
264-
boxdiff(int_img::AbstractArray{T, 2}, y::UnitRange, x::UnitRange) where {T} = boxdiff(int_img, y.start, x.start, y.stop, x.stop)
265-
boxdiff(int_img::AbstractArray{T, 2}, tl::CartesianIndex, br::CartesianIndex) where {T} = boxdiff(int_img, tl[1], tl[2], br[1], br[2])
266-
267-
function boxdiff(int_img::AbstractArray{T, 2}, tl_y::Integer, tl_x::Integer, br_y::Integer, br_x::Integer) where T
268-
sum = int_img[br_y, br_x]
269-
sum -= tl_x > 1 ? int_img[br_y, tl_x - 1] : zero(T)
270-
sum -= tl_y > 1 ? int_img[tl_y - 1, br_x] : zero(T)
271-
sum += tl_y > 1 && tl_x > 1 ? int_img[tl_y - 1, tl_x - 1] : zero(T)
272-
sum
273-
end
274-
275219
"""
276220
```
277221
pyramid = gaussian_pyramid(img, n_scales, downsample, sigma)

src/deprecations.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,3 +1138,9 @@ function std(A::AbstractArray{C}; dims=nothing, kwargs...) where C<:Colorant
11381138
Base.depwarn("`std(A::AbstractArray{<:Colorant})` is deprecated (and not recommended, use a MathTypes colorspace instead), please use `colorview($Cbase, std(channelview(A); dims=$newdims))[]` instead.", :var)
11391139
return colorview(Cbase, std(channelview(A); dims=newdims, kwargs...))[]
11401140
end
1141+
1142+
# Integral arrays
1143+
@deprecate integral_image(A) IntegralArray(A)
1144+
@deprecate boxdiff(Ai::IntegralArray{T,2}, y::UnitRange, x::UnitRange) where T Ai[first(y)..last(y), first(x)..last(x)]
1145+
@deprecate boxdiff(Ai::IntegralArray{T,2}, tl::CartesianIndex, br::CartesianIndex) where T Ai[tl[1]..br[1], tl[2]..br[2]]
1146+
@deprecate boxdiff(Ai::IntegralArray{T,2}, tl_y::Integer, tl_x::Integer, br_y::Integer, br_x::Integer) where T Ai[tl_y..br_y, tl_x..br_x]

test/algorithms.jl

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -51,57 +51,6 @@ using Test, Suppressor
5151
@test Images.test_approx_eq_sigma_eps(a[:,1:end-1], a[1:end-1,:], [3,3], 0.1) < 0.1
5252
@test_throws ErrorException Images.test_approx_eq_sigma_eps(a[:,1:end-1], a[1:end-1,:], [3,3], 0.01)
5353

54-
a = zeros(10, 10)
55-
int_img = integral_image(a)
56-
@test all(int_img == a)
57-
58-
a = ones(10,10)
59-
int_img = integral_image(a)
60-
chk = Array(1:10)
61-
@test all([vec(int_img[i, :]) == chk * i for i in 1:10])
62-
63-
int_sum = boxdiff(int_img, 1, 1, 5, 2)
64-
@test int_sum == 10.0
65-
int_sum = boxdiff(int_img, 1:5, 1:2)
66-
@test int_sum == 10.0
67-
int_sum = boxdiff(int_img, CartesianIndex((1, 1)), CartesianIndex((5, 2)))
68-
@test int_sum == 10.0
69-
int_sum = boxdiff(int_img, 1, 1, 2, 5)
70-
@test int_sum == 10.0
71-
int_sum = boxdiff(int_img, 1:2, 1:5)
72-
@test int_sum == 10.0
73-
int_sum = boxdiff(int_img, CartesianIndex((1, 1)), CartesianIndex((2, 5)))
74-
@test int_sum == 10.0
75-
int_sum = boxdiff(int_img, 4, 4, 8, 8)
76-
@test int_sum == 25.0
77-
int_sum = boxdiff(int_img, 4:8, 4:8)
78-
@test int_sum == 25.0
79-
int_sum = boxdiff(int_img, CartesianIndex((4, 4)), CartesianIndex((8, 8)))
80-
@test int_sum == 25.0
81-
82-
a = reshape(1:100, 10, 10)
83-
int_img = integral_image(a)
84-
@test int_img[diagind(int_img)] == Array([1, 26, 108, 280, 575, 1026, 1666, 2528, 3645, 5050])
85-
86-
int_sum = boxdiff(int_img, 1, 1, 3, 3)
87-
@test int_sum == 108
88-
int_sum = boxdiff(int_img, 1:3, 1:3)
89-
@test int_sum == 108
90-
int_sum = boxdiff(int_img, CartesianIndex((1, 1)), CartesianIndex((3, 3)))
91-
@test int_sum == 108
92-
int_sum = boxdiff(int_img, 1, 1, 5, 2)
93-
@test int_sum == 80
94-
int_sum = boxdiff(int_img, 1:5, 1:2)
95-
@test int_sum == 80
96-
int_sum = boxdiff(int_img, CartesianIndex((1, 1)), CartesianIndex((5, 2)))
97-
@test int_sum == 80
98-
int_sum = boxdiff(int_img, 4, 4, 8, 8)
99-
@test int_sum == 1400
100-
int_sum = boxdiff(int_img, 4:8, 4:8)
101-
@test int_sum == 1400
102-
int_sum = boxdiff(int_img, CartesianIndex((4, 4)), CartesianIndex((8, 8)))
103-
@test int_sum == 1400
104-
10554
img = zeros(70, 70)
10655
img[20:51, 20:51] .= 1
10756
pyramid = gaussian_pyramid(img, 3, 2, 1.0)

test/deprecated.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,57 @@
5959
@test bilinear_interpolation(img, 1.5, 4.5) == 19.0
6060
@test bilinear_interpolation(img, 1.5, 5.5) == 10.75
6161
end
62+
63+
@testset "boxdiff" begin
64+
a = zeros(10, 10)
65+
int_img = integral_image(a)
66+
@test all(int_img == a)
67+
68+
a = ones(10,10)
69+
int_img = integral_image(a)
70+
chk = Array(1:10)
71+
@test all([vec(int_img[i, :]) == chk * i for i in 1:10])
72+
73+
int_sum = boxdiff(int_img, 1, 1, 5, 2)
74+
@test int_sum == 10.0
75+
int_sum = boxdiff(int_img, 1:5, 1:2)
76+
@test int_sum == 10.0
77+
int_sum = boxdiff(int_img, CartesianIndex((1, 1)), CartesianIndex((5, 2)))
78+
@test int_sum == 10.0
79+
int_sum = boxdiff(int_img, 1, 1, 2, 5)
80+
@test int_sum == 10.0
81+
int_sum = boxdiff(int_img, 1:2, 1:5)
82+
@test int_sum == 10.0
83+
int_sum = boxdiff(int_img, CartesianIndex((1, 1)), CartesianIndex((2, 5)))
84+
@test int_sum == 10.0
85+
int_sum = boxdiff(int_img, 4, 4, 8, 8)
86+
@test int_sum == 25.0
87+
int_sum = boxdiff(int_img, 4:8, 4:8)
88+
@test int_sum == 25.0
89+
int_sum = boxdiff(int_img, CartesianIndex((4, 4)), CartesianIndex((8, 8)))
90+
@test int_sum == 25.0
91+
92+
a = reshape(1:100, 10, 10)
93+
int_img = integral_image(a)
94+
@test int_img[diagind(int_img)] == Array([1, 26, 108, 280, 575, 1026, 1666, 2528, 3645, 5050])
95+
96+
int_sum = boxdiff(int_img, 1, 1, 3, 3)
97+
@test int_sum == 108
98+
int_sum = boxdiff(int_img, 1:3, 1:3)
99+
@test int_sum == 108
100+
int_sum = boxdiff(int_img, CartesianIndex((1, 1)), CartesianIndex((3, 3)))
101+
@test int_sum == 108
102+
int_sum = boxdiff(int_img, 1, 1, 5, 2)
103+
@test int_sum == 80
104+
int_sum = boxdiff(int_img, 1:5, 1:2)
105+
@test int_sum == 80
106+
int_sum = boxdiff(int_img, CartesianIndex((1, 1)), CartesianIndex((5, 2)))
107+
@test int_sum == 80
108+
int_sum = boxdiff(int_img, 4, 4, 8, 8)
109+
@test int_sum == 1400
110+
int_sum = boxdiff(int_img, 4:8, 4:8)
111+
@test int_sum == 1400
112+
int_sum = boxdiff(int_img, CartesianIndex((4, 4)), CartesianIndex((8, 8)))
113+
@test int_sum == 1400
114+
end
62115
end

0 commit comments

Comments
 (0)