3

Julia にグレースケールの画像があり、画像に直線を描きたいと思います。私は2組の座標を持っています。これらは、線の開始位置と終了位置の開始 (x1,y1) および終了 (x2,y2) ピクセル位置を表します。私の線が画像に表示されるように、これらの2つの点の間にあるピクセル位置を見つける方法がわかりません。

たとえば、画像に指定された正確な座標に基づいて多くの画像に対してこれを行う必要があるため、インタラクティブツールや注釈を使用してこれを行いたくありません。

私のコードはこれまでのところ次のようになります。

using Images, Colors, ImageView

function convert_rgb_image_to_greyscale(imagefilepath)
    img = load(imagefilepath)
    my_img_grey = convert(Image{Gray}, my_img)
    view(my_img_grey, pixelspacing = [1,1])

    return my_img_grey
end

imagefilepath = "myimage.jpg"
my_img_grey = convert_rgb_image_to_greyscale(imagefilepath)

start_pos = [1048 48] # (x1,y1)
end_pos = [1050 155] # (x2,y2)

Interpolation.jl と、ここやブログなどの画像処理の投稿を見てみましたが、うまくいかないようです。

私が持っているもの (色は無視してください)グレースケールに変換する画像 私が欲しいもの (色は無視してください)これのグレースケール版

4

1 に答える 1

5

Tasos Papastylianou に感謝しますここで Python コードを見つけ、Julia 用に簡単に変更できました。

function bresenhams_line_algorithm(x1::Int, y1::Int, x2::Int, y2::Int)
# Calculate distances
dx = x2 - x1
dy = y2 - y1

# Determine how steep the line is
is_steep = abs(dy) > abs(dx)

# Rotate line
if is_steep == true
    x1, y1 = y1, x1
    x2, y2 = y2, x2
end

# Swap start and end points if necessary and store swap state
swapped = false
if x1 > x2
    x1, x2 = x2, x1
    y1, y2 = y2, y1
    swapped = true
end
# Recalculate differentials
dx = x2 - x1
dy = y2 - y1

# Calculate error
error = round(Int, dx/2.0)

if y1 < y2
    ystep = 1
else
    ystep = -1
end

# Iterate over bounding box generating points between start and end
y = y1
points = []
for x in x1:(x2+1)
    if is_steep == true
        coord = (y, x)
    else
        coord = (x, y)
    end
    push!(points,coord)
    error -= abs(dy)

    if error < 0
        y += ystep
        error += dx
    end
end

# Reverse the list if the coordinates were swapped
if swapped == true
    points = points[end:-1:1]
end

    return points
end

# Small test
x1 = 0
y1 = 0
x2 = 5
y2 = 5

points = bresenhams_line_algorithm(x1, y1, x2, y2)
于 2016-10-27T01:46:53.693 に答える