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

[GUI] Add handy ti.imscale(img, w, h) for scaling image #1735

Merged
merged 9 commits into from
Aug 22, 2020

Conversation

archibate
Copy link
Collaborator

Related issue = #

[Click here for the format server]


When I was debugging MGPCG, I realized that a small resolution is fast but hardly-visible. So it would be great if I could scale that small image up.

@codecov
Copy link

codecov bot commented Aug 19, 2020

Codecov Report

Merging #1735 into master will decrease coverage by 0.10%.
The diff coverage is 35.71%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1735      +/-   ##
==========================================
- Coverage   43.45%   43.35%   -0.11%     
==========================================
  Files          44       44              
  Lines        6015     6053      +38     
  Branches     1042     1045       +3     
==========================================
+ Hits         2614     2624      +10     
- Misses       3250     3275      +25     
- Partials      151      154       +3     
Impacted Files Coverage Δ
python/taichi/misc/image.py 41.26% <35.71%> (+0.09%) ⬆️
python/taichi/lang/__init__.py 51.48% <0.00%> (-2.13%) ⬇️
python/taichi/lang/impl.py 64.68% <0.00%> (-0.55%) ⬇️
python/taichi/misc/util.py 17.46% <0.00%> (+1.42%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 215d6d1...466dfdc. Read the comment docs.

docs/gui.rst Outdated Show resolved Hide resolved
docs/gui.rst Outdated
@@ -522,3 +522,14 @@ Image I/O
This function will create an instance of ``ti.GUI`` and show the input image on the screen.

It has the same logic as ``ti.imwrite`` for different datatypes.

.. function:: ti.imscale(img, w=512, h=None):
Copy link
Contributor

@Rullec Rullec Aug 20, 2020

Choose a reason for hiding this comment

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

Suggested change
.. function:: ti.imscale(img, w=512, h=None):
.. function:: ti.imscale(img, w=512, h=512):

I would suggest to use a meaningful number in the parameter h, because it can guide our users better even though when h is None it will be the same as w.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How about ti.imscale(img, w=512, h=w)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes! I love that ;)

docs/gui.rst Outdated Show resolved Hide resolved
python/taichi/misc/image.py Outdated Show resolved Hide resolved
@archibate archibate requested a review from Rullec August 20, 2020 05:16
Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

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

Cool! Given you are passing in an actual image size, I think we should use imresize instead of imscale. The latter hints the users to pass in ratios (instead of pixels).

python/taichi/misc/image.py Outdated Show resolved Hide resolved
python/taichi/misc/image.py Outdated Show resolved Hide resolved
u, v = (img.shape[0] - 1) / (w - 1), (img.shape[1] - 1) / (h - 1)
x = np.clip(np.arange(w) * u, 0, img.shape[0] - 1).astype(np.int32)
y = np.clip(np.arange(h) * v, 0, img.shape[1] - 1).astype(np.int32)
return img[tuple(np.meshgrid(x, y))].swapaxes(0, 1)
Copy link
Member

Choose a reason for hiding this comment

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

Why is swapaxes necessary here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

np.meshgrid is row-major (y, x), while our image is column major (x, y).

Copy link
Member

Choose a reason for hiding this comment

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

OK sounds good. It's still very confusing to me, we should enforce correctness via a test.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can now verify them by clicking the four endpoint of window of game_of_life.py.

Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

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

Thank you so much. Please rename all imscale to imresize.

Please also add a test.

docs/gui.rst Outdated Show resolved Hide resolved
docs/gui.rst Outdated Show resolved Hide resolved
u, v = (img.shape[0] - 1) / (w - 1), (img.shape[1] - 1) / (h - 1)
x = np.clip(np.arange(w) * u, 0, img.shape[0] - 1).astype(np.int32)
y = np.clip(np.arange(h) * v, 0, img.shape[1] - 1).astype(np.int32)
return img[tuple(np.meshgrid(x, y))].swapaxes(0, 1)
Copy link
Member

Choose a reason for hiding this comment

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

OK sounds good. It's still very confusing to me, we should enforce correctness via a test.

@archibate archibate requested a review from yuanming-hu August 22, 2020 16:16
Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

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

Thanks. LGTM now.

@archibate archibate added the LGTM label Aug 22, 2020
@yuanming-hu yuanming-hu merged commit a58a9af into taichi-dev:master Aug 22, 2020
@yuanming-hu yuanming-hu mentioned this pull request Aug 22, 2020
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.

3 participants