Work with matrix in emgu cv

前端 未结 2 1738
后悔当初
后悔当初 2021-01-26 19:22

I have a matrix (2D) of an image in EMGU cv,how can I fill the rest of the matrix with zeros but keep a certain area(rectangle) with the original data?

2条回答
  •  梦谈多话
    2021-01-26 19:43

        // Rectangle's parameters
        static int x = 3;
        static int y = 3;
        static int width = 2;
        static int height = 2;
    
        Rectangle specificArea = new Rectangle(x, y, width, height);
    
        // Your image matrix; 20x20 just a sample
        Matrix imageMatrix = new Matrix(20, 20);
    
    
        public Matrix cropMatrix()
        {
            // Crop a specific area from image matrix
            Matrix specificMatrix = imageMatrix.GetSubRect(specificArea);
    
            // Matrix with full of zeros and same size with imageMatrix
            Matrix croppedMatrix = imageMatrix.CopyBlank();
    
            for (int i = x; i < x+width; i++)
            {
                for (int j = y; j < y+height; j++)
                {
                    // Set croppedMatrix with saved values
                    croppedMatrix[i, j] = specificMatrix[i-x, j-y];
                }
            }
    
            return croppedMatrix;
        }
    

提交回复
热议问题