目錄
效果
項(xiàng)目
代碼
下載
效果
玉米粒計(jì)數(shù)
項(xiàng)目
代碼
主要處理步驟
1、二值化操作
2、腐蝕
3、距離變換
4、形態(tài)學(xué)處理
5、找到種子的輪廓區(qū)域
????????OpenCV中,函數(shù)distanceTransform()用于計(jì)算圖像中每一個(gè)非零點(diǎn)像素與其最近的零點(diǎn)像素之間的距離,
????????輸出的是保存每一個(gè)非零點(diǎn)與最近零點(diǎn)的距離信息,圖像上越亮的點(diǎn),代表了離零點(diǎn)的距離越遠(yuǎn)。
????????用途:
????????可以根據(jù)距離變換的這個(gè)性質(zhì),經(jīng)過簡單的運(yùn)算,用于細(xì)化字符的輪廓和查找物體質(zhì)心(中心)。
????????距離變換的處理圖像通常都是二值圖像,而二值圖像其實(shí)就是把圖像分為兩部分,即背景和物體兩部分,物體通常又稱為前景目標(biāo)。
????????通常我們把前景目標(biāo)的灰度值設(shè)為255(即白色),背景的灰度值設(shè)為0(即黑色)。
????????所以定義中的非零像素點(diǎn)即為前景目標(biāo),零像素點(diǎn)即為背景。
????????所以圖像中前景目標(biāo)中的像素點(diǎn)距離背景越遠(yuǎn),那么距離就越大,如果我們用這個(gè)距離值替換像素值,那么新生成的圖像中這個(gè)點(diǎn)越亮。
//User:用戶自定義
//L1: ?曼哈頓距離
//L2: ?歐式距離
//C: ? 棋盤距離
Cv2.DistanceTransform(morhImage, dist, DistanceTypes.L1, DistanceTransformMasks.Mask3);
//
// 摘要:
// ? ? Applies a fixed-level threshold to each array element.
//
// 參數(shù):
// ? src:
// ? ? input array (single-channel, 8-bit or 32-bit floating point).
//
// ? dst:
// ? ? output array of the same size and type as src.
//
// ? thresh:
// ? ? threshold value.
//
// ? maxval:
// ? ? maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding
// ? ? types.
//
// ? type:
// ? ? thresholding type (see the details below).
//
// 返回結(jié)果:
// ? ? the computed threshold value when type == OTSU
public static double Threshold(InputArray src, OutputArray dst, double thresh, double maxval, ThresholdTypes type)文章來源:http://www.zghlxwxcb.cn/news/detail-758419.html
using OpenCvSharp;
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace OpenCvSharp_Demo
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
string image_path = "";
DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now;
Mat image;
Mat result_image;
StringBuilder sb = new StringBuilder();
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
pictureBox1.Image = null;
pictureBox2.Image = null;
textBox1.Text = "";
image_path = ofd.FileName;
pictureBox1.Image = new Bitmap(image_path);
image = new Mat(image_path);
}
private void Form1_Load(object sender, EventArgs e)
{
//test
image_path = "test_img/1.jpg";
image = new Mat(image_path);
pictureBox1.Image = new Bitmap(image_path);
}
private void button2_Click(object sender, EventArgs e)
{
if (image_path == "")
{
return;
}
textBox1.Text = "檢測中,請稍等……";
pictureBox2.Image = null;
Application.DoEvents();
result_image = image.Clone();
//二值化操作
Mat grayimg = new Mat();
Cv2.CvtColor(image, grayimg, ColorConversionCodes.BGR2GRAY);
Mat BinaryImg = new Mat();
Cv2.Threshold(grayimg, BinaryImg, 240, 255, ThresholdTypes.Binary);
//Cv2.ImShow("二值化", BinaryImg);
//腐蝕
Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(15, 15));
Mat morhImage = new Mat();
Cv2.Dilate(BinaryImg, morhImage, kernel, null, 2);
//Cv2.ImShow("morphology", morhImage);
//距離變換:用于二值化圖像中的每一個(gè)非零點(diǎn)距自己最近的零點(diǎn)的距離,距離變換圖像上越亮的點(diǎn),代表了這一點(diǎn)距離零點(diǎn)的距離越遠(yuǎn)
Mat dist = new Mat();
Cv2.BitwiseNot(morhImage, morhImage);
/*
OpenCV中,函數(shù)distanceTransform()用于計(jì)算圖像中每一個(gè)非零點(diǎn)像素與其最近的零點(diǎn)像素之間的距離,
輸出的是保存每一個(gè)非零點(diǎn)與最近零點(diǎn)的距離信息,圖像上越亮的點(diǎn),代表了離零點(diǎn)的距離越遠(yuǎn)。
用途:
可以根據(jù)距離變換的這個(gè)性質(zhì),經(jīng)過簡單的運(yùn)算,用于細(xì)化字符的輪廓和查找物體質(zhì)心(中心)。
*/
/*
距離變換的處理圖像通常都是二值圖像,而二值圖像其實(shí)就是把圖像分為兩部分,即背景和物體兩部分,物體通常又稱為前景目標(biāo)。
通常我們把前景目標(biāo)的灰度值設(shè)為255(即白色),背景的灰度值設(shè)為0(即黑色)。
所以定義中的非零像素點(diǎn)即為前景目標(biāo),零像素點(diǎn)即為背景。
所以圖像中前景目標(biāo)中的像素點(diǎn)距離背景越遠(yuǎn),那么距離就越大,如果我們用這個(gè)距離值替換像素值,那么新生成的圖像中這個(gè)點(diǎn)越亮。
*/
//User:用戶自定義
//L1: 曼哈頓距離
//L2: 歐式距離
//C: 棋盤距離
Cv2.DistanceTransform(morhImage, dist, DistanceTypes.L1, DistanceTransformMasks.Mask3);
Cv2.Normalize(dist, dist, 0, 1.0, NormTypes.MinMax); //范圍在0~1之間
//Cv2.ImShow("distance", dist);
//形態(tài)學(xué)處理
Mat MorphImg = new Mat();
dist.ConvertTo(MorphImg, MatType.CV_8U);
Cv2.Threshold(MorphImg, MorphImg, 0.99, 255, ThresholdTypes.Binary); //上圖像素值在0~1之間
kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(7, 3), new OpenCvSharp.Point(-1, -1));
Cv2.MorphologyEx(MorphImg, MorphImg, MorphTypes.Open, kernel); //開操作
//Cv2.ImShow("t-distance", MorphImg);
//找到種子的輪廓區(qū)域
OpenCvSharp.Point[][] contours;
HierarchyIndex[] hierarchly;
Cv2.FindContours(MorphImg, out contours, out hierarchly, RetrievalModes.External, ContourApproximationModes.ApproxSimple, new OpenCvSharp.Point(0, 0));
Mat markers = Mat.Zeros(image.Size(), MatType.CV_8UC3);
int x, y, w, h;
Rect rect;
for (int i = 0; i < contours.Length; i++)
{
// Cv2.DrawContours(markers, contours, i, Scalar.RandomColor(), 2, LineTypes.Link8, hierarchly);
rect = Cv2.BoundingRect(contours[i]);
x = rect.X;
y = rect.Y;
w = rect.Width;
h = rect.Height;
Cv2.Circle(result_image, x + w / 2, y + h / 2, 20, new Scalar(0, 0, 255), -1);
if (i >= 9)
{
Cv2.PutText(result_image, (i + 1).ToString(), new OpenCvSharp.Point(x + w / 2 - 18, y + h / 2 + 8), HersheyFonts.HersheySimplex, 0.8, new Scalar(0, 255, 0), 2);
}
else
{
Cv2.PutText(result_image, (i + 1).ToString(), new OpenCvSharp.Point(x + w / 2 - 8, y + h / 2 + 8), HersheyFonts.HersheySimplex, 0.8, new Scalar(0, 255, 0), 2);
}
}
textBox1.Text = "number of corns: " + contours.Length;
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
}
private void pictureBox2_DoubleClick(object sender, EventArgs e)
{
Common.ShowNormalImg(pictureBox2.Image);
}
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
Common.ShowNormalImg(pictureBox1.Image);
}
}
}
下載
Demo下載文章來源地址http://www.zghlxwxcb.cn/news/detail-758419.html
到了這里,關(guān)于C# OpenCvSharp 玉米粒計(jì)數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!