Class2.cs 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using OpenCvSharp;
  8. namespace ceju
  9. {
  10. class SubFunction
  11. {
  12. /*
  13. * 摘要:
  14. * 计算图片非零像素的平均阈值
  15. * 参数:
  16. * image:
  17. * 输入图片,(单通道)
  18. * threshold:
  19. * 输出阈值
  20. */
  21. public static void AverageThreshold(Mat image, out double threshould)
  22. {
  23. // 计算非零像素的个数
  24. int count = 0;
  25. BasFunction.Count(image, out count);
  26. // 计算像素和
  27. double sum = 0;
  28. for (int i = 0; i < image.Rows; i++)
  29. {
  30. for (int j = 0; j < image.Cols; j++)
  31. {
  32. sum += image.Get<int>(i, j);
  33. }
  34. }
  35. // 求平均阈值
  36. threshould = sum / count;
  37. }
  38. /*
  39. * 摘要:
  40. * 提取图像的中部区域的边界
  41. * 参数:
  42. * image:
  43. * 阈值分割后的二值0,1图像
  44. * 返回:
  45. * 数组middleArea;
  46. * middleArea[0]:中部左边界
  47. * middleArea[1]:中部右边界
  48. * 方法:
  49. * 对每列求和,得到行向量;
  50. * 得到行向量的最大值;
  51. * 分别对行向量从左往右和从右往左遍历,找到大于最大值-10的列
  52. */
  53. public static int[] GetMiddleArea(Mat image)
  54. {
  55. //求行数和列数
  56. int rows = image.Rows;
  57. int cols = image.Cols;
  58. //将Mat类中的数据放到数组中
  59. int[,] arr = BasFunction.Mat2Array(image);
  60. //将数组转换成0,1数组
  61. arr = BasFunction.ConversionRange(arr);
  62. //中部边界的数组
  63. int[] middleArea = new int[2];
  64. //对数组每列求和得到行向量
  65. int[] sumC = BasFunction.Sum(arr, 1);
  66. //得到该行向量的最大值
  67. int sumCMax = BasFunction.Max(sumC);
  68. //从左向右遍历,大于临界值时,得到中部左边界
  69. for (int j = 0; j < cols; j++)
  70. {
  71. if (sumC[j] > sumCMax - 10)
  72. {
  73. middleArea[0] = j;
  74. break;
  75. }
  76. }
  77. //从右向左遍历,大于临界值时,得到中部右边界
  78. for (int j = cols - 1; j >= 0; j--)
  79. {
  80. if (sumC[j] > sumCMax - 10)
  81. {
  82. middleArea[1] = j;
  83. break;
  84. }
  85. }
  86. return middleArea;
  87. }
  88. /*
  89. * 摘要:
  90. * 得到数据提取区域的边界
  91. * 参数:
  92. * image:
  93. * 输入阈值分割后的图像
  94. * middleArea:
  95. * 输入中间区域的边界,
  96. * middleArea[0]:中部左边界
  97. * middleArea[1]:中部右边界
  98. * 返回:
  99. * dataArea:
  100. * 数据提取区域的边界
  101. * dataArea[0]:左半边的左边界
  102. * dataArea[1]:左半边的右边界
  103. * dataArea[2]:右半边的左边界
  104. * dataArea[3]:右半边的右边界
  105. */
  106. public static int[] GetDataArea(Mat image, int[] middleArea)
  107. {
  108. //图片行列数
  109. int rows = image.Rows;
  110. int cols = image.Cols;
  111. //图片填充的上界
  112. int y = 0;
  113. //将Mat类数据存入数组中
  114. int[,] arr = BasFunction.Mat2Array(image);
  115. //将数组转换成0,1数组
  116. arr = BasFunction.ConversionRange(arr);
  117. //对二维数组每行求和,输出列向量
  118. int[] sumR = BasFunction.Sum(arr, 2);
  119. //列向量的最大值
  120. int sumRMax = BasFunction.Max(sumR);
  121. //当列向量某行的值大于最大值的一半时,得到填充上界
  122. for (int i = 1; i < rows; i++)
  123. {
  124. if (sumR[i] > sumRMax / 2)
  125. {
  126. y = i;
  127. break;
  128. }
  129. }
  130. //将二维数组,y以下的所有行都变为1
  131. int[,] arrFill = arr;
  132. for (int i = y; i < rows; i++)
  133. {
  134. for (int j = 1; j < cols; j++)
  135. {
  136. arrFill[i, j] = 1;
  137. }
  138. }
  139. //对填充后的数组每列求和,得到行向量
  140. int[] sumCArrayFill = BasFunction.Sum(arrFill, 1);
  141. //该行向量的middleArea[0] - 500到middleArea[0]区间,左半边
  142. int[] sumCArrayFillLeft = BasFunction.Intercept(sumCArrayFill, middleArea[0] - 500, middleArea[0]);
  143. //该行向量的middleArea[1]到middleArea[1] + 500区间,右半边
  144. int[] sumCArrayFillRight = BasFunction.Intercept(sumCArrayFill, middleArea[1], middleArea[1] + 500);
  145. //左半边的最大值
  146. int sumCArrayFillLeftMax = BasFunction.Max(sumCArrayFillLeft);
  147. //右半边最大值
  148. int sumCArrayFillRightMax = BasFunction.Max(sumCArrayFillRight);
  149. //数据提取区域,分别是左半边左右边界,右半边左右边界
  150. int[] dataArea = new int[4];
  151. for (int j = middleArea[0] - 500; j < middleArea[0]; j++)
  152. {
  153. if (sumCArrayFill[j] > sumCArrayFillLeftMax - 10)
  154. {
  155. dataArea[0] = j;
  156. break;
  157. }
  158. }
  159. for (int j = middleArea[0]; j > middleArea[0] - 500; j--)
  160. {
  161. if (sumCArrayFill[j] > sumCArrayFillLeftMax - 5)
  162. {
  163. dataArea[1] = j;
  164. break;
  165. }
  166. }
  167. for (int j = middleArea[1]; j < middleArea[1] + 500; j++)
  168. {
  169. if (sumCArrayFill[j] > sumCArrayFillRightMax - 5)
  170. {
  171. dataArea[2] = j;
  172. break;
  173. }
  174. }
  175. for (int j = middleArea[1] + 500; j > middleArea[1]; j--)
  176. {
  177. if (sumCArrayFill[j] > sumCArrayFillRightMax - 10)
  178. {
  179. dataArea[3] = j;
  180. break;
  181. }
  182. }
  183. return dataArea;
  184. }
  185. /*
  186. * 摘要:
  187. * 提取线条,保存每列第一个点,需要没有间断点
  188. * 参数:
  189. * array:
  190. * 输入二维数组,0,1二值
  191. * line:
  192. * 输出线条,0,1二值
  193. * averageCoordinate:
  194. * 输出平均纵坐标
  195. * leftBorder:
  196. * 左边界
  197. * rightBorder:
  198. * 右边界
  199. */
  200. public static void ExtractLines(int[,] array, out int[,] line, out double averageCoordinate, int leftBorder, int rightBorder)
  201. {
  202. //数组长宽
  203. int rows = array.GetLength(0);
  204. int cols = array.GetLength(1);
  205. //输出的线条数组
  206. line = new int[rows, cols];
  207. //记录线条上点的纵坐标数组
  208. int[] coordinate = new int[cols];
  209. //线条平均纵坐标
  210. averageCoordinate = 0;
  211. //纵坐标之和
  212. int sum = 0;
  213. //线条上点的个数
  214. int count = 0;
  215. //遍历,原数组上该位置有点时,线条上的该位置置1
  216. for (int j = leftBorder; j < rightBorder; j++)
  217. {
  218. for (int i = 0; i < rows; i++)
  219. {
  220. if (array[i, j] == 1)
  221. {
  222. line[i, j] = 1;
  223. coordinate[j] = i;
  224. break;
  225. }
  226. }
  227. }
  228. //计算纵坐标之和
  229. BasFunction.Sum(coordinate, out sum);
  230. //计算线条内点的个数
  231. BasFunction.Count(coordinate, out count);
  232. //平均纵坐标
  233. averageCoordinate = sum / count;
  234. }
  235. /*
  236. * 摘要:
  237. * 提取线条,保存每列第一个点,需要没有间断点,不输出线条
  238. * 参数:
  239. * array:
  240. * 输入二维数组,0,1二值
  241. * averageCoordinate:
  242. * 输出平均纵坐标
  243. * leftBorder:
  244. * 左边界
  245. * rightBorder:
  246. * 右边界
  247. */
  248. public static void ExtractLines(int[,] array, out double averageCoordinate, int leftBorder, int rightBorder)
  249. {
  250. //数组长宽
  251. int rows = array.GetLength(0);
  252. int cols = array.GetLength(1);
  253. //记录线条上点的纵坐标数组
  254. int[] coordinate = new int[cols];
  255. //线条平均纵坐标
  256. averageCoordinate = 0;
  257. //纵坐标之和
  258. int sum = 0;
  259. //线条上点的个数
  260. int count = 0;
  261. //遍历,查询有点的个数
  262. for (int j = leftBorder; j < rightBorder; j++)
  263. {
  264. for (int i = 0; i < rows; i++)
  265. {
  266. if (array[i, j] == 1)
  267. {
  268. coordinate[j] = i;
  269. break;
  270. }
  271. }
  272. }
  273. //计算纵坐标之和
  274. BasFunction.Sum(coordinate, out sum);
  275. //计算线条内点的个数
  276. BasFunction.Count(coordinate, out count);
  277. //平均纵坐标
  278. averageCoordinate = sum / count;
  279. }
  280. /*
  281. * 摘要:
  282. * 提取线条,保存每列第一个点,需要没有间断点
  283. * 参数:
  284. * array:
  285. * 输入二维数组,0,1二值
  286. * line:
  287. * 输出线条,0,1二值
  288. * averageCoordinateNew:
  289. * 输出平均纵坐标
  290. * leftBorder:
  291. * 左边界
  292. * rightBorder:
  293. * 右边界
  294. * averageCoordinate:
  295. * 作为基准的上一条线的平均纵坐标
  296. */
  297. public static void ExtractLines(int[,] array, out int[,] line, out double averageCoordinateNew, int leftBorder, int rightBorder, double averageCoordinate)
  298. {
  299. //数组长宽
  300. int rows = array.GetLength(0);
  301. int cols = array.GetLength(1);
  302. //输出的线条数组
  303. line = new int[rows, cols];
  304. //新的记录线条纵坐标的数组
  305. int[] coordinateNew = new int[cols];
  306. //遍历范围的上界和下界
  307. int upperBound = (int)averageCoordinate + 15;
  308. int lowerBound = (int)averageCoordinate + 45;
  309. //初始化,记录纵坐标的数组全为0
  310. for (int j = 0; j < cols; j++)
  311. {
  312. coordinateNew[j] = 0;
  313. }
  314. //计算线条内点的个数
  315. int count = 0;
  316. BasFunction.Count(coordinateNew, out count);
  317. //当点的个数少于5的时候循环
  318. while (count < 5)
  319. {
  320. //每次循环,寻找范围向下移动5个像素
  321. upperBound = upperBound + 5;
  322. lowerBound = lowerBound + 5;
  323. //遍历,寻找线条上的点,并记录纵坐标
  324. for (int j = leftBorder; j < rightBorder; j++)
  325. {
  326. for (int i = upperBound; i < lowerBound; i++)
  327. {
  328. if (array[i, j] > 0)
  329. {
  330. line[i, j] = 1;
  331. coordinateNew[j] = i;
  332. break;
  333. }
  334. }
  335. }
  336. //重新计算线条内点的个数
  337. BasFunction.Count(coordinateNew, out count);
  338. }
  339. //纵坐标之和
  340. int sum = 0;
  341. BasFunction.Sum(coordinateNew, out sum);
  342. //平均纵坐标
  343. averageCoordinateNew = 0;
  344. averageCoordinateNew = sum / count;
  345. }
  346. /*
  347. * 摘要:
  348. * 提取线条,保存每列第一个点,需要没有间断点,不输出线条,只输出纵坐标
  349. * 参数:
  350. * array:
  351. * 输入二维数组,0,1二值
  352. * averageCoordinateNew:
  353. * 输出平均纵坐标
  354. * leftBorder:
  355. * 左边界
  356. * rightBorder:
  357. * 右边界
  358. * averageCoordinate:
  359. * 作为基准的上一条线的平均纵坐标
  360. */
  361. public static void ExtractLines(int[,] array, out double averageCoordinateNew, int leftBorder, int rightBorder, double averageCoordinate)
  362. {
  363. //数组长宽
  364. int rows = array.GetLength(0);
  365. int cols = array.GetLength(1);
  366. //新的记录线条纵坐标的数组
  367. int[] coordinateNew = new int[cols];
  368. //遍历范围的上界和下界
  369. int upperBound = (int)averageCoordinate + 15;
  370. int lowerBound = (int)averageCoordinate + 45;
  371. //初始化,记录纵坐标的数组全为0
  372. for (int j = 0; j < cols; j++)
  373. {
  374. coordinateNew[j] = 0;
  375. }
  376. //计算线条内点的个数
  377. int count = 0;
  378. BasFunction.Count(coordinateNew, out count);
  379. //当点的个数少于5的时候循环
  380. while (count < 5)
  381. {
  382. //每次循环,寻找范围向下移动5个像素
  383. upperBound = upperBound + 5;
  384. lowerBound = lowerBound + 5;
  385. //遍历,寻找线条上的点,并记录纵坐标
  386. for (int j = leftBorder; j < rightBorder; j++)
  387. {
  388. for (int i = upperBound; i < lowerBound; i++)
  389. {
  390. if (array[i, j] > 0)
  391. {
  392. coordinateNew[j] = i;
  393. break;
  394. }
  395. }
  396. }
  397. //重新计算线条内点的个数
  398. BasFunction.Count(coordinateNew, out count);
  399. }
  400. //纵坐标之和
  401. int sum = 0;
  402. BasFunction.Sum(coordinateNew, out sum);
  403. //平均纵坐标
  404. averageCoordinateNew = 0;
  405. averageCoordinateNew = sum / count;
  406. }
  407. /*
  408. * 摘要:
  409. * 提取线条,保存每列第一个点,需要没有间断点,不输出线条,只输出纵坐标(从下往上)
  410. * 参数:
  411. * array:
  412. * 输入二维数组,0,1二值
  413. * averageCoordinateNew:
  414. * 输出平均纵坐标
  415. * leftBorder:
  416. * 左边界
  417. * rightBorder:
  418. * 右边界
  419. * averageCoordinate:
  420. * 作为基准的上一条线的平均纵坐标
  421. */
  422. public static void ExtractLines2(int[,] array, out double averageCoordinateNew, int leftBorder, int rightBorder, double averageCoordinate)
  423. {
  424. //数组长宽
  425. int rows = array.GetLength(0);
  426. int cols = array.GetLength(1);
  427. //新的记录线条纵坐标的数组
  428. int[] coordinateNew = new int[cols];
  429. //遍历范围的上界和下界
  430. int upperBound = (int)averageCoordinate - 45;
  431. int lowerBound = (int)averageCoordinate - 10;
  432. //初始化,记录纵坐标的数组全为0
  433. for (int j = 0; j < cols; j++)
  434. {
  435. coordinateNew[j] = 0;
  436. }
  437. //计算线条内点的个数
  438. int count = 0;
  439. BasFunction.Count(coordinateNew, out count);
  440. //当点的个数少于5的时候循环
  441. while (count < 5)
  442. {
  443. //每次循环,寻找范围向下移动5个像素
  444. upperBound = upperBound - 5;
  445. lowerBound = lowerBound - 5;
  446. //遍历,寻找线条上的点,并记录纵坐标
  447. for (int j = leftBorder; j < rightBorder; j++)
  448. {
  449. for (int i = lowerBound; i > upperBound; i--)
  450. {
  451. if (array[i, j] > 0)
  452. {
  453. coordinateNew[j] = i;
  454. break;
  455. }
  456. }
  457. }
  458. //重新计算线条内点的个数
  459. BasFunction.Count(coordinateNew, out count);
  460. }
  461. //纵坐标之和
  462. int sum = 0;
  463. BasFunction.Sum(coordinateNew, out sum);
  464. //平均纵坐标
  465. averageCoordinateNew = 0;
  466. averageCoordinateNew = sum / count;
  467. }
  468. /*
  469. * 摘要:
  470. * 计算孔径的起止点
  471. * 参数:
  472. * array:
  473. * 阈值分割后的目标区域数组
  474. * aperture:
  475. * 输出孔径起止点
  476. * aperture[0]:左起始点
  477. * aperture[1]:右结束点
  478. * ordinate1:
  479. * 左部分纵坐标
  480. * ordinate2:
  481. * 右部分纵坐标
  482. * middleArea:
  483. * 中部区域边界
  484. * middleArea[0]:中部左边界
  485. * middleArea[1]:中部右边界
  486. * dataArea:
  487. * 数据提取区域边界
  488. * dataArea[0]:左部分左边界
  489. * dataArea[1]:左部分右边界
  490. * dataArea[2]:右部分左边界
  491. * dataArea[3]:右部分右边界
  492. */
  493. public static void GetAperture(int[,] array, out int[] aperture, int ordinate1, int ordinate2, int[] middleArea, int[] dataArea)
  494. {
  495. //孔径起始点
  496. aperture = new int[2];
  497. //数组行列数
  498. int rows = array.GetLength(0);
  499. int cols = array.GetLength(1);
  500. //中部区域的中心
  501. int middle = (middleArea[0] + middleArea[1]) / 2;
  502. //左右截取的区域
  503. int[,] leftSeg = new int[rows, cols];
  504. int[,] rightSeg = new int[rows, cols];
  505. leftSeg = BasFunction.InterceptArray(array, leftSeg, ordinate1 - 10, ordinate1 + 10, dataArea[0], middleArea[0] + 5, 1);
  506. rightSeg = BasFunction.InterceptArray(array, rightSeg, ordinate2 - 10, ordinate2 + 10, middleArea[1] - 5, dataArea[3], 1);
  507. //左部分列求和,行向量
  508. int[] sum1 = BasFunction.Sum(leftSeg, 1);
  509. //右部分列求和,行向量
  510. int[] sum2 = BasFunction.Sum(rightSeg, 1);
  511. //左部分遍历,从中间到左边界,当大于0时记录为孔径起始点
  512. for (int j = middle; j > dataArea[0]; j--)
  513. {
  514. if (sum1[j] > 0)
  515. {
  516. aperture[0] = j;
  517. break;
  518. }
  519. }
  520. //右部分遍历,从中间到右边界,当大于0时记录为孔径结束点
  521. for (int j = middle; j < dataArea[3]; j++)
  522. {
  523. if (sum2[j] > 0)
  524. {
  525. aperture[1] = j;
  526. break;
  527. }
  528. }
  529. }
  530. /*
  531. * 摘要:
  532. * 计算孔铜距离并得到对应的曲面上的点坐标
  533. * 参数:
  534. * line:
  535. * 第一条线
  536. * apertureBeign:
  537. * 孔径左起始点坐标,先纵坐标后横坐标
  538. * apertureEnd:
  539. * 孔径右截止点坐标,先纵坐标后横坐标
  540. * kongtong:
  541. * 输出孔铜数据数组,先左后右
  542. * pointLeft:
  543. * 曲面上左面的点坐标,先纵坐标后横坐标
  544. * pointRight:
  545. * 曲面上右面的点坐标,先纵坐标后横坐标
  546. */
  547. public static void GetKongtong(int[,] line, int[] apertureBegin, int[] apertureEnd, out double[] kongtong, out int[] pointLeft, out int[] pointRight)
  548. {
  549. //数组行列数
  550. int rows = line.GetLength(0);
  551. int cols = line.GetLength(1);
  552. //截取曲面线条
  553. int[,] lineSeg = new int[rows, cols];
  554. lineSeg = BasFunction.InterceptArray(line, lineSeg, 0, rows, apertureBegin[1], apertureEnd[1]);
  555. //曲面上点的个数
  556. int count = 0;
  557. BasFunction.Count(lineSeg, out count);
  558. //得到曲面线条上点的坐标位置
  559. int[,] coordinate = new int[count, 2];
  560. BasFunction.Find(lineSeg, out coordinate);
  561. //计算距离,最小的距离分别为左孔铜和右孔铜
  562. double leftKongtong = 1000;
  563. double rightKongtong = 1000;
  564. double distance1 = 0;
  565. double distance2 = 0;
  566. //当距离最短是曲线上点的坐标,第一列是纵坐标(行数),第二列是横坐标(列数)
  567. pointLeft = new int[2];
  568. pointRight = new int[2];
  569. for (int i = 0; i < count; i++)
  570. {
  571. //计算曲面点到左起始点的距离
  572. distance1 = Math.Sqrt(Math.Pow((coordinate[i, 0] - apertureBegin[0]), 2) + Math.Pow((coordinate[i, 1] - apertureBegin[1]), 2));
  573. //计算曲面点到右截止点的距离
  574. distance2 = Math.Sqrt(Math.Pow((coordinate[i, 0] - apertureEnd[0]), 2) + Math.Pow((coordinate[i, 1] - apertureEnd[1]), 2));
  575. //得到左右孔铜以及对应的曲面坐标
  576. if (leftKongtong > distance1)
  577. {
  578. leftKongtong = distance1;
  579. pointLeft[0] = coordinate[i, 0];
  580. pointLeft[1] = coordinate[i, 1];
  581. }
  582. if (rightKongtong > distance2)
  583. {
  584. rightKongtong = distance2;
  585. pointRight[0] = coordinate[i, 0];
  586. pointRight[1] = coordinate[i, 1];
  587. }
  588. }
  589. kongtong = new double[2] { leftKongtong, rightKongtong };
  590. }
  591. /*
  592. * 摘要:
  593. * 得到孔深,并且得到曲面最凸点或者最凹点的坐标
  594. * 参数:
  595. * line:
  596. * 线条
  597. * apertureBegin:
  598. * 孔径左侧起始点坐标,先纵后横
  599. * apertureEnd:
  600. * 孔径右侧结束点坐标,先纵后横
  601. * curveVertex:
  602. * 曲面凸点的坐标
  603. */
  604. public static void CurveVertex(int[,] line, int[] apertureBegin, int[] apertureEnd, out int[] curveVertex, out double middleApertureY)
  605. {
  606. //曲面顶点的坐标
  607. curveVertex = new int[2];
  608. //数组行列数
  609. int rows = line.GetLength(0);
  610. int cols = line.GetLength(1);
  611. //孔径平均横坐标
  612. double middleAperture = (apertureBegin[1] + apertureEnd[1]) / 2;
  613. //孔径平均高度
  614. int[] apertureY = new int[2];
  615. for (int i = 0; i < rows; i++)
  616. {
  617. if (line[i, apertureBegin[1]] > 0)
  618. {
  619. apertureY[0] = i;
  620. break;
  621. }
  622. }
  623. for (int i = 0; i < rows; i++)
  624. {
  625. if (line[i, apertureEnd[1]] > 0)
  626. {
  627. apertureY[1] = i;
  628. break;
  629. }
  630. }
  631. middleApertureY = (apertureY[0] + apertureY[1]) / 2;
  632. //截取曲面线条
  633. int[,] lineSeg = new int[rows, cols];
  634. lineSeg = BasFunction.InterceptArray(line, lineSeg, 0, rows, apertureBegin[1], apertureEnd[1]);
  635. //曲面上点的个数
  636. int count = 0;
  637. BasFunction.Count(lineSeg, out count);
  638. //得到曲面线条上点的坐标位置
  639. int[,] coordinate = new int[count, 2];
  640. BasFunction.Find(lineSeg, out coordinate);
  641. //孔深,取曲面上的点到孔径平均高度只差的最大值
  642. double kongshenAomian = 0;
  643. double kongshenTumian = 0;
  644. int[] coordinateAomian = new int[2];
  645. int[] coordinateTumian = new int[2];
  646. double tAomian = 0;
  647. double tTumian = 0;
  648. for (int i = 0; i < count; i++)
  649. {
  650. tAomian = coordinate[i, 0] - middleApertureY;
  651. tTumian = middleApertureY - coordinate[i, 0];
  652. if (tAomian > kongshenAomian)
  653. {
  654. kongshenAomian = tAomian;
  655. //纵坐标
  656. coordinateAomian[0] = coordinate[i, 0];
  657. //横坐标
  658. coordinateAomian[1] = coordinate[i, 1];
  659. }
  660. if (tTumian > kongshenTumian)
  661. {
  662. kongshenTumian = tTumian;
  663. coordinateTumian[0] = coordinate[i, 0];
  664. coordinateTumian[1] = coordinate[i, 1];
  665. }
  666. }
  667. //判断哪个点离孔径中心更近,更近的为曲面坐标
  668. double absAomian = Math.Abs(coordinateAomian[1] - middleAperture);
  669. double absTumian = Math.Abs(coordinateTumian[1] - middleAperture);
  670. if (absAomian < absTumian)
  671. {
  672. curveVertex = coordinateAomian;
  673. }
  674. else
  675. {
  676. curveVertex = coordinateTumian;
  677. }
  678. }
  679. /*
  680. * 摘要:
  681. * 得到内部线条,将内部线条进行腐蚀后,求平均纵坐标
  682. * 参数:
  683. * image:
  684. * 输入图像,一般是绿色通道
  685. * upperBound, lowerBound, leftBoundary, rightBoundary:
  686. * 内部区域的上下左右边界
  687. * meanOrdinate:
  688. * 输出的纵坐标
  689. *
  690. */
  691. public static void InsideLine(Mat image, int upperBound, int lowerBound, int leftBoundary, int rightBoundary, out double meanOrdinate)
  692. {
  693. // 内部区域的行列数
  694. int rows = lowerBound - upperBound;
  695. int cols = rightBoundary - leftBoundary;
  696. // 内部区域的图像
  697. Mat inside = new Mat(rows, cols, image.Type());
  698. // 滤波提高对比度后的图像
  699. Mat filter = new Mat(rows, cols, MatType.CV_8UC1);
  700. // 绘制内部区域像素
  701. for (int i = 0; i < rows; i++)
  702. {
  703. for (int j = 0; j < cols; j++)
  704. {
  705. int value = image.Get<int>(i + upperBound, j + leftBoundary);
  706. inside.Set<int>(i, j, 255 - value);
  707. }
  708. }
  709. // 滤波核
  710. InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
  711. // 滤波
  712. Cv2.Filter2D(inside, filter, -1, kernel);
  713. Cv2.ConvertScaleAbs(filter, filter);
  714. // 腐蚀宽度
  715. double width = (rightBoundary - leftBoundary) * 0.3;
  716. // 腐蚀结构元素
  717. Mat seErode = Cv2.GetStructuringElement(MorphShapes.Rect, new Size((int)width, 1));
  718. // 腐蚀
  719. Mat erode = new Mat();
  720. //Cv2.Erode(filter, erode, seErode);
  721. // 二值化
  722. Mat thresh = new Mat(filter.Size(), filter.Type());
  723. thresh = filter.Threshold(0, 255, ThresholdTypes.Otsu);
  724. // 再次腐蚀
  725. Cv2.Erode(thresh, erode, seErode);
  726. // 边缘检测
  727. Mat grad_y = new Mat();
  728. Mat grad_y2 = new Mat();
  729. Cv2.Sobel(erode, grad_y, MatType.CV_16S, 0, 1);
  730. Cv2.ConvertScaleAbs(grad_y, grad_y2);
  731. //new Window("zengqiang", WindowMode.Normal, filter);
  732. //new Window("erzhihua", WindowMode.Normal, thresh);
  733. //new Window("fushi", WindowMode.Normal, erode);
  734. //new Window("bianyuanjiance", WindowMode.Normal, grad_y2);
  735. //Cv2.WaitKey(0);
  736. int[,] arraySobel = new int[rows, cols];
  737. arraySobel = BasFunction.Mat2Array(grad_y2);
  738. arraySobel = BasFunction.ConversionRange(arraySobel);
  739. int[,] line6 = new int[rows, cols];
  740. meanOrdinate = 0;
  741. //SubFunction.ExtractLines(arraySobel, out line6, out meanOrdinateL6, 0, 180,1);
  742. //meanOrdinateL6 = meanOrdinateL6-3 + upperBound;
  743. // 非零元素坐标
  744. int[,] coordianate;
  745. BasFunction.Find(arraySobel, out coordianate);
  746. // 非零元素个数
  747. int count = coordianate.GetLength(0);
  748. double sum = 0;
  749. for (int i = 0; i < count; i++)
  750. {
  751. sum += coordianate[i, 0];
  752. }
  753. // 得到内部线的纵坐标(平均坐标加上上界)
  754. meanOrdinate = sum / count + upperBound;
  755. //new Window("inside", WindowMode.AutoSize, inside);
  756. //new Window("filter", WindowMode.AutoSize, filter);
  757. //new Window("thresh", WindowMode.AutoSize, thresh);
  758. //new Window("sobel", WindowMode.AutoSize, grad_y2);
  759. //Cv2.WaitKey(0);
  760. }
  761. public static double AverageBrightness(double[,] array)
  762. {
  763. //数组中大于0的个数
  764. int count = 0;
  765. BasFunction.Count(array, out count);
  766. //数组中所有亮度之和
  767. double sum = 0;
  768. BasFunction.Sum(array, out sum);
  769. //数组中的平均亮度
  770. double result = sum / count;
  771. return result;
  772. }
  773. /*
  774. * 摘要:
  775. * 绘制线条
  776. * 参数:
  777. * image:
  778. * 绘制的图片
  779. * x1:点1的横坐标
  780. * y1:点1的纵坐标
  781. * x2:点2的横坐标
  782. * y2:点2的纵坐标
  783. */
  784. public static void LineShow(Mat image, int x1, int y1, int x2, int y2)
  785. {
  786. Point p1 = new Point();
  787. Point p2 = new Point();
  788. p1.X = x1;
  789. p1.Y = y1;
  790. p2.X = x2;
  791. p2.Y = y2;
  792. Scalar color = new Scalar(0, 0, 255);
  793. //颜色
  794. Cv2.Line(image, p1, p2, color, 2, LineTypes.Link8);
  795. }
  796. /*
  797. * 摘要:
  798. * 将三通道图片分割成单通道
  799. * 参数:
  800. * image:
  801. * 三通道图片
  802. * imageBlue,imageGreen,imageRed:
  803. * 蓝绿红图片
  804. * colorChoose:
  805. * 选择线条颜色,blue,green,red
  806. */
  807. public static void LineShow(Mat image, int x1, int y1, int x2, int y2, string colorChoose)
  808. {
  809. Point p1 = new Point();
  810. Point p2 = new Point();
  811. p1.X = x1;
  812. p1.Y = y1;
  813. p2.X = x2;
  814. p2.Y = y2;
  815. Scalar color = new Scalar();//颜色
  816. switch (colorChoose)
  817. {
  818. case "blue":
  819. color = new Scalar(255, 0, 0);
  820. break;
  821. case "green":
  822. color = new Scalar(0, 255, 0);
  823. break;
  824. case "red":
  825. color = new Scalar(0, 0, 255);
  826. break;
  827. default:
  828. break;
  829. }
  830. Cv2.Line(image, p1, p2, color, 2, LineTypes.Link8);
  831. }
  832. /*
  833. * 摘要:
  834. * 显示文字
  835. * 参数:
  836. * image:
  837. * 背景图片
  838. * data:
  839. * 显示数字
  840. * x,y:
  841. * 显示坐标
  842. */
  843. public static void TextShow(Mat image, double data, int x, int y)
  844. {
  845. data = Math.Round(data, 2);
  846. Scalar color = new Scalar(255, 0, 0);
  847. Point p = new Point();
  848. p.X = x;
  849. p.Y = y;
  850. Cv2.PutText(image, data.ToString() + "um", p, HersheyFonts.HersheyComplex, 0.8, color, 2, LineTypes.Link8);
  851. }
  852. /*
  853. * 摘要:
  854. * 将三通道图片分割成单通道
  855. * 参数:
  856. * image:
  857. * 三通道图片
  858. * imageBlue,imageGreen,imageRed:
  859. * 蓝绿红图片
  860. */
  861. public static void SeparateRGB(Mat image, out Mat imageBlue, out Mat imageGreen, out Mat imageRed)
  862. {
  863. int rows = image.Rows;
  864. int cols = image.Cols;
  865. imageBlue = new Mat(rows, cols, MatType.CV_8UC1);
  866. imageGreen = new Mat(rows, cols, MatType.CV_8UC1);
  867. imageRed = new Mat(rows, cols, MatType.CV_8UC1);
  868. for (int i = 0; i < rows; i++)
  869. {
  870. for (int j = 0; j < cols; j++)
  871. {
  872. int b = image.Get<Vec3b>(i, j)[0];
  873. int g = image.Get<Vec3b>(i, j)[1];
  874. int r = image.Get<Vec3b>(i, j)[2];
  875. imageBlue.Set<int>(i, j, b);
  876. imageGreen.Set<int>(i, j, g);
  877. imageRed.Set<int>(i, j, r);
  878. }
  879. }
  880. }
  881. /*
  882. * 摘要:
  883. * 对图片进行Sobel边缘检测,输出的是横向检测与纵向检测混合之后的结果
  884. */
  885. public static void Sobel(Mat imageContour,out Mat imageSobel)
  886. {
  887. //横向检测
  888. Mat grad_x = new Mat();
  889. Mat grad_x2 = new Mat();
  890. Cv2.Sobel(imageContour, grad_x, MatType.CV_16S, 1, 0);
  891. Cv2.ConvertScaleAbs(grad_x, grad_x2);
  892. //纵向检测
  893. Mat grad_y = new Mat();
  894. Mat grad_y2 = new Mat();
  895. Cv2.Sobel(imageContour, grad_y, MatType.CV_16S, 0, 1);
  896. Cv2.ConvertScaleAbs(grad_y, grad_y2);
  897. //横纵向混合平均
  898. imageSobel = new Mat();
  899. Cv2.AddWeighted(grad_x2, 0.5, grad_y2, 0.5, 0, imageSobel);
  900. }
  901. /*
  902. * 摘要:
  903. * 标注线条(竖向的标记线条)
  904. * 参数:
  905. * image:
  906. * 画线条的图片
  907. * x1,y1,x2,y2:
  908. * 线条端点的横纵坐标
  909. * data:
  910. * 显示数据
  911. */
  912. public static void LabelVertical(Mat image, int x1, int y1, int x2, int y2, double data)
  913. {
  914. double proportion = 0.2689;
  915. data = data * proportion;
  916. double middle = (y1 + y2) / 2;
  917. LineShow(image, x1, y1, x2, y2, "blue");
  918. LineShow(image, x1 - 5, y1, x1 + 5, y1, "blue");
  919. LineShow(image, x1 - 5, y2, x1 + 5, y2, "blue");
  920. TextShow(image, data, x1, (int)middle);
  921. }
  922. /*
  923. * 摘要:
  924. * 标注线条(横向的标记线条)
  925. * 参数:
  926. * image:
  927. * 画线条的图片
  928. * x1,y1,x2,y2:
  929. * 线条端点的横纵坐标
  930. * data:
  931. * 显示数据
  932. */
  933. public static void LableHorizontal(Mat image, int x1, int y1, int x2, int y2, double data)
  934. {
  935. double proportion = 0.2689;
  936. data = data * proportion;
  937. double middle = (x1 + x2) / 2;
  938. LineShow(image, x1, y1, x2, y2, "blue");
  939. LineShow(image, x1, y1 - 5, x1, y1 + 5, "blue");
  940. LineShow(image, x2, y1 - 5, x2, y1 + 5, "blue");
  941. TextShow(image, data, (int)middle, y1);
  942. }
  943. // 深盲孔子程序
  944. /*
  945. * 摘要:
  946. * 获得胶体区域的二值图
  947. * 参数:
  948. * imageContour:
  949. * 目标区域图,用作掩膜
  950. * imageRed:
  951. * 原图红色通道图
  952. * result:
  953. * 输出图片
  954. * upperBound:
  955. * 上界
  956. * lowerBound:
  957. * 下界
  958. */
  959. public static void GlueArea(Mat imageContour, Mat imageRed, out Mat result, int upperBound, int lowerBound,int leftBound,int rightBound)
  960. {
  961. int rows = lowerBound - upperBound;
  962. int cols = rightBound - leftBound;
  963. // 掩膜
  964. Mat image = new Mat(imageRed.Size(), imageRed.Type());
  965. image.CopyTo(imageRed, ~imageContour);
  966. // 得到胶体区域
  967. Mat glue = new Mat(rows, cols, imageRed.Type());
  968. BasFunction.InterceptMat(image, out glue, upperBound, lowerBound, leftBound, rightBound);
  969. // 滤波
  970. Mat glueFilter = new Mat(glue.Size(), glue.Type());
  971. Cv2.Blur(glue, glueFilter, new OpenCvSharp.Size(3, 3));
  972. // 计算平均阈值
  973. double threshold = 0;
  974. AverageThreshold(glueFilter, out threshold);
  975. // 阈值分割
  976. Mat thresh = new Mat();
  977. Cv2.Threshold(glueFilter, thresh, threshold, 255, ThresholdTypes.Binary);
  978. // 闭运算去噪(去除小孔)
  979. Mat close = new Mat();
  980. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));// 结构元素
  981. Cv2.MorphologyEx(thresh, close, MorphTypes.Close, se);
  982. // 腐蚀去噪(去除小点)
  983. Mat se2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(10, 1));// 结构元素
  984. Mat erode = new Mat();
  985. Cv2.Erode(close, erode, se);// 腐蚀
  986. // 边缘检测
  987. Mat sobel = new Mat();
  988. Sobel(erode, out sobel);
  989. // 输出
  990. result =new Mat(imageRed.Size(),imageRed.Type());
  991. for (int i = 0; i < rows; i++)
  992. {
  993. for (int j = 0; j < cols; j--)
  994. {
  995. int value = erode.Get<int>(i, j);
  996. result.Set<int>(i + upperBound, j + leftBound, value);
  997. }
  998. }
  999. }
  1000. /*
  1001. * 摘要:
  1002. * 计算胶内缩数据的上胶边界
  1003. * 参数:
  1004. * imageContour:
  1005. * 目标区域图
  1006. * upperWaist:
  1007. * 输出的上胶边界,先左后右
  1008. * upperBound:
  1009. * 提取区域的上界
  1010. * lowerBound:
  1011. * 提取区域的下界
  1012. * middle:
  1013. * 应是孔径的中心
  1014. */
  1015. public static void GetWaist(Mat imageContour, out int[] upperWaist, int upperBound, int lowerBound, int middle)
  1016. {
  1017. // 上胶区域
  1018. Mat waist = new Mat();
  1019. BasFunction.InterceptMat(~imageContour, out waist, upperBound, lowerBound, 0, imageContour.Cols);
  1020. // 转数组
  1021. int[,] arrayWaist = BasFunction.Mat2Array(waist);
  1022. arrayWaist = BasFunction.ConversionRange(arrayWaist);
  1023. // 每列求和
  1024. int[] sum = BasFunction.Sum(arrayWaist, 1);
  1025. // 遍历,找到上胶边界
  1026. int leftWaist = 0;
  1027. int rightWaist = 0;
  1028. for (int j = middle; j > 1; j--)
  1029. {
  1030. if (sum[j] > 0)
  1031. {
  1032. leftWaist = j + 1;
  1033. break;
  1034. }
  1035. }
  1036. for (int j = middle; j < imageContour.Cols; j++)
  1037. {
  1038. if (sum[j] > 0)
  1039. {
  1040. rightWaist = j - 1;
  1041. break;
  1042. }
  1043. }
  1044. // 打包输出
  1045. upperWaist = new int[2] { leftWaist, rightWaist };
  1046. }
  1047. /*
  1048. * 摘要:
  1049. * 计算下胶边界(外部)
  1050. * 参数:
  1051. * imageContour:
  1052. * 目标区域图
  1053. * lowerWaist:
  1054. * 输出的边界,先左后右
  1055. * upperBound:
  1056. * 区域上界
  1057. * lowerBound:
  1058. * 区域下界
  1059. * dataArea:
  1060. * 数据提取区域,其中dataArea[0]与dataArea[3]用作左右边界
  1061. */
  1062. public static void GetLowerWaist(Mat imageContour, out int[] lowerWaist, int upperBound, int lowerBound, int[] dataArea)
  1063. {
  1064. // 截取下胶区域
  1065. Mat waist = new Mat();
  1066. BasFunction.InterceptMat(imageContour, out waist, upperBound - 10, lowerBound - 20, dataArea[0], dataArea[3]);
  1067. // 转数组
  1068. int[,] arrayWaist = BasFunction.Mat2Array(waist);
  1069. arrayWaist = BasFunction.ConversionRange(arrayWaist);
  1070. // 每列求和
  1071. int[] sum = BasFunction.Sum(arrayWaist, 1);
  1072. // 遍历找到下胶边界
  1073. int leftWaist = 0;
  1074. int rightWaist = 0;
  1075. for (int j = dataArea[0]; j < dataArea[3]; j++)
  1076. {
  1077. if (sum[j] > 0)
  1078. {
  1079. leftWaist = j - 1;
  1080. break;
  1081. }
  1082. }
  1083. for (int j = dataArea[3]; j > dataArea[0]; j--)
  1084. {
  1085. if (sum[j] > 0)
  1086. {
  1087. rightWaist = j + 1;
  1088. break;
  1089. }
  1090. }
  1091. lowerWaist = new int[2] { leftWaist, rightWaist };
  1092. }
  1093. // 槽孔子程序
  1094. /*
  1095. * 摘要:
  1096. * 为了去除亮光,计算两部分区域的边界
  1097. * 参数:
  1098. * array:
  1099. * 输入二值化后的目标区域图片数组
  1100. * b2,b3:
  1101. * 输出的两部分边界
  1102. */
  1103. public static void SplitArea(int[,] array, out int b2, out int b3)
  1104. {
  1105. int[] sum = BasFunction.Sum(array, 1);// 每列求和
  1106. int b1 = 0;// 左边开始
  1107. b2 = 0;// 左边结束
  1108. b3 = 0;// 右边结束
  1109. int b4 = 0;// 右边开始
  1110. for (int j = 0; j < sum.Length; j++)
  1111. {
  1112. if (sum[j] > 0)
  1113. {
  1114. b1 = j;
  1115. break;
  1116. }
  1117. }
  1118. for (int j = b1; j < sum.Length; j++)
  1119. {
  1120. if (sum[j] == 0)
  1121. {
  1122. b2 = j;
  1123. break;
  1124. }
  1125. }
  1126. for (int j = sum.Length - 1; j > b2; j--)
  1127. {
  1128. if (sum[j] > 0)
  1129. {
  1130. b4 = j;
  1131. break;
  1132. }
  1133. }
  1134. for (int j = b4; j > b2; j--)
  1135. {
  1136. if (sum[j] == 0)
  1137. {
  1138. b3 = j;
  1139. break;
  1140. }
  1141. }
  1142. }
  1143. /*
  1144. * 摘要:
  1145. * 将槽孔的中间部分填充
  1146. * 参数:
  1147. * imageContour:
  1148. * 输入目标区域图片
  1149. * result:
  1150. * 输出填充好的结果
  1151. */
  1152. public static void Fill(Mat imageContour, out Mat result)
  1153. {
  1154. int[,] arrayContour = BasFunction.Mat2Array(imageContour);
  1155. arrayContour = BasFunction.ConversionRange(arrayContour);
  1156. int[] sum2 = BasFunction.Sum(arrayContour, 2);// 每行求和
  1157. int max = BasFunction.Max(sum2);
  1158. int y1 = 0;
  1159. int y2 = 0;// 填充上下界
  1160. for (int i = 0; i < imageContour.Rows; i++)
  1161. {
  1162. if (sum2[i] > max - 50)
  1163. {
  1164. y1 = i;
  1165. break;
  1166. }
  1167. }
  1168. for (int i = imageContour.Rows - 1; i > 0; i--)
  1169. {
  1170. if (sum2[i] > max - 50)
  1171. {
  1172. y2 = i;
  1173. break;
  1174. }
  1175. }
  1176. BasFunction.SetNumber(imageContour, out result, y1, y2, 0, imageContour.Cols, 255);
  1177. }
  1178. /*
  1179. * 摘要:
  1180. * 求槽孔的数据提取区域
  1181. * 参数:
  1182. * image:
  1183. * 输入的填充后的图像
  1184. * dataArea:
  1185. * 输出的提取区域,先左后右
  1186. */
  1187. public static void DataArea2(Mat image, out int[] dataArea)
  1188. {
  1189. // 转成数组
  1190. int[,] array = BasFunction.Mat2Array(image);
  1191. array = BasFunction.ConversionRange(array);
  1192. int[] sum = BasFunction.Sum(array, 1);// 每列求和
  1193. int max = BasFunction.Max(sum);// 和最大值
  1194. dataArea = new int[2];
  1195. for (int j = 0; j < sum.Length; j++)
  1196. {
  1197. if (sum[j] > max - 10)
  1198. {
  1199. dataArea[0] = j;
  1200. break;
  1201. }
  1202. }
  1203. for (int j = sum.Length - 1; j > 0; j--)
  1204. {
  1205. if (sum[j] > max - 10)
  1206. {
  1207. dataArea[1] = j;
  1208. break;
  1209. }
  1210. }
  1211. }
  1212. /*
  1213. * 摘要:
  1214. * 提取竖线(从右往左)
  1215. * 参数:
  1216. * array:
  1217. * 输入轮廓线的数组
  1218. * result:
  1219. * 输出平均横坐标
  1220. * upperBound,lowerBound:
  1221. * 提取的上下边界
  1222. */
  1223. public static void ExtractVerticalLines(int[,] array, out double result, int upperBound, int lowerBound)
  1224. {
  1225. int rows = array.GetLength(0);
  1226. int cols = array.GetLength(1);
  1227. int[] coordinate = new int[rows];// 线条内有点的横坐标
  1228. for (int i = upperBound; i < lowerBound; i++)
  1229. {
  1230. for (int j = cols - 1; j > 0; j--)
  1231. {
  1232. if (array[i, j] > 0)
  1233. {
  1234. coordinate[i] = j;
  1235. break;
  1236. }
  1237. }
  1238. }
  1239. // 计算横坐标之和
  1240. int sum = 0;
  1241. BasFunction.Sum(coordinate, out sum);
  1242. // 点的个数
  1243. int count = 0;
  1244. BasFunction.Count(coordinate, out count);
  1245. // 输出平均横坐标
  1246. result = sum / count;
  1247. }
  1248. /*
  1249. * 摘要:
  1250. * 提取非第一列竖线(从右往左)
  1251. * 参数:
  1252. * array:
  1253. * 轮廓线数组
  1254. * result:
  1255. * 输出平均横坐标结果
  1256. * upperBound,lowerBound:
  1257. * 提取区域上下界
  1258. * basic:
  1259. * 前一条竖线横坐标,作为基准
  1260. */
  1261. public static void ExtractVerticalLines(int[,] array, out double result, int upperBound, int lowerBound,double basic)
  1262. {
  1263. int rows = array.GetLength(0);
  1264. int cols = array.GetLength(1);
  1265. int[] coordinate = new int[rows];// 线条内有点的横坐标
  1266. // 寻找的左右范围
  1267. int leftBound = (int)basic - 45;
  1268. int rightBound = (int)basic - 15;
  1269. //初始化,记录横坐标的数组全为0
  1270. for (int i = 0; i < rows; i++)
  1271. {
  1272. coordinate[i] = 0;
  1273. }
  1274. //计算线条内非零点的个数
  1275. int count = 0;
  1276. BasFunction.Count(coordinate, out count);
  1277. //当点的个数少于5的时候循环
  1278. while (count < 5)
  1279. {
  1280. //每次循环,寻找范围向左移动5个像素
  1281. leftBound = leftBound - 5;
  1282. rightBound = rightBound - 5;
  1283. //遍历,寻找线条上的点,并记录横坐标
  1284. for (int i = upperBound; i<lowerBound; i++)
  1285. {
  1286. for (int j = rightBound; j >leftBound; j--)
  1287. {
  1288. if (array[i, j] > 0)
  1289. {
  1290. coordinate[i] = j;
  1291. break;
  1292. }
  1293. }
  1294. }
  1295. //重新计算线条内点的个数
  1296. BasFunction.Count(coordinate, out count);
  1297. }
  1298. //横坐标之和
  1299. int sum = 0;
  1300. BasFunction.Sum(coordinate, out sum);
  1301. //平均横坐标
  1302. result = sum / count;
  1303. }
  1304. /*
  1305. * 摘要:
  1306. * 提取竖线(从左往右)
  1307. * 参数:
  1308. * array:
  1309. * 输入轮廓线的数组
  1310. * result:
  1311. * 输出平均横坐标
  1312. * upperBound,lowerBound:
  1313. * 提取的上下边界
  1314. */
  1315. public static void ExtractVerticalLines2(int[,] array, out double result, int upperBound, int lowerBound)
  1316. {
  1317. int rows = array.GetLength(0);
  1318. int cols = array.GetLength(1);
  1319. int[] coordinate = new int[rows];// 线条内有点的横坐标
  1320. for (int i = upperBound; i < lowerBound; i++)
  1321. {
  1322. for (int j = 0; j < cols; j++)
  1323. {
  1324. if (array[i, j] > 0)
  1325. {
  1326. coordinate[i] = j;
  1327. break;
  1328. }
  1329. }
  1330. }
  1331. // 计算横坐标之和
  1332. int sum = 0;
  1333. BasFunction.Sum(coordinate, out sum);
  1334. // 点的个数
  1335. int count = 0;
  1336. BasFunction.Count(coordinate, out count);
  1337. // 输出平均横坐标
  1338. result = sum / count;
  1339. }
  1340. /*
  1341. * 摘要:
  1342. * 提取非第一列竖线(从左往右)
  1343. * 参数:
  1344. * array:
  1345. * 轮廓线数组
  1346. * result:
  1347. * 输出平均横坐标结果
  1348. * upperBound,lowerBound:
  1349. * 提取区域上下界
  1350. * basic:
  1351. * 前一条竖线横坐标,作为基准
  1352. */
  1353. public static void ExtractVerticalLines2(int[,] array, out double result, int upperBound, int lowerBound, double basic)
  1354. {
  1355. int rows = array.GetLength(0);
  1356. int cols = array.GetLength(1);
  1357. int[] coordinate = new int[rows];// 线条内有点的横坐标
  1358. // 寻找的左右范围
  1359. int leftBound = (int)basic + 15;
  1360. int rightBound = (int)basic + 45;
  1361. //初始化,记录横坐标的数组全为0
  1362. for (int i = 0; i < rows; i++)
  1363. {
  1364. coordinate[i] = 0;
  1365. }
  1366. //计算线条内非零点的个数
  1367. int count = 0;
  1368. BasFunction.Count(coordinate, out count);
  1369. //当点的个数少于5的时候循环
  1370. while (count < 5)
  1371. {
  1372. //每次循环,寻找范围向左移动5个像素
  1373. leftBound = leftBound + 5;
  1374. rightBound = rightBound + 5;
  1375. //遍历,寻找线条上的点,并记录横坐标
  1376. for (int i = upperBound; i < lowerBound; i++)
  1377. {
  1378. for (int j = leftBound; j < rightBound; j++)
  1379. {
  1380. if (array[i, j] > 0)
  1381. {
  1382. coordinate[i] = j;
  1383. break;
  1384. }
  1385. }
  1386. }
  1387. //重新计算线条内点的个数
  1388. BasFunction.Count(coordinate, out count);
  1389. }
  1390. //横坐标之和
  1391. int sum = 0;
  1392. BasFunction.Sum(coordinate, out sum);
  1393. //平均横坐标
  1394. result = sum / count;
  1395. }
  1396. /*
  1397. * 摘要:
  1398. * 计算粗糙度
  1399. * 参数:
  1400. * array:
  1401. * 轮廓线条数组
  1402. * upperBound,lowerBound:
  1403. * 粗糙度上下界
  1404. * basic:
  1405. * 基准线条
  1406. * cucaodu:
  1407. * 输出粗糙度
  1408. */
  1409. public static void Cucaodu(int[,] array, int upperBound, int lowerBound, int basic, out int cucaodu, out int[] coordinate)
  1410. {
  1411. // 截取粗糙度附近线条
  1412. int[,] seg = BasFunction.InterceptArray(array, upperBound, lowerBound, 0, array.GetLength(1));
  1413. // 得到线上点的坐标
  1414. int[,] zuobiao ;
  1415. BasFunction.Find(seg, out zuobiao);
  1416. // 遍历,计算粗糙度最大的时候的值和坐标位置
  1417. cucaodu = 0;
  1418. int t = 0;
  1419. coordinate = new int[2];
  1420. for (int i = 0; i < zuobiao.GetLength(0); i++)
  1421. {
  1422. t = Math.Abs(zuobiao[i, 1] - basic);
  1423. if (cucaodu < t && t < 50)
  1424. {
  1425. cucaodu = t;
  1426. coordinate[0] = zuobiao[i,0] + upperBound;
  1427. coordinate[1] = zuobiao[i,1];
  1428. }
  1429. }
  1430. }
  1431. /*
  1432. * 摘要:
  1433. * 将槽孔四层的亮光去掉
  1434. * 参数:
  1435. * imageContour:
  1436. * 目标区域二值化图
  1437. * imageRed:
  1438. * 红色通道图
  1439. * result:
  1440. * 输出去除亮光的目标区域图
  1441. * direction:
  1442. * 选择是左边还是右边,“left”,“right”
  1443. */
  1444. public static void RemoveLightSiceng(Mat imageContour, Mat imageRed, out Mat result, string direction)
  1445. {
  1446. // 对红色通道进行边缘检测
  1447. Mat imageRedSobel = new Mat();
  1448. SubFunction.Sobel(imageRed, out imageRedSobel);
  1449. // 竖向腐蚀三次
  1450. Mat se2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 3));
  1451. Mat imageErode = new Mat();
  1452. Cv2.Erode(imageRedSobel, imageErode, se2);
  1453. Cv2.Erode(imageErode, imageErode, se2);
  1454. Cv2.Erode(imageErode, imageErode, se2);
  1455. // 转成数组
  1456. int[,] arrayErode = BasFunction.Mat2Array(imageErode);
  1457. arrayErode = BasFunction.ConversionRange(arrayErode);
  1458. // 每列求和
  1459. int[] sum = BasFunction.Sum(arrayErode, 1);
  1460. // 最大值
  1461. int max = BasFunction.Max(sum);
  1462. // 遍历寻找边界
  1463. int border = 0;
  1464. result = imageContour;
  1465. switch (direction)
  1466. {
  1467. case "left":
  1468. for (int j = imageRed.Cols - 1; j > 0; j--)
  1469. {
  1470. if (sum[j] > max - 50)
  1471. {
  1472. border = j;
  1473. break;
  1474. }
  1475. }
  1476. for (int i = 0; i < imageRed.Rows; i++)
  1477. {
  1478. for (int j = border; j < imageRed.Cols; j++)
  1479. {
  1480. result.Set<byte>(i, j, 0);
  1481. }
  1482. }
  1483. break;
  1484. case "right":
  1485. for (int j = 0; j < imageRed.Cols; j++)
  1486. {
  1487. if (sum[j] > max - 50)
  1488. {
  1489. border = j;
  1490. break;
  1491. }
  1492. }
  1493. for (int i = 0; i < imageRed.Rows; i++)
  1494. {
  1495. for (int j = 0; j < border; j++)
  1496. {
  1497. result.Set<byte>(i, j, 0);
  1498. }
  1499. }
  1500. break;
  1501. default:
  1502. break;
  1503. }
  1504. }
  1505. // 蚀刻因子
  1506. /*
  1507. * 摘要:
  1508. * 提取蚀刻因子的检测区域
  1509. * 参数:
  1510. * imageContour:
  1511. * 输入目标区域二值化图像
  1512. * dataArea:
  1513. * 输出提取区域数组,分别是上、下、左、右边界
  1514. */
  1515. public static void SKYZDataArea(Mat imageContour, out int[] dataArea)
  1516. {
  1517. // 转数组
  1518. int[,] arrayContour = BasFunction.Mat2Array(imageContour);
  1519. arrayContour = BasFunction.ConversionRange(arrayContour);
  1520. // 上下边界
  1521. int[] sum = BasFunction.Sum(arrayContour, 2);
  1522. dataArea = new int[4];
  1523. for (int i = 0; i < imageContour.Rows; i++)
  1524. {
  1525. if (sum[i] > 0)
  1526. {
  1527. dataArea[0] = i;// 当某行不全为0时为上界
  1528. break;
  1529. }
  1530. }
  1531. for (int i = dataArea[0]; i < imageContour.Rows; i++)
  1532. {
  1533. if (sum[i] == 0)
  1534. {
  1535. dataArea[1] = i;// 当某行全为0时为下界
  1536. break;
  1537. }
  1538. }
  1539. // 左右边界
  1540. int[,] arrayContour2 = BasFunction.InterceptArray(arrayContour, dataArea[0], dataArea[1], 0, imageContour.Cols);
  1541. int[] sum2 = BasFunction.Sum(arrayContour2, 1);
  1542. int middle = imageContour.Cols / 2;
  1543. // 如果中间列大于0,则向左右找到全为0的列,如果中间列为0,则向右找到非零列和全零列
  1544. if (sum2[middle] == 0)
  1545. {
  1546. for (int j = middle; j < imageContour.Cols; j++)
  1547. {
  1548. if (sum2[j] > 0)
  1549. {
  1550. dataArea[2] = j;
  1551. break;
  1552. }
  1553. }
  1554. for (int j = dataArea[2]; j < imageContour.Cols; j++)
  1555. {
  1556. if (sum2[j] == 0)
  1557. {
  1558. dataArea[3] = j;
  1559. break;
  1560. }
  1561. }
  1562. }
  1563. else
  1564. {
  1565. for (int j = middle; j > 0; j--)
  1566. {
  1567. if (sum2[j] == 0)
  1568. {
  1569. dataArea[2] = j;
  1570. break;
  1571. }
  1572. }
  1573. for (int j = middle; j < imageContour.Cols; j++)
  1574. {
  1575. if (sum2[j] == 0)
  1576. {
  1577. dataArea[3] = j;
  1578. break;
  1579. }
  1580. }
  1581. }
  1582. }
  1583. /*
  1584. * 摘要:
  1585. * 提取最上面的线横纵坐标
  1586. * 参数:
  1587. * arrayData:
  1588. * 输入截取后的数组
  1589. * dataData:
  1590. * 检测区域
  1591. * upperX:
  1592. * 横坐标(先左后右)
  1593. */
  1594. public static void UpperLine(int[,] arrayData, int[] dataArea,out int[] upperX)
  1595. {
  1596. // 计算上横线位置
  1597. int[] size = new int[2] { dataArea[1] - dataArea[0], dataArea[3] - dataArea[2] }; // 先行数,后列数
  1598. int[] sum = BasFunction.Sum(arrayData, 2);
  1599. int upperY = 0;// 上横线纵坐标
  1600. for (int i = 0; i < size[0]; i++)
  1601. {
  1602. if (sum[i] > size[1] * 4 / 5)
  1603. {
  1604. upperY = i;
  1605. break;
  1606. }
  1607. }
  1608. upperX = new int[2];// 上横线横坐标边界
  1609. for (int j = 0; j < size[1]; j++)
  1610. {
  1611. if (arrayData[upperY, j] > 0)
  1612. {
  1613. upperX[0] = j;//左边界
  1614. break;
  1615. }
  1616. }
  1617. for (int j = upperX[0]; j < size[1]; j++)
  1618. {
  1619. if (arrayData[upperY, j] == 0)
  1620. {
  1621. upperX[1] = j;//右边界
  1622. break;
  1623. }
  1624. }
  1625. }
  1626. /*
  1627. * 摘要:
  1628. * 计算上下顶点的坐标
  1629. * 参数:
  1630. * arrayData:
  1631. * 输入截取后的数组
  1632. * basic:
  1633. * 寻找宽度的某一条线(基准)
  1634. * upperY:
  1635. * 上顶点的纵坐标
  1636. * lowerY:
  1637. * 下顶点的纵坐标
  1638. */
  1639. public static void LowerLine(int[,] arrayData, int basic, out int upperY,out int lowerY)
  1640. {
  1641. upperY = 0;
  1642. lowerY = 0;
  1643. int[] sum = BasFunction.Sum(arrayData,2);
  1644. for (int i = 0; i < sum.Length; i++)
  1645. {
  1646. if (sum[i] > 0)
  1647. {
  1648. upperY = i;
  1649. break;
  1650. }
  1651. }
  1652. for (int i = upperY; i < sum.Length; i++)
  1653. {
  1654. if (arrayData[i, basic] > 0)
  1655. {
  1656. upperY = i;
  1657. break;
  1658. }
  1659. }
  1660. for (int i = sum.Length - 1; i > 0; i--)
  1661. {
  1662. if (sum[i] > 0)
  1663. {
  1664. lowerY = i;
  1665. break;
  1666. }
  1667. }
  1668. for (int i = lowerY - 1; i > 0; i--)
  1669. {
  1670. if (arrayData[i, basic] > 0)
  1671. {
  1672. lowerY = i;
  1673. break;
  1674. }
  1675. }
  1676. }
  1677. }
  1678. }