1. An image processing apparatus comprising: a first memory configured to store frequency values of respective feature amounts; a second memory configured to store some of the frequency values stored in the first memory, wherein the second memory has smaller capacity than the first memory and is accessible at higher speed than the first memory; a program memory; and a processor in communication with the program memory to function as: a generation unit configured to generate a histogram of feature amounts of respective pixels of an image by receiving the feature amount of each pixel of the image and, if a frequency value of the feature amount is registered in the second memory, increasing the frequency value in the second memory, and, if the frequency value is not registered in the second memory, reading out the frequency value from the first memory into the second memory, and increasing the frequency value in the second memory; and a regeneration unit configured to regenerate the histogram by arranging bins of the histogram in the first memory so that bins with high frequency values are read out into a same storage area in the second memory, wherein the regeneration unit includes: a selection unit configured to obtain, for each bit pattern of bit patterns with a predetermined length, a total value of frequency values for a plurality of bit strings, each of which includes the bit pattern in a predetermined portion, and select one of the bit patterns based on respective total values for the bit patterns; a transforming unit configured to generate, for each bit string as a bin of the histogram, a bit string by rearranging bit values of the bit string, and transform the generated bit string by performing, for the generated bit string, the same transforming processing as that for making the bit values of the bit pattern selected by the selection unit equal to each other; and a sorting unit configured to sort, in ascending order of a value of the bit string, data sets each of which corresponds to each of the bit strings transformed by the transforming unit and includes the bit string and a frequency value corresponding to the bin as a generation source of the bit string in the histogram.