comment here
This commit is contained in:
65
src/ImageUtils.jl
Normal file
65
src/ImageUtils.jl
Normal file
@@ -0,0 +1,65 @@
|
||||
module ImageUtils
|
||||
|
||||
export np2juliaImage, juliaImg2npImg, imgScalePadding, url_to_cv2_image
|
||||
|
||||
include("interface.jl")
|
||||
using .interface
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # imageUtils
|
||||
173
src/interface.jl
Normal file
173
src/interface.jl
Normal file
@@ -0,0 +1,173 @@
|
||||
module interface
|
||||
|
||||
export np2juliaImage, juliaImg2npImg, imgScalePadding, url_to_cv2_image
|
||||
|
||||
using Images, Colors
|
||||
using Luxor
|
||||
|
||||
using CondaPkg; CondaPkg.add_pip("pybase64"); CondaPkg.add_pip("opencv-python"); CondaPkg.add_pip("urllib3"); CondaPkg.add_pip("Pillow"); CondaPkg.add("numpy");
|
||||
using PythonCall
|
||||
# np = pyimport("numpy")
|
||||
# base64 = pyimport("pybase64")
|
||||
# cv = pyimport("cv2")
|
||||
# # equivalent to from urllib.request import urlopen in python
|
||||
# urlopen = pyimport("urllib.request" => "urlopen")
|
||||
|
||||
const py_np = PythonCall.pynew()
|
||||
const py_cv2 = PythonCall.pynew()
|
||||
const py_io = PythonCall.pynew()
|
||||
function __init__()
|
||||
PythonCall.pycopy!(py_np, pyimport("numpy"))
|
||||
PythonCall.pycopy!(py_cv2, pyimport("cv2"))
|
||||
|
||||
# equivalent to from urllib.request import urlopen in python
|
||||
PythonCall.pycopy!(py_io, pyimport("skimage" => "io"))
|
||||
end
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
"""
|
||||
get image from url, image in PythonCall python-obj numpy array
|
||||
"""
|
||||
function url_to_cv2_image(url)
|
||||
np_rgb_img = py_io.imread(url)
|
||||
cv2_bgr_img = py_cv2.cvtColor(np_rgb_img, py_cv2.COLOR_RGB2BGR)
|
||||
|
||||
julia_array_img = pyconvert(Array, cv2_bgr_img)
|
||||
julia_rgb_img = np2juliaImage(julia_array_img)
|
||||
return julia_rgb_img, cv2_bgr_img
|
||||
end
|
||||
|
||||
|
||||
"""
|
||||
Convert "julia array FROM opencv numpy array image" into RGB image
|
||||
|
||||
# Example
|
||||
|
||||
julia> using CondaPkg; CondaPkg.add("opencv");
|
||||
julia> using PythonCall
|
||||
julia> cv2 = pyimport("cv2") # import opencv
|
||||
|
||||
julia> img_cv2 = cv2.imread("20.jpg") # julia's PythonCall python-obj numpy array
|
||||
julia> img_julia_array = pyconvert(Array, img_cv2) # julia array but in numpy's row-major format
|
||||
julia> img_julia_rgb = np2juliaImage(img_julia_array) # julia RGB image
|
||||
"""
|
||||
np2juliaImage(img::AbstractArray) = RGB.(reinterpretc(BGR{N0f8}, PermutedDimsArray(img, (3, 1, 2))))
|
||||
|
||||
|
||||
"""
|
||||
Convert julia RGB image to numpy-ready-julia-array BGR image
|
||||
|
||||
# Example
|
||||
|
||||
julia> using Images
|
||||
|
||||
julia> img = load("20.jpg") # julia RGB image
|
||||
julia> img_bgr = juliaImg2npImg(img) # ready to use with python numpy
|
||||
|
||||
# After getting img_bgr, get python's numpy array in pycall obj
|
||||
julia> using CondaPkg; CondaPkg.add("numpy");
|
||||
julia> using PythonCall
|
||||
julia> np = pyimport("numpy")
|
||||
julia> img_np = np.array(img_bgr) # julia's PythonCall python-obj numpy array can be passed to PythonCall's python function
|
||||
"""
|
||||
function juliaImg2npImg(img_julia_RGB::Matrix{RGB{N0f8}})
|
||||
|
||||
# julia image use 0-1 color range but python's opencv use 0-255 color range
|
||||
img_rgb2 = img_julia_RGB .* 255;
|
||||
imgch = channelview(img_rgb2);
|
||||
imgch = Int.(imgch) # opencv use Integer
|
||||
img_permuted = PermutedDimsArray(imgch, (2, 3, 1));
|
||||
|
||||
# build BGR image from RGB image because opencv use BGR format
|
||||
img_bgr = [
|
||||
img_permuted[:,:,3];;; # blue
|
||||
img_permuted[:,:,2];;; # green
|
||||
img_permuted[:,:,1] # red
|
||||
];
|
||||
|
||||
return img_bgr
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
"""
|
||||
Scale image to 1 specific dimension with padding. The longest side of an image will be used as
|
||||
primary dimension.
|
||||
"""
|
||||
function imgScalePadding(img::T, dimension::Integer, paddingType=nothing) where T <:DenseMatrix{RGB{N0f8}}
|
||||
w, h = size(img) # width = horizonal length, high = vertical length
|
||||
primaryDimension = w > h ? w : h
|
||||
dim = primaryDimension == w ? 1 : 2
|
||||
|
||||
percentage_scale = dimension / size(img)[dim];
|
||||
new_size = trunc.(Int, size(img) .* percentage_scale);
|
||||
img_rescaled = imresize(img, new_size);
|
||||
|
||||
return img_rescaled
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # interface
|
||||
Reference in New Issue
Block a user