Пример #1
0
        /// <summary>
        /// Convert the source image to the current image, if the size are different, the current image will be a resized version of the srcImage.
        /// </summary>
        /// <typeparam name="TSrcColor">The color type of the source image</typeparam>
        /// <typeparam name="TSrcDepth">The color depth of the source image</typeparam>
        /// <param name="srcImage">The sourceImage</param>
        public void ConvertFrom <TSrcColor, TSrcDepth>(OclImage <TSrcColor, TSrcDepth> srcImage)
            where TSrcColor : struct, IColor
            where TSrcDepth : new()
        {
            if (!Size.Equals(srcImage.Size))
            { //if the size of the source image do not match the size of the current image
                using (OclImage <TSrcColor, TSrcDepth> tmp = srcImage.Resize(Size, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR))
                {
                    ConvertFrom(tmp);
                    return;
                }
            }

            if (typeof(TColor) == typeof(TSrcColor))
            {
                #region same color
                if (typeof(TDepth) == typeof(TSrcDepth)) //same depth
                {
                    OclInvoke.Copy(srcImage.Ptr, Ptr, IntPtr.Zero);
                }
                else //different depth
                {
                    if (typeof(TDepth) == typeof(Byte) && typeof(TSrcDepth) != typeof(Byte))
                    {
                        double[] minVal, maxVal;
                        Point[]  minLoc, maxLoc;
                        srcImage.MinMax(out minVal, out maxVal, out minLoc, out maxLoc);
                        double min = minVal[0];
                        double max = maxVal[0];
                        for (int i = 1; i < minVal.Length; i++)
                        {
                            min = Math.Min(min, minVal[i]);
                            max = Math.Max(max, maxVal[i]);
                        }
                        double scale = 1.0, shift = 0.0;
                        if (max > 255.0 || min < 0)
                        {
                            scale = (max == min) ? 0.0 : 255.0 / (max - min);
                            shift = (scale == 0) ? min : -min * scale;
                        }

                        OclInvoke.ConvertTo(srcImage.Ptr, Ptr, scale, shift);
                    }
                    else
                    {
                        OclInvoke.ConvertTo(srcImage.Ptr, Ptr, 1.0, 0.0);
                    }
                }
                #endregion
            }
            else
            {
                #region different color
                if (typeof(TDepth) == typeof(TSrcDepth))
                { //same depth
                    ConvertColor(srcImage.Ptr, Ptr, typeof(TSrcColor), typeof(TColor), Size);
                }
                else
                {                                                                                     //different depth
                    using (OclImage <TSrcColor, TDepth> tmp = srcImage.Convert <TSrcColor, TDepth>()) //convert depth
                        ConvertColor(tmp.Ptr, Ptr, typeof(TSrcColor), typeof(TColor), Size);
                }
                #endregion
            }
        }