Marthee commited on
Commit
a5d8550
·
1 Parent(s): c92b933

Update pilecaps_adr.py

Browse files
Files changed (1) hide show
  1. pilecaps_adr.py +44 -52
pilecaps_adr.py CHANGED
@@ -50,7 +50,7 @@ def detectCircles(imgOriginal ):
50
  # Apply Hough transform on the blurred image.
51
  # min distance between circles, Upper threshold for the internal Canny edge detector.
52
  detected_circles = cv2.HoughCircles( er1, cv2.HOUGH_GRADIENT, 1, 50, param1= 550,
53
- param2 =21, minRadius = 20, maxRadius = 40) #18 param2
54
 
55
  # Draw circles that are detected.
56
  if detected_circles is not None:
@@ -60,9 +60,6 @@ def detectCircles(imgOriginal ):
60
  #DRAW CIRCLES
61
  for (x, y, r) in detected_circles:
62
  cv2.circle(im, (x, y), r, (255, 255, 255), 5)
63
- im=cv2.medianBlur(im,1)
64
- print('circles')
65
- # cv2_imshow(im)
66
  return im
67
 
68
  def detectSmallCircles(img ):
@@ -74,7 +71,7 @@ def detectSmallCircles(img ):
74
  # Apply Hough transform on the blurred image.
75
  # min distance between circles, Upper threshold for the internal Canny edge detector.
76
  detected_circles = cv2.HoughCircles( imgGry1, cv2.HOUGH_GRADIENT, 1, 60, param1 =550,
77
- param2 =13, minRadius = 1, maxRadius = 10) #18 param2
78
 
79
  # Draw circles that are detected.
80
  if detected_circles is not None:
@@ -122,7 +119,7 @@ def removeDashedLines(img4, imgBW ,max,min):
122
 
123
  for i in range(len(imgLines)):
124
  for x1,y1,x2,y2 in imgLines[i]:
125
- cv2.line(imgBW,(x1,y1),(x2,y2),(0,255,0),2)
126
 
127
  im_copy=imgBW.copy()
128
  green=im_copy[:,:,1]
@@ -138,8 +135,8 @@ def removeSmallDashes(imgOriginal,green):
138
  img1=cv2.dilate(smalldashes, kernel3, iterations=2)
139
  img2=cv2.erode(img1, kernel3, iterations=2)
140
 
141
- smalldashes=cv2.medianBlur(img2,5)
142
- smalldashes=cv2.medianBlur(smalldashes,7)
143
  # cv2_imshow(smalldashes)
144
  smalldashesOut=green.copy()
145
  smalldashesOut=cv2.cvtColor(smalldashesOut,cv2.COLOR_GRAY2BGR)
@@ -148,7 +145,7 @@ def removeSmallDashes(imgOriginal,green):
148
  imgCopy=imgOriginal.copy()
149
  for i in range(len(imgLines)):
150
  for x1,y1,x2,y2 in imgLines[i]:
151
- cv2.line(smalldashesOut,(x1,y1),(x2,y2),(0,255,0),2)
152
 
153
 
154
  smalldashesOut=smalldashesOut[:,:,1]
@@ -167,13 +164,13 @@ def euclidian_distance(point1, point2):
167
  def removeDashedLinesSmall(img4, imgBW ,max,min):
168
 
169
  imgBW=cv2.cvtColor(imgBW,cv2.COLOR_GRAY2BGR)
