Example #1
0
        private List <byte[]> get_palette(List <byte[]> samples1)
        {
            //def get_palette(samples, options, return_mask= False, kmeans_iter= 40):
            //'''Extract the palette for the set of sampled RGB values. The first
            //palette entry is always the background color; the rest are determined
            //from foreground pixels by running K - means clustering.Returns the
            //palette, as well as a mask corresponding to the foreground pixels.
            var bg_color = get_bg_color(samples1, 6);
            var fg_mask  = get_fg_mask(bg_color, samples1);

            //var kmeans_iter = 40; // Can't specify this at run time. 44 iterations are run versus 40 in python.
            var kmeans = new Accord.MachineLearning.KMeans(num_colors - 1);

            //Convert List of Byte[] to double[][] for Accord KMeans
            var doubleSamples = new double[samples1.Count][];

            for (int i = 0; i < samples1.Count; i++)
            {
                doubleSamples[i] = new double[] { samples1[i][0], samples1[i][1], samples1[i][2] }
            }
            ;

            // Filter samples to only true items
            var countOfTrue     = fg_mask.Where(x => x == true).Count();
            var filteredSamples = new double[countOfTrue][];
            var c = 0;

            for (int i = 0; i < fg_mask.Count; i++)
            {
                if (fg_mask[i])
                {
                    filteredSamples[c] = new double[] { doubleSamples[i][0], doubleSamples[i][1], doubleSamples[i][2] };
                    c++;
                }
            }

            // Accord KMeans returns different values than scipy kmeans.
            var clusters = kmeans.Learn(filteredSamples);
            var palette1 = clusters.Centroids.ToList();

            palette1.Insert(0, new double[] { bg_color.R, bg_color.G, bg_color.B });

            var bytePal = new List <byte[]>();

            for (int i = 0; i < palette1.Count; i++)
            {
                bytePal.Add(new byte[] { (byte)palette1[i][0], (byte)palette1[i][1], (byte)palette1[i][2] });
            }

            return(bytePal);
        }
        /// <summary>
        /// Get the palette of an image
        /// </summary>
        /// <param name="image"></param>
        public static List <Color> GetPalette(Image image, int paletteSize)
        {
            PaletteManager.PaletteSize = paletteSize;

            // Get data for kmeans clustering
            KMeansData data = GetKMeansData(image.Pixels.Select(p => p.Color).ToList());

            // Format color data for KMeans (using Lab color space)
            double[][] formattedData = FormatData(data.means);
            double[][] centroids     = FormatData(data.centroids);

            // Perform KMeans w/additional black datapoint
            Accord.MachineLearning.KMeans kMeans = new Accord.MachineLearning.KMeans(PaletteSize + 1);
            kMeans.Centroids = centroids;

            var clusters = kMeans.Learn(formattedData);

            int[] labels = clusters.Decide(formattedData);

            // Get palette values
            double[][]   doublePalette = kMeans.Centroids;
            List <Color> Palette       = new List <Color>();

            foreach (double[] lab in doublePalette)
            {
                Palette.Add(new Color(L: lab[0], a: lab[1], b: lab[2]));
            }

            // Sort palette by luminance
            Palette = Palette.OrderBy(p => p.L).ToList();

            // Take all actual palette members
            Palette = Palette.TakeLast(PaletteSize).ToList();

            return(Palette);
        }
Example #3
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string         outputFileName = "";
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter      = "Wave File (*.wav) | *.wav|MP3 Files (*.mp3) | *.mp3|All Files (*.*) | *.*";
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                outputFileName = openFileDialog.FileName;
            }
            else
            {
                return;
            }
            if (openFileDialog.FileName.Contains(".mp3"))
            {
                outputFileName = outputFileName.Substring(0, outputFileName.Length - 3) + "wav";
                Mp3ToWav(openFileDialog.FileName, outputFileName);
            }

            //OpenFileDialog open = new OpenFileDialog();
            //open.Filter = "Wave File (*.wav) | *.wav";
            //if (open.ShowDialog() != DialogResult.OK) return;

            waveViewer1.WaveStream = new NAudio.Wave.WaveFileReader(outputFileName);
            waveViewer1.GetTotal   = true;
            waveViewer1.Refresh();

            var datalow  = waveViewer1.Datax;
            var datahigh = waveViewer1.Datay;
            //take the data and look for patterns in the frame of 8 secs
            var peaks  = detectpeak(200, datahigh, true);
            var trough = detectpeak(200, datahigh, false);

            peaks              = filterpeak(20, peaks);
            trough             = filterpeak(20, trough);
            waveViewer1.peaks  = peaks;
            waveViewer1.trough = trough;

            double[][] highamp = new double[peaks.Count][];
            for (int a = 0; a < peaks.Count - 1; a++)
            {
                highamp[a]    = new double[2];
                highamp[a][0] = peaks[a + 1] - peaks[a];
                highamp[a][1] = datahigh[peaks[a]];
            }
            highamp[peaks.Count - 1]    = new double[2];
            highamp[peaks.Count - 1][0] = 0; highamp[peaks.Count - 1][1] = 0;
            double[][] lowamp = new double[trough.Count][];
            for (int a = 0; a < trough.Count - 1; a++)
            {
                lowamp[a]    = new double[2];
                lowamp[a][0] = trough[a + 1] - trough[a];
                lowamp[a][1] = datahigh[trough[a]];
            }
            lowamp[trough.Count - 1]    = new double[2];
            lowamp[trough.Count - 1][0] = 0; lowamp[trough.Count - 1][1] = 0;
            //cluster both

            Accord.MachineLearning.KMeans gm  = new Accord.MachineLearning.KMeans(5);
            Accord.MachineLearning.KMeans gml = new Accord.MachineLearning.KMeans(5);
            var ans  = gm.Compute(highamp);
            var lans = gml.Compute(lowamp);

            var fclus = filtercluster(ans, peaks);

            cluspos = MixerControls.ToDict(fclus);
            var flans = filtercluster(lans, trough);//ignore bot

            waveViewer1.peakclus   = ans;
            waveViewer1.troughclus = lans;

            IWavePlayer play;

            play  = new NAudio.Wave.WaveOut();
            audio = new AudioFileReader(outputFileName);
            play.Init(audio);
            play.Play();
            Application.Idle += Application_Idle;
            timer1.Interval   = 20;
            timer1.Enabled    = true;
            timer1.Start();
            Rectangle workingArea = Screen.GetWorkingArea(this);

            this.Location = new Point(workingArea.Right - Size.Width - 20,
                                      workingArea.Bottom - Size.Height - 20);
            //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.TopMost = true;
            this.Opacity = .8;
            openToolStripMenuItem.Visible = false;
            menuStrip1.Visible            = false;
            this.pictureBox1.Image        = Image.FromFile("D:\\AllVSProject232015\\AudioMix\\AudioMix\\Hypnoctivity-Logo.png");
        }
