ahmedfgad / HOGNumPy

Histogram of oriented gradients (HOG) Python implementation using NumPy.
https://www.linkedin.com/in/ahmedfgad
35 stars 30 forks source link

HOG returns empty descriptors/features #1

Open ssetty opened 5 years ago

ssetty commented 5 years ago

Hi, When I try to run your code on sample image, returns empty descriptors. Kindly let me know solution.

Thanks Thor_org020

ahmedfgad commented 5 years ago

Hi, I downloaded the projects and ran it and everything is working as expected. Not only tested on the image located within the project but also on other images and everything is working. It might be a bug due to feeding an RGB color image to the code while it accepts only gray images. I edited the HOG_implementation.py file so that the image is ensured to be a gray using the rgb2gray() function. Simply try it again and let me know if something is not working.

ssetty commented 5 years ago

Hi, I attempted to change gray image.. but still gives empty results. Please run code against attached image Thor_org020.png. Should we change some parameters related to HOG?

img = skimage.io.imread("Thor_org020.png") img = rgb2gray(img) Thor_org020

ssetty commented 5 years ago

Hi, I attempted to change gray image.. but still gives empty results. Please run code against attached image "Thor_org020.png". Should we change some parameters related to HOG?

Thanks

On Fri, Jul 12, 2019 at 4:58 PM Ahmed Gad notifications@github.com wrote:

Hi, I downloaded the projects and ran it and everything is working as expected. Not only tested on the image located within the project but also on other images and everything is working. It might be a bug due to feeding an RGB color image to the code while it accepts only gray images. I edited the HOG_implementation.py file so that the image is ensured to be a gray using the rgb2gray() function. Simply try it again and let me know if something is not working.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ahmedfgad/HOGNumPy/issues/1?email_source=notifications&email_token=AENWRVWZZU7PWTGAAFIBRMTP7BTHFA5CNFSM4ICFAYSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZZPZ7Y#issuecomment-510852351, or mute the thread https://github.com/notifications/unsubscribe-auth/AENWRVWOAVVE7Z2JIRYAPITP7BTHFANCNFSM4ICFAYSA .

ahmedfgad commented 5 years ago

Yes I downloaded the attached image and tested the code and everything is working fine in my end. Could you trace the code to figure out what step is causing this to happen?

ssetty commented 5 years ago

Hi,

Kindly find attached zip files:

HOGNumPy-master-not-working.zip : horizontal and vertical gradients return zeros

HOGNumPy-mastert-working.zip : Just applied image processing techniques to find largest contour - then this works. I am surprised what causes this approach to work. Please explain.

You can try yourselt, I am using python 3.6.3

Thanks

On Sun, Jul 14, 2019 at 3:53 AM Ahmed Gad notifications@github.com wrote:

Yes I downloaded the attached image and tested the code and everything is working fine in my end. Could you trace the code to figure out what step is causing this to happen?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ahmedfgad/HOGNumPy/issues/1?email_source=notifications&email_token=AENWRVSFP6YWEYJ6BQKMARDP7JIVXA5CNFSM4ICFAYSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZ32N2Q#issuecomment-511157994, or mute the thread https://github.com/notifications/unsubscribe-auth/AENWRVVY63WDJF2M2T72UGDP7JIVXANCNFSM4ICFAYSA .

ahmedfgad commented 5 years ago

Nothing attached. Please make sure to send the files.

ssetty commented 5 years ago

Thanks

On Thu, Jul 18, 2019 at 2:10 AM Ahmed Gad notifications@github.com wrote:

Nothing attached. Please make sure to send the files.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ahmedfgad/HOGNumPy/issues/1?email_source=notifications&email_token=AENWRVSOOMWIWYOPAW2PXFLP757VDA5CNFSM4ICFAYSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2GQX4Y#issuecomment-512560115, or mute the thread https://github.com/notifications/unsubscribe-auth/AENWRVQXT25MB7P7NGX2XXDP757VDANCNFSM4ICFAYSA .

ssetty commented 5 years ago

Hi Ahmed.

Any update on this ?

Regards

On Thu, Jul 18, 2019 at 6:34 AM Satish Setty setsat2011@gmail.com wrote:

Thanks

On Thu, Jul 18, 2019 at 2:10 AM Ahmed Gad notifications@github.com wrote:

Nothing attached. Please make sure to send the files.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ahmedfgad/HOGNumPy/issues/1?email_source=notifications&email_token=AENWRVSOOMWIWYOPAW2PXFLP757VDA5CNFSM4ICFAYSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2GQX4Y#issuecomment-512560115, or mute the thread https://github.com/notifications/unsubscribe-auth/AENWRVQXT25MB7P7NGX2XXDP757VDANCNFSM4ICFAYSA .

ahmedfgad commented 5 years ago

Hi, You asked me to check an attached zip file and I mentioned in the previous comment that nothing is attached. So, I have nothing to do.

Please make sure the files are attached successfully in order to check them.

Regards

ahmedfgad commented 5 years ago

Hi, Thanks for attaching the files. I download the "HOGNumPy-master-not-working.txt" file, converted it to zip, and extracted the files. After looking at the HOG_implementation.py file, I found that you did some modifications. For example, creating a new function for cropping the image and using OpenCV. I discarded all of these modifications to see why my code is not working.

