本文整理汇总了C#中Mat.CvtColor方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.CvtColor方法的具体用法?C# Mat.CvtColor怎么用?C# Mat.CvtColor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mat
的用法示例。
在下文中一共展示了Mat.CvtColor方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Update
// Update is called once per frame
void Update()
{
using ( Mat image = new Mat() ) {
// Webカメラから画像を取得する
video.Read( image );
// OpenCVのデータがBGRなのでRGBに変える
// Bitmap形式に変えてテクスチャに流し込む
using ( var cvtImage = image.CvtColor( ColorConversion.BgrToRgb ) ) {
texture.LoadRawTextureData( cvtImage.ImEncode( ".bmp" ) );
texture.Apply();
}
}
}
示例2: Blob
private static void Blob()
{
Mat src = new Mat("data/shapes.png", ImreadModes.Color);
Mat gray = src.CvtColor(ColorConversionCodes.BGR2GRAY);
Mat binary = gray.Threshold(0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary);
Mat labelView = new Mat();
Mat rectView = binary.CvtColor(ColorConversionCodes.GRAY2BGR);
ConnectedComponents cc = Cv2.ConnectedComponentsEx(binary);
if (cc.LabelCount <= 1)
return;
// draw labels
/*
Scalar[] colors = cc.Blobs.Select(_ => Scalar.RandomColor()).ToArray();
int height = cc.Labels.GetLength(0);
int width = cc.Labels.GetLength(1);
var labelViewIndexer = labelView.GetGenericIndexer<Vec3b>();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int labelValue = cc.Labels[y, x];
labelViewIndexer[y, x] = colors[labelValue].ToVec3b();
}
}
*/
cc.RenderBlobs(labelView);
// draw bonding boxes except background
foreach (var blob in cc.Blobs.Skip(1))
{
rectView.Rectangle(blob.Rect, Scalar.Red);
}
// filter maximum blob
ConnectedComponents.Blob maxBlob = cc.GetLargestBlob();
//cc.Blobs.Skip(1).OrderByDescending(b => b.Area).First();
Mat filtered = new Mat();
cc.FilterByBlob(src, filtered, maxBlob);
using (new Window("src", src))
using (new Window("binary", binary))
using (new Window("labels", labelView))
using (new Window("bonding boxes", rectView))
using (new Window("maximum blob", filtered))
{
Cv2.WaitKey();
}
}
示例3: Main
public static void Main(string[] args)
{
Mat src = new Mat("TestImages/2013-12-27-09G37_TRB.jpg", LoadMode.Color);
src = src.Resize (new Size (420, 625));
Mat srcGrey = src.CvtColor(ColorConversion.BgrToGray);
Mat dst = new Mat ();
Point[][] contours;
Mat invertColour = new Mat (src.Rows, src.Cols, src.Type(), new Scalar(255, 255, 255) );
HiearchyIndex[] hierarchy;
Mat contoursLines = new Mat(src.Rows, src.Cols, src.Type());
// InputArray element = Cv2.GetStructuringElement (StructuringElementShape.Ellipse, new Size (16, 16));
// Mat thresh = srcGrey.Threshold (230, 255, ThresholdType.Binary).Dilate(element);
Mat thresh = srcGrey.Threshold (230, 255, ThresholdType.Binary);
Cv2.FindContours (thresh, out contours, out hierarchy, ContourRetrieval.Tree, ContourChain.ApproxSimple);
// Find center of mass in contour
Moments center = Cv2.Moments (contours [0]);
// Convert moment matrix to X/Y coords
int x = (int) (center.M10 / center.M00);
int y = (int) (center.M01 / center.M00);
src.Circle (new Point (x, y), 5, CvColor.CornflowerBlue, 2);
src.DrawContours (contours, 1, CvColor.Green, 2);
bool intersect = IsIntersecting (selector[0], selector[1], contours[1][0], contours[1][1]);
using (var orig = new Window ("src image", src))
{
orig.OnMouseCallback += new CvMouseCallback(MouseMove);
Cv2.WaitKey();
}
}
示例4: Update
// Update is called once per frame
void Update()
{
using ( Mat image = new Mat() ) {
// Webカメラから画像を取得する
video.Read( image );
// 顔を検出する
var faces = cascade.DetectMultiScale( image );
if ( faces.Length > 0 ) {
var face = faces[0];
// 顔の矩形を描画する
image.Rectangle( face, new Scalar( 255, 0, 0 ), 2 );
// 中心の座標を計算する
var x = face.TopLeft.X + (face.Size.Width / 2);
var y = face.TopLeft.Y + (face.Size.Height / 2);
// オブジェクトを移動する
if ( Object !=null ) {
Object.transform.localPosition = Vector2ToVector3( new Vector2( x, y ) );
}
}
// OpenCVのデータがBGRなのでRGBに変える
// Bitmap形式に変えてテクスチャに流し込む
using(var cvtImage = image.CvtColor( ColorConversion.BgrToRgb )){
texture.LoadRawTextureData( cvtImage.ImEncode( ".bmp" ) );
texture.Apply();
}
}
}
示例5: Converter
//画像二値化
Mat Converter(Mat colorImage, Scalar colorHsv, int range)
{
int channel = colorImage.Channels();
int imageW = colorImage.Width;
int imageH = colorImage.Height;
colorImage.CvtColor(OpenCvSharp.ColorConversion.BgrToHsv);
Mat grayImage = new Mat(imageH, imageW, MatType.CV_8UC1);
unsafe
{
byte* matPtr = grayImage.DataPointer;
byte* colorPtr = colorImage.DataPointer;
for (int i = 0; i < imageW * imageH; i++)
{
//color Comperer
if (*(colorPtr + i * channel) < (colorHsv.Val0 + range) && *(colorPtr + i * channel) > (colorHsv.Val0 - range) &&
(*(colorPtr + i * channel + 1) < (colorHsv.Val1 + range) && *(colorPtr + i * channel + 1) > (colorHsv.Val1 - range)) &&
(*(colorPtr + i * channel + 2) < (colorHsv.Val2 + range) && *(colorPtr + i * channel + 2) > (colorHsv.Val2 - range)))
{
*(matPtr + i) = 255;
}
else
{
*(matPtr + i) = 0;
}
}
}
return grayImage;
}
示例6: Execute2
public static void Execute2()
{
var frogRect = new Rectangle(320, 218, 150, 150);
if (OtherOperation(frogRect))
return;
var screenChecks = ScreenChecks();
screenChecks = ResolveChecks(screenChecks, @"..\..\Zuma.Data");
Func<Bitmap, string> check = image => screenChecks.Where(_check => image.Check(_check.Points)).Select(_check => _check.Name).FirstOrDefault();
var vmClient = new VmClient();
var gameScreenRect = vmClient.GameScreenRect;
var game_width = gameScreenRect.Width;
var game_height = gameScreenRect.Height;
//var startButtonPoint = new Point(950, 430);
//var startMissionPoint = new Point(600, 750);
var startButtonPoint = new Point(950, 520);
var startMissionPoint = new Point(180, 360);
var endChallengePoint = new Point(320, 590);
var vm_host = VmIp;
var client = new MouseClient(vm_host);
var background = LoadBitmap("../../Zuma.Data/background.png");
var hsv_background = new Mat(background.ToIplImage(), true).CvtColor(ColorConversion.RgbToHsv).Split();
if (false)
{
var fireflyImage = LoadBitmap(@"p:\Projects\_Other\Cs10\Gamering\bin\Release\FireFlies.h\46.png");
var hsv_fireflyImage = new Mat(fireflyImage.ToIplImage(), true).CvtColor(ColorConversion.RgbToHsv).Split();
//for (var i = 0; i < 3; ++i)
//{
// hsv_fireflyImage[i].Absdiff(hsv_background[i]).ImWrite(string.Format("ff{0}.png", i));
//}
var hsv_v = hsv_fireflyImage[2].Absdiff(hsv_background[2]);
hsv_v.Rectangle(new Rect(frogRect.X, frogRect.Y, frogRect.Width, frogRect.Height), new Scalar(0), -1);
hsv_v = hsv_v.Threshold(80, 255, ThresholdType.Binary);
hsv_v.ImWrite("ff_v.png");
return;
}
foreach (var file in System.IO.Directory.GetFiles("History"))
System.IO.File.Delete(file);
string prevScreenName = null;
string prevKnownScreenName = null;
var history = new List<Bitmap>();
var isSave = false;
var lastActionTime = DateTime.UtcNow;
for (var tick = 0; ; tick++)
{
try
{
var bmp = GetScreenImage(gameScreenRect);
if (Console.KeyAvailable)
{
var keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.Escape)
break;
else if (keyInfo.Key == ConsoleKey.Spacebar)
{
bmp.Save("0.png");
for (var i = 0; i < history.Count; ++i)
history[i].Save(string.Format("{0}.png", i + 1));
Console.WriteLine("save");
}
}
var screenName = check(bmp);
//var screenName = "action";
Console.Write("{0}: {1}{2}", tick, screenName, new string(' ', 20) + new string('\x8', 40));
switch (screenName)
{
case "main":
Console.WriteLine("main");
client.MouseEvent(MouseEventFlags.MOVE | MouseEventFlags.ABSOLUTE, startButtonPoint.X * 65536 / game_width, startButtonPoint.Y * 65536 / game_height);
System.Threading.Thread.Sleep(400);
client.MouseEvent(MouseEventFlags.LEFTDOWN, 0, 0);
System.Threading.Thread.Sleep(150);
client.MouseEvent(MouseEventFlags.LEFTUP, 0, 0);
System.Threading.Thread.Sleep(50);
System.Threading.Thread.Sleep(4000);
break;
case "challenge":
client.MouseEvent(MouseEventFlags.MOVE | MouseEventFlags.ABSOLUTE, startMissionPoint.X * 65536 / game_width, startMissionPoint.Y * 65536 / game_height);
//.........这里部分代码省略.........
示例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 })
//.........这里部分代码省略.........