Matの使い方が分かってきたので、CvtColorを使ってカラー画像をグレースケールに変換してみます。
Cv2 クラスにメソッドが用意されている模様。
色々用意されているメソッドもこれで使えそうです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
using OpenCvSharp.CPlusPlus; // コレ
using System.Diagnostics;
namespace _03_matTest
{
class Program
{
static void Main(string[] args)
{
Mat matImage = new Mat(@"C:\Users\hogehoge\Pictures\hoge.jpg");
Mat grayImage = new Mat(); // グレースケール用Mat
// グレースケール変換 (param3はインテリセンスに頼ります)
Cv2.CvtColor(matImage, grayImage, ColorConversion.RgbToGray);
using (new Window("gray image", grayImage))
{
Debug.WriteLine(matImage.Width + " : " + grayImage.Height); // 4896 : 3264
Debug.WriteLine(grayImage.Depth() + " : " + grayImage.Type()); // 0 : CV_8UC1
Cv.WaitKey();
}
}
}
}
2017年10月9日月曜日
2017年10月8日日曜日
Matを使うのがイマ風?
openCvSharp2 以降はC++ interfaceを使えるので、indexerでピクセルを触りたい場合は "Mat"を使うとよいようだ (IplImage, CvMatではなく)
---
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// ここから追加
using OpenCvSharp;
using OpenCvSharp.CPlusPlus; // Matを使うなら必要
using System.Diagnostics;
namespace _03_matTest
{
class Program
{
static void Main(string[] args)
{
Mat matImage = new Mat(@"C:\Users\hoge\Pictures\hoge.jpg");
var indexer = matImage.GetGenericIndexer<Vec3b>(); // indexer でアクセスする例
var px = indexer[5, 5]; // x:5 y:5のpixel
Debug.WriteLine(px[0] + ":" + px[1] + ":" + px[2]);
px[0] = 0; // B
px[1] = 0; // G
px[2] = 255; // R
indexer[5, 5] = px;
using (new Window("photo", matImage)) // ウィンドウを作成して画像を表示
{
Debug.WriteLine(matImage.Width + " : " + matImage.Height);
Cv.WaitKey();
}
}
}
}
参考にさせていただきました
http://schima.hatenablog.com/entry/2014/03/29/140106
http://sourcechord.hatenablog.com/entry/2016/08/15/235654
---
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// ここから追加
using OpenCvSharp;
using OpenCvSharp.CPlusPlus; // Matを使うなら必要
using System.Diagnostics;
namespace _03_matTest
{
class Program
{
static void Main(string[] args)
{
Mat matImage = new Mat(@"C:\Users\hoge\Pictures\hoge.jpg");
var indexer = matImage.GetGenericIndexer<Vec3b>(); // indexer でアクセスする例
var px = indexer[5, 5]; // x:5 y:5のpixel
Debug.WriteLine(px[0] + ":" + px[1] + ":" + px[2]);
px[0] = 0; // B
px[1] = 0; // G
px[2] = 255; // R
indexer[5, 5] = px;
using (new Window("photo", matImage)) // ウィンドウを作成して画像を表示
{
Debug.WriteLine(matImage.Width + " : " + matImage.Height);
Cv.WaitKey();
}
}
}
}
参考にさせていただきました
http://schima.hatenablog.com/entry/2014/03/29/140106
http://sourcechord.hatenablog.com/entry/2016/08/15/235654
2017年10月4日水曜日
Remote Desktop から Windows Media player でAudio CDが"不明"になる
Windows Media Playerだけの問題なのかは不明ですが、グループポリシーの設定変更で解決できました。
1. Administratorでログイン
2. スタート -> "グループポリシー" で検索 -> "グループポリシーの編集"を選択
3. コンピュータの構成 -> 管理用テンプレート -> システム -> リムーバブル領域
4. 2すべてのリムーバブル記憶域:リモート セッションでの直接アクセスを許可する" を有効にする
こちらを参考にさせていただきました。
http://d.hatena.ne.jp/kamenoi/20170210/p1
1. Administratorでログイン
2. スタート -> "グループポリシー" で検索 -> "グループポリシーの編集"を選択
3. コンピュータの構成 -> 管理用テンプレート -> システム -> リムーバブル領域
4. 2すべてのリムーバブル記憶域:リモート セッションでの直接アクセスを許可する" を有効にする
こちらを参考にさせていただきました。
http://d.hatena.ne.jp/kamenoi/20170210/p1
2017年10月3日火曜日
物体のエッジ検出フィルタ(canyフィルタ)をかけてみる
例によってまるパクリです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
namespace _02_cameraFilter
{
class Program
{
static void Main(string[] args)
{
ShowCamera();
}
static void ShowCamera()
{
//カメラ映像を表示
int cameraPort = 0;
using (var capture = Cv.CreateCameraCapture(cameraPort))
{
IplImage frame = new IplImage();
// W640 x H480 (default resolution)のウィンドウを作る
int w = 640, h = 480;
// カメラ映像の画サイズ設定
Cv.SetCaptureProperty(capture, CaptureProperty.FrameWidth, w);
Cv.SetCaptureProperty(capture, CaptureProperty.FrameHeight, h);
//ESCキー押さない限り
while (Cv.WaitKey(1) != 27)
{
// 画像の取得
frame = Cv.QueryFrame(capture);
if (frame != null)
{
int threshold1 = 120;
int threshold2 = 80;
//trimmingするための座標
int trimmingX = 50;
int trimmingY = 20;
int trimmingW = 300;
int trimmingH = 300;
IplImage edgeFrame = new IplImage(frame.Width, frame.Height, BitDepth.U8, 1);
frame.Canny(edgeFrame, threshold1, threshold2);
//frameをトリミングする
IplImage trimmedFrame = trimming(edgeFrame, trimmingX, trimmingY, trimmingW, trimmingH);
Cv.ShowImage("Liveカメラ(元)", frame);
Cv.ShowImage("Liveカメラ(Edge)", edgeFrame);
Cv.ShowImage("抽出した映像", trimmedFrame);
//メモリ解放
Cv.ReleaseImage(trimmedFrame);
Cv.ReleaseImage(edgeFrame);
Cv.ReleaseImage(frame);
}
}
//ESCキーが押されたら
//全ての画像やwindowを削除
Cv.DestroyAllWindows();
capture.Dispose();
}
}
//frameをtrimmingする
static IplImage trimming(IplImage src, int x, int y, int width, int height)
{
IplImage dest = new IplImage(width, height, src.Depth, src.NChannels);
Cv.SetImageROI(src, new CvRect(x, y, width, height));
dest = Cv.CloneImage(src);
Cv.ResetImageROI(src);
return dest;
}
}
}
参照
http://www.thedesignium.com/development/8237
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
namespace _02_cameraFilter
{
class Program
{
static void Main(string[] args)
{
ShowCamera();
}
static void ShowCamera()
{
//カメラ映像を表示
int cameraPort = 0;
using (var capture = Cv.CreateCameraCapture(cameraPort))
{
IplImage frame = new IplImage();
// W640 x H480 (default resolution)のウィンドウを作る
int w = 640, h = 480;
// カメラ映像の画サイズ設定
Cv.SetCaptureProperty(capture, CaptureProperty.FrameWidth, w);
Cv.SetCaptureProperty(capture, CaptureProperty.FrameHeight, h);
//ESCキー押さない限り
while (Cv.WaitKey(1) != 27)
{
// 画像の取得
frame = Cv.QueryFrame(capture);
if (frame != null)
{
int threshold1 = 120;
int threshold2 = 80;
//trimmingするための座標
int trimmingX = 50;
int trimmingY = 20;
int trimmingW = 300;
int trimmingH = 300;
IplImage edgeFrame = new IplImage(frame.Width, frame.Height, BitDepth.U8, 1);
frame.Canny(edgeFrame, threshold1, threshold2);
//frameをトリミングする
IplImage trimmedFrame = trimming(edgeFrame, trimmingX, trimmingY, trimmingW, trimmingH);
Cv.ShowImage("Liveカメラ(元)", frame);
Cv.ShowImage("Liveカメラ(Edge)", edgeFrame);
Cv.ShowImage("抽出した映像", trimmedFrame);
//メモリ解放
Cv.ReleaseImage(trimmedFrame);
Cv.ReleaseImage(edgeFrame);
Cv.ReleaseImage(frame);
}
}
//ESCキーが押されたら
//全ての画像やwindowを削除
Cv.DestroyAllWindows();
capture.Dispose();
}
}
//frameをtrimmingする
static IplImage trimming(IplImage src, int x, int y, int width, int height)
{
IplImage dest = new IplImage(width, height, src.Depth, src.NChannels);
Cv.SetImageROI(src, new CvRect(x, y, width, height));
dest = Cv.CloneImage(src);
Cv.ResetImageROI(src);
return dest;
}
}
}
参照
http://www.thedesignium.com/development/8237
2017年10月2日月曜日
OpenCvSharpでカメラ画像を表示してみる
べた書きするとこんな感じ
カメラの番号が異なる場合はcameraPortを変更します
(タリーランプがついていれば大丈夫。異なっていればusingでexception発生)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
namespace _01_cameraThrough
{
class Program
{
static void Main(string[] args)
{
ShowCamera();
}
static void ShowCamera()
{
//カメラ映像を表示
int cameraPort = 0;
using (var capture = Cv.CreateCameraCapture(cameraPort))
{
IplImage frame = new IplImage();
// W640 x H480 (default resolution)のウィンドウを作る
int w = 640, h = 480;
// カメラ映像の画サイズ設定
Cv.SetCaptureProperty(capture, CaptureProperty.FrameWidth, w);
Cv.SetCaptureProperty(capture, CaptureProperty.FrameHeight, h);
//ESCキー押さない限り
while (Cv.WaitKey(1) != 27)
{
// 画像の取得
frame = Cv.QueryFrame(capture);
if (frame != null)
{
//frameを表示
Cv.ShowImage("Liveカメラ", frame);
}
}
//ESCキーが押されたら
//全ての画像やwindowを削除
Cv.DestroyAllWindows();
capture.Dispose();
}
}
}
}
参考にさせていただきました
http://www.thedesignium.com/development/8237
カメラの番号が異なる場合はcameraPortを変更します
(タリーランプがついていれば大丈夫。異なっていればusingでexception発生)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
namespace _01_cameraThrough
{
class Program
{
static void Main(string[] args)
{
ShowCamera();
}
static void ShowCamera()
{
//カメラ映像を表示
int cameraPort = 0;
using (var capture = Cv.CreateCameraCapture(cameraPort))
{
IplImage frame = new IplImage();
// W640 x H480 (default resolution)のウィンドウを作る
int w = 640, h = 480;
// カメラ映像の画サイズ設定
Cv.SetCaptureProperty(capture, CaptureProperty.FrameWidth, w);
Cv.SetCaptureProperty(capture, CaptureProperty.FrameHeight, h);
//ESCキー押さない限り
while (Cv.WaitKey(1) != 27)
{
// 画像の取得
frame = Cv.QueryFrame(capture);
if (frame != null)
{
//frameを表示
Cv.ShowImage("Liveカメラ", frame);
}
}
//ESCキーが押されたら
//全ての画像やwindowを削除
Cv.DestroyAllWindows();
capture.Dispose();
}
}
}
}
参考にさせていただきました
http://www.thedesignium.com/development/8237
表示画像を縮小してみる(Resize)
こんな感じで...
using (var img = new IplImage(@"C:\Users\hogehohe\Pictures\hoge.png")) // 画像を読み込み
{
CvSize size = new CvSize(img.Width / 2, img.Height / 2);
IplImage img2 = new IplImage(size,img.Depth, img.NChannels); // サイズ違いのimgを作成
Cv.Resize(img, img2); // リサイズ
using (new CvWindow(img2)) // ウィンドウを作成して画像を表示
{
Cv.WaitKey();
}
}
using (var img = new IplImage(@"C:\Users\hogehohe\Pictures\hoge.png")) // 画像を読み込み
{
CvSize size = new CvSize(img.Width / 2, img.Height / 2);
IplImage img2 = new IplImage(size,img.Depth, img.NChannels); // サイズ違いのimgを作成
Cv.Resize(img, img2); // リサイズ
using (new CvWindow(img2)) // ウィンドウを作成して画像を表示
{
Cv.WaitKey();
}
}
2017年10月1日日曜日
OpenCvSharpのはじめの一歩
1. Visual Studio 2015などで、File -> プロジェクトの作成
2. ツール -> NuGeパッケージマネージャ -> 参照 -> OpenCvSharp で検索 -> インストール(チェックボックスをチェックしてインストールボタン)
3. 以下のコードを書く
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp; // 追加
namespace consoleApplicationSample
{
class Program
{
static void Main(string[] args)
{
using (var img = new IplImage(@"C:\Users\hogehoge\Pictures\hoge.png")) // 画像を読み込み
{
Cv.Not(img, img); // 画像をネガポジ反転
using (new CvWindow(img)) // ウィンドウを作成して画像を表示
{
Cv.WaitKey();
}
}
}
}
}
2. ツール -> NuGeパッケージマネージャ -> 参照 -> OpenCvSharp で検索 -> インストール(チェックボックスをチェックしてインストールボタン)
3. 以下のコードを書く
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp; // 追加
namespace consoleApplicationSample
{
class Program
{
static void Main(string[] args)
{
using (var img = new IplImage(@"C:\Users\hogehoge\Pictures\hoge.png")) // 画像を読み込み
{
Cv.Not(img, img); // 画像をネガポジ反転
using (new CvWindow(img)) // ウィンドウを作成して画像を表示
{
Cv.WaitKey();
}
}
}
}
}
登録:
投稿 (Atom)