我是 Julia 編程的初學者,我想實作 Sobel 運算子。不幸的是,以下代碼的輸出只是一個黑色影像。
using Images, Colors, FileIO, Plots;
img = load("Lenna.png");
img_gray = Gray.(img);
sobel_image = convert(Array{Float64}, img_gray);
kernel_x = Array{Float64}([1 0 -1; 2 0 -2; 1 0 -1]);
kernel_y = Array{Float64}([1 2 1; 0 0 0; -1 -2 -1]);
#plot(img_gray)
function sobel(img)
edge_img = zeros(Gray{Float64}, size(img, 1), size(img, 2));
for x in 1:size(edge_img, 1) - size(kernel_x,1)
for y in 1:size(edge_img, 2) - size(kernel_y,2)
gx = sum(Gray{Float64}.(
@view edge_img[x:x size(kernel_x,1)-1, y:y size(kernel_y,2)-1]) .* kernel_x)
gy = sum(Gray{Float64}.(
@view edge_img[x:x size(kernel_x,1)-1, y:y size(kernel_y,2)-1]) .* kernel_y)
edge_img[x 1, y 1] = hypot(gx, gy)
end
end
return edge_img;
end
last_image = sobel(sobel_image)
plot(last_image)
我將kernel_x和轉換kernel_y為 Array64。基本思想是將代碼從 python 轉換為 Julia,因為我不知道語法。你能幫我一些提示嗎?謝謝!
uj5u.com熱心網友回復:
您的代碼可能會使用一些編輯(不必要的型別呼叫、全域變數等),但我認為這與錯誤結果無關。
看起來你的sobel函式接受 an img,但它只在第一行使用它來創建一個 black (all 0s) edge_img,然后只對edge_img函式的其余部分起作用。我認為這就是為什么你最終得到一張黑色影像的原因。也許你從意味著指數值img在gx = ...與gy = ...線?
uj5u.com熱心網友回復:
您可以嘗試使用ImageFiltering:
using ImageFiltering, TestImages, Plots
im = testimage("cameraman")
kernel = [1 0 -1; 2 0 -2;1 0 -1]
ims = imfilter(im, kernel)
Plots.plot(hcat(im, ims))

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/391212.html
上一篇:RoR路由的命名空間設定
