Commit 3229c272 SK

AccImage上传

1 个父辈 8af4571e
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Costura.Fody.3.2.0\build\Costura.Fody.props" Condition="Exists('..\packages\Costura.Fody.3.2.0\build\Costura.Fody.props')" />
<Import Project="..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props" Condition="Exists('..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{28B5864A-A0C4-4235-8F5E-D3E7299750E0}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AccImage</RootNamespace>
<AssemblyName>AccImage</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Costura, Version=3.2.0.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
<HintPath>..\packages\Costura.Fody.3.2.0\lib\net46\Costura.dll</HintPath>
</Reference>
<Reference Include="OpenCvSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
<HintPath>..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net40\OpenCvSharp.dll</HintPath>
</Reference>
<Reference Include="OpenCvSharp.Blob, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
<HintPath>..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net40\OpenCvSharp.Blob.dll</HintPath>
</Reference>
<Reference Include="OpenCvSharp.Extensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
<HintPath>..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net40\OpenCvSharp.Extensions.dll</HintPath>
</Reference>
<Reference Include="OpenCvSharp.UserInterface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
<HintPath>..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net40\OpenCvSharp.UserInterface.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ImageUtil.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="FodyWeavers.xml" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props'))" />
<Error Condition="!Exists('..\packages\Fody.3.3.2\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.3.3.2\build\Fody.targets'))" />
<Error Condition="!Exists('..\packages\Costura.Fody.3.2.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Costura.Fody.3.2.0\build\Costura.Fody.props'))" />
</Target>
<Import Project="..\packages\Fody.3.3.2\build\Fody.targets" Condition="Exists('..\packages\Fody.3.3.2\build\Fody.targets')" />
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Costura>
<PreloadOrder>
OpenCvSharpExtern
opencv_ffmpeg400_64
</PreloadOrder>
</Costura>
</Weavers>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuild. -->
<xs:element name="Weavers">
<xs:complexType>
<xs:all>
<xs:element name="Costura" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:all>
<xs:element minOccurs="0" maxOccurs="1" name="ExcludeAssemblies" type="xs:string">
<xs:annotation>
<xs:documentation>A list of assembly names to exclude from the default action of "embed all Copy Local references", delimited with line breaks</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element minOccurs="0" maxOccurs="1" name="IncludeAssemblies" type="xs:string">
<xs:annotation>
<xs:documentation>A list of assembly names to include from the default action of "embed all Copy Local references", delimited with line breaks.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element minOccurs="0" maxOccurs="1" name="Unmanaged32Assemblies" type="xs:string">
<xs:annotation>
<xs:documentation>A list of unmanaged 32 bit assembly names to include, delimited with line breaks.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element minOccurs="0" maxOccurs="1" name="Unmanaged64Assemblies" type="xs:string">
<xs:annotation>
<xs:documentation>A list of unmanaged 64 bit assembly names to include, delimited with line breaks.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element minOccurs="0" maxOccurs="1" name="PreloadOrder" type="xs:string">
<xs:annotation>
<xs:documentation>The order of preloaded assemblies, delimited with line breaks.</xs:documentation>
</xs:annotation>
</xs:element>
</xs:all>
<xs:attribute name="CreateTemporaryAssemblies" type="xs:boolean">
<xs:annotation>
<xs:documentation>This will copy embedded files to disk before loading them into memory. This is helpful for some scenarios that expected an assembly to be loaded from a physical file.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="IncludeDebugSymbols" type="xs:boolean">
<xs:annotation>
<xs:documentation>Controls if .pdbs for reference assemblies are also embedded.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="DisableCompression" type="xs:boolean">
<xs:annotation>
<xs:documentation>Embedded assemblies are compressed by default, and uncompressed when they are loaded. You can turn compression off with this option.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="DisableCleanup" type="xs:boolean">
<xs:annotation>
<xs:documentation>As part of Costura, embedded assemblies are no longer included as part of the build. This cleanup can be turned off.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="LoadAtModuleInit" type="xs:boolean">
<xs:annotation>
<xs:documentation>Costura by default will load as part of the module initialization. This flag disables that behavior. Make sure you call CosturaUtility.Initialize() somewhere in your code.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="IgnoreSatelliteAssemblies" type="xs:boolean">
<xs:annotation>
<xs:documentation>Costura will by default use assemblies with a name like 'resources.dll' as a satellite resource and prepend the output path. This flag disables that behavior.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="ExcludeAssemblies" type="xs:string">
<xs:annotation>
<xs:documentation>A list of assembly names to exclude from the default action of "embed all Copy Local references", delimited with |</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="IncludeAssemblies" type="xs:string">
<xs:annotation>
<xs:documentation>A list of assembly names to include from the default action of "embed all Copy Local references", delimited with |.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="Unmanaged32Assemblies" type="xs:string">
<xs:annotation>
<xs:documentation>A list of unmanaged 32 bit assembly names to include, delimited with |.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="Unmanaged64Assemblies" type="xs:string">
<xs:annotation>
<xs:documentation>A list of unmanaged 64 bit assembly names to include, delimited with |.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="PreloadOrder" type="xs:string">
<xs:annotation>
<xs:documentation>The order of preloaded assemblies, delimited with |.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:all>
<xs:attribute name="VerifyAssembly" type="xs:boolean">
<xs:annotation>
<xs:documentation>'true' to run assembly verification on the target assembly after all weavers have been finished.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="VerifyIgnoreCodes" type="xs:string">
<xs:annotation>
<xs:documentation>A comma separated list of error codes that can be safely ignored in assembly verification.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
\ No newline at end of file
using OpenCvSharp;
using OpenCvSharp.Blob;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
namespace Acc.Img
{
public class ImageUtil
{
/// <summary>
/// 读取图片,,支持格式*.raw,*.bmp;*.gif;*.jpg;*.png
/// </summary>
/// <param name="imagePath"></param>
/// <returns>读取到的图片对像,读取失败时返回null</returns>
public static Image ReadImage(string imagePath)
{
Image image = null;
try
{
if (imagePath.ToLower().EndsWith(".raw"))
{
Bitmap[] images = ReadRaw(imagePath);
if (images != null)
{
image = images[1];
}
}
else
{
image = Image.FromFile(imagePath);
}
}catch(Exception)
{
}
return image;
}
public static Image FindCircle(Image image, int thresh, bool inv)
{
Mat imageMat = BitmapConverter.ToMat(new Bitmap(image));
Cv2.PyrDown(imageMat, imageMat);
Mat threshMat = Threshhold(imageMat, thresh, inv);
//Cv2.GaussianBlur(imageMat, imageMat, new OpenCvSharp.Size(7, 7), 5);
//threshMat = Threshhold(imageMat, thresh, inv);
Mat k1 = Mat.Ones(new OpenCvSharp.Size(21, 21), MatType.CV_8UC1);
Cv2.MorphologyEx(threshMat, threshMat, MorphTypes.Open, k1);
CircleSegment[] circles = Cv2.HoughCircles(threshMat, HoughMethods.Gradient, 1, 5);
foreach(CircleSegment circle in circles)
{
Point2f center = circle.Center;
Cv2.Circle(imageMat, new OpenCvSharp.Point(center.X, center.Y), (int)circle.Radius, Scalar.White);
}
return BitmapConverter.ToBitmap(threshMat);
}
/// <summary>
/// 二值化图像
/// </summary>
/// <param name="image">图像数据</param>
/// <param name="thresh">阈值</param>
/// <param name="inv">true表示元器件为白色,false表示元器件为黑色</param>
/// <returns>二值化后的图像</returns>
public static Image Threshhold(Image image, int thresh, bool inv=true)
{
Mat imageMat =BitmapConverter.ToMat(new Bitmap(image));
Mat threshMat = Threshhold(imageMat, thresh, inv);
return BitmapConverter.ToBitmap(threshMat);
}
/// <summary>
/// 获取鼠标位置的元器件特征值
/// </summary>
/// <param name="image">输入的图像</param>
/// <param name="markX">鼠标的X点坐标</param>
/// <param name="markY">鼠标的Y点坐标</param>
/// <param name="thresh">二值化阈值</param>
/// <param name="inv">true表示元器件为白色,false表示元器件为黑色</param>
/// <returns>鼠标指向的元器件特征值</returns>
public static int GetItemFeature(Image image, int markX = -1, int markY = -1, int thresh = -1, bool inv = true)
{
Mat imageMat = BitmapConverter.ToMat(new Bitmap(image));
List<CvBlob> blobList = GetBlobs(imageMat, thresh, inv);
int blobCount = blobList.Count;
int selectIndex = blobCount / 2;
if (markX != -1 && markY != -1)
{
//查找标记的Blob
int markIndex = -1;
for (int i = 0; i < blobCount; i++)
{
CvBlob blob = blobList[i];
if (blob.Rect.Contains(new OpenCvSharp.Point(markX, markY)))
{
if (markIndex == -1 || blobList[i].Area < blobList[markIndex].Area)
{
markIndex = i;
}
}
}
if (markIndex != -1)
{
int area = blobList[markIndex].Area;
area = area * 5/3 - 23;
return area;
}
}
return 0;
}
/// <summary>
/// 根据元器件特征统计图片中的元器件数量
/// </summary>
/// <param name="image"></param>
/// <param name="itemFeature"></param>
/// <param name="thresh"></param>
/// <param name="inv"></param>
/// <returns></returns>
public static int CountItems(ref Image image, int itemFeature, int thresh = -1, bool inv = true)
{
Mat imageMat = BitmapConverter.ToMat(new Bitmap(image));
List<CvBlob> blobList = GetBlobs(imageMat, thresh, inv);
int itemArea = (itemFeature + 23) * 3 /5;
if(itemArea <= 0)
{
itemArea = 3;
}
int totalCount = CountBlobs(blobList, itemArea, ref imageMat);
image = BitmapConverter.ToBitmap(imageMat);
return totalCount;
}
private static void LabelBlobsInCircle(ref string[] labels, List<CvBlob> blobList, double centerX, double centerY, double radius)
{
int labelCount = 0;
double minX = 0;
double maxX = 0;
double minY = 0;
double maxY = 0;
for (int i = 0; i < labels.Length; i++)
{
if (labels[i] == null)
{
CvBlob anotherBlob = blobList[i];
//左上,右上,左下,右下
bool isNeighbour = false;
if (Math.Abs(anotherBlob.MinX - centerX) < radius && Math.Abs(anotherBlob.MinY - centerY) < radius)
{
isNeighbour = true;
}
else if (Math.Abs(anotherBlob.MaxX - centerX) < radius && Math.Abs(anotherBlob.MinY - centerY) < radius)
{
isNeighbour = true;
}
else if (Math.Abs(anotherBlob.MinX - centerX) < radius && Math.Abs(anotherBlob.MaxY - centerY) < radius)
{
isNeighbour = true;
}
else if (Math.Abs(anotherBlob.MaxX - centerX) < radius && Math.Abs(anotherBlob.MaxY - centerY) < radius)
{
isNeighbour = true;
}
if (isNeighbour)
{
labels[i] = "1";
labelCount = labelCount + 1;
if (anotherBlob.MinX < minX || minX == 0)
{
minX = anotherBlob.MinX;
}
if (anotherBlob.MinY < minY || minY == 0)
{
minY = anotherBlob.MinY;
}
if (anotherBlob.MaxX > maxX)
{
maxX = anotherBlob.MaxX;
}
if (anotherBlob.MaxY> maxY)
{
maxY = anotherBlob.MaxY;
}
}
}
}
if (labelCount > 0)
{
double rightX = centerX;
do
{
rightX = rightX + radius;
LabelBlobsInCircle(ref labels, blobList, rightX, centerY, radius);
} while (rightX < maxX);
double leftX = centerX;
do
{
leftX = leftX - radius;
LabelBlobsInCircle(ref labels, blobList, leftX, centerY, radius);
} while (leftX > minX);
double downY = centerY;
do
{
downY = downY + radius;
LabelBlobsInCircle(ref labels, blobList, centerX, downY, radius);
} while (downY < maxY);
double upY = centerY;
do
{
upY = upY - radius;
LabelBlobsInCircle(ref labels, blobList, centerX, upY, radius);
} while (upY > minY);
}
}
private static double GetLabelStep(List<CvBlob> blobList, int avgArea, out CvBlob markBlob)
{
int leastNeighbourBlobCount = 15;
int selectIndex = blobList.Count / 2;
markBlob = blobList[selectIndex];
for(int i = selectIndex; i< blobList.Count; i ++)
{
CvBlob blob = blobList[i];
int blobArea = markBlob.Area;
if(BlobHasItem(avgArea,blob) == 1)
// if(blobArea > 0.9 * avgArea && blobArea < 1.1 * avgArea)
{
//面积与给定的面积差不多,以其为中心,周围至少要有15个Blob
int neighbourCount = 0;
int blobRectSize = (blob.MaxX-blob.MinX) < (blob.MaxY - blob.MinY) ? (blob.MaxX - blob.MinX) : (blob.MaxY - blob.MinY);
double radius = blobRectSize;
while (neighbourCount != blobList.Count)
{
neighbourCount = blobList.Count(b => {
if (BlobHasItem(avgArea, b) >= 1)
{
double distance = blob.Centroid.DistanceTo(b.Centroid);
return distance < radius;
}
return false;
});
if (neighbourCount > leastNeighbourBlobCount)
{
markBlob = blob;
return radius;
}
radius = radius + blobRectSize;
if (radius > leastNeighbourBlobCount * blobRectSize)
{
break;
}
}
}
}
return Math.Sqrt(avgArea);
}
private static int CountBlobs(List<CvBlob> blobList, int avgArea, ref Mat srcMat)
{
List<CvBlob> filterBlobList = blobList.Where(b => b.Area > 0.3 * avgArea).ToList();
if (blobList.Count == 0)
{
return 0;
}
CvBlob markBlob = null;
double labelStep = GetLabelStep(blobList, avgArea, out markBlob);
string[] labels = new string[filterBlobList.Count];
double markBlobX = markBlob.Centroid.X;
double markBlobY = markBlob.Centroid.Y;
LabelBlobsInCircle(ref labels, filterBlobList, markBlobX, markBlobY, labelStep);
int totalCount = 0;
for (int i = 0; i < labels.Length; i++)
{
if (labels[i] != null)
{
CvBlob blob = filterBlobList[i];
Scalar color = Scalar.Red;
int count = BlobHasItem(avgArea, blob);
if (count > 0)
{
if (count == 1)
{
color = Scalar.Green;
}
else if (count == 2)
{
color = Scalar.Blue;
//Cv2.PutText(srcMat, count + "", blob.Centroid, HersheyFonts.HersheySimplex, 0.5, color);
}
else if (count >= 3)
{
color = Scalar.Red;
Point2d center = blob.Centroid;
Cv2.PutText(srcMat, count + "", new OpenCvSharp.Point(center.X, center.Y), HersheyFonts.HersheySimplex, 0.5, color);
}
totalCount = totalCount + count;
blob.Contour.Render(srcMat, color);
}
}
}
string countText = "Count: " + totalCount;
int baseLine = 0;
OpenCvSharp.Size textSize = Cv2.GetTextSize(countText, HersheyFonts.HersheySimplex, 1, 1, out baseLine);
Cv2.PutText(srcMat, countText, new OpenCvSharp.Point(srcMat.Width / 2 - textSize.Width / 2, srcMat.Height / 2 - textSize.Height / 2), HersheyFonts.HersheySimplex, 1, Scalar.Blue);
//Cv2.Circle(srcMat, markBlob.Centroid, (int)labelStep, Scalar.Red, 2);
return totalCount;
}
/// <summary>
/// 二值化图像
/// </summary>
/// <param name="imageMat"></param>
/// <param name="thresh"></param>
/// <param name="inv"></param>
/// <returns></returns>
private static Mat Threshhold(Mat imageMat, int thresh = -1, bool inv = false)
{
Mat dst = new Mat();
Cv2.CvtColor(imageMat, dst, ColorConversionCodes.RGB2GRAY);
if (thresh == -1)
{
//自动局部二值化
Binarizer.Sauvola(dst, dst, 21, 0.2, 32);
}
else
{
//全局二值化
Cv2.Threshold(dst, dst, thresh, 255, ThresholdTypes.Binary);
}
if (inv)
{
Cv2.Threshold(dst, dst, thresh, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu);
}
return dst;
}
/// <summary>
/// 获取所有Blobs
/// </summary>
/// <param name="imageMat"></param>
/// <param name="thresh"></param>
/// <param name="inv"></param>
/// <returns></returns>
private static List<CvBlob> GetBlobs(Mat imageMat, int thresh = -1, bool inv = false)
{
Cv2.CvtColor(imageMat, imageMat, ColorConversionCodes.RGBA2BGR);
//Cv2.CvtColor(imageMat, imageMat, ColorConversionCodes.RGB2GRAY);
Mat dst = Threshhold(imageMat, thresh, inv);
Mat k1 = Mat.Ones(new OpenCvSharp.Size(3, 3), MatType.CV_8UC1);
Cv2.MorphologyEx(dst, dst, MorphTypes.Open, k1);
CvBlobs blobs = new CvBlobs();
blobs.Label(dst);
List<CvBlob> blobList = blobs.Values.Where(b => b.Area > 3).ToList();
return blobList;
}
/// <summary>
/// 给定Blob包含几个元器件
/// </summary>
/// <param name="averageArea"></param>
/// <param name="blob"></param>
/// <returns></returns>
public static int BlobHasItem(int averageArea, CvBlob blob)
{
int blobArea = blob.Area;
double minArea = 0.5 * averageArea;
if (averageArea < 50)
{
minArea = 0.2 * averageArea;
}
if (blobArea < minArea)
{
return 0;
}
//if (blobArea >= 0.5 * averageArea && blobArea <= 1.5 * averageArea)
//{
// return 1;
//}
//else if (blobArea > 1.5 * averageArea && blobArea <= 3 * averageArea)
//{
// return 2;
//}
//else if (blobArea > 3.5 * averageArea && blobArea < 5.5 * averageArea)
//{
// return 3;
//}
int count = (int)((blobArea+1.5 * averageArea) / (1.5 * averageArea));
if(count == 0)
{
count = 1;
}
if (count <= 50)
{
return count;
}
return -1;
}
/// <summary>
/// 读取Raw格式图片
/// </summary>
/// <param name="imagePath"></param>
/// <returns></returns>
private static Bitmap[] ReadRaw(string imagePath)
{
byte[] buff = System.IO.File.ReadAllBytes(imagePath);
if (buff == null || buff.Length == 0)
{
return null;
}
bool needRevertLayer = false;
for (int i = 0; i < buff.Length; i++)
{
if (buff[i] != 0)
{
if (i > 65535)
{
byte[] b = new byte[buff.Length - 65535];
Array.Copy(buff, 65535, b, 0, b.Length);
buff = b;
needRevertLayer = true;
}
break;
}
}
byte[] buffer_src = new byte[buff.Length / 2];
byte[] buffer_filter = new byte[buff.Length / 2];
for (int i = 0; i < buffer_src.Length; i++)
{
byte currentByte = buff[i * 2];
byte filterByte = buff[i * 2 + 1];
if (needRevertLayer)
{
currentByte = (byte)(currentByte * 3);
filterByte = (byte)(filterByte * 3);
}
buffer_src[i] = currentByte;
if (filterByte == 1)
buffer_filter[i] = currentByte;
else
{
//buffer_filter[i] = 255;
buffer_filter[i] = filterByte;
}
}
Bitmap filter_bitmap = ToImage32(buffer_filter);
Bitmap src_bitmap = ToImage32(buffer_src);
//翻转图层
if (needRevertLayer)
{
return new Bitmap[] { filter_bitmap, src_bitmap};
}
else
{
return new Bitmap[] { src_bitmap, filter_bitmap};
}
}
/// <summary>
/// 8位灰度转32位图像
/// </summary>
/// <returns></returns>
private static Bitmap ToImage32(byte[] buff)
{
int w = Convert.ToInt32(Math.Sqrt(buff.Length));
int a = buff.Length % w;
if (a != 0)
{
w = w - 1;
}
int n = 0;
byte[] bb = new byte[buff.Length * 4];
for (int i = 0; i < buff.Length; i++)
{
byte currentByte = buff[i];
//currentByte = (byte)(Math.Log(1 + currentByte) * 255);
bb[n++] = currentByte;
bb[n++] = currentByte;
bb[n++] = currentByte;
bb[n++] = 255;
}
Bitmap bmp = new Bitmap(w, w, PixelFormat.Format32bppArgb);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, w, w), ImageLockMode.ReadWrite, bmp.PixelFormat);
IntPtr ptrBmp = bmpData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(bb, 0, ptrBmp, bmpData.Stride * w);
bmp.UnlockBits(bmpData);
return bmp;
}
}
}
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("AccImage")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AccImage")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("28b5864a-a0c4-4235-8f5e-d3e7299750e0")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Costura.Fody" version="3.2.0" targetFramework="net46" />
<package id="Fody" version="3.3.2" targetFramework="net40" developmentDependency="true" />
<package id="OpenCvSharp3-AnyCPU" version="4.0.0.20181129" targetFramework="net40" />
</packages>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
</startup>
</configuration>
......@@ -9,7 +9,7 @@
<OutputType>WinExe</OutputType>
<RootNamespace>Acc.Demo</RootNamespace>
<AssemblyName>Demo</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<NuGetPackageImportStamp>
......@@ -25,6 +25,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
......@@ -34,6 +35,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<StartupObject>Acc.Demo.Program</StartupObject>
......@@ -45,6 +47,9 @@
<SignManifests>false</SignManifests>
</PropertyGroup>
<ItemGroup>
<Reference Include="Costura, Version=3.2.0.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
<HintPath>..\packages\Costura.Fody.3.2.0\lib\net46\Costura.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
......@@ -94,8 +99,8 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\AccImage\AccImage.csproj">
<Project>{2626350b-63da-416c-b72a-7fe7825c3711}</Project>
<ProjectReference Include="..\AccImage\AccImage.csproj">
<Project>{28b5864a-a0c4-4235-8f5e-d3e7299750e0}</Project>
<Name>AccImage</Name>
</ProjectReference>
<ProjectReference Include="..\ImageBox\ImageBox.csproj">
......@@ -114,8 +119,8 @@
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Costura.Fody.3.2.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Costura.Fody.3.2.0\build\Costura.Fody.props'))" />
<Error Condition="!Exists('..\packages\Fody.3.3.2\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.3.3.2\build\Fody.targets'))" />
<Error Condition="!Exists('..\packages\Costura.Fody.3.2.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Costura.Fody.3.2.0\build\Costura.Fody.props'))" />
</Target>
<Import Project="..\packages\Fody.3.3.2\build\Fody.targets" Condition="Exists('..\packages\Fody.3.3.2\build\Fody.targets')" />
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Costura.Fody" version="3.2.0" targetFramework="net40" />
<package id="Costura.Fody" version="3.2.0" targetFramework="net46" />
<package id="Fody" version="3.3.2" targetFramework="net40" developmentDependency="true" />
</packages>
\ No newline at end of file
......@@ -5,10 +5,10 @@ VisualStudioVersion = 15.0.27703.2035
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo", "Demo\Demo.csproj", "{2F0D362D-2A49-49C4-963D-99F958A616D7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccImage", "..\AccImage\AccImage.csproj", "{2626350B-63DA-416C-B72A-7FE7825C3711}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageBox", "ImageBox\ImageBox.csproj", "{DB03ED04-5E06-4FEB-B891-0633448F24EC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccImage", "AccImage\AccImage.csproj", "{28B5864A-A0C4-4235-8F5E-D3E7299750E0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -25,14 +25,6 @@ Global
{2F0D362D-2A49-49C4-963D-99F958A616D7}.Release|Any CPU.Build.0 = Release|Any CPU
{2F0D362D-2A49-49C4-963D-99F958A616D7}.Release|x64.ActiveCfg = Release|Any CPU
{2F0D362D-2A49-49C4-963D-99F958A616D7}.Release|x64.Build.0 = Release|Any CPU
{2626350B-63DA-416C-B72A-7FE7825C3711}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2626350B-63DA-416C-B72A-7FE7825C3711}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2626350B-63DA-416C-B72A-7FE7825C3711}.Debug|x64.ActiveCfg = Debug|x64
{2626350B-63DA-416C-B72A-7FE7825C3711}.Debug|x64.Build.0 = Debug|x64
{2626350B-63DA-416C-B72A-7FE7825C3711}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2626350B-63DA-416C-B72A-7FE7825C3711}.Release|Any CPU.Build.0 = Release|Any CPU
{2626350B-63DA-416C-B72A-7FE7825C3711}.Release|x64.ActiveCfg = Release|x64
{2626350B-63DA-416C-B72A-7FE7825C3711}.Release|x64.Build.0 = Release|x64
{DB03ED04-5E06-4FEB-B891-0633448F24EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DB03ED04-5E06-4FEB-B891-0633448F24EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DB03ED04-5E06-4FEB-B891-0633448F24EC}.Debug|x64.ActiveCfg = Debug|Any CPU
......@@ -41,6 +33,14 @@ Global
{DB03ED04-5E06-4FEB-B891-0633448F24EC}.Release|Any CPU.Build.0 = Release|Any CPU
{DB03ED04-5E06-4FEB-B891-0633448F24EC}.Release|x64.ActiveCfg = Release|Any CPU
{DB03ED04-5E06-4FEB-B891-0633448F24EC}.Release|x64.Build.0 = Release|Any CPU
{28B5864A-A0C4-4235-8F5E-D3E7299750E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28B5864A-A0C4-4235-8F5E-D3E7299750E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28B5864A-A0C4-4235-8F5E-D3E7299750E0}.Debug|x64.ActiveCfg = Debug|Any CPU
{28B5864A-A0C4-4235-8F5E-D3E7299750E0}.Debug|x64.Build.0 = Debug|Any CPU
{28B5864A-A0C4-4235-8F5E-D3E7299750E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28B5864A-A0C4-4235-8F5E-D3E7299750E0}.Release|Any CPU.Build.0 = Release|Any CPU
{28B5864A-A0C4-4235-8F5E-D3E7299750E0}.Release|x64.ActiveCfg = Release|Any CPU
{28B5864A-A0C4-4235-8F5E-D3E7299750E0}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
......@@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ImageBox</RootNamespace>
<AssemblyName>ImageBox</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
......@@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
......@@ -29,6 +30,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>false</SignAssembly>
......
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\FeedCounter\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props" Condition="Exists('..\FeedCounter\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props')" />
<Import Project="..\FeedCounter\packages\Costura.Fody.3.2.0\build\Costura.Fody.props" Condition="Exists('..\FeedCounter\packages\Costura.Fody.3.2.0\build\Costura.Fody.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{2626350B-63DA-416C-B72A-7FE7825C3711}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AccImage</RootNamespace>
<AssemblyName>AccImage</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="OpenCvSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
<HintPath>..\FeedCounter\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net40\OpenCvSharp.dll</HintPath>
</Reference>
<Reference Include="OpenCvSharp.Blob, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
<HintPath>..\FeedCounter\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net40\OpenCvSharp.Blob.dll</HintPath>
</Reference>
<Reference Include="OpenCvSharp.Extensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
<HintPath>..\FeedCounter\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net40\OpenCvSharp.Extensions.dll</HintPath>
</Reference>
<Reference Include="OpenCvSharp.UserInterface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
<HintPath>..\FeedCounter\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net40\OpenCvSharp.UserInterface.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ImageUtil.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Content Include="FodyWeavers.xml">
<SubType>Designer</SubType>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\FeedCounter\packages\Costura.Fody.3.2.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', '..\FeedCounter\packages\Costura.Fody.3.2.0\build\Costura.Fody.props'))" />
<Error Condition="!Exists('..\FeedCounter\packages\Fody.3.3.2\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\FeedCounter\packages\Fody.3.3.2\build\Fody.targets'))" />
<Error Condition="!Exists('..\FeedCounter\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props')" Text="$([System.String]::Format('$(ErrorText)', '..\FeedCounter\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props'))" />
</Target>
<Import Project="..\FeedCounter\packages\Fody.3.3.2\build\Fody.targets" Condition="Exists('..\FeedCounter\packages\Fody.3.3.2\build\Fody.targets')" />
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Costura.Fody" version="3.2.0" targetFramework="net40" />
<package id="Fody" version="3.3.2" targetFramework="net40" developmentDependency="true" />
<package id="OpenCvSharp3-AnyCPU" version="4.0.0.20181129" targetFramework="net40" />
</packages>
\ No newline at end of file
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!