170
- imgLines= cv2.HoughLinesP(img4,1,np.pi/100,20,minLineLength=(max-min)//2.2,maxLineGap = 70) #2.1
171
 
172
  for i in range(len(imgLines)):
173
  for x1,y1,x2,y2 in imgLines[i]:
174
  dist=euclidian_distance((x1,y1), (x2,y2))
175
  # if dist > 1300 and dist <1450:
176
- if dist >= (max-min)//2.1 and dist < (max-min)//1.9: #1.4
177
  cv2.line(imgBW,(x1,y1),(x2,y2),(0,255,0),3)
178
 
179
  im_copy=imgBW.copy()
@@ -185,12 +182,12 @@ def ConnectBeamLines(smalldashesOut):
185
  green1=cv2.bitwise_not(smalldashesOut)
186
  green2=smalldashesOut.copy()
187
  green2=cv2.cvtColor(green2,cv2.COLOR_GRAY2BGR)
188
- imgLines= cv2.HoughLinesP(green1,0.05,np.pi/250,10,minLineLength=25,maxLineGap = 20) #was w-h , gap=150 #50
189
- for i in range(len(imgLines)):
190
- for x1,y1,x2,y2 in imgLines[i]:
191
- cv2.line(green2,(x1,y1),(x2,y2),(0,0,0),2)
192
 
193
- imgLines= cv2.HoughLinesP(green1,1,np.pi/250,37,minLineLength=25,maxLineGap = 22) #try 180
194
 
195
 
196
  for i in range(len(imgLines)):
@@ -217,17 +214,14 @@ def ChangeBrightness(img,k):
217
  return imgdarker
218
 
219
  def preprocessold(img,number):
220
-
221
  blurG = ChangeBrightness(img,3)
222
  imgGry = cv2.cvtColor(blurG, cv2.COLOR_BGR2GRAY)
223
- kernel=np.ones((3,3),np.uint8)
224
- er1=cv2.dilate(imgGry,kernel, iterations=2) #thinning
225
- kernel=np.ones((2,2),np.uint8)
226
- er1=cv2.erode(er1,kernel, iterations=3) #thicken
227
- # er1=cv2.dilate(er1,kernel, iterations=1) #thicken
228
- # er1=cv2.dilate(er1,kernel, iterations=1) #thicken
229
  ret3, thresh = cv2.threshold(er1, 254, 255, cv2.THRESH_BINARY_INV )
230
-
231
  return thresh
232
 
233
 
@@ -250,8 +244,8 @@ def preprocess(imgOriginal,number,green2,flag,layeredflag,BlackmaskDetected1=0):
250
  x, y , width, height = cv2.boundingRect(cnt)
251
  if flag.startswith('perimeter'):
252
  kernel=np.ones((2,2),np.uint8)
253
- cv2.drawContours(Blackmask,[cnt],0,(255,255,255), 5)
254
- Blackmask=cv2.dilate(Blackmask,kernel, iterations=2)
255
  elif flag.startswith('area'):
256
  kernel=np.ones((2,2),np.uint8)
257
  cv2.drawContours(Blackmask,[cnt],0,(255,255,255), 4)
@@ -543,9 +537,9 @@ def getAreasPerimeter(green2,img,number,ratioarea,ratioperim,flag,finalColorArra
543
  for contour in range(len(contourss)):
544
  # imgArea1=img.copy()
545
  area1 = cv2.contourArea(contourss[contour][0])
546
- if (area1 >2000 ):
547
  perimeter = cv2.arcLength(contourss[contour][0],True)
548
- approx = cv2.approxPolyDP(contourss[contour][0], 0.01* perimeter, True)
549
  perimeter1 = cv2.arcLength(approx, True)
550
  # approx = cv2.approxPolyDP(contourss[contour][0], 0.01* perimeter, True)
551
  # area1 = cv2.contourArea(approx)
@@ -556,13 +550,13 @@ def getAreasPerimeter(green2,img,number,ratioarea,ratioperim,flag,finalColorArra
556
  if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
557
  width=widthR
558
  height=heightR
559
- if (area1 > 2000 ): #check perimeter kman fl condition -- 2800
560
  # print('areass')
561
  if ratioarea!=0 and ratioperim!=0:
562
  # print('numss')
563
  # if flag=='area':
564
  # addedMargin=area1+perimeter1*2
565
- areaa=round(area1* ratioarea , 2) # true value of area of any shape/ area px value of same shape
566
  appended.append([areaa,width,height])
567
 
568
  #else:
@@ -677,8 +671,8 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
677
  if eachcolor==firstcolor: # 3shan a3rf el array of colors et3adet kam mara - to support embedded levels
678
  counter+=1
679
 
680
- contourss,rgbcolor,invertedmask=findContoursFullImage(green2,maskDone,number,finalColorArray,ratioarea,ratioperim,'perimeter',eachcolor)
681
- contourssA,rgbcolor,invertedmask=findContoursFullImage(green2,maskDone,number,finalColorArray,ratioarea,ratioperim,'area',eachcolor)
682
  SimilarAreaDictionary, colorsUsed , areas_Perimeters= FillDictionary(green2,SimilarAreaDictionary,maskDone,number,ratioarea,ratioperim,flag,finalColorArray,rgbcolor,eachcolor)
683
 
684
  for contour in range(len(contourss)):
@@ -688,7 +682,7 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
688
  area1 = cv2.contourArea(contourssA[contour][0])
689
 
690
  # perimeter1 = cv2.arcLength(contourss[contour][0], True)
691
- if (area1 > 2000 ): #check perimeter kman fl condition -- 2800
692
  rect = cv2.minAreaRect(contourss[contour][0])
693
 
694
  angleR,widthR ,heightR= StraightenImage(contourss[contour][0],imgArea1)
@@ -703,10 +697,10 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
703
  perimeter = cv2.arcLength(contourss[contour][0], True)
704
  # approx = cv2.approxPolyDP(contourssA[contour][0], 0.0002* perimeter, True)
705
  # area1 = cv2.contourArea(approx)
706
- approx = cv2.approxPolyDP(contourss[contour][0], 0.03 * perimeter, True) #0.0009
707
  perimeter1 = cv2.arcLength(approx, True)
708
 
709
- for point in contourss[contour][0]:
710
  x1, y1 = point[0]
711
  p1 = fitz.Point(x1*ratio,y1*ratio)
712
  p1=p1*page.derotation_matrix
@@ -719,17 +713,15 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
719
  lengthShape = width
720
  else:
721
  lengthShape = height
722
- widthMin= width-10
723
- widthMax= width+10
724
- heightMin=height-10
725
- heightMax= height+10
726
  if ratioarea !=0 and ratioperim!=0:
727
  widthh=round(width*ratioperim,3)
728
  heightt=round(height*ratioperim,3)
729
  # if flag=='area':
730
  areaa=round(area1* ratioarea, 3) # true value of area of any shape/ area px value of same shape
731
-
732
- # elif flag=='perimeter':
733
  perimeterr=round(perimeter1* ratioperim,3)
734
  lengthShape=round(lengthShape* ratioperim,3)
735
  else:
@@ -737,8 +729,8 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
737
  perimeterr=perimeter1
738
 
739
  # if flag=='area':
740
- areaPerimeterMin= round(areaa,1) - 0.2
741
- areaPerimeterMax= round(areaa,1) + 0.2
742
  # areaPerimeterMin= round(perimeterr,1) - 0.3
743
  # areaPerimeterMax= round(perimeterr,1) + 0.3
744
  masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) &(SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
@@ -798,7 +790,7 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
798
  for contour in range(len(contourss)):
799
  # imgArea1=img.copy()
800
  area1 = cv2.contourArea(contourssA[contour][0])
801
- if (area1 >2000 ):
802
  perimeter = cv2.arcLength(contourss[contour][0],True)
803
 
804
  shape=[]
@@ -808,9 +800,9 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
808
  # approx = cv2.approxPolyDP(contourss[contour][0], 0.005 * perimeter, True)
809
  # approxa = cv2.approxPolyDP(contourssA[contour][0], 0.0002* perimeter, True)
810
  # area1 = cv2.contourArea(approxa)
811
- approx = cv2.approxPolyDP(contourss[contour][0], 0.03 * perimeter, True) #0.0009
812
  perimeter1 = cv2.arcLength(approx, True)
813
- for point in contourss[contour][0]:
814
  x1, y1 = point[0]
815
  p1 = fitz.Point(x1*ratio,y1*ratio)
816
  p1=p1*page.derotation_matrix
@@ -823,10 +815,10 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
823
  if height>width:
824
  lengthShape = height
825
 
826
- widthMin= width-10
827
- widthMax= width+10
828
- heightMin=height-10
829
- heightMax= height+10
830
 
831
  if ratioarea !=0 and ratioperim!=0:
832
  areaa= round(area1* ratioarea,3) # true value of area of any shape/ area px value of same shape
@@ -834,8 +826,8 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
834
  lengthShape=round(lengthShape* ratioperim,3)
835
  else:
836
  perimeterr=perimeter1
837
- areaPerimeterMin= round(areaa,1) - 0.2
838
- areaPerimeterMax= round(areaa,1) + 0.2
839
  masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) & (SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
840
  passed=0
841
  for i, row in masked.iterrows():
 
50
  # Apply Hough transform on the blurred image.
51
  # min distance between circles, Upper threshold for the internal Canny edge detector.
52
  detected_circles = cv2.HoughCircles( er1, cv2.HOUGH_GRADIENT, 1, 50, param1= 550,
53
+ param2 =21, minRadius = 20, maxRadius = 50) #18 param2
54
 
55
  # Draw circles that are detected.
56
  if detected_circles is not None:
 
60
  #DRAW CIRCLES
61
  for (x, y, r) in detected_circles:
62
  cv2.circle(im, (x, y), r, (255, 255, 255), 5)
 
 
 
63
  return im
64
 
65
  def detectSmallCircles(img ):
 
71
  # Apply Hough transform on the blurred image.
72
  # min distance between circles, Upper threshold for the internal Canny edge detector.
73
  detected_circles = cv2.HoughCircles( imgGry1, cv2.HOUGH_GRADIENT, 1, 60, param1 =550,
74
+ param2 =13, minRadius = 1, maxRadius = 15) #18 param2
75
 
76
  # Draw circles that are detected.
77
  if detected_circles is not None:
 
119
 
120
  for i in range(len(imgLines)):
121
  for x1,y1,x2,y2 in imgLines[i]:
122
+ cv2.line(imgBW,(x1,y1),(x2,y2),(0,255,0),3)
123
 
124
  im_copy=imgBW.copy()
125
  green=im_copy[:,:,1]
 
135
  img1=cv2.dilate(smalldashes, kernel3, iterations=2)
136
  img2=cv2.erode(img1, kernel3, iterations=2)
137
 
138
+ smalldashes=cv2.medianBlur(img2,7)
139
+ smalldashes=cv2.medianBlur(smalldashes,9)
140
  # cv2_imshow(smalldashes)
141
  smalldashesOut=green.copy()
142
  smalldashesOut=cv2.cvtColor(smalldashesOut,cv2.COLOR_GRAY2BGR)
 
145
  imgCopy=imgOriginal.copy()
146
  for i in range(len(imgLines)):
147
  for x1,y1,x2,y2 in imgLines[i]:
148
+ cv2.line(smalldashesOut,(x1,y1),(x2,y2),(0,255,0),3)
149
 
150
 
151
  smalldashesOut=smalldashesOut[:,:,1]
 
164
  def removeDashedLinesSmall(img4, imgBW ,max,min):
165
 
166
  imgBW=cv2.cvtColor(imgBW,cv2.COLOR_GRAY2BGR)
167
+ imgLines= cv2.HoughLinesP(img4,1,np.pi/100,20,minLineLength=(max-min)//3.5,maxLineGap = 70) #2.1
168
 
169
  for i in range(len(imgLines)):
170
  for x1,y1,x2,y2 in imgLines[i]:
171
  dist=euclidian_distance((x1,y1), (x2,y2))
172
  # if dist > 1300 and dist <1450:
173
+ if dist >= (max-min)//2.3 and dist < (max-min)//1.9: #1.4
174
  cv2.line(imgBW,(x1,y1),(x2,y2),(0,255,0),3)
175
 
176
  im_copy=imgBW.copy()
 
182
  green1=cv2.bitwise_not(smalldashesOut)
183
  green2=smalldashesOut.copy()
184
  green2=cv2.cvtColor(green2,cv2.COLOR_GRAY2BGR)
185
+ # imgLines= cv2.HoughLinesP(green1,0.05,np.pi/250,10,minLineLength=25,maxLineGap = 20) #was w-h , gap=150 #50
186
+ # for i in range(len(imgLines)):
187
+ # for x1,y1,x2,y2 in imgLines[i]:
188
+ # cv2.line(green2,(x1,y1),(x2,y2),(0,0,0),2)
189
 
190
+ imgLines= cv2.HoughLinesP(green1,0.3,np.pi/180,20,minLineLength=25,maxLineGap = 25) #try 180
191
 
192
 
193
  for i in range(len(imgLines)):
 
214
  return imgdarker
215
 
216
  def preprocessold(img,number):
 
217
  blurG = ChangeBrightness(img,3)
218
  imgGry = cv2.cvtColor(blurG, cv2.COLOR_BGR2GRAY)
219
+ kernel1=np.ones((3,3),np.uint8)
220
+ er1=cv2.dilate(imgGry,kernel1, iterations=1) #thinning
221
+ kernel2=np.ones((2,2),np.uint8)
222
+ er1=cv2.dilate(er1,kernel2, iterations=1) #thicken
 
 
223
  ret3, thresh = cv2.threshold(er1, 254, 255, cv2.THRESH_BINARY_INV )
224
+ thresh=cv2.medianBlur(thresh,5)
225
  return thresh
226
 
227
 
 
244
  x, y , width, height = cv2.boundingRect(cnt)
245
  if flag.startswith('perimeter'):
246
  kernel=np.ones((2,2),np.uint8)
247
+ cv2.drawContours(Blackmask,[cnt],0,(255,255,255), 7)
248
+ Blackmask=cv2.dilate(Blackmask,kernel, iterations=1)
249
  elif flag.startswith('area'):
250
  kernel=np.ones((2,2),np.uint8)
251
  cv2.drawContours(Blackmask,[cnt],0,(255,255,255), 4)
 
537
  for contour in range(len(contourss)):
538
  # imgArea1=img.copy()
539
  area1 = cv2.contourArea(contourss[contour][0])
540
+ if (area1 >3500 ):
541
  perimeter = cv2.arcLength(contourss[contour][0],True)
542
+ approx = cv2.approxPolyDP(contourss[contour][0], 0.007* perimeter, True)
543
  perimeter1 = cv2.arcLength(approx, True)
544
  # approx = cv2.approxPolyDP(contourss[contour][0], 0.01* perimeter, True)
545
  # area1 = cv2.contourArea(approx)
 
550
  if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
551
  width=widthR
552
  height=heightR
553
+ if (area1 > 3500 ): #check perimeter kman fl condition -- 2800
554
  # print('areass')
555
  if ratioarea!=0 and ratioperim!=0:
556
  # print('numss')
557
  # if flag=='area':
558
  # addedMargin=area1+perimeter1*2
559
+ areaa=area1* ratioarea# true value of area of any shape/ area px value of same shape
560
  appended.append([areaa,width,height])
561
 
562
  #else:
 
671
  if eachcolor==firstcolor: # 3shan a3rf el array of colors et3adet kam mara - to support embedded levels
672
  counter+=1
673
 
674
+ contourss,rgbcolor,invertedmask=findContoursFullImage(green2,maskDone,number,finalColorArray,ratioarea,ratioperim,'area',eachcolor)
675
+ contourssA,rgbcolor,invertedmask=findContoursFullImage(green2,maskDone,number,finalColorArray,ratioarea,ratioperim,'perimeter',eachcolor)
676
  SimilarAreaDictionary, colorsUsed , areas_Perimeters= FillDictionary(green2,SimilarAreaDictionary,maskDone,number,ratioarea,ratioperim,flag,finalColorArray,rgbcolor,eachcolor)
677
 
678
  for contour in range(len(contourss)):
 
682
  area1 = cv2.contourArea(contourssA[contour][0])
683
 
684
  # perimeter1 = cv2.arcLength(contourss[contour][0], True)
685
+ if (area1 > 3500 ): #check perimeter kman fl condition -- 2800
686
  rect = cv2.minAreaRect(contourss[contour][0])
687
 
688
  angleR,widthR ,heightR= StraightenImage(contourss[contour][0],imgArea1)
 
697
  perimeter = cv2.arcLength(contourss[contour][0], True)
698
  # approx = cv2.approxPolyDP(contourssA[contour][0], 0.0002* perimeter, True)
699
  # area1 = cv2.contourArea(approx)
700
+ approx = cv2.approxPolyDP(contourss[contour][0], 0.007 * perimeter, True) #0.0009
701
  perimeter1 = cv2.arcLength(approx, True)
702
 
703
+ for point in approx:
704
  x1, y1 = point[0]
705
  p1 = fitz.Point(x1*ratio,y1*ratio)
706
  p1=p1*page.derotation_matrix
 
713
  lengthShape = width
714
  else:
715
  lengthShape = height
716
+ widthMin= width-15
717
+ widthMax= width+15
718
+ heightMin=height-15
719
+ heightMax= height+15
720
  if ratioarea !=0 and ratioperim!=0:
721
  widthh=round(width*ratioperim,3)
722
  heightt=round(height*ratioperim,3)
723
  # if flag=='area':
724
  areaa=round(area1* ratioarea, 3) # true value of area of any shape/ area px value of same shape
 
 
725
  perimeterr=round(perimeter1* ratioperim,3)
726
  lengthShape=round(lengthShape* ratioperim,3)
727
  else:
 
729
  perimeterr=perimeter1
730
 
731
  # if flag=='area':
732
+ areaPerimeterMin= round(areaa,1) - 0.25
733
+ areaPerimeterMax= round(areaa,1) + 0.25
734
  # areaPerimeterMin= round(perimeterr,1) - 0.3
735
  # areaPerimeterMax= round(perimeterr,1) + 0.3
736
  masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) &(SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
 
790
  for contour in range(len(contourss)):
791
  # imgArea1=img.copy()
792
  area1 = cv2.contourArea(contourssA[contour][0])
793
+ if (area1 >3500 ):
794
  perimeter = cv2.arcLength(contourss[contour][0],True)
795
 
796
  shape=[]
 
800
  # approx = cv2.approxPolyDP(contourss[contour][0], 0.005 * perimeter, True)
801
  # approxa = cv2.approxPolyDP(contourssA[contour][0], 0.0002* perimeter, True)
802
  # area1 = cv2.contourArea(approxa)
803
+ approx = cv2.approxPolyDP(contourss[contour][0], 0.007 * perimeter, True) #0.0009
804
  perimeter1 = cv2.arcLength(approx, True)
805
+ for point in approx:
806
  x1, y1 = point[0]
807
  p1 = fitz.Point(x1*ratio,y1*ratio)
808
  p1=p1*page.derotation_matrix
 
815
  if height>width:
816
  lengthShape = height
817
 
818
+ widthMin= width-15
819
+ widthMax= width+15
820
+ heightMin=height-15
821
+ heightMax= height+15
822
 
823
  if ratioarea !=0 and ratioperim!=0:
824
  areaa= round(area1* ratioarea,3) # true value of area of any shape/ area px value of same shape
 
826
  lengthShape=round(lengthShape* ratioperim,3)
827
  else:
828
  perimeterr=perimeter1
829
+ areaPerimeterMin= round(areaa,1) - 0.25
830
+ areaPerimeterMax= round(areaa,1) + 0.25
831
  masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) & (SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
832
  passed=0
833
  for i, row in masked.iterrows():