publicstaticvoidReadTensorFromMatBgr(Mat image, Tensor tensor, int inputHeight =-1, int inputWidth =-1, float inputMean =0.0f, float scale =1.0f){//if (image.NumberOfChannels != 3)//{// throw new ArgumentException("Input must be 3 channel BGR image");//}
Emgu.CV.CvEnum.DepthType depth = image.Depth;if(!(depth == Emgu.CV.CvEnum.DepthType.Cv8U || depth == Emgu.CV.CvEnum.DepthType.Cv32F)){thrownewArgumentException("Input image must be 8U or 32F");}//resize
int finalHeight = inputHeight ==-1? image.Height : inputHeight;
int finalWidth = inputWidth ==-1? image.Width : inputWidth;
Size finalSize =newSize(finalWidth, finalHeight);//int[] dim = tensor.Dims;//if (dim[0] != 1)// throw new ArgumentException("First dimension of the tensor must be 1 (batch size)");//if (dim[1] != finalHeight)// throw new ArgumentException("Second dimension of the tensor must match the input height");//if (dim[2] != finalWidth)// throw new ArgumentException("Third dimension of the tensor must match the input width");//if (dim[3] != 3)// throw new ArgumentException("Fourth dimension of the tensor must be 3 (BGR has 3 channels)");if(image.Size != finalSize){using(Mat tmp =newMat()){
CvInvoke.Resize(image, tmp, finalSize);ReadTensorFromMatBgr(tmp, inputMean, scale, tensor);
tmp.Dispose();}}else{ReadTensorFromMatBgr(image, inputMean, scale, tensor);}}privatestaticvoidReadTensorFromMatBgr(Mat image, float inputMean, float scale, Tensor t){
DataType type = t.Type;if(type == DataType.Float32){using(Mat matF =newMat(image.Size, Emgu.CV.CvEnum.DepthType.Cv32F,3, t.DataPointer,sizeof(float)*3* image.Width)){
image.ConvertTo(matF, Emgu.CV.CvEnum.DepthType.Cv32F);}}elseif(type == DataType.UInt8){using(Mat matB =newMat(image.Size, Emgu.CV.CvEnum.DepthType.Cv8U,3, t.DataPointer,sizeof(byte)*3* image.Width)){if(scale ==1.0f && inputMean ==0){
image.CopyTo(matB);}else
CvInvoke.ConvertScaleAbs(image, matB, scale,-inputMean * scale);}}else{thrownewException(String.Format("Data Type of {0} is not supported.", type));}}}