OpenCVSharp #11
نویسنده: وحید نصیری
تاریخ: ۱۳۹۴/۰۳/۲۴ ۱:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
var src = new Mat(@"..\..\Images\fruits.jpg", LoadMode.AnyDepth | LoadMode.AnyColor);
Cv2.ImShow("Source", src);
Cv2.WaitKey(1); // do events
Cv2.Blur(src, src, new Size(15, 15));
Cv2.ImShow("Blurred Image", src);
Cv2.WaitKey(1); // do events
// Converts the MxNx3 image into a Kx3 matrix where K=MxN and
// each row is now a vector in the 3-D space of RGB.
// change to a Mx3 column vector (M is number of pixels in image)
var columnVector = src.Reshape(cn: 3, rows: src.Rows * src.Cols);
// convert to floating point, it is a requirement of the k-means method of OpenCV.
var samples = new Mat();
columnVector.ConvertTo(samples, MatType.CV_32FC3);
for (var clustersCount = 2; clustersCount <= 8; clustersCount += 2)
{
var bestLabels = new Mat();
var centers = new Mat();
Cv2.Kmeans(
data: samples,
k: clustersCount,
bestLabels: bestLabels,
criteria:
new TermCriteria(type: CriteriaType.Epsilon | CriteriaType.Iteration, maxCount: 10, epsilon: 1.0),
attempts: 3,
flags: KMeansFlag.PpCenters,
centers: centers);
var clusteredImage = new Mat(src.Rows, src.Cols, src.Type());
for (var size = 0; size < src.Cols * src.Rows; size++)
{
var clusterIndex = bestLabels.At<int>(0, size);
var newPixel = new Vec3b
{
Item0 = (byte)(centers.At<float>(clusterIndex, 0)), // B
Item1 = (byte)(centers.At<float>(clusterIndex, 1)), // G
Item2 = (byte)(centers.At<float>(clusterIndex, 2)) // R
};
clusteredImage.Set(size / src.Cols, size % src.Cols, newPixel);
}
Cv2.ImShow(string.Format("Clustered Image [k:{0}]", clustersCount), clusteredImage);
Cv2.WaitKey(1); // do events
}
Cv2.WaitKey();
Cv2.DestroyAllWindows();
using (var src = new Mat(@"test.jpg", LoadMode.Color))
{
image1.Source = KMeans(src, 2).ToWriteableBitmap();
image2.Source = Binary(src).ToWriteableBitmap();
} private static Mat Binary(Mat src)
{
// Convert the image to Gray
src = src.CvtColor(ColorConversion.RgbToGray);
// Convert the Gray to Binary with auto threshold
Cv2.Threshold(src, src, 0, 255, ThresholdType.Binary | ThresholdType.Otsu);
// Convert the Gray to Binary with manual threshold
//Cv2.Threshold(src, src, 127, 255, ThresholdType.Binary);
return src;
}
private static Mat KMeans(Mat src, int k)
{
var columnVector = src.Reshape(cn: 3, rows: src.Rows * src.Cols);
var samples = new Mat();
columnVector.ConvertTo(samples, MatType.CV_32FC3);
var bestLabels = new Mat();
var centers = new Mat();
Cv2.Kmeans(
data: samples,
k: k,
bestLabels: bestLabels,
criteria: new TermCriteria(type: CriteriaType.Epsilon | CriteriaType.Iteration, maxCount: 10, epsilon: 1.0),
attempts: 3,
flags: KMeansFlag.PpCenters,
centers: centers);
var dst = new Mat(src.Rows, src.Cols, src.Type());
for (var size = 0; size < src.Cols * src.Rows; size++)
{
var clusterIndex = bestLabels.At<int>(0, size);
var newPixel = new Vec3b
{
Item0 = (byte)(centers.At<float>(clusterIndex, 0)), // B
Item1 = (byte)(centers.At<float>(clusterIndex, 1)), // G
Item2 = (byte)(centers.At<float>(clusterIndex, 2)) // R
};
dst.Set(size / src.Cols, size % src.Cols, newPixel);
}
return dst;
}