mat_conversion.jl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const CV_CN_MAX = 512
  2. const CV_CN_SHIFT = 3
  3. const CV_DEPTH_MAX = (1 << CV_CN_SHIFT)
  4. const CV_8U = 0
  5. const CV_8S = 1
  6. const CV_16U = 2
  7. const CV_16S = 3
  8. const CV_32S = 4
  9. const CV_32F = 5
  10. const CV_64F = 6
  11. const CV_MAT_DEPTH_MASK = (CV_DEPTH_MAX - 1)
  12. CV_MAT_DEPTH(flags) = ((flags) & CV_MAT_DEPTH_MASK)
  13. CV_MAKETYPE(depth,cn) = (CV_MAT_DEPTH(depth) + (((cn)-1) << CV_CN_SHIFT))
  14. CV_MAKE_TYPE = CV_MAKETYPE
  15. function cpp_to_julia(mat::CxxMat)
  16. rets = jlopencv_core_Mat_mutable_data(mat)
  17. if rets[2] == CV_MAKE_TYPE(CV_8U, rets[3])
  18. dtype = UInt8
  19. elseif rets[2]==CV_MAKE_TYPE(CV_8S, rets[3])
  20. dtype = Int8
  21. elseif rets[2]==CV_MAKE_TYPE(CV_16U, rets[3])
  22. dtype = UInt16
  23. elseif rets[2]==CV_MAKE_TYPE(CV_16S, rets[3])
  24. dtype = Int16
  25. elseif rets[2]==CV_MAKE_TYPE(CV_32S, rets[3])
  26. dtype = Int32
  27. elseif rets[2]==CV_MAKE_TYPE(CV_32F, rets[3])
  28. dtype = Float32
  29. elseif rets[2]==CV_MAKE_TYPE(CV_64F, rets[3])
  30. dtype = Float64
  31. else
  32. error("Bad type returned from OpenCV")
  33. end
  34. steps = [rets[6]/sizeof(dtype), rets[7]/sizeof(dtype)]
  35. # println(steps[1]/rets[3], steps[2]/rets[3]/rets[4])
  36. #TODO: Implement views when steps do not result in continous memory
  37. arr = Base.unsafe_wrap(Array{dtype, 3}, Ptr{dtype}(rets[1].cpp_object), (rets[3], rets[4], rets[5]))
  38. #Preserve Mat so that array allocated by C++ isn't deallocated
  39. return Mat{dtype}(mat, arr)
  40. end
  41. function julia_to_cpp(img::InputArray)
  42. if typeof(img) <: CxxMat
  43. return img
  44. end
  45. steps = 0
  46. try
  47. steps = strides(img)
  48. catch
  49. # Copy array since array is not strided
  50. img = img[:, :, :]
  51. steps = strides(img)
  52. end
  53. if steps[1] <= steps[2] <= steps[3] && steps[1]==1
  54. steps_a = Array{size_t, 1}()
  55. ndims_a = Array{Int32, 1}()
  56. sz = sizeof(eltype(img))
  57. push!(steps_a, UInt64(steps[3]*sz))
  58. push!(steps_a, UInt64(steps[2]*sz))
  59. push!(steps_a, UInt64(steps[1]*sz))
  60. push!(ndims_a, Int32(size(img)[3]))
  61. push!(ndims_a, Int32(size(img)[2]))
  62. if eltype(img) == UInt8
  63. return CxxMat(2, pointer(ndims_a), CV_MAKE_TYPE(CV_8U, size(img)[1]), Ptr{Nothing}(pointer(img)), pointer(steps_a))
  64. elseif eltype(img) == UInt16
  65. return CxxMat(2, pointer(ndims_a), CV_MAKE_TYPE(CV_16U, size(img)[1]), Ptr{Nothing}(pointer(img)), pointer(steps_a))
  66. elseif eltype(img) == Int8
  67. return CxxMat(2, pointer(ndims_a), CV_MAKE_TYPE(CV_8S, size(img)[1]), Ptr{Nothing}(pointer(img)), pointer(steps_a))
  68. elseif eltype(img) == Int16
  69. return CxxMat(2, pointer(ndims_a), CV_MAKE_TYPE(CV_16S, size(img)[1]), Ptr{Nothing}(pointer(img)), pointer(steps_a))
  70. elseif eltype(img) == Int32
  71. return CxxMat(2, pointer(ndims_a), CV_MAKE_TYPE(CV_32S, size(img)[1]), Ptr{Nothing}(pointer(img)), pointer(steps_a))
  72. elseif eltype(img) == Float32
  73. return CxxMat(2, pointer(ndims_a), CV_MAKE_TYPE(CV_32F, size(img)[1]), Ptr{Nothing}(pointer(img)), pointer(steps_a))
  74. elseif eltype(img) == Float64
  75. return CxxMat(2, pointer(ndims_a), CV_MAKE_TYPE(CV_64F, size(img)[1]), Ptr{Nothing}(pointer(img)), pointer(steps_a))
  76. end
  77. else
  78. # Copy array, invalid config
  79. return julia_to_cpp(img[:, :, :])
  80. end
  81. end
  82. function julia_to_cpp(var::Array{T, 1}) where {T <: InputArray}
  83. ret = CxxWrap.StdVector{CxxMat}()
  84. for x in var
  85. push!(ret, julia_to_cpp(x))
  86. end
  87. return ret
  88. end
  89. function cpp_to_julia(var::CxxWrap.StdVector{T}) where {T <: CxxMat}
  90. ret = Array{Mat, 1}()
  91. for x in var
  92. push!(ret, cpp_to_julia(x))
  93. end
  94. return ret
  95. end