本文整理汇总了C#中Mat.Split方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.Split方法的具体用法?C# Mat.Split怎么用?C# Mat.Split使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mat
的用法示例。
在下文中一共展示了Mat.Split方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SubSampling
public static Mat SubSampling(Mat img)
{
ImageViewer.Show(img, String.Format(
"Canny Gray"));
//img = new Mat("diplomka.png", LoadImageType.Color);
var colorChannels = img.Split();
var rSobelX = new Mat(img.Size, DepthType.Cv8U, 1);
var rSobelY = new Mat(img.Size, DepthType.Cv8U, 1);
var gSobelX = new Mat(img.Size, DepthType.Cv8U, 1);
var gSobelY = new Mat(img.Size, DepthType.Cv8U, 1);
var bSobelX = new Mat(img.Size, DepthType.Cv8U, 1);
var bSobelY = new Mat(img.Size, DepthType.Cv8U, 1);
CvInvoke.Sobel(colorChannels[2], rSobelX, DepthType.Cv8U, 1, 0);
CvInvoke.Sobel(colorChannels[2], rSobelY, DepthType.Cv8U, 0, 1);
CvInvoke.Sobel(colorChannels[1], gSobelX, DepthType.Cv8U, 1, 0);
CvInvoke.Sobel(colorChannels[1], gSobelY, DepthType.Cv8U, 0, 1);
CvInvoke.Sobel(colorChannels[0], bSobelX, DepthType.Cv8U, 1, 0);
CvInvoke.Sobel(colorChannels[0], bSobelY, DepthType.Cv8U, 0, 1);
int cols = img.Cols;
int rows = img.Rows;
var edgeValues = new double[rows, cols];
for(int rowIndex = 0; rowIndex < rows; rowIndex++)
{
for(int columnIndex = 0; columnIndex < cols; columnIndex++)
{
var rX = rSobelX.GetData(rowIndex, columnIndex)[0];
var rY = rSobelY.GetData(rowIndex, columnIndex)[0];
var gX = gSobelX.GetData(rowIndex, columnIndex)[0];
var gY = gSobelY.GetData(rowIndex, columnIndex)[0];
var bX = bSobelX.GetData(rowIndex, columnIndex)[0];
var bY = bSobelY.GetData(rowIndex, columnIndex)[0];
var edgeRed = Math.Sqrt(rX*rX + rY*rY);
var edgeGreen = Math.Sqrt(gX*gX + gY*gY);
var edgeBlue = Math.Sqrt(bX*bX + bY*bY);
var edgeValue = (new double[]{edgeRed, edgeGreen, edgeBlue}).Max();
edgeValues[rowIndex, columnIndex] = edgeValue;
}
}
var samples = new byte[rows, cols, 3];
var samplesList = new List<RGBSample>();
var samplesHistogram = new RGBSample[256, 256, 256];
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
for (int columnIndex = 0; columnIndex < cols; columnIndex++)
{
var isLocalMinima = false;
if (rowIndex > 0
&& rowIndex < rows - 1
&& columnIndex > 0
&& columnIndex < cols - 1 )
{
isLocalMinima = IsLocalMinima(new Point(rowIndex, columnIndex), edgeValues);
}
if(!isLocalMinima)
{
samples[rowIndex, columnIndex, 2] = 255;
samples[rowIndex, columnIndex, 1] = 255;
samples[rowIndex, columnIndex, 0] = 255;
}
else
{
var bytes = img.GetData(rowIndex, columnIndex);
var r = bytes[2];
var g = bytes[1];
var b = bytes[0];
samples[rowIndex, columnIndex, 2] = r;
samples[rowIndex, columnIndex, 1] = g;
samples[rowIndex, columnIndex, 0] = b;
if(samplesHistogram[r , g, b] == null)
{
var sample = new RGBSample() { Red = r, Green = g, Blue = b};
samplesHistogram[r, g, b] = sample;
samplesList.Add(sample);
}
samplesHistogram[r, g, b].Points.Add(new Point(rowIndex, columnIndex));
}
}
}
Image<Bgr, byte> img4 = new Image<Bgr, byte>(samples);
ImageViewer.Show(img4, String.Format(
"44 Gray"));
//.........这里部分代码省略.........
示例2: detectHair
void detectHair(PersonFace personFace, Mat hairImage)
{
Rect faceRect = personFace.GetFace();
double adjWidth = faceRect.width * 0.85;
double adjHeight = faceRect.height * 1.2;
double adjX = faceRect.x + (faceRect.width - adjWidth) / 2;
double adjY = faceRect.y + (faceRect.height - adjHeight) / 2;
Rect adjFaceRect = new Rect((int)adjX, (int)adjY, (int)adjWidth, (int)adjHeight);
double[] faceLineData = personFace.GetFaceLineData();
PointGenerator faceLine = new PointGenerator(faceLineData[0], faceLineData[1]);
Point faceTopPoint = faceLine.GetFromY(adjFaceRect.y);
Point faceBottomPoint = faceLine.GetFromY(adjFaceRect.y + adjFaceRect.height);
//Draw face line
//Imgproc.line(hairImage, faceTopPoint, faceBottomPoint, new Scalar(255, 0, 0), 2);
//Get face feature angle
double faceFeatureAngle = Math.Atan(faceLineData[0]);
faceFeatureAngle = RadianToDegree(faceFeatureAngle);
faceFeatureAngle += faceFeatureAngle > 0 ? -90 : 90;
//Imgproc.rectangle(hairImage, adjFaceRect, new Scalar(0, 255, 255), 2);
/*Imgproc.ellipse(hairImage,
new Point(adjFaceRect.x + adjFaceRect.width / 2, adjFaceRect.y + adjFaceRect.height / 2),
new Size(adjFaceRect.width/2, adjFaceRect.height/2), faceFeatureAngle, 0, 360, new Scalar(255, 0, 0), 2);
Imgproc.ellipse(hairImage,
new Point(adjFaceRect.x + adjFaceRect.width / 2, adjFaceRect.y + adjFaceRect.height / 2),
new Size((int)(adjFaceRect.width / 1.8), (int)(adjFaceRect.height / 1.8)), faceFeatureAngle, 0, 360, new Scalar(255, 0, 0), 2);*/
Mat[] imgComponents = hairImage.Split();
for (int i = 0; i < 5; i++)
{
double factor = 1.8 - i * 0.2;
Mat maskImage = new Image<Gray, byte>(hairImage.width(), hairImage.height(), new Gray(0)).Mat;
Imgproc.ellipse(maskImage,
new Point(adjFaceRect.x + adjFaceRect.width / 2, adjFaceRect.y + adjFaceRect.height / 2),
new Size((int)(adjFaceRect.width / factor), (int)(adjFaceRect.height / factor)), faceFeatureAngle + 180, 0, 180, new Scalar(255), -1);
Imgproc.ellipse(maskImage,
new Point(adjFaceRect.x + adjFaceRect.width / 2, adjFaceRect.y + adjFaceRect.height / 2),
new Size(adjFaceRect.width / 2, adjFaceRect.height / 2), faceFeatureAngle, 0, 360, new Scalar(0), -1);
//imageBox13.Image = maskImage;
Mat testImg = new Mat();
hairImage.CopyTo(testImg, maskImage);
imageBox13.Image = testImg;
Stack<string> titleStack = new Stack<string>();
titleStack.Push("Red");
titleStack.Push("Green");
titleStack.Push("Blue");
HistogramForm histForm = new HistogramForm();
foreach (Mat img in imgComponents)
{
//try histogram only the upper half to detect the most probable hair color range
Mat hist = new Mat();
CvInvoke.CalcHist(new VectorOfMat(img), new int[] { 0 }, maskImage, hist, new int[] { 256 }, new float[] { 0, 255 }, false);
string color = titleStack.Pop();
histForm.AddHist(hist, color, System.Drawing.Color.FromName(color));
/*byte[] testBuffer = new byte[256];
hist.CopyTo(testBuffer);
string msg = "";
foreach (byte value in testBuffer)
msg += value + " ";
msg += Environment.NewLine;
msg += Environment.NewLine;
textBox1.AppendText(msg);*/
}
histForm.Show(i.ToString());
}
Image<Bgr, byte> testImg2 = new Image<Bgr, byte>(hairImage.Bitmap);
imageBox13.Image = testImg2.InRange(new Bgr(25, 25, 25), new Bgr(100, 85, 100));
//createHistogram(new Image<Bgr, byte>(maskImage.Bitmap), 256, "teste");
//.........这里部分代码省略.........
示例3: EdgePreservingSmoothing
public static Mat EdgePreservingSmoothing(Mat img)
{
var colorChannels = img.Split();
var rChannel = colorChannels[2];
var gChannel = colorChannels[1];
var bChannel = colorChannels[0];
int cols = img.Cols;
int rows = img.Rows;
//result image will be smaller because the pixels on the border have less then 8 neighbours.
//we are going to ignore them
Mat result = new Mat(img.Rows - 2, img.Cols - 2, img.Depth, 1);
byte[, ,] resultValues = new byte[img.Rows, img.Cols, 3];
for(int rowIndex = 0; rowIndex < rows; rowIndex++)
{
for(int columnIndex = 0; columnIndex < cols; columnIndex++)
{
if(rowIndex == 0
|| rowIndex == rows - 1
|| columnIndex == 0
|| columnIndex == cols - 1)
{
//pixel is on the border
resultValues[rowIndex, columnIndex, 0] = 0;
resultValues[rowIndex, columnIndex, 1] = 0;
resultValues[rowIndex, columnIndex, 2] = 0;
continue;
}
var newValue = ComputeManhattanColorDistances(rChannel, gChannel, bChannel, new Point(rowIndex, columnIndex), 10);
resultValues[rowIndex, columnIndex, 0] = Convert.ToByte(newValue[2]);
resultValues[rowIndex, columnIndex, 1] = Convert.ToByte(newValue[1]);
resultValues[rowIndex, columnIndex, 2] = Convert.ToByte(newValue[0]);
}
}
Image<Bgr, byte> img2 = new Image<Bgr, byte>(resultValues);
//ImageViewer.Show(img2, String.Format(
// "Img Gray"));
return img2.Mat;
}
示例4: EdgePreservingSmoothing
public static byte[, ,] EdgePreservingSmoothing(Mat img, VectorOfPoint points, int numberOfCycles = 5)
{
byte[, ,] resultValues = new byte[img.Rows, img.Cols, 3];
for (int i = 0; i < 1; i++)
{
var colorChannels = img.Split();
var rChannel = colorChannels[2];
var gChannel = colorChannels[1];
var bChannel = colorChannels[0];
int cols = img.Cols;
int rows = img.Rows;
//result image will be smaller because the pixels on the border have less then 8 neighbours.
//we are going to ignore them
Mat result = new Mat(img.Rows - 2, img.Cols - 2, img.Depth, 1);
for (int pointIndex = 0; pointIndex < points.Size; pointIndex++)
{
var point = points[pointIndex];
if (point.Y == 0
|| point.Y == rows - 1
|| point.X == 0
|| point.X == cols - 1)
{
//pixel is on the border
resultValues[point.Y, point.X, 0] = 0;
resultValues[point.Y, point.X, 1] = 0;
resultValues[point.Y, point.X, 2] = 0;
continue;
}
var newValue = ComputeManhattanColorDistances(rChannel, gChannel, bChannel, new System.Drawing.Point(point.Y, point.X), 10);
resultValues[point.Y, point.X, 0] = Convert.ToByte(newValue[2]);
resultValues[point.Y, point.X, 1] = Convert.ToByte(newValue[1]);
resultValues[point.Y, point.X, 2] = Convert.ToByte(newValue[0]);
}
Image<Bgr, byte> img2 = new Image<Bgr, byte>(resultValues);
img = img2.Mat;
}
//ImageViewer.Show(img2, String.Format(
// "Img Gray"));
return resultValues;
}
示例5: ComputeEdgeMagnitudes
public static double[, ,] ComputeEdgeMagnitudes(Mat img)
{
var colorChannels = img.Split();
var rSobelX = new Mat(img.Size, DepthType.Cv8U, 1);
var rSobelY = new Mat(img.Size, DepthType.Cv8U, 1);
var gSobelX = new Mat(img.Size, DepthType.Cv8U, 1);
var gSobelY = new Mat(img.Size, DepthType.Cv8U, 1);
var bSobelX = new Mat(img.Size, DepthType.Cv8U, 1);
var bSobelY = new Mat(img.Size, DepthType.Cv8U, 1);
CvInvoke.Sobel(colorChannels[2], rSobelX, DepthType.Cv8U, 1, 0);
CvInvoke.Sobel(colorChannels[2], rSobelY, DepthType.Cv8U, 0, 1);
CvInvoke.Sobel(colorChannels[1], gSobelX, DepthType.Cv8U, 1, 0);
CvInvoke.Sobel(colorChannels[1], gSobelY, DepthType.Cv8U, 0, 1);
CvInvoke.Sobel(colorChannels[0], bSobelX, DepthType.Cv8U, 1, 0);
CvInvoke.Sobel(colorChannels[0], bSobelY, DepthType.Cv8U, 0, 1);
int cols = img.Cols;
int rows = img.Rows;
var edgeValues = new double[rows, cols, 1];
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
for (int columnIndex = 0; columnIndex < cols; columnIndex++)
{
var rX = rSobelX.GetData(rowIndex, columnIndex)[0];
var rY = rSobelY.GetData(rowIndex, columnIndex)[0];
var gX = gSobelX.GetData(rowIndex, columnIndex)[0];
var gY = gSobelY.GetData(rowIndex, columnIndex)[0];
var bX = bSobelX.GetData(rowIndex, columnIndex)[0];
var bY = bSobelY.GetData(rowIndex, columnIndex)[0];
var edgeRed = Math.Sqrt(rX * rX + rY * rY);
var edgeGreen = Math.Sqrt(gX * gX + gY * gY);
var edgeBlue = Math.Sqrt(bX * bX + bY * bY);
var edgeValue = (new double[] { edgeRed, edgeGreen, edgeBlue }).Max();
edgeValues[rowIndex, columnIndex, 0] = edgeValue;
}
}
return edgeValues;
}
示例6: SubSampling
public async Task<byte[, ,]> SubSampling(Mat img, VectorOfPoint points)
{
//ProcessingImageWorkflow.Add(ToBitmapSource(img));
//img = new Mat("diplomka.png", LoadImageType.Color);
var colorChannels = img.Split();
var rSobelX = new Mat(img.Size, DepthType.Cv8U, 1);
var rSobelY = new Mat(img.Size, DepthType.Cv8U, 1);
var gSobelX = new Mat(img.Size, DepthType.Cv8U, 1);
var gSobelY = new Mat(img.Size, DepthType.Cv8U, 1);
var bSobelX = new Mat(img.Size, DepthType.Cv8U, 1);
var bSobelY = new Mat(img.Size, DepthType.Cv8U, 1);
CvInvoke.Sobel(colorChannels[2], rSobelX, DepthType.Cv8U, 1, 0);
CvInvoke.Sobel(colorChannels[2], rSobelY, DepthType.Cv8U, 0, 1);
CvInvoke.Sobel(colorChannels[1], gSobelX, DepthType.Cv8U, 1, 0);
CvInvoke.Sobel(colorChannels[1], gSobelY, DepthType.Cv8U, 0, 1);
CvInvoke.Sobel(colorChannels[0], bSobelX, DepthType.Cv8U, 1, 0);
CvInvoke.Sobel(colorChannels[0], bSobelY, DepthType.Cv8U, 0, 1);
int cols = img.Cols;
int rows = img.Rows;
var edgeValues = new double[rows, cols];
var edgeValuesTest = new byte[rows, cols, 1];
for (int pointIndex = 0; pointIndex < points.Size; pointIndex++)
{
var point = points[pointIndex];
var rX = rSobelX.GetData(point.Y, point.X)[0];
var rY = rSobelY.GetData(point.Y, point.X)[0];
var gX = gSobelX.GetData(point.Y, point.X)[0];
var gY = gSobelY.GetData(point.Y, point.X)[0];
var bX = bSobelX.GetData(point.Y, point.X)[0];
var bY = bSobelY.GetData(point.Y, point.X)[0];
var edgeRed = Math.Sqrt(rX * rX + rY * rY);
var edgeGreen = Math.Sqrt(gX * gX + gY * gY);
var edgeBlue = Math.Sqrt(bX * bX + bY * bY);
var edgeValue = (new double[] { edgeRed, edgeGreen, edgeBlue }).Max();
edgeValuesTest[point.Y, point.X, 0] = ToByte(edgeValue);
edgeValues[point.Y, point.X] = edgeValue;
}
Image<Gray, byte> edGE = new Image<Gray, byte>(edgeValuesTest);
await Dispatcher.BeginInvoke(_addImageToTheList,
edGE.Mat);
var samples = new byte[rows, cols, 3];
var samplesList = new List<RGBSample>();
var samplesHistogram = new RGBSample[256, 256, 256];
for (int pointIndex = 0; pointIndex < points.Size; pointIndex++)
{
var point = points[pointIndex];
var isLocalMinima = false;
if (point.Y > 0
&& point.Y < rows - 1
&& point.X > 0
&& point.X < cols - 1)
{
isLocalMinima = IsLocalMinima(new System.Drawing.Point(point.Y, point.X), edgeValues);
}
if (!isLocalMinima)
{
samples[point.Y, point.X, 2] = 255;
samples[point.Y, point.X, 1] = 255;
samples[point.Y, point.X, 0] = 255;
}
else
{
var bytes = img.GetData(point.Y, point.X);
var r = bytes[2];
var g = bytes[1];
var b = bytes[0];
samples[point.Y, point.X, 2] = r;
samples[point.Y, point.X, 1] = g;
samples[point.Y, point.X, 0] = b;
if (samplesHistogram[r, g, b] == null)
{
var sample = new RGBSample() { Red = r, Green = g, Blue = b };
samplesHistogram[r, g, b] = sample;
samplesList.Add(sample);
}
samplesHistogram[r, g, b].Points.Add(new Point(point.Y, point.X));
}
//.........这里部分代码省略.........
示例7: MakeImagesForArticle
private static void MakeImagesForArticle()
{
var resizeK = 0.2;
var dir = "Example/";
var src = new Mat("0.bmp");
var src_g = new Mat("0.bmp", LoadMode.GrayScale);
var src_1 = new Mat("1.bmp");
var src_1_g = new Mat("1.bmp", LoadMode.GrayScale);
var background = new Mat("background.bmp");
var background_g = new Mat("background.bmp", LoadMode.GrayScale);
src.Resize(resizeK).ImWrite(dir + "0.png");
src_g.Resize(resizeK).ImWrite(dir + "0 g.png");
src_g.ThresholdStairs().Resize(resizeK).ImWrite(dir + "0 g th.png");
var canny = new Mat();
Cv2.Canny(src_g, canny, 50, 200);
canny.Resize(0.5).ImWrite(dir + "0 canny.png");
Mat[] src_channels;
Cv2.Split(src, out src_channels);
for (var i = 0; i < src_channels.Length; ++i)
{
var channels = Enumerable.Range(0, src_channels.Length).Select(j => new Mat(src_channels[0].Rows, src_channels[0].Cols, src_channels[0].Type())).ToArray();
channels[i] = src_channels[i];
var dst = new Mat();
Cv2.Merge(channels, dst);
dst.Resize(resizeK).ImWrite(dir + string.Format("0 ch{0}.png", i));
src_channels[i].ThresholdStairs().Resize(resizeK).ImWrite(dir + string.Format("0 ch{0} th.png", i));
}
if (true)
{
src.Resize(0.4).ImWrite(dir + "0.png");
src_1.Resize(0.4).ImWrite(dir + "1.png");
background.Resize(0.4).ImWrite(dir + "bg.png");
var dst_01 = new Mat();
Cv2.Absdiff(src, src_1, dst_01);
dst_01.Resize(resizeK).ImWrite(dir + "01.png");
dst_01.Cut(new Rect(50, src.Height * 4 / 5 - 50, src.Width / 5, src.Height / 5)).ImWrite(dir + "01 part.png");
dst_01.Cut(new Rect(50, src.Height * 4 / 5 - 50, src.Width / 5, src.Height / 5)).CvtColor(ColorConversion.RgbToGray).ImWrite(dir + "01 g.png");
dst_01.CvtColor(ColorConversion.RgbToGray).ThresholdStairs().Resize(resizeK).ImWrite(dir + "01 g th.png");
var dst_01_g = new Mat();
Cv2.Absdiff(src_g, src_1_g, dst_01_g);
dst_01_g.Cut(new Rect(50, src.Height * 4 / 5 - 50, src.Width / 5, src.Height / 5)).ImWrite(dir + "0g1g.png");
dst_01_g.ThresholdStairs().Resize(resizeK).ImWrite(dir + "0g1g th.png");
}
if (true)
{
var dst_0b = new Mat();
Cv2.Absdiff(src, background, dst_0b);
dst_0b.Resize(0.6).ImWrite(dir + "0b.png");
var dst_0b_g = new Mat();
Cv2.Absdiff(src_g, background_g, dst_0b_g);
dst_0b_g.Resize(0.3).ImWrite(dir + "0b g.png");
dst_0b_g.ThresholdStairs().Resize(0.3).ImWrite(dir + "0b g th.png");
}
if (true)
{
var hsv_src = new Mat();
Cv2.CvtColor(src, hsv_src, ColorConversion.RgbToHsv);
var hsv_background = new Mat();
Cv2.CvtColor(background, hsv_background, ColorConversion.RgbToHsv);
var hsv_background_channels = hsv_background.Split();
var hsv_src_channels = hsv_src.Split();
if (true)
{
var all = new Mat(src.ToIplImage(), true);
for (var i = 0; i < hsv_src_channels.Length; ++i)
{
hsv_src_channels[i].CvtColor(ColorConversion.GrayToRgb).CopyTo(all, new Rect(i * src.Width / 3, src.Height / 2, src.Width / 3, src.Height / 2));
}
src_g.CvtColor(ColorConversion.GrayToRgb).CopyTo(all, new Rect(src.Width / 2, 0, src.Width / 2, src.Height / 2));
all.Resize(0.3).ImWrite(dir + "all.png");
}
foreach (var pair in new[] { "h", "s", "v" }.Select((channel, index) => new { channel, index }))
{
var diff = new Mat();
Cv2.Absdiff(hsv_src_channels[pair.index], hsv_background_channels[pair.index], diff);
diff.Resize(0.3).With_Title(pair.channel).ImWrite(dir + string.Format("0b {0}.png", pair.channel));
diff.ThresholdStairs().Resize(0.3).ImWrite(dir + string.Format("0b {0} th.png", pair.channel));
hsv_src_channels[pair.index].Resize(resizeK).With_Title(pair.channel).ImWrite(dir + string.Format("0 {0}.png", pair.channel));
foreach (var d in new[] { -100, -50, 50, 100 })
//.........这里部分代码省略.........