Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display ROI #40

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft

Display ROI #40

wants to merge 4 commits into from

Conversation

jiviteshjain
Copy link

Adds the display_roi functionality.

Adds inset_roi that enlarges and displays a single ROI inside the image itself.

Some considerations:

  • Does not follow the usual x (first coordinate) refers to columns and y (second coordinate) refers to rows convention, because ROIs are more likely to be interpreted in terms of pixel indices, I assumed. Hence also does not use Point to denote the diagonal points of the rectangle so as to avoid confusion. Let me know if I assumed wrong.
  • Natively supports drawing borders with a different type of colorant, even though draw does not, because highlighting ROIs in color on gray images is fairly common.

The second commit e71f317 adds a function signature that could support showing up to 4 different ROIs inside the image along the 4 corners, let me know if something like that would be desirable.

Example

screenshot-julia

using Images, ImageDraw, TestImages
img = testimage("cameraman")
inset_roi(img, ((170, 170), (200, 300)), 2, ImageDraw.bottom_left, RGB(0, 1, 1))

@johnnychen94

@jiviteshjain jiviteshjain marked this pull request as draft July 20, 2020 19:36
@coveralls
Copy link

coveralls commented Jul 20, 2020

Coverage Status

Coverage decreased (-30.4%) to 66.486% when pulling d627648 on MLH-Fellowship:roi into 1b444e8 on JuliaImages:master.

Copy link
Member

@johnnychen94 johnnychen94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more convenient to make scale, align and color optional. With the thickness feature implemented #31, we could also expose a border_width keyword.

  • scale: we can infer scale so that maximum(size(img) ./ size(resize(img_roi, scale)) == 2, or prod(size(img)) / prod(size(resize(img_roi, scale))) == 6 (or 8)
  • align: we can infer the align to be the first option that doesn't introduce overlap.
  • color: it depends on personal preferences, but I guess most people won't be sensitive to whatever color we make as a default value.

Hence also does not use Point to denote the diagonal points of the rectangle so as to avoid confusion. Let me know if I assumed wrong.

I was always confused that Point(x, y) represents (second_dim, first_dim) 😕 We can file an issue for further discussion on whether we should change this.

IMO, It is less confusing to make roi::Union{NTuple{2, <:AbstractUnitRange}, CartesianIndices{2}}, how do you think?

src/roi.jl Outdated
Comment on lines 8 to 14
function inset_roi(
img::AbstractArray{T, 2},
roi::NTuple{2, NTuple{2, <:Int}},
scale::R,
align::AlignOptions,
color::C
) where {T<:Colorant, R<:Real, C<:Colorant}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If roi is the top-left corner of the image, and we set align = top_left, there can be some overlap here.

This won't be a big issue as people are using it in a try-and-see manner.

It would be more convenient to make align optional.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this

src/roi.jl Outdated
function inset_roi(
img::AbstractArray{T, 2},
roi::NTuple{2, NTuple{2, <:Int}},
scale::R,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to ensure scale is positive

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@jiviteshjain
Copy link
Author

jiviteshjain commented Jul 23, 2020

inset_roi now tries to place the ROI view automatically so as to not overlap with the actual ROI. The scale is inferred as well to occupy at most 1/6th of the image's area and not exceed the image's size.

Some considerations:

@johnnychen94

@jiviteshjain
Copy link
Author

It would be more convenient to make scale, align and color optional. With the thickness feature implemented #31, we could also expose a border_width keyword.

  • scale: we can infer scale so that maximum(size(img) ./ size(resize(img_roi, scale)) == 2, or prod(size(img)) / prod(size(resize(img_roi, scale))) == 6 (or 8)
  • align: we can infer the align to be the first option that doesn't introduce overlap.
  • color: it depends on personal preferences, but I guess most people won't be sensitive to whatever color we make as a default value.

These arguments are now optional. Is #31 ready to merge, or should I wait before rebasing?

I was always confused that Point(x, y) represents (second_dim, first_dim) We can file an issue for further discussion on whether we should change this.

I guess that is to keep x as the "horizontal" dimension and y as the "vertical". Can't say it is intuitive for me when thinking about arrays, but it may be in a more mathematical context.

IMO, It is less confusing to make roi::Union{NTuple{2, <:AbstractUnitRange}, CartesianIndices{2}}, how do you think?

Yes, done!

@jiviteshjain
Copy link
Author

What happened to the tests? Error while compiling Images doesn't make sense. I haven't changed anything in the tests yet, and they all passed locally 😨

Copy link
Member

@johnnychen94 johnnychen94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appropriate usage of CartesianIndex helps make codes simpler than you'd imagine, so I recommend you to take a look at this blog. And perhaps another example that I just wrote down yesterday: JuliaImages/ImageFiltering.jl#179

I've suggested some changes to your code so that you can use it as a start.

These arguments are now optional. Is #31 ready to merge, or should I wait before rebasing?

We don't need to wait for #31 given that it's inactive for a long time. Also, I don't think that code is nicely coded in Julia -- the API style. I've opened an issue #42 so that we can discuss this there.

Comment on lines +17 to +41
if typeof(roi) <: CartesianIndices{2}
sx = first(roi)[1]
sy = first(roi)[2]

ex = last(roi)[1]
ey = last(roi)[2]
else
sx = first(first(roi))
ex = last(first(roi))

sy = first(last(roi))
ey = last(last(roi))

end

if sx >= ex || sy >= ey
throw(ArgumentError("Region of interest must be an increasing range"))
end

if !checkindex(Bool, axes(img, 1), sx) ||
!checkindex(Bool, axes(img, 1), ex) ||
!checkindex(Bool, axes(img, 2), sy) ||
!checkindex(Bool, axes(img, 2), ey)
throw(ArgumentError("Region of interest lies out of image"))
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CartesianIndex helps handle N-D case in a generic way:

R = CartesianIndices(img)
roi = CartesianIndices(roi)
I_first, I_last = first(roi), last(roi)

(I_first in R && I_last in R) || throw(ArgumentError("Region of interest lies out of image"))

We don't need to check sx >= ex || sy >= ey because cases like -2:-1:-5 is not a AbstractUnitRange

@johnnychen94
Copy link
Member

What happened to the tests? Error while compiling Images doesn't make sense. I haven't changed anything in the tests yet, and they all passed locally 😨

Ref: JuliaGraphics/ColorTypes.jl#209

You can temporarily disable the nightly test to continue this:

julia-version: ['1.0', '1', 'nightly']

@kimikage
Copy link

Maybe fixed in ColorTypes v0.10.7.

@ashwanirathee
Copy link
Member

I was always confused that Point(x, y) represents (second_dim, first_dim) We can file an issue for further discussion on whether we should change this.

Yeah,I had quite a lot of trouble with this too 😓 🤣 for rectangle one.I read this part just now,so we maybe need some changes in #52 and ofcourse on how the data is handled.CartesianIndex + Point,it gets eerily hard to makes sense of it..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants