<commit_before>﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using OpenCvSharp.Util;

namespace OpenCvSharp
{
    /// <summary>
    /// connected components that is returned from Cv2.ConnectedComponentsEx
    /// </summary>
    public class ConnectedComponents
    {
        /// <summary>
        /// All blobs
        /// </summary>
        public ReadOnlyCollection<Blob> Blobs { get; internal set; }

        /// <summary>
        /// destination labeled value
        /// </summary>
        public int[,] Labels { get; internal set; }

        /// <summary>
        /// The number of labels -1
        /// </summary>
        public int LabelCount { get; internal set; }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="blobs"></param>
        /// <param name="labels"></param>
        /// <param name="labelCount"></param>
        internal ConnectedComponents(IList<Blob> blobs, int[,] labels, int labelCount)
        {
            Blobs = new ReadOnlyCollection<Blob>(blobs);
            Labels = labels;
            LabelCount = labelCount;
        }

        /// <summary>
        /// Filter a image with the specified label value. 
        /// </summary>
        /// <param name="img">Source image.</param>
        /// <param name="labelValue">Label value.</param>
        /// <returns>Filtered image.</returns>
        public Mat FilterByLabel(Mat img, int labelValue)
        {
            return FilterByLabels(img, new[] {labelValue});
        }

        /// <summary>
        /// Filter a image with the specified label values. 
        /// </summary>
        /// <param name="img">Source image.</param>
        /// <param name="labelValues">Label values.</param>
        /// <returns>Filtered image.</returns>
        public Mat FilterByLabels(Mat img, IEnumerable<int> labelValues)
        {
            if (img == null)
                throw new ArgumentNullException("img");
            if (labelValues == null)
                throw new ArgumentNullException("labelValues");
            int[] labelArray = EnumerableEx.ToArray(labelValues);
            if (labelArray.Length == 0)
                throw new ArgumentException("empty labelValues");

            foreach (int labelValue in labelArray)
            {
                if (labelValue < 0 || labelValue >= LabelCount)
                    throw new ArgumentException("0 <= x < LabelCount");
            }

            // マスク用Matを用意し、Andで切り抜く
            using (Mat mask = GetLabelMask(labelArray[0]))
            {
                for (int i = 1; i < labelArray.Length; i++)
                {
                    using (var maskI = GetLabelMask(labelArray[i]))
                    {
                        Cv2.BitwiseOr(mask, maskI, mask);
                    }
                }
                Mat dst = new Mat();
                img.CopyTo(dst, mask);
                return dst;
            }
        }

        /// <summary>
        /// Filter a image with the specified blob object. 
        /// </summary>
        /// <param name="img">Source image.</param>
        /// <param name="blob">Blob value.</param>
        /// <returns>Filtered image.</returns>
        public Mat FilterByBlob(Mat img, Blob blob)
        {
            return FilterByLabels(img, new[] {blob.Label});
        }

        /// <summary>
        /// Filter a image with the specified blob objects. 
        /// </summary>
        /// <param name="img">Source image.</param>
        /// <param name="blobs">Blob values.</param>
        /// <returns>Filtered image.</returns>
        public Mat FilterBlobs(Mat img, IEnumerable<Blob> blobs)
        {
            return FilterByLabels(img, EnumerableEx.Select(blobs, b => b.Label));
        }

        /// <summary>
        /// Draws all blobs to the specified image.
        /// </summary>
        /// <param name="img">The target image to be drawn.</param>
        public void RenderBlobs(Mat img)
        {
            if (img == null) 
                throw new ArgumentNullException("img");
            if (img.Empty())
                throw new ArgumentException("img is empty");
            if (img.Type() != MatType.CV_8UC3)
                throw new ArgumentException("img must be CV_8UC3");

            if (Blobs == null || Blobs.Count == 0)
                throw new OpenCvSharpException("Blobs is empty");

            Scalar[] colors = new Scalar[Blobs.Count];
            colors[0] = Scalar.All(0);
            for (int i = 1; i < Blobs.Count; i++)
            {
                colors[i] = Scalar.RandomColor();
            }

            int height = Labels.GetLength(0);
            int width = Labels.GetLength(1);
            using (var imgt = new MatOfByte3(img))
            {
                var indexer = imgt.GetIndexer();
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        int labelValue = Labels[y, x];
                        indexer[y, x] = colors[labelValue].ToVec3b();
                    }
                }
            }
        }

        /// <summary>
        /// Find the largest blob.
        /// </summary>
        /// <returns>the largest blob</returns>
        public Blob GetLargestBlob()<commit_msg>RenderBlobs creates dst Mat<commit_after>﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using OpenCvSharp.Util;

namespace OpenCvSharp
{
    /// <summary>
    /// connected components that is returned from Cv2.ConnectedComponentsEx
    /// </summary>
    public class ConnectedComponents
    {
        /// <summary>
        /// All blobs
        /// </summary>
        public ReadOnlyCollection<Blob> Blobs { get; internal set; }

