Skip to content

Commit

Permalink
Merge pull request #47 from timholy/kms/box_annotation
Browse files Browse the repository at this point in the history
Added box annotations.
  • Loading branch information
kmsquire committed Sep 19, 2014
2 parents 4f86054 + 963f216 commit 9563355
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ idx = ImageView.annotate!(imgc, img2, ImageView.AnnotationText(x, y, "x", color=
idx2 = ImageView.annotate!(imgc, img2, ImageView.AnnotationPoint(x+10, y, shape='.', size=4, color=RGB(1,0,0)))
idx3 = ImageView.annotate!(imgc, img2, ImageView.AnnotationPoint(x+20, y-6, shape='.', size=1, color=RGB(1,0,0), linecolor=RGB(0,0,0), scale=true))
idx4 = ImageView.annotate!(imgc, img2, ImageView.AnnotationLine(x+10, y, x+20, y-6, linewidth=2, color=RGB(0,1,0)))
idx5 = ImageView.annotate!(imgc, img2, ImageView.AnnotationBox(x+10, y, x+20, y-6, linewidth=2, color=RGB(0,0,1)))
ImageView.delete!(imgc, idx)
```

Expand Down Expand Up @@ -267,6 +268,22 @@ Properties:
* `coord_order` - for matrix or coordinate inputs, the order of the coordinates (e.g., "xyxy", "xxyy", "yyxx")


```
AnnotationBox(left, top, right, bottom | (x1,y1), (x2,y2) | bb::Base.Graphics.BoundingBox;
z = NaN, t = NaN,
color = RGB(1,1,1), linewidth=1.0, coord_order="xyxy")
```

Draw a box. Box can be specified using four values for `(left, top, right, bottom)`, as a pair of tuples, `(x1,y1),(x2,y2)`, or as a `BoundingBox`. The coordinate order the pair of tuples may be specified by `coord_order`, which defaults to "xyxy".

Properties:

* `z` - position on z axis, for 3D images
* `t` - position on time axis, for movie-like images
* `color`
* `linewidth` - width of the lines


## Additional notes

### Calling view from a script file
Expand Down
5 changes: 5 additions & 0 deletions src/ImageView.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ include("contrast.jl")
include("display.jl")

export # types
AnnotationPoint,
AnnotationPoints,
AnnotationLine,
AnnotationLines,
AnnotationBox,
AnnotationText,
AnnotationScalebarFixed,
# display functions
Expand Down
52 changes: 51 additions & 1 deletion src/annotations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type AnnotationScalebarFixed{T}
end
# AnnotationScalebar{T}(width::T, height::T, getsize::Function, centerx::Real, centery::Real, color::ColorValue = RGB(1,1,1)) = AnnotationScalebar{T}(width, height, getsize, float64(centerx), float64(centery), color)

## Text annotations

type AnnotationText
x::Float64
y::Float64
Expand Down Expand Up @@ -62,6 +64,9 @@ end

fontdescription(fontfamily, fontoptions, fontsize) = string(fontfamily, " ", fontoptions, " ", fontsize)


## Point annotations

type AnnotationPoints{R<:Union(Real,(Real,Real)),T<:Union(R,Vector{R},Matrix{R})}
pts::T
z::Float64
Expand All @@ -82,6 +87,7 @@ AnnotationPoint(xy::(Real,Real); z = NaN, t = NaN, size=10.0, shape::Char='x', c

AnnotationPoint(x::Real, y::Real; args...) = AnnotationPoint((float64(x), float64(y)); args...)

## Line annotations

type AnnotationLines{R<:Union(Real,(Real,Real)),T<:Union((R,R),Vector{(R,R)},Matrix{R})}
lines::T
Expand Down Expand Up @@ -112,9 +118,36 @@ function AnnotationLine(c1::Real, c2::Real, c3::Real, c4::Real; coord_order="xyx
AnnotationLine((float64(x1),float64(y1)),(float64(x2),float64(y2)); args...)
end

## Box annotations

type AnnotationBox
left::Float64
top::Float64
right::Float64
bottom::Float64
z::Float64
t::Float64
linecolor::ColorValue
linewidth::Float64
end

function AnnotationBox(c1::Real, c2::Real, c3::Real, c4::Real; z = NaN, t = NaN, color=RGB(1,1,1), linewidth=1.0, coord_order="xyxy")
ord = sortperm(coord_order.data)
@assert coord_order[ord] == "xxyy"
(x1,x2,y1,y2) = [c1,c2,c3,c4][ord]
(x1,x2) = minmax(x1,x2)
(y1,y2) = minmax(y1,y2)
AnnotationBox(x1,y1,x2,y2, z, t, color, linewidth)
end

AnnotationBox(pt1::(Real,Real), pt2::(Real,Real); coord_order="xyxy", args...) = AnnotationBox(pt1..., pt2...; coord_order=coord_order, args...)
AnnotationBox(bb::Base.Graphics.BoundingBox; args...) = AnnotationBox(bb.xmin, bb.ymin, bb.xmax, bb.ymax; args...)

##############

setvalid!(ann::AnchoredAnnotation, z, t) = (ann.valid = annotation_isvalid(ann.data, z, t))

annotation_isvalid(dat::Union(AnnotationText, AnnotationPoints, AnnotationLines), z, t) =
annotation_isvalid(dat::Union(AnnotationText, AnnotationPoints, AnnotationLines, AnnotationBox), z, t) =
(isnan(dat.z) || round(dat.z) == z) && (isnan(dat.t) || round(dat.t) == t)

annotation_isvalid(x, z, t) = true
Expand Down Expand Up @@ -272,3 +305,20 @@ function draw_line(ctx::CairoContext, line::(Real,Real,Real,Real))
line_to(ctx, x2,y2)
stroke(ctx)
end

## Box

function draw_anchored(ctx::CairoContext, data::AnnotationBox, args...)
set_line_width(ctx, data.linewidth)
set_source(ctx, data.linecolor)
draw_box(ctx, data.top, data.bottom, data.left, data.right)
end

function draw_box(ctx::CairoContext, top, bottom, left, right)
move_to(ctx, left, top)
line_to(ctx, right, top)
line_to(ctx, right, bottom)
line_to(ctx, left, bottom)
line_to(ctx, left, top)
stroke(ctx)
end
1 change: 1 addition & 0 deletions test/annotations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ idx = ImageView.annotate!(imgc, img2, ImageView.AnnotationText(x, y, "x", color=
idx2 = ImageView.annotate!(imgc, img2, ImageView.AnnotationPoint(x+10, y, shape='.', size=4, color=RGB(1,0,0)))
idx3 = ImageView.annotate!(imgc, img2, ImageView.AnnotationPoint(x+20, y-6, shape='.', size=1, color=RGB(1,0,0), linecolor=RGB(0,0,0), scale=true))
idx4 = ImageView.annotate!(imgc, img2, ImageView.AnnotationLine(x+10, y, x+20, y-6, linewidth=2, color=RGB(0,1,0)))
idx5 = ImageView.annotate!(imgc, img2, ImageView.AnnotationBox(x+10, y, x+20, y-6, linewidth=2, color=RGB(0,0,1)))
# Also test the following:
# ImageView.delete!(imgc, idx)

0 comments on commit 9563355

Please sign in to comment.