2016-10-26 2 views
3

나는 줄리아에서 회색 음영 이미지가 있고 이미지에 직선을 그려야합니다. 두 쌍의 좌표가 있습니다. 그것들은 라인이 시작하고 끝나는 곳의 시작 (x1, y1)과 끝 (x2, y2) 픽셀 위치를 나타냅니다. 색칠 할 필요가있는 두 점 사이에있는 픽셀 위치를 찾는 방법을 잘 모르겠습니다. 따라서 이미지에 내 선이 나타나야합니다.줄리아에서 회색 음영 이미지의 두 픽셀 사이에 선 그리기

예를 들어 대화식 도구 나 주석을 사용하여이를 수행하고 싶지는 않습니다. 이미지에 지정된 정확한 좌표를 기반으로 많은 이미지에서이 작업을 수행해야하기 때문입니다.

내 코드는 지금까지 다음과 같습니다 : 나는 Interpolation.jl보고 여기와 블로그 등 몇 가지 이미지 처리 게시물하지만이 작업을 얻이 수없는 것 해봤

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) 

.

내가 가진 내가 파이썬 코드 here을 발견하고 쉽게 줄리아에 대한 수정 할 수 있었다 Greyscale version of this

+1

[Bresenham 's line algorithm] (https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm) –

답변

5

감사 Tasos Papastylianou An image which I convert to greyscale 내가 원하는 무엇 (색상 무시) (색상 무시) 무엇 :

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) 
+0

도와 드리겠습니다. 그것은 아름다운 알고리즘입니다, 그렇지 않습니까? :) –

관련 문제