85129

opencv version 3.* HogDescriptor takes at most 1 argument (5 given)

I am trying to use HogDescriptor and I am getting this error. I saw in the document that the constructor can take more then one argument. I am working in python 3.6 and opencv 3.2

This is my code:

def _extract_feature(X): """Performs feature extraction :param X: data (rows=images, cols=pixels) :param feature: which feature to extract - "hog": HOG features :returns: X (rows=samples, cols=features) """ X = [cv2.cvtColor(x, cv2.COLOR_BGR2GRAY) for x in X] # operate on smaller image small_size = (32, 32) X = [cv2.resize(x, small_size) for x in X] # histogram of gradients block_size = (small_size[0] / 2, small_size[1] / 2) block_stride = (small_size[0] / 4, small_size[1] / 4) cell_size = block_stride num_bins = 9 hog = cv2.HOGDescriptor(small_size, block_size, block_stride, cell_size, num_bins) X = [hog.compute(x) for x in X] X = [x.flatten() for x in X] return X

So why am I getting the error:

TypeError: HogDescriptor takes at most 1 argument (5 given) <hr>

Update: I installed python 2.7 and tried it and it works.

Answer1:

The issue you're observing is due to combination of several factors:

    <li>PEP-238 – Changing the Division Operator</li> <li>Mapping to Python and dispatching of overloaded C++ functions (as done in OpenCV)</li> </ul>

    Division in Python

    The meaning of operator / has changed between Python 2.x and Python 3.x.

    From PEP-238:

      <li>Classic division will remain the default in the Python 2.x series; true division will be standard in Python 3.0</li> </ul>

      For example, in Python 2.x you have

      >>> small_size = (32, 32) >>> block_size = (small_size[0] / 2, small_size[1] / 2) >>> print(block_size) (16, 16)

      And in Python 3.x you have

      >>> small_size = (32, 32) >>> block_size = (small_size[0] / 2, small_size[1] / 2) >>> print(block_size) (16.0, 16.0)

      In order to get the same result, your Python 3.x code would need to use operator //

      >>> small_size = (32, 32) >>> block_size = (small_size[0] // 2, small_size[1] // 2) >>> print(block_size) (16, 16)

      Mapping and Handling of Overloads

      Without going into detail (that would warrant a separate question), the overload resolution depends on the types of arguments. The matching of types is quite strict, e.g. you may not pass a floating point number to an integer argument.

      The C++ signature of the constructor in question (truncated to only the relevant arguments) is

      HOGDescriptor (Size _winSize , Size _blockSize , Size _blockStride , Size _cellSize , int _nbins, ...)

      cv::Size is a class that contains two integers. In the Python API, it is represented as a tuple containing two integers.

      Therefore, to call this overload in Python, we need to provide:

        <li>4x a tuple containing two integers (for _winSize, _blockSize, _blockStride, and _cellSize)</li> <li>1x an integer (for _nbins)</li> </ul>

        We can test this out in the Python interpreter.

        >>> import cv2 >>> cv2.HOGDescriptor((1,1),(1,1),(1,1),(1,1),1) <HOGDescriptor 0393D0F0>

        This matches our expectations. Let's try some other options.

        >>> cv2.HOGDescriptor((1,1,1),(1,1),(1,1),(1,1),1) TypeError: HOGDescriptor() takes at most 1 argument (5 given) >>> cv2.HOGDescriptor((1,),(1,1),(1,1),(1,1),1) TypeError: HOGDescriptor() takes at most 1 argument (5 given)

        We see the tuples need to have 2 values, no more, no less.

        >>> cv2.HOGDescriptor([1,1],(1,1),(1,1),(1,1),1) TypeError: HOGDescriptor() takes at most 1 argument (5 given)

        Lists won't do, it has to be a tuple.

        >>> cv2.HOGDescriptor((1.0,1.0),(1,1),(1,1),(1,1),1) TypeError: HOGDescriptor() takes at most 1 argument (5 given)

        Tuples of floats won't work either.

        >>> cv2.HOGDescriptor((1,1),(1,1),(1,1),(1,1),1.0) TypeError: HOGDescriptor() takes at most 1 argument (5 given)

        Nor can we substitute an float for an integer.

        Pulling it All Together

        Considering the above, in terms of the values you pass the constructor, the two variants are:

          <li>Python 2.x: cv2.HOGDescriptor((32,32),(16,16),(8,8),(8,8),9)</li> <li>Python 3.x: cv2.HOGDescriptor((32,32),(16.0,16.0),(8.0,8.0),(8.0,8.0),9)</li> </ul>

          We can clearly see that the parameters in Python 3.x don't satisfy the requirements of the overload.

          As to why we get the misleading error message, that seems like another topic warranting a separate question, and I haven't done all the research necessary to answer this authoritatively.

          Answer2:

          HOGDescriptor is an overloaded function.

          If you look at the c++ documentation, it shows 4 versions - one with no parameters, one with 12, one with string and one with an option I'm not immediately familiar with.

          You've tried to use one with 5 arguments. There isn't a function for that. You need to use either none, all 12 or one. If you look at this StackOverflow question, this person has supplied an xml file for the control specification - this fits the string filename option and hence is a valid function.

Recommend

  • Counter on JFrame
  • Will the Hotspot VM inline functions as necessary?
  • python not strict type check for bool
  • Importing requests module does not work
  • How can i access a variable or change the state of an element(like tabNavigator) from one mxml to an
  • undirected graphs algorithms
  • dot 2 pixels together
  • OrderedDict not staying in order
  • So we've got MEF working with MVC4, how about the convention based model?
  • Msql: Counting growth over time
  • Why doesn't the Python interpreter implicitly create the generator?
  • PowerShell IComparable with subclasses
  • best layer for placing dependency injection related code in layered proeject
  • Apache Spark: How to structure code of a Spark Application (especially when using Broadcasts)
  • Why is my database not being updated when someone visits my website?
  • Go template remove the last comma in range loop
  • integrating GWT with CDI (Seam/Weld)
  • Assembly Language Absolute addresses and segment registers
  • Wait for Swing to finish updating JProgressBar before continuing
  • Dependency Injection - Choose DLL and class implementation at runtime through configuration file
  • Necessary to pin_ptr on C++ CLR Value types, why?
  • Trying to delete files older than X number of days on insert in .NET MVC
  • Removing levels from data frame read from CSV file - R
  • NodeJS - Disable JSONP on specific routes
  • How read between delimiters in php DOM of a XML file?
  • Xmonad multiple submap key combos
  • C# - Most efficient way to iterate through multiple arrays/list
  • unable to get jsonEncode in magento2
  • VBA Excel, loop through variables
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • import scipy.sparse failed
  • Rest Services conventions
  • R Split data.frame using a column that represents and on/off switch
  • SonarQube: Cannot deactivate rule with missing quality profile
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Python pickle not one-to-one: different pickles give same object
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • SetUp method failed while running tests from teamcity
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed