Ejemplo n.º 1
0
        /// <summary>
        /// Adds a new image to the image set.
        /// </summary>
        /// <param name="nIdx">Specifies the index on where to add the image.</param>
        /// <param name="d">Specifies the image data.</param>
        /// <returns>If added successfully within the load limit, <i>true</i> is returned, otherwise <i>false</i> is returned.</returns>
        public bool Add(int nIdx, SimpleDatum d)
        {
            if (m_nLoadLimit > 0 && m_rgImagesLimitLoaded.Count == m_nLoadLimit)
            {
                return(false);
            }

            m_rgImages[nIdx] = d;
            m_nLoadedCount++;

            if (m_nLoadLimit > 0)
            {
                m_rgImagesLimitLoaded.Add(d);
            }

            bool bAdded = false;

            foreach (LabelSet ls in m_rgLabelSet)
            {
                if (ls.Label.ActiveLabel == d.Label)
                {
                    ls.Add(d);
                    bAdded = true;
                    break;
                }
            }

            if (!bAdded)
            {
                LabelSet ls = new LabelSet(new LabelDescriptor(d.Label, d.Label, "label #" + d.Label.ToString(), 0));
                ls.Add(d);
                m_rgLabelSet.Add(ls);
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns the image based on its label and image selection method.
        /// </summary>
        /// <param name="nIdx">Specifies the image index to use when loading sequentially.</param>
        /// <param name="labelSelectionMethod">Specifies the label selection method.</param>
        /// <param name="imageSelectionMethod">Specifies the image selection method.</param>
        /// <param name="log">Specifies the Log for status output.</param>
        /// <returns>The SimpleDatum containing the image is returned.</returns>
        public SimpleDatum GetImage(int nIdx, IMGDB_LABEL_SELECTION_METHOD labelSelectionMethod, IMGDB_IMAGE_SELECTION_METHOD imageSelectionMethod, Log log)
        {
            lock (m_syncObj)
            {
                SimpleDatum[] rgImages = m_rgImages;

                if (m_nLoadLimit > 0 && m_rgImagesLimitLoaded.Count == m_nLoadLimit)
                {
                    rgImages = m_rgImagesLimitLoaded.ToArray();
                }

                if (rgImages.Length == 0)
                {
                    throw new Exception("There are no images in the dataset '" + m_src.Name + "' to select from!");
                }

                SimpleDatum sd = null;

                if ((labelSelectionMethod & IMGDB_LABEL_SELECTION_METHOD.RANDOM) == IMGDB_LABEL_SELECTION_METHOD.RANDOM)
                {
                    if (m_rgLabelSet.Count == 0)
                    {
                        throw new Exception("There are no label specified in the Labels table for the dataset '" + m_src.Name + "'!");
                    }

                    LabelSet labelSet = getLabelSet(labelSelectionMethod);
                    sd = labelSet.GetImage(nIdx, imageSelectionMethod);
                }

                int nImageIdx = 0;

                if (sd == null)
                {
                    sd = LabelSet.GetImage(rgImages, rgImages.Length, nIdx, m_random, imageSelectionMethod, ref m_nLastIdx, ref m_nFixedIndex, out nImageIdx);
                }


                //-----------------------------------------
                //  Handle dynamic loading of the image.
                //-----------------------------------------

                if (sd == null)
                {
                    int nRetries = 1;

                    if ((imageSelectionMethod & IMGDB_IMAGE_SELECTION_METHOD.RANDOM) == IMGDB_IMAGE_SELECTION_METHOD.RANDOM)
                    {
                        nRetries = 5;
                    }

                    for (int i = 0; i < nRetries; i++)
                    {
                        sd = m_factory.LoadImageAt(nImageIdx);
                        if (sd != null)
                        {
                            Add(nImageIdx, sd);
                            break;
                        }

                        if (i < nRetries - 1)
                        {
                            nImageIdx = m_random.Next(rgImages.Length);
                        }
                    }

                    if (sd == null)
                    {
                        log.WriteLine("WARNING! The dataset needs to be re-indexed. Could not find the image at index " + nImageIdx.ToString() + " - attempting several random queries to get an image.");
                    }
                }

                return(sd);
            }
        }