I am attempting an image convolution using a 3x3 kernel_test and am running into a challenge with returning the correct type so I can view an image back (after the convolution). My code is as below.
begin
function update_mat_val(org_clr::AbstractRGB, new_clr::AbstractRGB, n::Number)
r = reinterpret(N0f8, (red(org_clr) + (red(new_clr) * n)))
g = reinterpret(N0f8, (green(org_clr) + (green(new_clr) * n)))
b = reinterpret(N0f8, (blue(org_clr) + (blue(new_clr) * n)))
return RGB(r, g, b)
end
end
function convolve_image(M::AbstractMatrix, K::AbstractMatrix)
#M is the matrix, K is the kernel
(m_height, m_width) = size(M)
(k_height, k_width) = size(K)
conv_mk = zeros(typeof(M[1,1]), m_height+k_height-1, m_width+k_width-1)
(conv_height, conv_width) = size(conv_mk)
for i in 1:m_height, j in 1:m_width
for p in 1:k_height, q in 1:k_width
conv_mk[i+p-1,j+q-1] = update_mat_val(conv_mk[i+p-1,j+q-1], M[i,j], K[p,q])
end
end
return conv_mk
end
convolve_image(image, kernel_test)
Initially, I did not use the reinterpret method and the values were falling outside the 0-255 values that the RGB can hold. So after reading up a little online, I tried to use reinterpret. The error before I attempted to reinterpret and after are given in corresponding order below. How do I correctly process the values so I get an image back?
ArgumentError: element type FixedPointNumbers.Normed{UInt8,8} is an 8-bit type representing 256 values from 0.0 to 1.0,
but the values (0.9254901960784314, 0.9686274509803922, 1.0019607843137255) do not lie within this range.
See the READMEs for FixedPointNumbers and ColorTypes for more information.
throw_colorerror_(::Type{FixedPointNumbers.Normed{UInt8,8}}, ::Tuple{Float64,Float64,Float64})@types.jl:686
throw_colorerror(::Type{ColorTypes.RGB{FixedPointNumbers.Normed{UInt8,8}}}, ::Tuple{Float64,Float64,Float64})@types.jl:736
checkval@types.jl:654[inlined]
RGB@types.jl:101[inlined]
_convert@conversions.jl:87[inlined]
cconvert@conversions.jl:76[inlined]
convert@conversions.jl:73[inlined]
setindex!@array.jl:849[inlined]
convolve_image(::Array{ColorTypes.RGB{FixedPointNumbers.Normed{UInt8,8}},2}, ::Array{Float64,2})@Other: 12
top-level scope@Local: 1
bitcast: target type not a leaf primitive type
reinterpret@essentials.jl:414[inlined]
update_mat_val@Other: 3[inlined]
convolve_image(::Array{ColorTypes.RGB{FixedPointNumbers.Normed{UInt8,8}},2}, ::Array{Float64,2})@Other: 12
top-level scope@Local: 1