本文整理汇总了C#中OpenCvSharp.Mat.CvtColor方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.CvtColor方法的具体用法?C# Mat.CvtColor怎么用?C# Mat.CvtColor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OpenCvSharp.Mat
的用法示例。
在下文中一共展示了Mat.CvtColor方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public void Run()
{
Mat src = new Mat("Data/Image/shapes.png", ImreadModes.Color);
Mat gray = src.CvtColor(ColorConversionCodes.BGR2GRAY);
Mat binary = gray.Threshold(0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary);
Mat labelView = src.EmptyClone();
Mat rectView = binary.CvtColor(ColorConversionCodes.GRAY2BGR);
ConnectedComponents cc = Cv2.ConnectedComponentsEx(binary);
if (cc.LabelCount <= 1)
return;
// draw labels
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
var maxBlob = cc.GetLargestBlob();
var 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();
}
}
示例2: Update
// Update is called once per frame
void Update () {
if (isVid) {
cap.Read (frame);
}
if (!frame.Empty()){
//assume this part of the frame contains only background
bkrnd_win = frame.Clone(bkrnd_rect);
bkrnd_win = bkrnd_win.CvtColor(ColorConversionCodes.BGR2HSV);
frame_hsv = frame.CvtColor (ColorConversionCodes.BGR2HSV);
Rangef[] ranges = { new Rangef (0, 180) };
//calc the *h* (hsv) histogram of the background
Cv2.CalcHist (new Mat[]{ bkrnd_win }, new int[]{ 0 }, null, hist, 1, new int[]{ 180 }, ranges);
hist = hist.Normalize (0, 255, NormTypes.MinMax);
Cv2.CalcBackProject (new Mat[]{ frame_hsv }, new int[]{ 0 }, hist, frame_backproj, ranges);
Mat kernel = Cv2.GetStructuringElement (MorphShapes.Ellipse, new Size (7, 7));
Cv2.Filter2D (frame_backproj, dst, dst.Type (), kernel);
Cv2.Threshold (dst, thresh, 10.0, 255.0, ThresholdTypes.Binary);
thresh = 255 - thresh;
//Debug.Log (thresh.Size ().ToString ());
Cv2.MorphologyEx (thresh, thresh, MorphTypes.Open, kernel,null,3);
Cv2.MorphologyEx (thresh, thresh, MorphTypes.ERODE, kernel,null,1);
contours = Cv2.FindContoursAsMat (thresh , RetrievalModes.List, ContourApproximationModes.ApproxSimple);
max_area_contour = 0;
max_area_contour_ind = 0;
for (int i = 0; i < contours.Length; i++) {
if (contours [i].ContourArea () > max_area_contour) {
max_area_contour = contours [i].ContourArea ();
max_area_contour_ind = i;
}
}
mask = new Mat (thresh.Size (), thresh.Type (), Scalar.All (0));
//Cv2.DrawContours (mask, contours, 0, Scalar.All (255));
//Debug.Log(max_area_contour_ind.ToString());
//mask.DrawContours(contours, 1,Scalar.All (255));
Cv2.Merge(new Mat[]{mask,mask,mask},mask);
Cv2.BitwiseAnd (mask, frame, mask);
//Cv2.Merge(new Mat[]{frame_backproj,frame_backproj,frame_backproj},frame_backproj);
tex.LoadImage (frame.ToBytes (".png", new int[]{ 0 }));
}
}
示例3: Update
// Update is called once per frame
void Update () {
if (isVid) {
cap.Read (frame);
}
if (!frame.Empty()){
bkrnd_win = frame.Clone(bkrnd_rect);
//calc the hsv histogram inside that window
Rangef[] ranges = { new Rangef (0, 180), new Rangef (0, 256) };
bkrnd_win = bkrnd_win.CvtColor(ColorConversionCodes.BGR2HSV);
frame_hsv = frame.CvtColor (ColorConversionCodes.BGR2HSV);
Cv2.CalcHist (new Mat[]{ bkrnd_win }, new int[]{ 0, 1 }, null, hist, 2, new int[]{ 180, 256 }, ranges);
hist = hist.Normalize (0, 255, NormTypes.MinMax);
Point min_loc, max_loc;
Cv2.MinMaxLoc (hist, out min_loc, out max_loc);
Debug.Log (max_loc.X.ToString ());
//double[] lowerb = {0,0,0};
//double[] upperb = {180,255,255};
//Mat M = new Mat(1, 3, frame_hsv.Type(), Scalar (0, 0, 0));
Mat lowerb = new Mat (new Size (1, 3), frame_hsv.Type (), Scalar.All(100));
Mat upperb = new Mat(new Size(1,3), frame_hsv.Type(),Scalar.All(255));
//Debug.Log(frame_hsv.Type().ToString());
Cv2.InRange (frame_hsv, lowerb, upperb, frame_backproj);
//Cv2.CalcBackProject (new Mat[]{ frame_hsv }, new int[]{ 0, 1 }, hist, frame_backproj, ranges);
Mat kernel = Cv2.GetStructuringElement (MorphShapes.Ellipse, new Size (5, 5));
Cv2.Filter2D (frame_backproj, mask, mask.Type (), kernel);
//thresh = Cv2.Threshold (mask, mask, 0.0, 255.0, ThresholdTypes.Otsu);
//mask = 255 - mask;
kernel = Cv2.GetStructuringElement (MorphShapes.Rect, new Size (3, 3));
Cv2.MorphologyEx (mask, mask, MorphTypes.ERODE, kernel,null,2);
kernel = Cv2.GetStructuringElement (MorphShapes.Rect, new Size (15, 15));
Cv2.MorphologyEx (mask, mask, MorphTypes.Close, kernel,null,5);
Cv2.Merge(new Mat[]{mask,mask,mask},mask);
Cv2.BitwiseAnd (mask, frame, mask);
Cv2.Merge(new Mat[]{frame_backproj,frame_backproj,frame_backproj},frame_backproj);
tex.LoadImage (frame_backproj.ToBytes (".png", new int[]{ 0 }));
}
}
示例4: ExtractMask
public static Mat ExtractMask(Mat srcMat, ColorConversionCodes code,
int ch1Lower, int ch1Upper,
int ch2Lower, int ch2Upper,
int ch3Lower, int ch3Upper)
{
if (srcMat == null)
throw new ArgumentNullException("srcMat");
var colorMat = srcMat.CvtColor(code);
var lut = new Mat(256, 1, MatType.CV_8UC3);
var lower = new int[3] { ch1Lower, ch2Lower, ch3Lower };
var upper = new int[3] { ch1Upper, ch2Upper, ch3Upper };
// cv::Mat_<cv::Vec3b>
var mat3 = new MatOfByte3(lut);
var indexer = mat3.GetIndexer();
for (int i = 0; i < 256; i++)
{
var color = indexer[i];
byte temp;
for (int k = 0; k < 3; k++)
{
if (lower[k] <= upper[k])
{
if ((lower[k] <= i) && (i <= upper[k]))
{
temp = 255;
}
else
{
temp = 0;
}
}
else
{
if ((i <= upper[k]) || (lower[k] <= i))
{
temp = 255;
}
else
{
temp = 0;
}
}
color[k] = temp;
}
indexer[i] = color;
}
Cv2.LUT(colorMat, lut, colorMat);
var channelMat = colorMat.Split();
var maskMat = new Mat();
Cv2.BitwiseAnd(channelMat[0], channelMat[1], maskMat);
Cv2.BitwiseAnd(maskMat, channelMat[2], maskMat);
return maskMat;
}