/// <summary> /// ウィンドウにトラックバーを作成し、作成したトラックバーを返す /// </summary> /// <param name="name">トラックバーの名前</param> /// <param name="value">スライダの初期位置</param> /// <param name="max">スライダの最大値.最小値は常に 0.</param> /// <param name="callback">スライダの位置が変更されるたびに呼び出されるデリゲート</param> /// <param name="userdata"></param> #else /// <summary> /// Creates the trackbar and attaches it to this window /// </summary> /// <param name="name">Name of created trackbar. </param> /// <param name="value">The position of the slider</param> /// <param name="max">Maximal position of the slider. Minimal position is always 0. </param> /// <param name="callback">the function to be called every time the slider changes the position. This function should be prototyped as void Foo(int);</param> /// <param name="userdata"></param> /// <returns></returns> #endif public CvTrackbar CreateTrackbar2(string name, int value, int max, CvTrackbarCallback2 callback, object userdata) { CvTrackbar trackbar = new CvTrackbar(name, this.name, value, max, callback, userdata); trackbars.Add(name, trackbar); return(trackbar); }
/// <summary> /// ウィンドウにトラックバーを作成し、作成したトラックバーを返す /// </summary> /// <param name="name">トラックバーの名前</param> /// <param name="value">スライダの初期位置</param> /// <param name="max">スライダの最大値.最小値は常に 0.</param> /// <param name="callback">スライダの位置が変更されるたびに呼び出されるデリゲート</param> #else /// <summary> /// Creates the trackbar and attaches it to this window /// </summary> /// <param name="name">Name of created trackbar. </param> /// <param name="value">The position of the slider</param> /// <param name="max">Maximal position of the slider. Minimal position is always 0. </param> /// <param name="callback">the function to be called every time the slider changes the position. This function should be prototyped as void Foo(int);</param> /// <returns></returns> #endif public CvTrackbar CreateTrackbar(string name, int value, int max, CvTrackbarCallback callback) { CvTrackbar trackbar = new CvTrackbar(name, this.name, value, max, callback); trackbars.Add(name, trackbar); return(trackbar); }
private void InitializeCv() { CvWindow objectSeg = new CvWindow("Thresholded View"); CvWindow colorPick = new CvWindow("Color Picker"); CvTrackbarCallback lowerHCallback = delegate(int pos) { if (this.upperH > pos) { this.lowerH = pos; } }; CvTrackbarCallback upperHCallback = delegate(int pos) { if (pos > this.lowerH) { this.upperH = pos; } }; CvTrackbarCallback lowerSCallback = delegate(int pos) { if (this.upperS > pos) { this.lowerS = pos; } }; CvTrackbarCallback upperSCallback = delegate(int pos) { if (pos > this.lowerS) { this.upperS = pos; } }; CvTrackbarCallback lowerVCallback = delegate(int pos) { if (this.upperV > pos) { this.lowerV = pos; } }; CvTrackbarCallback upperVCallback = delegate(int pos) { if (pos > this.lowerV) { this.upperV = pos; } }; CvTrackbar lowerH = colorPick.CreateTrackbar("lower H", this.lowerH, 179, lowerHCallback); CvTrackbar upperH = colorPick.CreateTrackbar("upper H", this.upperH, 179, upperHCallback); CvTrackbar lowerS = colorPick.CreateTrackbar("lower S", this.lowerS, 255, lowerSCallback); CvTrackbar upperS = colorPick.CreateTrackbar("upper S", this.upperS, 255, upperSCallback); CvTrackbar lowerV = colorPick.CreateTrackbar("lower V", this.lowerV, 255, lowerVCallback); CvTrackbar upperV = colorPick.CreateTrackbar("upper V", this.upperV, 255, upperVCallback); }
public Histogram() { // cvCalcHist // コントラストや明度をいろいろ変えられるサンプル const int histSize = 64; float[] range0 = { 0, 256 }; float[][] ranges = { range0 }; // 画像の読み込み using (IplImage srcImg = new IplImage(Const.ImageLenna, LoadMode.GrayScale)) using (IplImage dstImg = srcImg.Clone()) using (IplImage histImg = new IplImage(new CvSize(400, 400), BitDepth.U8, 1)) using (CvHistogram hist = new CvHistogram(new int[] { histSize }, HistogramFormat.Array, ranges, true)) { using (CvWindow windowImage = new CvWindow("image", WindowMode.AutoSize)) using (CvWindow windowHist = new CvWindow("histogram", WindowMode.AutoSize)) { // トラックバーが動かされた時の処理 CvTrackbar ctBrightness = null; CvTrackbar ctContrast = null; CvTrackbarCallback callback = delegate(int pos) { int brightness = ctBrightness.Pos - 100; int contrast = ctContrast.Pos - 100; // LUTの適用 byte[] lut = CalcLut(contrast, brightness); srcImg.LUT(dstImg, lut); // ヒストグラムの描画 CalcHist(dstImg, hist); DrawHist(histImg, hist, histSize); // ウィンドウに表示 windowImage.ShowImage(dstImg); windowHist.ShowImage(histImg); dstImg.Zero(); histImg.Zero(); }; // トラックバーの作成 // (OpenCVでは現在位置にポインタを渡すことでトラックバーの位置の変化が取得できるが、 // .NETではGCによりポインタが移動してしまうので廃止した。別の方法でうまく取得すべし。) ctBrightness = windowImage.CreateTrackbar("brightness", 100, 200, callback); ctContrast = windowImage.CreateTrackbar("contrast", 100, 200, callback); // 初回描画 callback(0); // キー入力待ち Cv.WaitKey(0); } } }
public IplImage BuildHist(IplImage src_tmp) { const int histSize = 64; float[] range0 = { 0, 256 }; float[][] ranges = { range0 }; // 화상의 읽기 using (IplImage srcImg = new IplImage(src_tmp.Size, BitDepth.U8, 1)) using (IplImage dstImg = new IplImage(src_tmp.Size, BitDepth.U8, 1)) using (IplImage histImg = new IplImage(new CvSize(400, 400), BitDepth.U8, 1)) using (CvHistogram hist = new CvHistogram(new int[] { histSize }, HistogramFormat.Array, ranges, true)) { src_tmp.CvtColor(srcImg, ColorConversion.BgrToGray); srcImg.Copy(dstImg); using (CvWindow windowImage = new CvWindow("변환된 이미지", WindowMode.AutoSize)) using (CvWindow windowHist = new CvWindow("히스토그램", WindowMode.AutoSize)) { // 트랙바가 동작되었을 때의 처리 CvTrackbar ctBrightness = null; CvTrackbar ctContrast = null; CvTrackbarCallback callback = delegate(int pos) { int brightness = ctBrightness.Pos - 100; int contrast = ctContrast.Pos - 100; // LUT의 적용 byte[] lut = CalcLut(contrast, brightness); srcImg.LUT(dstImg, lut); // 히스토그램 그리기 CalcHist(dstImg, hist); DrawHist(histImg, hist, histSize); // 윈도우에 표시 DstHist = histImg.Clone(); windowImage.ShowImage(dstImg); windowHist.ShowImage(histImg); dstImg.Zero(); histImg.Zero(); }; // 트랙바의 작성 ctBrightness = windowImage.CreateTrackbar("명도", 100, 200, callback); ctContrast = windowImage.CreateTrackbar("대조", 100, 200, callback); // 첫회 그리기 callback(0); // 키 입력대기 Cv.WaitKey(0); } return(DstHist); } }
public Histogram() { // cvCalcHist const int histSize = 64; float[] range0 = { 0, 256 }; float[][] ranges = { range0 }; using (IplImage srcImg = new IplImage(FilePath.Image.Lenna, LoadMode.GrayScale)) using (IplImage dstImg = srcImg.Clone()) using (IplImage histImg = new IplImage(new CvSize(400, 400), BitDepth.U8, 1)) using (CvHistogram hist = new CvHistogram(new int[] { histSize }, HistogramFormat.Array, ranges, true)) { using (CvWindow windowImage = new CvWindow("image", WindowMode.AutoSize)) using (CvWindow windowHist = new CvWindow("histogram", WindowMode.AutoSize)) { CvTrackbar ctBrightness = null; CvTrackbar ctContrast = null; CvTrackbarCallback callback = delegate(int pos) { int brightness = ctBrightness.Pos - 100; int contrast = ctContrast.Pos - 100; // perform LUT byte[] lut = CalcLut(contrast, brightness); srcImg.LUT(dstImg, lut); // draws histogram CalcHist(dstImg, hist); DrawHist(histImg, hist, histSize); windowImage.ShowImage(dstImg); windowHist.ShowImage(histImg); dstImg.Zero(); histImg.Zero(); }; ctBrightness = windowImage.CreateTrackbar("brightness", 100, 200, callback); ctContrast = windowImage.CreateTrackbar("contrast", 100, 200, callback); // initial action callback(0); Cv.WaitKey(0); } } }
public Mat Crop() { using (CroppingWindow = new OpenCvSharp.Window("Cropper", WindowMode.FullScreen, srcImage)) { CvMouseCallback onMouse = new CvMouseCallback(mouseCallback); CroppingWindow.SetMouseCallback(onMouse); CvTrackbarCallback2 onZoom = new CvTrackbarCallback2(trackbarCallback); CvTrackbar zoom = CroppingWindow.CreateTrackbar2("Zoom", 100, 200, onZoom, null); Cv2.WaitKey(); // This line is very important // OpenCV is written in C++, which means it's unmanaged. // So when a delegate callback variable is passed to it as a function point, // GC in C# can no longer identify the life cycle of this delegate variable // It's our reponsebility to make sure GC in C# will not collect it when it's still in use. // Otherwise an exception will be thrown. GC.KeepAlive(onMouse); } // seems that srcImage will be released by GC, so I must return a copy of it return(srcImage.Clone()); }
/// <summary> /// ウィンドウにトラックバーを作成し、作成したトラックバーを返す /// </summary> /// <param name="name">トラックバーの名前</param> /// <param name="value">スライダの初期位置</param> /// <param name="max">スライダの最大値.最小値は常に 0.</param> /// <param name="callback">スライダの位置が変更されるたびに呼び出されるデリゲート</param> /// <param name="userdata"></param> #else /// <summary> /// Creates the trackbar and attaches it to this window /// </summary> /// <param name="name">Name of created trackbar. </param> /// <param name="value">The position of the slider</param> /// <param name="max">Maximal position of the slider. Minimal position is always 0. </param> /// <param name="callback">the function to be called every time the slider changes the position. This function should be prototyped as void Foo(int);</param> /// <param name="userdata"></param> /// <returns></returns> #endif public CvTrackbar CreateTrackbar2(string name, int value, int max, CvTrackbarCallback2 callback, object userdata) { CvTrackbar trackbar = new CvTrackbar(name, this.name, value, max, callback, userdata); trackbars.Add(name, trackbar); return trackbar; }
/// <summary> /// ウィンドウにトラックバーを作成し、作成したトラックバーを返す /// </summary> /// <param name="name">トラックバーの名前</param> /// <param name="value">スライダの初期位置</param> /// <param name="max">スライダの最大値.最小値は常に 0.</param> /// <param name="callback">スライダの位置が変更されるたびに呼び出されるデリゲート</param> #else /// <summary> /// Creates the trackbar and attaches it to this window /// </summary> /// <param name="name">Name of created trackbar. </param> /// <param name="value">The position of the slider</param> /// <param name="max">Maximal position of the slider. Minimal position is always 0. </param> /// <param name="callback">the function to be called every time the slider changes the position. This function should be prototyped as void Foo(int);</param> /// <returns></returns> #endif public CvTrackbar CreateTrackbar(string name, int value, int max, CvTrackbarCallback callback) { CvTrackbar trackbar = new CvTrackbar(name, this.name, value, max, callback); trackbars.Add(name, trackbar); return trackbar; }