OpenCVではじめようディープラーニングによる画像認識 ch3
Mix.install(
[
{:evision, "~> 0.1.19"},
{:exla, "~> 0.4.0"},
{:kino, "~> 0.7.0"}
],
config: [
nx: [default_backend: EXLA.Backend]
]
)
ch3/3.1/blank_gray_image
setup
alias Evision, as: Ev
path = "/Users/shou/livebook_samples/images/yorkie.png"
width = 200
height = 100
value = 128.0
Ev.Mat.zeros({height, width}, :u8)
Nx.broadcast(value, {height, width})
|> Ev.Mat.from_nx()
ch3/3.1/blank_color_image
width = 200
height = 100
channel = 3
value = [0.0, 0.0, 255.0]
Ev.Mat.zeros({height, width}, :u8)
Nx.tensor(value)
|> Nx.broadcast({height, width, channel})
|> Evision.Mat.from_nx_2d()
ch3/3.1/read_pixel_value
x = 200
y = 100
channel = 0
img = Ev.imread(path)
img[[y, x]]
|> Ev.Mat.to_nx()
|> Nx.to_flat_list()
|> IO.inspect()
img[[y, x, channel]]
|> Ev.Mat.to_nx()
|> Nx.to_flat_list()
|> IO.inspect(charlists: :as_lists)
ch3/3.1/change_pixel_value
img = Ev.imread(path)
x = 200
y = 100
channel = 0
before = img[[y, x]] |> Ev.Mat.to_nx() |> Nx.to_flat_list() |> Enum.join(",")
IO.puts("before bgr_val(#{x}, #{y}) = #{before}")
# Evision Backendはslice系が未実装のためExla Backendに変換
img
|> Ev.Mat.to_nx(EXLA.Backend)
|> Nx.indexed_put(Nx.tensor([[y, x, 0], [y, x, 1], [y, x, 2]]), Nx.tensor([255, 255, 255]))
|> then(&IO.inspect(&1[y][x]))
before = img[[y, x, channel]] |> Ev.Mat.to_nx() |> Nx.to_flat_list() |> Enum.join(",")
IO.puts("before b_val(#{x}, #{y}) = #{before}")
img
|> Ev.Mat.to_nx(EXLA.Backend)
|> Nx.indexed_put(Nx.tensor([[y, x, channel]]), Nx.tensor([0]))
|> then(&IO.inspect(&1[y][x]))
ch3/3.1/image_shape_as_color
img = Ev.imread(path)
Ev.Mat.shape(img) |> IO.inspect()
Ev.Mat.type(img) |> IO.inspect()
ch3/3.1/image_shape_as_gray
img = Ev.imread(path, flags: Ev.cv_IMREAD_GRAYSCALE())
Ev.Mat.shape(img) |> IO.inspect()
Ev.Mat.type(img) |> IO.inspect()
ch3/3.1/image_size
img = Ev.imread(path)
{height, width, channel} = Ev.Mat.shape(img)
ch3/3.1/image_roi
img = Ev.imread(path)
img_roi = Ev.Mat.roi(img, [135..319, 150..290])
ch3/3.2/add1
x = Nx.tensor([250], type: :u8)
y = Nx.tensor([5], type: :u8)
z = Ev.add(x, y) |> Ev.Mat.to_nx()
ch3/3.2/add2
x = Nx.tensor([250], type: :u8)
y = Nx.tensor([10], type: :u8)
z = Ev.add(x, y) |> Ev.Mat.to_nx()
ch3/3.2/subtract1
x = Nx.tensor([10], type: :u8)
y = Nx.tensor([5], type: :u8)
z = Ev.subtract(x, y) |> Ev.Mat.to_nx()
ch3/3.2/subtract2
x = Nx.tensor([10], type: :u8)
y = Nx.tensor([20], type: :u8)
z = Ev.subtract(x, y) |> Ev.Mat.to_nx()
ch3/3.2/bitwise_and
src1 = Ev.imread(path)
src2 =
Nx.tensor([0, 0, 0], type: :u8)
|> Nx.broadcast(Ev.Mat.shape(src1))
|> Ev.Mat.from_nx_2d()
|> Ev.rectangle({150, 135}, {290, 315}, {255, 255, 255}, thickness: -1)
dst = Ev.Mat.bitwise_and(src1, src2)
ch3/3.2/bitwise_or
src1 = Ev.imread(path)
src2 =
Nx.tensor([0, 0, 0], type: :u8)
|> Nx.broadcast(Ev.Mat.shape(src1))
|> Ev.Mat.from_nx_2d()
|> Ev.rectangle({150, 135}, {290, 315}, {255, 255, 255}, thickness: -1)
dst = Ev.Mat.bitwise_or(src1, src2)
ch3/3.2/addWeighted
aloe_l = "/Users/shou/livebook_samples/images/aloeL.png"
aloe_r = "/Users/shou/livebook_samples/images/aloeR.png"
src1 = Ev.imread(aloe_l)
src2 = Ev.imread(aloe_r)
alpha = 0.5
beta = 0.5
gamma = 0.0
dat = Ev.addWeighted(src1, alpha, src2, beta, gamma)
ch3/3.2/absdiff
img1 = Nx.tensor([[1, 2, 3, 4, 5]], type: :u8) |> Ev.Mat.from_nx()
img2 = Nx.tensor([[5, 4, 3, 2, 1]], type: :u8) |> Ev.Mat.from_nx()
diff = Ev.absdiff(img1, img2)
ch3/3.2/copy_image
org_img = Ev.imread(path)
{w, h, ch} = Ev.Mat.shape(org_img)
mask = Ev.Mat.full({w, h}, 255, :u8)
cv_copy_img = Ev.copyTo(org_img, mask)
ch3/3.2/flip_image
src = Ev.imread(path)
data = Ev.flip(src, 0)
ch3/3.2/rotate_image
src = Ev.imread(path)
rotated = Ev.rotate(src, 0)
ch3/3.2/clipping
img = Nx.tensor([[0, 1, 2, 3, 4, 5, 6]], type: :u8) |> Ev.Mat.from_nx()
clip1 = Ev.Mat.clip(img, 0, 5)
# 挙動がおかしい
ch3/3.3/min_max
img = Nx.tensor([[1, 2, 3, 4, 5]], type: :u8)
Nx.reduce_max(img) |> IO.inspect()
Nx.reduce_min(img) |> IO.inspect()
ch3/3.3/minMaxLoc
img = Nx.tensor([[1, 2, 3], [4, 5, 6]], type: :u8) |> Ev.Mat.from_nx_2d()
{min_val, max_val, min_loc, max_loc} = Ev.minMaxLoc(img)
ch3/3.3/mean_value
img =
Nx.tensor([[1, 2, 3, 4, 5]])
|> Nx.mean()
ch3/3.3/sum_pixcel_value
img = Nx.tensor([[1, 2, 3, 4, 5]]) |> Nx.sum()
ch3/3.3/countNonZero
img = Nx.tensor([[1, 0, 0, 1, 1]], type: :u8) |> Ev.Mat.from_nx()
count = Ev.countNonZero(img)
ch3/3.3/check_same_image
is_same_image = fn img1, img2 ->
case Ev.absdiff(img1, img2) |> Ev.countNonZero() do
0 -> true
_ -> false
end
end
width = 200
height = 100
value1 = 120
value2 = 255
img1 = Ev.Mat.full({height, width}, value1, :u8)
img2 = Ev.Mat.full({height, width}, value2, :u8)
is_same_image.(img1, img1) |> IO.inspect()
is_same_image.(img1, img2) |> IO.inspect()
ch3/3.4/split_plane
img = Ev.imread(path)
[b_plane, g_plane, r_plane] = Ev.split(img)
b_plane
g_plane
b_plane
ch3/3.4/merge_plane
Ev.merge([b_plane, g_plane, r_plane])
ch3/3.4/hconcat
img1 = Ev.imread(path)
img2 = Ev.flip(img1, 0)
hconcat_img = Ev.hconcat([img1, img2])
ch3/3.4/hconcat_invalid
width1 = 200
height1 = 100
img1 = Ev.Mat.zeros({height1, width1}, :u8)
concat1 = Ev.hconcat([img1, img1])
IO.inspect(Ev.Mat.shape(concat1))
width2 = 200
height2 = 200
img2 = Ev.Mat.zeros({height2, width2}, :u8)
{:error, msg} = Ev.hconcat([img1, img2])
IO.puts(msg)
ch3/3.4/vconcat
img1 = Ev.imread(path)
img2 = Ev.flip(img1, 0)
vconcat_img = Ev.vconcat([img1, img2])
ch3/3.5/getBuildInformation
Ev.getBuildInformation() |> IO.puts()
ch3/3.5/measurement_time_once
img = Ev.imread(path)
timer = Ev.TickMeter.tickMeter()
Ev.TickMeter.start(timer)
gray = Ev.cvtColor(img, Ev.cv_COLOR_BGR2GRAY())
Ev.TickMeter.stop(timer)
measurement_time = Ev.TickMeter.getTimeMilli(timer)
ch3/3.5/measurement_time
img = Ev.imread(path)
timer = Ev.TickMeter.tickMeter()
for _i <- 1..5 do
Ev.TickMeter.reset(timer)
Ev.TickMeter.start(timer)
_gray = Ev.cvtColor(img, Ev.cv_COLOR_BGR2GRAY())
Ev.TickMeter.stop(timer)
IO.inspect(Ev.TickMeter.getTimeMilli(timer))
end
ch3/3.5/write_xml
filename = "output.xml"
fs = Ev.FileStorage.fileStorage(filename, 0)
# うまく動かない
ch3/3.5/read_xml
# skip
ch3/3.5/useOptimized
Ev.useOptimized()
ch3/3.5/setUseOptimized
Ev.setUseOptimized(true)
Ev.useOptimized() |> IO.inspect()
Ev.setUseOptimized(false)
Ev.useOptimized() |> IO.inspect()
Ev.setUseOptimized(true)