        /// <summary>
        /// destination labeled value
        /// </summary>
        public int[,] Labels { get; internal set; }

        /// <summary>
        /// The number of labels -1
        /// </summary>
        public int LabelCount { get; internal set; }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="blobs"></param>
        /// <param name="labels"></param>
        /// <param name="labelCount"></param>
        internal ConnectedComponents(IList<Blob> blobs, int[,] labels, int labelCount)
        {
            Blobs = new ReadOnlyCollection<Blob>(blobs);
            Labels = labels;
            LabelCount = labelCount;
        }

        /// <summary>
        /// Filter a image with the specified label value. 
        /// </summary>
        /// <param name="img">Source image.</param>
        /// <param name="labelValue">Label value.</param>
        /// <returns>Filtered image.</returns>
        public Mat FilterByLabel(Mat img, int labelValue)
        {
            return FilterByLabels(img, new[] {labelValue});
        }

        /// <summary>
        /// Filter a image with the specified label values. 
        /// </summary>
        /// <param name="img">Source image.</param>
        /// <param name="labelValues">Label values.</param>
        /// <returns>Filtered image.</returns>
        public Mat FilterByLabels(Mat img, IEnumerable<int> labelValues)
        {
            if (img == null)
                throw new ArgumentNullException("img");
            if (labelValues == null)
                throw new ArgumentNullException("labelValues");
            int[] labelArray = EnumerableEx.ToArray(labelValues);
            if (labelArray.Length == 0)
                throw new ArgumentException("empty labelValues");

            foreach (int labelValue in labelArray)
            {
                if (labelValue < 0 || labelValue >= LabelCount)
                    throw new ArgumentException("0 <= x < LabelCount");
            }

            // マスク用Matを用意し、Andで切り抜く
            using (Mat mask = GetLabelMask(labelArray[0]))
            {
                for (int i = 1; i < labelArray.Length; i++)
                {
                    using (var maskI = GetLabelMask(labelArray[i]))
                    {
                        Cv2.BitwiseOr(mask, maskI, mask);
                    }
                }
                Mat dst = new Mat();
                img.CopyTo(dst, mask);
                return dst;
            }
        }

        /// <summary>
        /// Filter a image with the specified blob object. 
        /// </summary>
        /// <param name="img">Source image.</param>
        /// <param name="blob">Blob value.</param>
        /// <returns>Filtered image.</returns>
        public Mat FilterByBlob(Mat img, Blob blob)
        {
            return FilterByLabels(img, new[] {blob.Label});
        }

        /// <summary>
        /// Filter a image with the specified blob objects. 
        /// </summary>
        /// <param name="img">Source image.</param>
        /// <param name="blobs">Blob values.</param>
        /// <returns>Filtered image.</returns>
        public Mat FilterBlobs(Mat img, IEnumerable<Blob> blobs)
        {
            return FilterByLabels(img, EnumerableEx.Select(blobs, b => b.Label));
        }

        /// <summary>
        /// Draws all blobs to the specified image.
        /// </summary>
        /// <param name="img">The target image to be drawn.</param>
        public void RenderBlobs(Mat img)
        {
            if (img == null) 
                throw new ArgumentNullException("img");
            /*
            if (img.Empty())
                throw new ArgumentException("img is empty");
            if (img.Type() != MatType.CV_8UC3)
                throw new ArgumentException("img must be CV_8UC3");*/
            if (Blobs == null || Blobs.Count == 0)
                throw new OpenCvSharpException("Blobs is empty");
            if (Labels == null)
                throw new OpenCvSharpException("Labels is empty");

            int height = Labels.GetLength(0);
            int width = Labels.GetLength(1);
            img.Create(new Size(width, height), MatType.CV_8UC3);

            Scalar[] colors = new Scalar[Blobs.Count];
            colors[0] = Scalar.All(0);
            for (int i = 1; i < Blobs.Count; i++)
            {
                colors[i] = Scalar.RandomColor();
            }
            
            using (var imgt = new MatOfByte3(img))
            {
                var indexer = imgt.GetIndexer();
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        int labelValue = Labels[y, x];
                        indexer[y, x] = colors[labelValue].ToVec3b();
                    }
                }
            }
        }

        /// <summary>
        /// Find the largest blob.
        /// </summary>
        /// <returns>the largest blob</returns>
        public Blob GetLargestBlob()
{'fraction_non_alphanumeric': 0.12949376375641966, 'fraction_numerical': 0.004218635363169479, 'mean_word_length': 1.435238945958017, 'pattern_counts': {'":': 0, '<': 139, '<?xml version=': 0, '>': 129, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3134584', 'n_tokens_mistral': 2921, 'n_tokens_neox': 2793, 'n_words': 746}