Repository navigation
Add multichannel support to feature.hog - #2870
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2870 +/- ##
=========================================
Coverage ? 86.25%
=========================================
Files ? 335
Lines ? 26895
Branches ? 0
=========================================
Hits ? 23198
Misses ? 3697
Partials ? 0
Continue to review full report at Codecov.
|
09a4ea6 to
356f92e
Compare
…mple to multichannel, added tests
|
This one is ready for review. |
| gy[-1, :] = 0 | ||
| gy[1:-1, :] = channel[2:, :] - channel[:-2, :] | ||
|
|
||
| return gx, gy |
There was a problem hiding this comment.
I feel like this should be using np.gradient or something? Also, gr, gc. ;)
There was a problem hiding this comment.
I feel like this should be using np.gradient or something
That's how it is in the paper (see #2809 (comment)).
Or np.convolve
Specially for you :) :
from datetime import datetime
import numpy as np
from scipy.signal import convolve2d
from skimage import data
if __name__ == '__main__':
img = data.coins().astype('float')
N = 1000
def grad_diff(image):
gx = np.empty(image.shape, dtype=np.double)
gx[:, 0] = 0
gx[:, -1] = 0
gx[:, 1:-1] = image[:, 2:] - image[:, :-2]
gy = np.empty(image.shape, dtype=np.double)
gy[0, :] = 0
gy[-1, :] = 0
gy[1:-1, :] = image[2:, :] - image[:-2, :]
return gx, gy
def grad_conv(image):
gx = convolve2d(image, np.array([[-1, 0, 1]]),
mode='same', boundary='fill', fillvalue=0)
gy = convolve2d(image, np.array([[-1], [0], [1]]),
mode='same', boundary='fill', fillvalue=0)
return gx, gy
time_start = datetime.now()
for i in range(N):
out1 = grad_diff(img)
time_elapsed = datetime.now() - time_start
print('Duration grad_diff: {}'.format(time_elapsed))
time_start = datetime.now()
for i in range(N):
out2 = grad_conv(img)
time_elapsed = datetime.now() - time_start
print('Duration grad_conv: {}'.format(time_elapsed))
Duration grad_diff: 0:00:01.874263
Duration grad_conv: 0:00:08.140994
There was a problem hiding this comment.
Wow! Well, that sucks. FWIW, ndimage.correlate is significantly faster, but not as much as your ugly manual diff method. (Sorry =P)
$ python timegrad.py
Duration grad_diff: 0:00:00.692133
Duration grad_conv: 0:00:05.982160
Duration grad_corr: 0:00:02.190535
| just before returning. | ||
| multichannel : boolean, optional | ||
| If True, the last `image` dimension is considered as a color channel, | ||
| otherwise as spatial. |
There was a problem hiding this comment.
Since HOG doesn't work for 3D images, perhaps this could have a default value of "None"? Then if multichannel is None: multichannel = (image.ndim == 3). I know we have a "don't guess" policy, but my feeling is that this applies when there is ambiguity.
There was a problem hiding this comment.
How you keep all of these issue cross-references in your head, I'll never know. =D Done.
| Returns | ||
| ------- | ||
| newarr : ndarray | ||
| out : ndarray |
| gx_by_ch[:, :, idx_ch], gy_by_ch[:, :, idx_ch] = \ | ||
| _hog_channel_gradient(image[:, :, idx_ch]) | ||
| g_magn[:, :, idx_ch] = np.hypot(gx_by_ch[:, :, idx_ch], | ||
| gy_by_ch[:, :, idx_ch]) |
There was a problem hiding this comment.
gr, gc, or maybe better: row_gradient, col_gradient
There was a problem hiding this comment.
@jni I'm going to change x, y to c, r, but notice that the underlying Cython function still use x, y notation, and the API is being parsed as x, y.
There was a problem hiding this comment.
Ok, done for Cython func, but the API issue holds.
Also, there are too many changes in this PR now, nobody will review it 😞 .
There was a problem hiding this comment.
I'm breaking x, y notation for pixels_per_cell and cells_per_block parameters. I think the current implementation is very confusing as the notation is not specified anywhere in the docstring, andm in addition, the returned HOG descriptor uses r, c notation.
|
@jni All addressed. |
|
@soupault if you like, add this to |
grlee77
left a comment
There was a problem hiding this comment.
This looks good to me. see my minor suggestion for using a sparse meshgrid. Using row/col has made the code easier to read.
| idcs_max = g_magn.argmax(axis=2) | ||
| rr, cc = np.meshgrid(np.arange(image.shape[0]), | ||
| np.arange(image.shape[1]), | ||
| indexing='ij') |
There was a problem hiding this comment.
I think you can also set sparse=True in meshgrid to save memory. The indexing below should still work by broadcasting.
| g_magn[:, :, idx_ch] = np.hypot(g_row_by_ch[:, :, idx_ch], | ||
| g_col_by_ch[:, :, idx_ch]) | ||
|
|
||
| # For each pixel select the channel with the highest gradient magnitude |
There was a problem hiding this comment.
I haven't worked with HOG previously. Is this the standard approach to dealing with color images or is it something that should be mentioned specifically in the Notes of the docstring?
There was a problem hiding this comment.
Yes, this is the recommended method from the original paper.
| image = np.atleast_2d(image) | ||
|
|
||
| if multichannel is None: | ||
| multichannel = (image.ndim == 3) |
There was a problem hiding this comment.
What happens when alpha images land here?
There was a problem hiding this comment.
AFAIK, most of our functions don't work well with RGBA (only some filters are implemented in an appropriate way).
There was a problem hiding this comment.
In any case, this line was proposed by @jni, so, please, consider blaming him 😄 . There was no guessing code in the previous version of this PR.
There was a problem hiding this comment.
This would still work totally fine with an RGBA image.
There was a problem hiding this comment.
And the result will be identical to using an RGBA image with multichannel=True, as you would. Namely, the gradients in the alpha layer will "count". Dunno if that's what you want, but this line isn't the problem.
There was a problem hiding this comment.
Yes, of course, you are right. I'll try to find some info on HOG for RGBA. In the original paper there weren't any hints on this case.
There was a problem hiding this comment.
I misread; thought this checked the final dimension, but see it only checks the number of dimensions.
|
Hey, lots of +1s! I think I'll merge now. =) Thanks @soupault! |
|
Thanks to everyone! 🍾 |
|
Another one down!! Thanks, @soupault |
Description
This PR implements the support for multichannel images for HOG descriptor.
Gallery example has been modified to use an RGB image.
Before:

With this PR:

Checklist
[It's fine to submit PRs which are a work in progress! But before they are merged, all PRs should provide:]
./doc/examples(new features only)[For detailed information on these and other aspects see scikit-image contribution guidelines]
References
Closes #1879.
Based on #1963.
For reviewers
(Don't remove the checklist below.)
later.
__init__.py.doc/release/release_dev.rst.