Example #4
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string outputFileName = "";
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Wave File (*.wav) | *.wav|MP3 Files (*.mp3) | *.mp3|All Files (*.*) | *.*";
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                outputFileName = openFileDialog.FileName;
            }
            else 
                return;
            if (openFileDialog.FileName.Contains(".mp3"))
            {
                outputFileName = outputFileName.Substring(0, outputFileName.Length - 3) + "wav";
                Mp3ToWav(openFileDialog.FileName, outputFileName);
            }

            //OpenFileDialog open = new OpenFileDialog();
            //open.Filter = "Wave File (*.wav) | *.wav";
            //if (open.ShowDialog() != DialogResult.OK) return;
         
            waveViewer1.WaveStream = new NAudio.Wave.WaveFileReader(outputFileName);
            waveViewer1.GetTotal = true;
            waveViewer1.Refresh();

            var datalow = waveViewer1.Datax;
            var datahigh = waveViewer1.Datay;
            //take the data and look for patterns in the frame of 8 secs
            var peaks = detectpeak(200, datahigh, true);
            var trough = detectpeak(200, datahigh, false);
            peaks = filterpeak(20, peaks);
            trough = filterpeak(20, trough);
            waveViewer1.peaks = peaks;
            waveViewer1.trough = trough;

            double[][] highamp = new double[peaks.Count][]; 
            for(int a = 0; a < peaks.Count-1; a++)
            {
                highamp[a] = new double[2];
                highamp[a][0] = peaks[a+1] - peaks[a];
                highamp[a][1] = datahigh[peaks[a]];

            }
            highamp[peaks.Count - 1] = new double[2];
            highamp[peaks.Count - 1][0] = 0; highamp[peaks.Count - 1][1] = 0;
            double[][] lowamp = new double[trough.Count][]; 
            for (int a = 0; a < trough.Count-1; a++)
            {
                lowamp[a] = new double[2];
                lowamp[a][0] = trough[a+1] - trough[a];
                lowamp[a][1] = datahigh[trough[a]];

            }
            lowamp[trough.Count - 1] = new double[2];
            lowamp[trough.Count - 1][0] = 0; lowamp[trough.Count - 1][1] = 0;
            //cluster both

            Accord.MachineLearning.KMeans gm = new Accord.MachineLearning.KMeans(5);
            Accord.MachineLearning.KMeans gml = new Accord.MachineLearning.KMeans(5);
            var ans = gm.Compute(highamp);
            var lans = gml.Compute(lowamp);

            var fclus = filtercluster(ans, peaks);
            cluspos = MixerControls.ToDict(fclus);
            var flans = filtercluster(lans, trough);//ignore bot

            waveViewer1.peakclus = ans;
            waveViewer1.troughclus = lans;

            IWavePlayer play;
            
            play = new NAudio.Wave.WaveOut();
            audio = new AudioFileReader(outputFileName); 
            play.Init(audio);
            play.Play();
            Application.Idle += Application_Idle;
            timer1.Interval = 20;
            timer1.Enabled = true;
            timer1.Start();
            Rectangle workingArea = Screen.GetWorkingArea(this);
            this.Location = new Point(workingArea.Right - Size.Width-20,
                                      workingArea.Bottom - Size.Height - 20);
            //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.TopMost = true;
            this.Opacity = .8;
            openToolStripMenuItem.Visible = false;
            menuStrip1.Visible = false;
            this.pictureBox1.Image = Image.FromFile("D:\\AllVSProject232015\\AudioMix\\AudioMix\\Hypnoctivity-Logo.png");
        }