-
Notifications
You must be signed in to change notification settings - Fork 26
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
Change julia array to C++ vector #27
base: master
Are you sure you want to change the base?
Conversation
src/OpenCV_Mat.jl
Outdated
@@ -194,18 +194,25 @@ function pixset(img, row::Int, col::Int, value) | |||
@cxx at_d(img, row, col, value); | |||
# RGB images (value:: std::vector<double>) | |||
elseif (cvtypeval(img) == CV_8SC3) | |||
cppvec = tostdvec(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is cppvec
passed next? I see that you pass colorvec
to pixset
and then convert the julia array to an std::vector
with tostdvec
. It seems then you should be passing it further e.g, @cxx at_vc(img, row, col, cppvec);
as so on to the other cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh no! I forgot to replace value
with cppvec
.
src/OpenCV_Mat.jl
Outdated
@@ -195,25 +195,25 @@ function pixset(img, row::Int, col::Int, value) | |||
# RGB images (value:: std::vector<double>) | |||
elseif (cvtypeval(img) == CV_8SC3) | |||
cppvec = tostdvec(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure still whether the conversion to std::vec
should be done in this function.
In any case, there is no need to repeat cppvec = tostdvec(value)
in every elseif statement.
Just state it once as shown below.
function pixset(img, row::Int, col::Int, value)
(row < 0 || col < 0 || row > rows(img) || col > cols(img)) ? throw(BoundsError()) : nothing
cppvec = tostdvec(value)
. . . code not shown . . .
elseif (cvtypeval(img) == CV_8SC3)
@cxx at_vc(img, row, col, cppvec);
elseif (cvtypeval(img) == CV_8UC3)
@cxx at_v3b(img, row, col, cppvec);
elseif (cvtypeval(img) == CV_16SC3)
@cxx at_v3s(img, row, col, cppvec);
elseif (cvtypeval(img) == CV_16UC3)
@cxx at_v3us(img, row, col, cppvec);
elseif (cvtypeval(img) == CV_32SC3)
@cxx at_v3i(img, row, col, cppvec);
elseif (cvtypeval(img) == CV_32FC3)
@cxx at_v3f(img, row, col, cppvec);
elseif (cvtypeval(img) == CV_64FC3)
@cxx at_v3d(img, row, col, cppvec);
else throw(ArgumentError("Image format not recognized!"))
end
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure still whether the conversion to std::vec should be done in this function
Yes, I think whether the conversion is made later on or before shouldn't make difference. But unexpectedly I found that the previous implementation led to the errors as mentioned above. Shall I add the details to reproduce the error ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please share the details which led to this error so we have it on record.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reproduce the error :
In test/runtests.jl
, import the following
using OpenCV
using Images
using Base.Test
using FileIO
In test/jl/tests.jl
, add the following test function
function test6()
println("test 6: Conversion of images from Images.jl to OpenCV Mat")
filename = joinpath(Pkg.dir("OpenCV"), "./test/images/mandrill.jpg")
image = FileIO.load(filename)
dst = convertToMat(image)
println("Image converted to Mat successfully")
end
test6()
src/OpenCV_Mat.jl
as in master branch.
In src/OpenCV_ImageSupport.jl
, the following function definition :
function convertToMat(image)
img = permuteddimsview(channelview(image), (2,3,1))
cd = Base.size(channelview(image))[1] > 3 ? 1 : 3
_rows = Base.size(image, 1)
_cols = Base.size(image, 2)
if (typeof(img[1,1,1].i) == UInt8)
if (cd < 3); mat = Mat(512, 512, CV_8UC1); end
if (cd == 3); mat = Mat(512, 512, CV_8UC3); end
elseif (typeof(img[1,1,1].i) == Float32)
if (cd < 3); mat = Mat(512, 512, CV_32FC1); end
if (cd == 3); mat = Mat(512, 512, CV_32FC3); end
elseif (typeof(img[1,1,1].i) == Float64)
if (cd < 3); mat = Mat(512, 512, CV_64FC1); end
if (cd == 3); mat = Mat(512, 512, CV_64FC3); end
else
throw(ArgumentError("Pixel format not supported!"))
end
if (cd < 3) # grayscale or binary
for j = 1:_rows # index row first (Mat is row-major order)
for k =1:_cols # index column second
# slow algorithm - will try to use pointer method (C++)!
pixset(mat, k, j, float(img[k,j,1].i))
end
end
end
if (cd == 3) # color (RGB) image
for j = 1:512 # index row first (Mat is row-major order)
for k =1:512 # index column second
colorvec = tostdvec([float(img[k,j,1].i),float(img[k,j,2].i),float(img[k,j,3].i)])
pixset(mat, k-1, j-1, colorvec) # -1 to have 0-indexing per C++
end
end
end
return(mat)
end
Encountered this bug while testing the test case 6.
With the earlier implementation of conversion of julia array to C++, I used to get the following error :
The present implementation solves the problem.