LoadError: DimensionMismatch

Hi everyone,

I was trying to rewrite the following python code:

import numpy as np
import cv2 
import matplotlib.pyplot as plt

img = cv2.imread('/home/julia_Hands_on/cat3.jpg')
img_tinted = img * [1, 0.95, 0.9]
# Show the original image
plt.subplot(1, 2, 1)

# Show the tinted image
plt.subplot(1, 2, 2)

In julia, I wrote it as:

using Images, ImageView, Plots
img_tinted = img * [1, 0.95, 0.9]

but, it gives the following error:

ERROR: LoadError: DimensionMismatch("matrix A has dimensions (214,393), vector B has length 3")
 [1] generic_matvecmul!(::Array{RGB{Float64},1}, ::Char, ::Array{RGB{Normed{UInt8,8}},2}, ::Array{Float64,1}, ::LinearAlgebra.MulAddMul{true,true,Bool,Bool}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/LinearAlgebra/src/matmul.jl:609
 [2] mul! at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/LinearAlgebra/src/matmul.jl:81 [inlined]
 [3] mul! at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/LinearAlgebra/src/matmul.jl:203 [inlined]
 [4] *(::Array{RGB{Normed{UInt8,8}},2}, ::Array{Float64,1}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/LinearAlgebra/src/matmul.jl:51
 [5] top-level scope at /home/g2-test/julia_Hands_on/python_julia/numpy_conversion.jl:56
 [6] include at ./boot.jl:328 [inlined]
 [7] include_relative(::Module, ::String) at ./loading.jl:1105
 [8] include(::Module, ::String) at ./Base.jl:31
 [9] include(::String) at ./client.jl:424
 [10] top-level scope at REPL[1]:1
in expression starting at /home/g2-test/julia_Hands_on/python_julia/numpy_conversion.jl:3

The python code ran without any errors. Why is it showing error for julia? Can someone help me.

I assume the 3 values are RGB channels? but how do you expect a 214x393 matrix to multiply with a 1x3 vector?

No, it is [1, 0.95, 0.9].

Then, why python is running and giving output?

well I know it is [1, 0.95, 0.9], but do they represent scaling on RBG channel? otherwise I don’t understand why you would multiply image pixels by 3 random number.

because img in python is probably not just a 2D matrix? Again, no language would know how to multiply a 214x393 matrix by a 1x3 vector out-of-box.

now, if your 1x3 vector represents scaling of different RGB channel, then you want to try using this

julia> img = testimage("mandrill")
julia> size(img)
(512, 512)

julia> size(img |> channelview)
(3, 512, 512)

julia> img_tinted = channelview(img) .* [1, 0.95, 0.9]


1 Like