Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Padding issue #1059

Open
hcfugro opened this issue Jul 4, 2024 · 2 comments
Open

Padding issue #1059

hcfugro opened this issue Jul 4, 2024 · 2 comments

Comments

@hcfugro
Copy link

hcfugro commented Jul 4, 2024

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.

@kimikage

This comment was marked as outdated.

@kimikage
Copy link

kimikage commented Jul 5, 2024

sobel_x_kernel is not a Matrix.

julia> sobel_x_kernel = [[-0.125, 0.0, 0.125]; [0.0, 0.0, 0.0]; [-0.125, 0.0, 0.125]]
9-element Vector{Float64}:
 -0.125
  0.0
  0.125
  0.0
  0.0
  0.0
 -0.125
  0.0
  0.125

Did you mean:

julia> [-0.125 0.0 0.125; 0.0 0.0 0.0; -0.125 0.0 0.125]
3×3 Matrix{Float64}:
 -0.125  0.0  0.125
  0.0    0.0  0.0
 -0.125  0.0  0.125

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants