Commit 38889d63 SK

merge

2 个父辈 c2c05934 933d1e47
...@@ -13,6 +13,7 @@ namespace Acc.Img ...@@ -13,6 +13,7 @@ namespace Acc.Img
{ {
public static bool selectB = false; public static bool selectB = false;
public static bool pngB = false;
/// <summary> /// <summary>
/// 读取图片,,支持格式*.raw,*.bmp;*.gif;*.jpg;*.png /// 读取图片,,支持格式*.raw,*.bmp;*.gif;*.jpg;*.png
/// </summary> /// </summary>
...@@ -25,6 +26,7 @@ namespace Acc.Img ...@@ -25,6 +26,7 @@ namespace Acc.Img
{ {
if (imagePath.ToLower().EndsWith(".raw")) if (imagePath.ToLower().EndsWith(".raw"))
{ {
pngB = false;
Bitmap[] images = ReadRaw(imagePath); Bitmap[] images = ReadRaw(imagePath);
if (images != null) if (images != null)
{ {
...@@ -33,7 +35,8 @@ namespace Acc.Img ...@@ -33,7 +35,8 @@ namespace Acc.Img
} }
else else
{ {
image = Image.FromFile(imagePath); pngB = true;
image = Image.FromFile(imagePath);
} }
}catch(Exception) }catch(Exception)
{ {
...@@ -146,7 +149,6 @@ namespace Acc.Img ...@@ -146,7 +149,6 @@ namespace Acc.Img
} }
} }
} }
//srcBlob.Area += (int)(srcBlob.Area * 0.2);
for (int i = 0; i < blobList.Count; i++) for (int i = 0; i < blobList.Count; i++)
{ {
if (srcBlob.Area < blobList[i].Area) if (srcBlob.Area < blobList[i].Area)
...@@ -211,15 +213,22 @@ namespace Acc.Img ...@@ -211,15 +213,22 @@ namespace Acc.Img
Mat imageMat = BitmapConverter.ToMat(new Bitmap(image)); Mat imageMat = BitmapConverter.ToMat(new Bitmap(image));
Mat grayMat = BitmapConverter.ToMat(new Bitmap(image)); Mat grayMat = BitmapConverter.ToMat(new Bitmap(image));
Cv2.CvtColor(grayMat, grayMat, ColorConversionCodes.RGBA2RGB); Cv2.CvtColor(grayMat, grayMat, ColorConversionCodes.RGBA2RGB);
Cv2.Threshold(imageMat, imageMat, 0, 255, ThresholdTypes.Binary); if (pngB)
{
Cv2.Threshold(imageMat, imageMat, 70, 255, ThresholdTypes.Binary);
}
else
{
Cv2.Threshold(imageMat, imageMat, 0, 255, ThresholdTypes.Binary);
}
List<CvBlob> blobList = GetBlobs(imageMat, thresh, inv); List<CvBlob> blobList = GetBlobs(imageMat, thresh, inv);
int itemArea = (itemFeature + 23) * 3 /5; int itemArea = (itemFeature + 23) * 3 /5;
if(itemArea <= 0) if(itemArea <= 0)
{ {
itemArea = 3; itemArea = 3;
} }
int totalCount = CountBlobs(blobList, itemArea, ref grayMat); int totalCount = CountBlobs(blobList, itemArea, ref imageMat);
image = BitmapConverter.ToBitmap(grayMat); image = BitmapConverter.ToBitmap(imageMat);
return totalCount; return totalCount;
} }
...@@ -510,18 +519,43 @@ namespace Acc.Img ...@@ -510,18 +519,43 @@ namespace Acc.Img
else else
{ {
//全局二值化 //全局二值化
Cv2.Threshold(dst, dst, 0, 255, ThresholdTypes.Binary); if (pngB)
{
Cv2.Threshold(dst, dst, 70, 255, ThresholdTypes.Binary);
}
else
{
Cv2.Threshold(dst, dst, 0, 255, ThresholdTypes.Binary);
}
} }
if (inv) if (inv)
{ {
Cv2.Threshold(dst, dst, 0, 150, ThresholdTypes.BinaryInv); if (pngB)
{
Cv2.Threshold(dst, dst, 70, 150, ThresholdTypes.BinaryInv);
}
else
{
Cv2.Threshold(dst, dst, 0, 150, ThresholdTypes.BinaryInv);
}
} }
} }
else else
{ {
Cv2.Threshold(dst, dst, 0, 255, ThresholdTypes.Binary); if (pngB)
Cv2.Threshold(dst, dst, 0, 150, ThresholdTypes.BinaryInv); {
Cv2.Threshold(dst, dst, 70, 255, ThresholdTypes.Binary);
Cv2.Threshold(dst, dst, 70, 150, ThresholdTypes.BinaryInv);
}
else
{
Cv2.Threshold(dst, dst, 0, 255, ThresholdTypes.Binary);
Cv2.Threshold(dst, dst, 0, 150, ThresholdTypes.BinaryInv);
}
} }
//if (thresh == -1) //if (thresh == -1)
//{ //{
...@@ -551,10 +585,9 @@ namespace Acc.Img ...@@ -551,10 +585,9 @@ namespace Acc.Img
{ {
Cv2.CvtColor(imageMat, imageMat, ColorConversionCodes.RGBA2BGR); Cv2.CvtColor(imageMat, imageMat, ColorConversionCodes.RGBA2BGR);
//Cv2.CvtColor(imageMat, imageMat, ColorConversionCodes.RGB2GRAY); //Cv2.CvtColor(imageMat, imageMat, ColorConversionCodes.RGB2GRAY);
Mat dst = Threshhold(imageMat, thresh, inv); Mat dst = Threshhold(imageMat, thresh, inv);
Mat k1 = Mat.Ones(new OpenCvSharp.Size(1, 1), MatType.CV_8UC1); Mat k1 = Mat.Ones(new OpenCvSharp.Size(1, 1), MatType.CV_8UC1);
Cv2.MorphologyEx(dst, dst, MorphTypes.Open, k1); Cv2.MorphologyEx(dst, dst, MorphTypes.Open, k1);
CvBlobs blobs = new CvBlobs(); CvBlobs blobs = new CvBlobs();
blobs.Label(dst); blobs.Label(dst);
List<CvBlob> blobList = blobs.Values.Where(b => b.Area > 0).ToList(); List<CvBlob> blobList = blobs.Values.Where(b => b.Area > 0).ToList();
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!