Python Image Processing (9): Hu Moment, python image processing hu

Source: Internet
Author: User

Python Image Processing (9): Hu Moment, python image processing hu

Happy shrimp

Http://blog.csdn.net/lights_joy/

Reprinted, but keep the author information


The geometric moment was proposed by Hu in 1962 and has the characteristics of translation, rotation, and scale immutability. The Calculation of Moment in opencv is completed by the Moments class in C ++:


//! raster image momentsclass CV_EXPORTS_W_MAP Moments{public:    //! the default constructor    Moments();    //! the full constructor    Moments(double m00, double m10, double m01, double m20, double m11,            double m02, double m30, double m21, double m12, double m03 );    //! the conversion from CvMoments    Moments( const CvMoments& moments );    //! the conversion to CvMoments    operator CvMoments() const;    //! spatial moments    CV_PROP_RW double  m00, m10, m01, m20, m11, m02, m30, m21, m12, m03;    //! central moments    CV_PROP_RW double  mu20, mu11, mu02, mu30, mu21, mu12, mu03;    //! central normalized moments    CV_PROP_RW double  nu20, nu11, nu02, nu30, nu21, nu12, nu03;};

It can be seen that it mainly includes space moment, center moment and center normalization moment.

I have never believed in the effect of geometric moment on identification of plant leaves, especially for blocked leaves. However, if you have learned this, try to calculate the Hu Moment in Python.

The image used is the color image separated from the preceding image:



We have obtained their binary images:


We use these two binary images as the input image for moment calculation.


#-*-Coding: UTF-8-*-import cv2import numpy as np # import matplotlib. pyplot as pltdir = 'f: \ projects \ src \ opencv \ images \ cotton \\'; # Hu moments unchanged # Read cotton image cotton = cv2.imread (dir + '39.mask.jpg ') cotton = cotton [:,:, 0] # This is a binary image, only use the first channel cv2.imshow ('cotton ', cotton) # Read weed image weed = cv2.imread (dir + '47.mask.jpg') weed = weed [:,:, 0] # This is a binary image. Only the first channel cv2.imshow ('weed', weed) is used. # Calculate the moment of cotton and the Hu Moment moments = cv2.moments (cotton) hu_moments = cv2.HuMoments (moments) print ('cotton moments: ') print (hu_moments) # Calculate the moment of weeds and Hu Moment moments = cv2.moments (weed) hu_moments = cv2.HuMoments (moments) print ('weed moments: ') print (hu_moments) cv2.waitKey ()

The calculation result is as follows:



The preceding code shows how to use Python to calculate Hu Moment.











Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.