As a summary, there was 2 issues. The first one is that the image after being converted to gray using skimage.color.rgb2gray() is not of type uint8 but float32. You can check that using the dtype property of the img array. To force converting it to uint8, this line should be inserted after the conversion: img = numpy.uint8(img*255)

By doing this, the issue still exists and the resultant feature vector is all zeros. The reason is that the image have many zero pixels at the top and button of it. These zero pixels make the returned result always zero. I cropped the image using an image editor to remove these zeros. After testing the code, everything is working as expected. The cropped image is attached.

Thor_org020

The modified complete code of the HOG_implementation.py file is as follows:

import skimage.io, skimage.color import numpy import matplotlib.pyplot import HOG

img = skimage.io.imread("Thor_org020.png")

img = skimage.color.rgb2gray(img) img = numpy.uint8(img*255)

horizontal_mask = numpy.array([-1, 0, 1]) vertical_mask = numpy.array([[-1], [0], [1]])

horizontal_gradient = HOG.calculate_gradient(img, horizontal_mask) vertical_gradient = HOG.calculate_gradient(img, vertical_mask)

grad_magnitude = HOG.gradient_magnitude(horizontal_gradient, vertical_gradient) grad_direction = HOG.gradient_direction(horizontal_gradient, vertical_gradient)

grad_direction = grad_direction % 180 hist_bins = numpy.array([10,30,50,70,90,110,130,150,170])

cell_direction = grad_direction[:8, :8] cell_magnitude = grad_magnitude[:8, :8] HOG_cell_hist = HOG.HOG_cell_histogram(cell_direction, cell_magnitude, hist_bins)

matplotlib.pyplot.bar(x=numpy.arange(9), height=HOG_cell_hist, align="center", width=0.8) matplotlib.pyplot.show()

ssetty commented 5 years ago

Thanks Ahmed... I too was suspecting similar so added crop function. For Auto cropping without any manual intervention (HOG features detection should be automated) any better technique?

Regards

On Sat, Jul 20, 2019 at 10:41 PM Ahmed Gad notifications@github.com wrote:

Hi, Thanks for attaching the files. I download the "HOGNumPy-master-not-working.txt" file, converted it to zip, and extracted the files. After looking at the HOG_implementation.py file, I found that you did some modifications. For example, creating a new function for cropping the image and using OpenCV. I discarded all of these modifications to see why my code is not working.

As a summary, there was 2 issues. The first one is that the image after being converted to gray using skimage.color.rgb2gray() is not of type uint8 but float32. You can check that using the dtype property of the img array. To force converting it to uint8, this line should be inserted after the conversion: img = numpy.uint8(img*255)

By doing this, the issue still exists and the resultant feature vector is all zeros. The reason is that the image have many zero pixels at the top and button of it. These zero pixels make the returned result always zero. I cropped the image using an image editor to remove these zeros. After testing the code, everything is working as expected. The cropped image is attached.

[image: Thor_org020] https://user-images.githubusercontent.com/16560492/61581713-fbe6fb00-ab21-11e9-837c-350392880254.png

The modified complete code of the HOG_implementation.py file is as follows:

`import skimage.io, skimage.color import numpy import matplotlib.pyplot import HOG

img = skimage.io.imread("Thor_org020.png")

img = skimage.color.rgb2gray(img) img = numpy.uint8(img*255)

horizontal_mask = numpy.array([-1, 0, 1]) vertical_mask = numpy.array([[-1], [0], [1]])

horizontal_gradient = HOG.calculate_gradient(img, horizontal_mask) vertical_gradient = HOG.calculate_gradient(img, vertical_mask)

grad_magnitude = HOG.gradient_magnitude(horizontal_gradient, vertical_gradient) grad_direction = HOG.gradient_direction(horizontal_gradient, vertical_gradient)

grad_direction = grad_direction % 180 hist_bins = numpy.array([10,30,50,70,90,110,130,150,170]) Histogram of the first cell in the first block.

cell_direction = grad_direction[:8, :8] cell_magnitude = grad_magnitude[:8, :8] HOG_cell_hist = HOG.HOG_cell_histogram(cell_direction, cell_magnitude, hist_bins)

matplotlib.pyplot.bar(x=numpy.arange(9), height=HOG_cell_hist, align="center", width=0.8) matplotlib.pyplot.show() `

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ahmedfgad/HOGNumPy/issues/1?email_source=notifications&email_token=AENWRVVIC7OKU2DS4RJ4LGLQANBLHA5CNFSM4ICFAYSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2NSJZY#issuecomment-513484007, or mute the thread https://github.com/notifications/unsubscribe-auth/AENWRVQHCW333XSH747WU2LQANBLHANCNFSM4ICFAYSA .

ahmedfgad commented 5 years ago

You can loop through the image and simply remove rows that have a mean of zero.

ssetty commented 5 years ago

Can you share code snippet assuming image is an numpy array?

Thanks

On Sun, Jul 21, 2019 at 8:47 PM Ahmed Gad notifications@github.com wrote:

You can loop through the image and simply remove rows that have a mean of zero.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ahmedfgad/HOGNumPy/issues/1?email_source=notifications&email_token=AENWRVSFBRE5ZJEMYGJVKGLQAR4ZBA5CNFSM4ICFAYSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2OFTUY#issuecomment-513563091, or mute the thread https://github.com/notifications/unsubscribe-auth/AENWRVV7WSUGT7Z53YSD5ODQAR4ZBANCNFSM4ICFAYSA .