Imageimg = new Image (@"path/to/image.jpg"); int count = CvInvoke.CountNonZero(img); Console.WriteLine(count);
Mat mat = new Mat(5, 5, DepthType.Cv8U, 1); mat.SetTo(new MCvScalar(0)); mat.ROI = new Rectangle(1, 1, 3, 3); mat.SetTo(new MCvScalar(255)); int count = CvInvoke.CountNonZero(mat); Console.WriteLine(count);In this example, we create a 5x5 matrix and set a 3x3 region in the center to white. We then count the number of non-zero pixels in this region. The result is printed to the console. This function is part of the Emgu.CV.World.dll package library.