Moments
moments(InputArray array, bool binaryImage=false )
Parameters: |
-
array – Raster image (single-channel, 8-bit or floating-point 2D array) or an array ( 1xN or Nx1 ) of 2D points (Point or Point2f ).
-
binaryImage – If it is true, all non-zero image pixels are treated as 1’s. The parameter is used for images only.
-
moments – Output moments.
|
The function computes
moments, up to the 3rd order, of a vector shape or a rasterized shape. The results are returned in the structure
Moments defined as:
class Moments
{
public:
Moments();
Moments(double m00, double m10, double m01, double m20, double m11,
double m02, double m30, double m21, double m12, double m03 );
Moments( const CvMoments& moments );
operator CvMoments() const;
// spatial moments
double m00, m10, m01, m20, m11, m02, m30, m21, m12, m03;
// central moments
double mu20, mu11, mu02, mu30, mu21, mu12, mu03;
// central normalized moments
double nu20, nu11, nu02, nu30, nu21, nu12, nu03;
}
In case of a raster image, the spatial
moments Moments:: m
ji are computed as:
The central
moments are computed as:
where
is the mass center:
The normalized central
moments Moments:: nu
ji are computed as:
The code provided below is slight modification of code given in OpenCV documentation.
Steps:
- Load image
- Convert to gray-scale.
- Remove noise by blurring with a Gaussian filter
- Detect edges using canny
- Find contours (findContours)
- Get the moments(Moments)
- Draw contours
- Calculate the area with the moments 00 and compare with the result of the OpenCV function
Functions:
Example:
-------------
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
RNG rng(12345);
void find_moments( Mat src );
int main( )
{
/// Load source image, convert it to gray and blur it
Mat src, gray;;
src = imread("shapes.jpg", 1 );
cvtColor( src, gray, CV_BGR2GRAY );
blur( gray, gray, Size(3,3) );
namedWindow( "Source", CV_WINDOW_AUTOSIZE );
imshow( "Source", src );
find_moments( gray );
waitKey(0);
return(0);
}
void find_moments( Mat gray )
{
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Detect edges using canny
Canny( gray, canny_output, 50, 150, 3 );
/// Find contours
findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
/// Get the moments
vector<Moments> mu(contours.size() );
for( int i = 0; i < contours.size(); i++ )
{ mu[i] = moments( contours[i], false ); }
/// Get the mass centers:
vector<Point2f> mc( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{ mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); }
/// Draw contours
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
circle( drawing, mc[i], 4, color, -1, 8, 0 );
}
/// Show in a window
namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing );
/// Calculate the area with the moments 00 and compare with the result of the OpenCV function
printf("\t Info: Area and Contour Length \n");
for( int i = 0; i< contours.size(); i++ )
printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f \n", i, mu[i].m00, contourArea(contours[i]), arcLength( contours[i], true ) );
}
-------------
This comment has been removed by a blog administrator.
ReplyDeleteInvestment is one of the best ways to achieve financial freedom. For a beginner there are so many challenges you face. It's hard to know how to get started. Trading on the Cryptocurrency market has really been a life changer for me. I almost gave up on crypto at some point not until saw a recommendation on Elon musk successfully success story and I got a proficient trader/broker Mr Bernie Doran , he gave me all the information required to succeed in trading. I made more profit than I could ever imagine. I'm not here to converse much but to share my testimony, I recovered my losses and I have made a total profit returns of $20,500 from an investment of just $2000 within 1 week. Thanks to Mr Bernie I'm really grateful,I have been able to make a great returns trading with his signals and strategies .I urge anyone interested in INVESTMENT to take bold step in investing in the Cryptocurrency Market, he can also help you recover your lost funds, you can reach him on WhatsApp : +1(424) 285-0682 or his Gmail : BERNIEDORANSIGNALS@GMAIL.COM tell him I referred you
DeleteCounselors. Our counselors have been logging in hours to connect with our families and also learn new skills. Tiffany has been working hard to fast track her College Counseling know how to b ready to support the Class of 2025. JJR has been working furiously to resolve and smooth out the final programming bumps and glitches - and both them are now shouldering larger counseling caseloads now that our guidance department is half the size!
Programming is really hard to understand. But thanks to your article. It's really a great help for me especially that I'm a beginners for this lessons. Keep it up. And please post more examples about this.
ReplyDeleteBob
www.gofastek.com
Great job, but why don't you let us see the image you've used as an input and the output (both image and text)? I'd be interested in those to see if it's worth to try or not / in which cases could it be useful, etc. Anyway, thanks for the tutorial :)
ReplyDeleteYou should provide Output in your article. Thanks
ReplyDeletecan you provide this in python
ReplyDeleteHow to counting pixel in contour? Thank
ReplyDeleteCompre documentos en línea, documentos originales y registrados.
ReplyDeleteAcerca de Permisodeespana, algunos dicen que somos los solucionadores de problemas, mientras que otros se refieren a nosotros como vendedores de soluciones. Contamos con cientos de clientes satisfechos a nivel mundial. Hacemos documentos falsos autorizados y aprobados como Permiso de Residencia Español, DNI, Pasaporte Español y Licencia de Conducir Española. Somos los fabricantes y proveedores de primer nivel de estos documentos, reconocidos a nivel mundial.
Comprar permiso de residencia,
permiso de residenciareal y falso en línea,
Compre licencia de conducir en línea,
Compre una licencia de conducir española falsa en línea,
Comprar tarjeta de identificación,
Licencia de conducir real y falsa,
Compre pasaporte real en línea,
Visit Here fpr more information. :- https://permisodeespana.com/licencia-de-conducir-espanola/
Address: 56 Guild Street, London, EC4A 3WU (UK)
Email: contact@permisodeespana.com
WhatsApp: +443455280186
أسباب ومخاطر تسرب المياه اولا: سبب تسرب المياه وهناك اسباب عديدة لتسرب المياه ومنها مشاكل مواسير المياه الداخلية. تخصص في المنزل نفسه ، أو تشققات وكسور في السياج فوقك ، لذلك سوف تتأثر به. ثانيًا: الضرر الناتج عن تسرب المياه في جميع أنواع التسربات يختلف التلف من نوع إلى آخر ولا يمكن تركه وفي حال تركه فترة طويلة ستجلب لك الكثير من المشاكل والسبب هو أن تسرب المياه يتفاعل مع مواد البناء ويلعب دوراً. البقع الموجودة على الحائط يكون لون هذه البقع اسود او اخضر حسب زمن التسرب لان الماء يتفاعل مع الاسمنت دهان الجدران وتسبب البقع والعفن مما يجعل الجدران هشة ولهذا تتعرض الجدران للتشقق وتحطم
ReplyDeleteشركة العطار للخدمات المنزلية
إن ترميم المنزل من الأمور المهمة التي يجب القيام بها ، ولإعادة المنزل إلى حالته السابقة ، فإنه يتأثر بالعوامل البيئية المختلفة ، مثل البرد الشديد أو البرد الشديد ، والأمطار وتسرب المياه ، وما إلى ذلك ، والتي تغيير مشهد وطبيعة المنزل .. وهذا أجمل من ترك المنزل إلى مكان حديث. تصبح عملية الترميم معالجة للمنزل ، تقضي على التلف والشقوق الناتجة عن أسباب طبيعية وغير طبيعية للمنزل ، حتى ضياع قطعة كبيرة من الجمال. الغرض من الترميم هو الادخار لمدة عام واحد فيما يتعلق بهيكل المبنى وحالة المرض الذي سببته المؤسسة القضائية على المبنى.
ReplyDeleteشركة منزل الشموخ لخدمات المنزل