文章目錄
- 一、概念
- 二、填充不規則圖形
- 1.原始素材
- 2.代碼
- 3.運行結果
- 二、最大輪廓填充顏色
- 1.原始素材
- 2.代碼
- 3.運行結果
一、概念
圖像的連通域是指圖像中具有相同像素值并且位置相鄰的像素組成的區域,連通域分析是指在圖像中尋找出彼此互相獨立的連通域并將其標記出來。
上一篇講的是利用ConnectedComponentsWithStats查找連通區域,如果我們已經得到了一個由n個點組成的輪廓,想把這個區域填充成某個顏色,怎么辦呢,Emgu CV中有這樣一個函數:
public static FillPoly
(IInputOutputArray img, // 輸入輸出圖像IInputArray points, // 點的集合,或者是輪廓MCvScalar color, // 要填充的顏色LineType lineType = LineType.EightConnected, // 線形int shift = 0, // 不知道,一般用不上Point offset = default(Point) // 不知道,一般用不上
)
此函數輸入圖像和輸出圖像使用一個變量傳入,然后只需要再設定points和color兩個參數就行。
二、填充不規則圖形
1.原始素材
原始素材srcMat如下圖:
圖像寬737,長349。
2.代碼
下面代碼實現隨機的五邊形,填充到原始圖像中:
Mat dstMat = srcMat.Clone();VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
VectorOfPoint vPoints1 = new VectorOfPoint();
System.Drawing.Point[] pt = new System.Drawing.Point[1];
int width = dstMat.Width;
int height = dstMat.Height;
Random random = new Random();
for (int i = 0; i < 5; i++)
{pt[0].X = Convert.ToInt32(random.Next(0, width));pt[0].Y = Convert.ToInt32(random.Next(0, height));vPoints1.Push(pt);
}contours.Push(vPoints1);
CvInvoke.FillPoly(dstMat, contours, new MCvScalar(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)));
CvInvoke.Imshow("Final result image, " + dstMat.Size.ToString(), dstMat);
3.運行結果
上面的代碼很簡單,直接上運行結果:
二、最大輪廓填充顏色
1.原始素材
原始素材srcMat如下圖:
圖像寬979,長755。
2.代碼
Mat tempMat = srcMat.Clone();
Mat dstMat = srcMat.Clone();
Mat gray = new Mat();
int threshold = Convert.ToInt16(TextBoxThreshold.Text.Trim().ToString());// 轉成灰度圖再二值化
CvInvoke.CvtColor(tempMat, gray, ColorConversion.Bgr2Gray);
CvInvoke.Threshold(gray, gray, threshold, 255, ThresholdType.Binary);
CvInvoke.Imshow("Gray and threshold", gray);// 定義輪廓集合
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
VectorOfRect hierarchy = new VectorOfRect();// 根據下拉框選擇輪廓檢索模式,查找輪廓并繪制
switch (ComboBoxType.Text)
{case "External":CvInvoke.FindContours(gray, contours, hierarchy, RetrType.External, ChainApproxMethod.ChainApproxNone);break;case "List":CvInvoke.FindContours(gray, contours, hierarchy, RetrType.List, ChainApproxMethod.ChainApproxNone);break;case "Ccomp":CvInvoke.FindContours(gray, contours, hierarchy, RetrType.Ccomp, ChainApproxMethod.ChainApproxNone);break;case "Tree":CvInvoke.FindContours(gray, contours, hierarchy, RetrType.Tree, ChainApproxMethod.ChainApproxNone);break;default:CvInvoke.FindContours(gray, contours, hierarchy, RetrType.Tree, ChainApproxMethod.ChainApproxNone);break;
}// 在一張黑色圖中畫出所有輪廓
Mat allContours = new Mat(new System.Drawing.Size(gray.Cols, gray.Rows), DepthType.Cv8U, 1);
allContours.SetTo(new MCvScalar(0, 0, 0));
CvInvoke.DrawContours(allContours, contours, -1, new MCvScalar(255, 255, 255), 1);// 按照面積篩選,太小的輪廓不計算
Dictionary<int, double> dict = new Dictionary<int, double>();
if (contours.Size > 0)
{for (int i = 0; i < contours.Size; i++){double girth = CvInvoke.ArcLength(contours[i], true);if (girth > 10 && girth < 3000000){dict.Add(i, girth);}}
}var item = dict.OrderByDescending(v => v.Value); // v.Value就代表周長,是降序排列// 給最大輪廓繪制顏色
Random random = new Random();
foreach (var it in item)
{int key = it.Key;CvInvoke.FillPoly(dstMat, contours[key], new MCvScalar(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)));break;
}CvInvoke.Imshow("All contours, " + dict.Count(), allContours);
CvInvoke.Imshow("Final result image, " + dstMat.Size.ToString(), dstMat);
3.運行結果
二值化閾值選擇40,輪廓檢索模式選擇External,運行結果:
二值化圖形中,輪廓周長最大的,就是下面的那個球,所以整體都被填充成新的顏色了。
原創不易,請勿抄襲。共同進步,相互學習。