开发者

emgu cv matrix access row and cloums with channel c#

开发者 https://www.devze.com 2023-03-05 21:15 出处:网络
How can I access a matrix with a specified row, column and channel? Matrix<double> tensor = new Matrix<double>(yMax + 1, xMax, 4); //4 channels

How can I access a matrix with a specified row, column and channel?

Matrix<double> tensor = new Matrix<double>(yMax + 1, xMax, 4); //4 channels
CvInvoke.cvZero(tensor);
for(int k  = 0; k<x.Count; ++k)
{
    double gx = Math.Cos(angle[k] * Math.PI / 180 + 90 * Math.PI / 180 + Math.PI);
    double gy = Math.Sin(angle[k] * Math.PI / 180 + 90 * Math.PI / 180 + Math.PI);
    tensor[y[k], x[k]] = gx * gx; //How can I access other channels?
    tensor.Data[y[k], x[k] + 1] = gx * gy; //How can I access开发者_JAVA技巧 other channels?
    tensor.Data[y[k], x[k] + 2] = gx * gy; //How can I access other channels?
    tensor.Data[y[k], x[k] + 3] = gy * gy; //How can I access other channels?
}


I think you should give a look at Split() method and then loop on channels array.


Here is an example of getting pixel value from two separate channels of Mat:

var flowResult = new Mat();
CvInvoke.CalcOpticalFlowFarneback(_scaledDownFrameOneColorImagePrev, scaledDownFrameOneColorImage, flowResult, 0.25, 3, 15, 5, 1, 1.2, OpticalflowFarnebackFlag.Default);

var flowResultChannels = flowResult.Split();
var flowResultX = flowResultChannels[0];
var flowResultY = flowResultChannels[1];

for (var r = 0; r < flowResult.Rows; r++)
{
    for (var c = 0; c < flowResult.Cols; c++)
    {
        var xValue = new float[1];
        Marshal.Copy(flowResultX.DataPointer + (((r * flowResultX.Cols) + c) * flowResultX.ElementSize), xValue, 0, 1);

        var yValue = new float[1];
        Marshal.Copy(flowResultY.DataPointer + (((r * flowResultY.Cols) + c) * flowResultY.ElementSize), yValue, 0, 1);

        if (Math.Abs(xValue[0]) > 3 || Math.Abs(yValue[0]) > 3)
        {
            Console.WriteLine("{0} {1}", xValue[0], yValue[0]);
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消