4
Answers

a small problem, Object reference not set to an instance of an object

Bahar Jalali

Bahar Jalali

11y
3.9k
1
hi
i want to run this source code but i can't;

here is code URL:
http://www.oliveragustin.com/background-subtraction-in-c-part-2/

a
nd it's the source code :

var image = new Image<Bgr, byte>(filename);


            // threshold values for each channel
            var thresholds = new[] { new TRange<int>(70, 255), new TRange<int>(0, 255), new TRange<int>(0, 255) };


            Image<Gray, byte>[] grays = image.Convert<Lab, byte>().Split();


            // threshold each channel
            for (int i = 0; i < 3; i++)
            {
                grays[i]._ThresholdToZero(new Gray(thresholds[i].LowerValue));
                grays[i]._ThresholdToZeroInv(new Gray(thresholds[i].UpperValue));
                grays[i]._ThresholdBinary(new Gray(1), new Gray(255));
            }


            // merge the thresholded array of binary images above
            var mask = grays[0].And(grays[1].And(grays[2]));


            // result
            var resultImage = image.Copy(mask);




my problem is this line :
var thresholds = new [] {new TRange<int>(70,255), new TRange<int>(0,255), new TRange<int>(0,255)};

i don't know how must i define above line in c#! what is TRange? i defined it like the following code but it has error
"Object reference not set to an instance of an object";

 
        public struct TRange<T>
        {
            public int LowerValue, UpperValue;

            public TRange(int x, int y)
            {
                LowerValue = x;
                UpperValue = y;
            }
        }


        // background subtraction in NewBGSubtraction
        private void NewBGSubtraction(Bitmap filename, bool displayResult)
        {
            var image = new Image<Bgr, byte>(filename);

            // threshold values for each channel
            var thresholds = new[] {new TRange<int>(70, 255), new TRange<int>(0, 255), new TRange<int>(0, 255) };

            Image<Gray, byte>[] grays = image.Convert<Lab, byte>().Split();

            // threshold each channel
            for (int i = 0; i < 3; i++)
            {
                grays[i]._ThresholdToZero(new Gray(thresholds[i].LowerValue));
                grays[i]._ThresholdToZeroInv(new Gray(thresholds[i].UpperValue));
                grays[i]._ThresholdBinary(new Gray(1), new Gray(255));
            }

            // merge the thresholded array of binary images above
            var mask = grays[0].And(grays[1].And(grays[2]));

            // result
            var resultImage = image.Copy(mask);
            pictureBox2.Image = resultImage.Bitmap;
        }

Answers (4)