# How to reduce extract image feature time?

**URL:** <https://forum.opencv.org/t/how-to-reduce-extract-image-feature-time/17635>\
**Category:** Python\
**Tags:** programming\
**Created:** [May 13, 2024, 7:04am UTC](https://forum.opencv.org/t/how-to-reduce-extract-image-feature-time/17635 "2024-05-13T07:04:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![phphuoc123](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/phphuoc123/32/10461_2.png) [@phphuoc123](https://forum.opencv.org/u/phphuoc123)\
**Post date:** [May 13, 2024, 7:04am UTC](https://forum.opencv.org/t/how-to-reduce-extract-image-feature-time/17635/1 "2024-05-13T07:04:24Z")

</div>

Hi everyone,  
I have just learned about computer vision. Currently, i am processing a problem about image classification. I used HoG to extract features from images, but it takes a very long time whenever i run it. Is there anyway to reduce the time run it because my dataset is very large (about 15k images). And HoG extracts an image into a (1, 16740) vector dimension.  
Here is the code i uses to extract features:

```auto
def calc_hog(imgPath):
    img = cv2.imread(imgPath)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    resized_img = cv2.resize(img, (256, 128))
    features, hog_image = hog(resized_img, orientations=9, pixels_per_cell=(8, 8),
                              cells_per_block=(2, 2), visualize=True, channel_axis=-1)
    return features

```

I am very thankful that you guys read my question, and i hope i can receive an answer soon.  
Regard.

Edited:  
This is the code which i use to load images:

```auto
def loadDataset(imgPath, dataf, features_extraction):
    x = []
    y = []

    sto = dataf.reset_index(drop=True)

    for i in range(len(sto)):
        img_path = os.path.join(imgPath, sto['image'][i])
        x.append(features_extraction(img_path))
        y.append(dataf['label'][i])

    x = np.array(x)
    y = np.array(y)

    return x, y

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 13, 2024, 7:13am UTC](https://forum.opencv.org/t/how-to-reduce-extract-image-feature-time/17635/2 "2024-05-13T07:13:46Z")

</div>

please find out how much time each line in your code takes. sum up the individual times per line across the entire dataset. use `time.perf_counter()`, or use a proper profiler

---

<div class="post-metadata">

**Author:** ![phphuoc123](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/phphuoc123/32/10461_2.png) [@phphuoc123](https://forum.opencv.org/u/phphuoc123)\
**Post date:** [May 13, 2024, 8:50am UTC](https://forum.opencv.org/t/how-to-reduce-extract-image-feature-time/17635/3 "2024-05-13T08:50:58Z")

</div>

It’s about 4683s (1 hour 18 minutes) for 12271 images to make a (12271, 16740) matrix from using HoG. And i still have 3k images more to run ☹.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 13, 2024, 10:02am UTC](https://forum.opencv.org/t/how-to-reduce-extract-image-feature-time/17635/4 "2024-05-13T10:02:27Z")

</div>

i don’t mean the grand total.

I mean you need to find out which line in `calc_hog` takes how long.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [May 13, 2024, 1:37pm UTC](https://forum.opencv.org/t/how-to-reduce-extract-image-feature-time/17635/6 "2024-05-13T13:37:34Z")

</div>

this should be a one-time, offline task.

.gather your hog features _once_ & serialize for later use

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 13, 2024, 1:45pm UTC](https://forum.opencv.org/t/how-to-reduce-extract-image-feature-time/17635/7 "2024-05-13T13:45:44Z")

</div>

Great!

So that looks like… _ **twenty nine seconds** _ (29 s) to merely read the image, and another 0.3 secs for the actual processing, the majority of which is the `hog()` call.

now I don’t know how `hog` is defined. you didn’t show that code. for all I know, it’s not even from OpenCV but from _`skimage`_

could you provide details on that?

---

<div class="post-metadata">

**Author:** ![phphuoc123](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/phphuoc123/32/10461_2.png) [@phphuoc123](https://forum.opencv.org/u/phphuoc123)\
**Post date:** [May 13, 2024, 1:52pm UTC](https://forum.opencv.org/t/how-to-reduce-extract-image-feature-time/17635/8 "2024-05-13T13:52:42Z")

</div>

yes, you are right, that hog is come from skimage. I dont know clearly how it’s work but they provided it in this documentation [Histogram of Oriented Gradients — skimage 0.23.2 documentation (scikit-image.org)](https://scikit-image.org/docs/stable/auto_examples/features_detection/plot_hog.html) and this [scikit-image/skimage/feature/\_hog.py at main · scikit-image/scikit-image · GitHub](https://github.com/scikit-image/scikit-image/blob/main/skimage/feature/_hog.py).  
By the way as berak say, can you suggest me a keyword or something so that i can findout way to do this once (i wonder if the time i load is more faster than process it for each time run).

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 13, 2024, 3:32pm UTC](https://forum.opencv.org/t/how-to-reduce-extract-image-feature-time/17635/9 "2024-05-13T15:32:08Z")

</div>

it’s just a numpy array, so you can use any method to store and load numpy arrays
