Skip to content

Padding issue #1059

@hcgitty

Description

@hcgitty

I'm trying to get gradient of an image matrix
Images v0.26.1
Julia Version 1.10.4

`using Images
#morph_image size is 301x301
sobel_x_kernel = [[-0.125, 0.0, 0.125]; [0.0, 0.0, 0.0]; [-0.125, 0.0, 0.125]]
sobel_y_kernel = transpose(sobel_x_kernel) # Transpose for y-direction

pad_size = 1 # Adjust pad size if needed

padded_image = Images.pad(morph_image, pad_size, pad_size)
gradient_x = imfilter(padded_image, sobel_x_kernel)
gradient_y = imfilter(padded_image, sobel_y_kernel) # Works without an issue
`

Here's a breakdown of the problem:

Expected behavior: When I apply padding or a background gradient to an image, the padding should create a space around the image content and the gradient should fill that space.
Actual behavior: Image remains the same size

Things I've tried:

I've verified that the padding and gradient styles are being applied to the correct element and that  image has a defined height and width.

M is 5x5 window of morph_image
julia> M

5×5 Matrix{Float64}:
1.0 11.0 11.0 11.0 11.0
1.0 11.0 11.0 11.0 11.0
1.0 11.0 11.0 11.0 11.0
1.0 1.0 11.0 11.0 11.0
1.0 1.0 1.0 1.0 11.0

julia> gradient_y = imfilter(M, sobel_y_kernel)

5×5 Matrix{Float64}:
0.0 0.0 0.0 1.25 1.25
0.0 0.0 0.0 1.25 1.25
0.0 0.0 0.0 1.25 1.25
0.0 0.0 0.0 0.0 1.25
1.25 1.25 0.0 0.0 0.0

julia> gradient_x = imfilter(M, sobel_x_kernel)

ERROR: ArgumentError: Pad{1}(:replicate, (4,), (4,)) lacks the proper padding sizes for an array with 2 dimensions
Stacktrace:
[1] BorderArray(inner::Matrix{Float64}, border::Pad{1})
@ ImageFiltering ~/.julia/packages/ImageFiltering/OyzRu/src/borderarray.jl:64
[2] padarray(::Type{Float64}, img::Matrix{Float64}, border::Pad{1})
@ ImageFiltering ~/.julia/packages/ImageFiltering/OyzRu/src/border.jl:326
[3] imfilter!(r::ComputationalResources.CPU1{…}, out::Matrix{…}, img::Matrix{…}, kernel::Tuple{…}, border::Pad{…})
@ ImageFiltering ~/.julia/packages/ImageFiltering/OyzRu/src/imfilter.jl:336
[4] imfilter!(r::ComputationalResources.CPU1{…}, out::Matrix{…}, img::Matrix{…}, kernel::Tuple{…}, border::Pad{…})
@ ImageFiltering ~/.julia/packages/ImageFiltering/OyzRu/src/imfilter.jl:327
[5] imfilter!(out::Matrix{…}, img::Matrix{…}, kernel::Tuple{…}, border::Pad{…}, alg::ImageFiltering.Algorithm.FIR)
@ ImageFiltering ~/.julia/packages/ImageFiltering/OyzRu/src/imfilter.jl:236
[6] imfilter!(out::Matrix{Float64}, img::Matrix{Float64}, kernel::Tuple{OffsetArrays.OffsetVector{Float64, Vector{Float64}}}, border::Pad{0})
@ ImageFiltering ~/.julia/packages/ImageFiltering/OyzRu/src/imfilter.jl:230
[7] imfilter
@ ~/.julia/packages/ImageFiltering/OyzRu/src/imfilter.jl:25 [inlined]
[8] imfilter(::Type{Float64}, ::Matrix{Float64}, ::Tuple{OffsetArrays.OffsetVector{Float64, Vector{Float64}}}, ::String)
@ ImageFiltering ~/.julia/packages/ImageFiltering/OyzRu/src/imfilter.jl:20
[9] imfilter(::Type{Float64}, ::Matrix{Float64}, ::Tuple{OffsetArrays.OffsetVector{Float64, Vector{Float64}}})
@ ImageFiltering ~/.julia/packages/ImageFiltering/OyzRu/src/imfilter.jl:16
[10] imfilter
@ ~/.julia/packages/ImageFiltering/OyzRu/src/imfilter.jl:8 [inlined]
[11] imfilter(::Matrix{Float64}, ::Vector{Float64})
@ ImageFiltering ~/.julia/packages/ImageFiltering/OyzRu/src/imfilter.jl:3
[12] top-level scope
@ REPL[314]:1

I also tried this:

SO I tried reflect but the test and reflected images They have the same size
test_image = rand(5, 5)  # Create a small test image
reflected_test_image = reflect(test_image)
println(size(test_image))
println(size(reflected_test_image))

Has anyone else experienced this issue? Any suggestions on how to troubleshoot or fix this would be greatly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions