Repository navigation
Expand file tree
/
Copy pathfunctions.py
More file actions
1205 lines (908 loc) · 47.5 KB
/
Copy pathfunctions.py
File metadata and controls
1205 lines (908 loc) · 47.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import cv2
import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
import glob
from scipy.signal import find_peaks, peak_widths
import os
import ast
from pathlib import Path
# mln = number of line in middle in between 2 markers
mln = 100 #50 #75,100,125
def identify_4_points(image, gray, output_image_path):
# Apply Canny edge detection
edges = cv2.Canny(gray, threshold1=60, threshold2=100)
# Find contours in the edges image
all_contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Create a copy of the image for visualization
image_with_contours = image.copy()
# Filter and draw the selected contours on the copy
selected_contours = []
prev_contour = None # To keep track of the previous contour
coordinates = [] # List to store (x, y, w, h) tuples
for contour in all_contours:
# Calculate the bounding rectangle of the contour
x, y, w, h = cv2.boundingRect(contour)
# Check if the bounding rectangle touches the left or right edge and has a width greater than 50 pixels
if (x <= 5 or x + w >= image.shape[1] - 5) and w > 50: ###10 10 20
# If previous contour exists and has the same X, then check Y difference
if prev_contour and x == prev_contour[0] and abs(y - prev_contour[1]) > 20: ####5
print("x:", x, "y:", y, "w:", w, "h:", h)
# If the previous contour doesn't exist or X is different, print anyway
elif not prev_contour or x != prev_contour[0]:
print("x:", x, "y:", y, "w:", w, "h:", h)
# Store the values of x, y, w, and h in the coordinates list
coordinates.append((x, y, w, h))
selected_contours.append(contour)
cv2.drawContours(image_with_contours, [contour], -1, (0, 255, 0), 1)
prev_contour = (x, y, w, h) # Update the previous contour
# # Display the image with selected contours using pyplot
# plt.figure(figsize=(12, 20)) # Optional: Set the figure size
# plt.imshow(cv2.cvtColor(image_with_contours, cv2.COLOR_BGR2RGB))
# plt.title('Image with Contours')
# plt.axis('off') # Optional: Turn off the axis labels
# plt.show()
# Save the modified image with contours
cv2.imwrite(output_image_path, image_with_contours)
# Return the coordinates list
return coordinates
def connect_middle_100(image, output_image_path, coordinates):
left = []
right = []
image1 = image.copy()
for x, y, w, h in coordinates:
if x <= 10:
left.append((x, y))
else:
right.append((x + w, y))
left_top = [point for point in left if point[1] < sum(y for _, y in left) / len(left)]
left_bottom = [point for point in left if point[1] >= sum(y for _, y in left) / len(left)]
right_top = [point for point in right if point[1] < sum(y for _, y in right) / len(right)]
right_bottom = [point for point in right if point[1] >= sum(y for _, y in right) / len(right)]
print("left_top =", left_top)
print("left_bottom = ", left_bottom)
print("right_top = ", right_top)
print("right_bottom = ", right_bottom)
# Calculate the increments for dividing the sides into mln pieces
left_y_increment = (left_bottom[0][1] - left_top[0][1]) / mln
right_y_increment = (right_bottom[0][1] - right_top[0][1]) / mln
print("left_side_increament = ", left_y_increment)
print("right_side_increament = ", right_y_increment)
# Create an array to store all the points of the connecting lines
connecting_points = []
# Generate the points for connecting lines
for i in range(mln+1):
left_point = (int(left_top[0][0]), int(left_top[0][1] + i * left_y_increment))
right_point = (int(right_top[0][0]), int(right_top[0][1] + i * right_y_increment))
connecting_points.append((left_point, right_point))
# Convert the points to a NumPy array
connecting_points = np.array(connecting_points)
print("total number of line in middle =", len(connecting_points))
# Draw the connecting lines on the image
for left_point, right_point in connecting_points:
cv2.line(image1, left_point, right_point, (0, 0, 255), 1) # Red color (0, 0, 255)
# Save the modified image
# cv2.imwrite(output_image_path, image1)
# # Display the modified image using pyplot
# plt.figure(figsize=(48, 80)) # Optional: Set the figure size
# plt.imshow(cv2.cvtColor(image1, cv2.COLOR_BGR2RGB))
# plt.title('Image with Contours')
# plt.axis('off') # Optional: Turn off the axis labels
# plt.show()
return left_bottom, right_bottom, left_y_increment, right_y_increment,left_top, right_top
def connect_bottom(image, output_image_path, left_bottom, right_bottom, left_y_increment, right_y_increment):
# Get the image dimensions
image_height, image_width, _ = image.shape
image2 = image.copy()
print(image_height)
# Create an array to store all the points of the connecting lines
connecting_points = []
# Determine the maximum number of iterations based on the available space
max_iterations = int(min((image_height - left_bottom[0][1]) / left_y_increment, (image_height - right_bottom[0][1]) / right_y_increment))
print("bottom part =", max_iterations)
# Generate the points for connecting lines
for i in range(max_iterations + 1):
left_point = (int(left_bottom[0][0]), int(left_bottom[0][1] + i * left_y_increment))
right_point = (int(right_bottom[0][0]), int(right_bottom[0][1] + i * right_y_increment))
connecting_points.append((left_point, right_point))
# Convert the points to a NumPy array
connecting_points = np.array(connecting_points)
# Draw the connecting lines on the image
for left_point, right_point in connecting_points:
cv2.line(image2, left_point, right_point, (0, 0, 255), 1) # Red color (0, 0, 255)
# Save the modified image
cv2.imwrite(output_image_path, image2)
# # Display the modified image using pyplot
# plt.figure(figsize=(48, 80)) # Optional: Set the figure size
# plt.imshow(cv2.cvtColor(image2, cv2.COLOR_BGR2RGB))
# plt.title('Image with Contours')
# plt.axis('off') # Optional: Turn off the axis labels
# plt.show()
def connect_top(image, output_image_path, left_top, right_top, left_y_increment, right_y_increment):
# Get the image dimensions
image_height, image_width, _ = image.shape
image3 = image.copy()
# Create an array to store all the points of the connecting lines
connecting_points = []
# Determine the maximum number of iterations based on the available space
max_iterations = int(min(left_top[0][1]/left_y_increment , right_top[0][1]/right_y_increment))
print("top part = ",max_iterations)
# Generate the points for connecting lines
for i in range(max_iterations+1):
left_point = (int(left_top[0][0]), int(left_top[0][1] - i * left_y_increment))
right_point = (int(right_top[0][0]), int(right_top[0][1] - i * right_y_increment))
connecting_points.append((left_point, right_point))
# Convert the points to a NumPy array
connecting_points = np.array(connecting_points)
# Draw the connecting lines
for left_point, right_point in connecting_points:
cv2.line(image3, left_point, right_point, (0, 0, 255), 1) # Red color (0, 0, 255)
# Save the modified image
output_path = 'top_with_lines.jpg'
cv2.imwrite(output_path, image3)
# # Display the image with selected and first/last contours using pyplot
# plt.figure(figsize=(48, 80)) # Optional: Set the figure size
# plt.imshow(cv2.cvtColor(image3, cv2.COLOR_BGR2RGB))
# plt.title('Image with Contours')
# plt.axis('off') # Optional: Turn off the axis labels
# plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
def connect_lines(image, output_image_path,coordinates):
image4 = image.copy()
## to store all connecting points
# Initialize an empty NumPy array to store all connecting points
all_connecting_points = np.empty((0, 2, 2), dtype=int)
top_lines = []
bottom_lines = []
# Get the image dimensions
image_height, image_width, _ = image.shape
# Left and right points categorization
left = []
right = []
for x, y, w, h in coordinates:
if x <= 10:
left.append((x, y))
else:
right.append((x + w, y))
left_top = [point for point in left if point[1] < sum(y for _, y in left) / len(left)]
left_bottom = [point for point in left if point[1] >= sum(y for _, y in left) / len(left)]
right_top = [point for point in right if point[1] < sum(y for _, y in right) / len(right)]
right_bottom = [point for point in right if point[1] >= sum(y for _, y in right) / len(right)]
# Calculate the increments for dividing the sides into mln pieces
left_y_increment = (left_bottom[0][1] - left_top[0][1]) / mln
right_y_increment = (right_bottom[0][1] - right_top[0][1]) / mln
#print_values
print("left_top =", left_top)
print("left_bottom = ", left_bottom)
print("right_top = ", right_top)
print("right_bottom = ", right_bottom)
print("left_side_increament = ", left_y_increment)
print("right_side_increament = ", right_y_increment)
### for center lines
# Create an array to store all the points of the connecting lines
connecting_points = []
# Generate the points for connecting lines
for i in range(mln+1):
left_point = (int(left_top[0][0]), int(left_top[0][1] + i * left_y_increment))
right_point = (int(right_top[0][0]), int(right_top[0][1] + i * right_y_increment))
connecting_points.append((left_point, right_point))
all_connecting_points = np.vstack((all_connecting_points, connecting_points))
### for the bottom lines
# Create an array to store all the points of the connecting lines
connecting_points = []
# Determine the maximum number of iterations based on the available space
max_iterations = int(min((image_height - left_bottom[0][1])/left_y_increment, (image_height - right_bottom[0][1])/right_y_increment))
# Generate the points for connecting lines
for i in range(max_iterations+1):
left_point = (int(left_bottom[0][0]), int(left_bottom[0][1] + i*left_y_increment))
right_point = (int(right_bottom[0][0]), int(right_bottom[0][1] + i*right_y_increment))
connecting_points.append((left_point, right_point))
all_connecting_points = np.vstack((all_connecting_points, connecting_points))
bottom_lines = max_iterations
### for top lines
# Create an array to store all the points of the connecting lines
connecting_points = []
# Determine the maximum number of iterations based on the available space
max_iterations = int(min(left_top[0][1]/left_y_increment , right_top[0][1]/right_y_increment))
# Generate the points for connecting lines
for i in range(max_iterations+1):
left_point = (int(left_top[0][0]), int(left_top[0][1] - i * left_y_increment))
right_point = (int(right_top[0][0]), int(right_top[0][1] - i * right_y_increment))
connecting_points.append((left_point, right_point))
all_connecting_points = np.vstack((all_connecting_points, connecting_points))
top_lines = max_iterations
# Draw the connecting lines on the image
for left_point, right_point in all_connecting_points:
cv2.line(image4, tuple(left_point), tuple(right_point), (0, 0, 255), 1) # Red color (0, 0, 255)
# Save the modified image
cv2.imwrite(output_image_path, image4)
# # Display the modified image using pyplot
# plt.figure(figsize=(48, 80)) # Optional: Set the figure size
# plt.imshow(cv2.cvtColor(image4, cv2.COLOR_BGR2RGB))
# plt.title('Image with Contours')
# plt.axis('off') # Optional: Turn off the axis labels
# plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
return all_connecting_points,top_lines, bottom_lines
def connect_lines_minimum(image, output_image_path,coordinates,min_top_line,min_bottom_line):
image4 = image.copy()
## to store all connecting points
# Initialize an empty NumPy array to store all connecting points
all_connecting_points_minimum = np.empty((0, 2, 2), dtype=int)
# Get the image dimensions
image_height, image_width, _ = image.shape
# Left and right points categorization
left = []
right = []
for x, y, w, h in coordinates:
if x <= 10:
left.append((x, y))
else:
right.append((x + w, y))
left_top = [point for point in left if point[1] < sum(y for _, y in left) / len(left)]
left_bottom = [point for point in left if point[1] >= sum(y for _, y in left) / len(left)]
right_top = [point for point in right if point[1] < sum(y for _, y in right) / len(right)]
right_bottom = [point for point in right if point[1] >= sum(y for _, y in right) / len(right)]
# Calculate the increments for dividing the sides into mln pieces
left_y_increment = (left_bottom[0][1] - left_top[0][1]) / mln
right_y_increment = (right_bottom[0][1] - right_top[0][1]) / mln
#print_values
print("left_top =", left_top)
print("left_bottom = ", left_bottom)
print("right_top = ", right_top)
print("right_bottom = ", right_bottom)
print("left_side_increament = ", left_y_increment)
print("right_side_increament = ", right_y_increment)
### for center lines
# Create an array to store all the points of the connecting lines
connecting_points = []
# Generate the points for connecting lines
for i in range(mln+1):
left_point = (int(left_top[0][0]), int(left_top[0][1] + i * left_y_increment))
right_point = (int(right_top[0][0]), int(right_top[0][1] + i * right_y_increment))
connecting_points.append((left_point, right_point))
all_connecting_points_minimum = np.vstack((all_connecting_points_minimum, connecting_points))
### for the bottom lines
# Create an array to store all the points of the connecting lines
connecting_points = []
# Determine the maximum number of iterations based on the available space
max_iterations_minimum = min_bottom_line
# Generate the points for connecting lines
for i in range(max_iterations_minimum+1):
left_point = (int(left_bottom[0][0]), int(left_bottom[0][1] + i*left_y_increment))
right_point = (int(right_bottom[0][0]), int(right_bottom[0][1] + i*right_y_increment))
connecting_points.append((left_point, right_point))
all_connecting_points_minimum = np.vstack((all_connecting_points_minimum, connecting_points))
### for top lines
# Create an array to store all the points of the connecting lines
connecting_points = []
# Determine the maximum number of iterations based on the available space
max_iterations_minimum = min_top_line
# Generate the points for connecting lines
for i in range(max_iterations_minimum+1):
left_point = (int(left_top[0][0]), int(left_top[0][1] - i * left_y_increment))
right_point = (int(right_top[0][0]), int(right_top[0][1] - i * right_y_increment))
connecting_points.append((left_point, right_point))
all_connecting_points_minimum = np.vstack((all_connecting_points_minimum, connecting_points))
# Draw the connecting lines on the image
for left_point, right_point in all_connecting_points_minimum:
cv2.line(image4, tuple(left_point), tuple(right_point), (0, 0, 255), 1) # Red color (0, 0, 255)
# Save the modified image
cv2.imwrite(output_image_path, image4)
# # Display the modified image using pyplot
# plt.figure(figsize=(48, 80)) # Optional: Set the figure size
# plt.imshow(cv2.cvtColor(image4, cv2.COLOR_BGR2RGB))
# plt.title('Image with Contours')
# plt.axis('off') # Optional: Turn off the axis labels
# plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
return all_connecting_points_minimum
def all_lines_contour(image, gray, output_path, all_connecting_points_minimum):
# Create a copy of the original image for drawing contours
contour_image = image.copy()
# Define the lower and upper threshold for contour detection (adjust these values as needed)
#lower_threshold = np.array([0, 0, 0])
#upper_threshold = np.array([100, 100, 100])
# Threshold the grayscale image to create a binary mask
_, binary_mask = cv2.threshold(gray, 1, 255, cv2.THRESH_BINARY)
# Create a mask for the lines (your previously drawn lines)
line_mask = np.zeros_like(binary_mask)
# Draw the lines on the line_mask
for left_point, right_point in all_connecting_points_minimum:
cv2.line(line_mask, left_point, right_point, 255, 2) # Draw lines in white (255)
# Subtract the line_mask from the binary_mask to exclude lines from contours
binary_mask = cv2.subtract(binary_mask, line_mask)
# Find contours in the modified binary mask
contours, _ = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Draw the contours on the contour_image
for contour in contours:
cv2.drawContours(contour_image, [contour], -1, (0, 0, 255), 1) # Red color (0, 0, 255)
# Save the modified image with contours
cv2.imwrite(output_path, contour_image)
# # Display the image with selected and first/last contours using pyplot
# plt.figure(figsize=(48, 80)) # Optional: Set the figure size
# plt.imshow(cv2.cvtColor(contour_image, cv2.COLOR_BGR2RGB))
# plt.title('Image with Contours')
# plt.axis('off') # Optional: Turn off the axis labels
# plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
def vertical_lines_detection(image, gray, output_path):
# Apply edge detection (optional, but can improve results)
edges = cv2.Canny(gray, 950, 700, apertureSize=5)
# Apply Probabilistic Hough Line Transform
lines = cv2.HoughLinesP(edges, 15, np.pi/500, threshold=150, minLineLength=100, maxLineGap=3)
# Draw the detected lines on a copy of the original image
line_mask = np.zeros_like(gray)
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line[0]
# Check if the line meets the minimum length requirement
line_length = int(np.sqrt((x2 - x1)**2 + (y2 - y1)**2))
if line_length >= 100:
# Calculate the equation of the line: y = mx + b
m = (y2 - y1) / (x2 - x1 + 1e-6)
b = y1 - m * x1
# Calculate the y-coordinates for top and bottom of the image
y_top = 0
y_bottom = image.shape[0]
# print(y_bottom)
# Check if the slope is close to zero
x_top = int((y_top - b) / (m + 1e-6))
if abs(m) < 1e-6:
x_bottom = int(x1)
else:
# Calculate the x-coordinates using the equation of the line
x_bottom = int((y_bottom - b) / (m + 1e-6))
# Draw the line from top to bottom
# Draw the line from top to bottom
cv2.line(line_mask, (int(x_top), int(y_top)), (int(x_bottom), int(y_bottom)), 255, 25)
# Invert the line mask to create a mask for the background
background_mask = cv2.bitwise_not(line_mask)
# Find contours excluding the lines
contours, _ = cv2.findContours(background_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Overlay the contours on the original image
image_with_contours = image.copy()
for contour in contours:
# Calculate the width of the contour's bounding box
x, y, w, h = cv2.boundingRect(contour)
# Check if the height is less than 100 pixels then height of image
if h > (image.shape[0] - 100):
cv2.drawContours(image_with_contours, [contour], -1, (0, 0, 255), 2)
# Save the image with highlighted lines and filtered contours
cv2.imwrite(output_path, image_with_contours)
# # Display the image with highlighted lines and filtered contours using pyplot
# plt.figure(figsize=(12, 8))
# plt.imshow(cv2.cvtColor(image_with_contours, cv2.COLOR_BGR2RGB))
# plt.title('Image with Probabilistic Hough Lines, Vertical Lines, and Contours (Filtered)')
# plt.axis('off')
# plt.show()
return line_mask
return image_with_contours
def clear_horizontal_contour(image, gray, all_connecting_points,output_path):
# Threshold the grayscale image to create a binary mask for the contour area
_, binary_mask = cv2.threshold(gray, 1, 255, cv2.THRESH_BINARY)
# Find contours in the binary mask
contours, _ = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Create a mask for the contour area
contour_mask = np.zeros_like(binary_mask)
for contour in contours:
cv2.drawContours(contour_mask, [contour], -1, 255, thickness=cv2.FILLED)
# Create a mask for the lines (your previously drawn lines)
line_mask_horizontal = np.zeros_like(binary_mask)
# Draw the lines on the line_mask_horizontal
for left_point, right_point in all_connecting_points:
cv2.line(line_mask_horizontal, left_point, right_point, 255, 2) # Draw lines in white (255)
# Subtract the line_mask_horizontal from the contour_mask to exclude lines from the contour area
contour_mask = cv2.subtract(contour_mask, line_mask_horizontal)
# Create a masked image by applying the contour mask to the original image
masked_image_horizontal = cv2.bitwise_and(image, image, mask=contour_mask)
# Save the masked image
cv2.imwrite(output_path, masked_image_horizontal)
# # Display the image with highlighted contours using pyplot
# plt.figure(figsize=(48, 80)) # Optional: Set the figure size
# plt.imshow(cv2.cvtColor(masked_image_horizontal, cv2.COLOR_BGR2RGB))
# plt.title('Image with Highlighted Contours')
# plt.axis('off') # Optional: Turn off the axis labels
# plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
return masked_image_horizontal
def clear_vertical_contour(image, gray, line_mask, output_path):
# Convert line_mask to uint8
line_mask = line_mask.astype(np.uint8)
# Invert the line mask to create a mask for the background
background_mask = cv2.bitwise_not(line_mask)
# Find contours excluding the lines
contours, _ = cv2.findContours(background_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Create a mask for the contour area
contour_mask = np.zeros_like(gray)
for contour in contours:
# Calculate the width of the contour's bounding box
x, y, w, h = cv2.boundingRect(contour)
# Check if the width is greater than or equal to 50 pixels
if h > (image.shape[0] - 100):
cv2.drawContours(contour_mask, [contour], -1, 255, thickness=cv2.FILLED)
# Create a masked image by applying the contour mask to the original color image
masked_image_vertical = cv2.bitwise_and(image, image, mask=contour_mask)
# Save the masked image
cv2.imwrite(output_path, masked_image_vertical)
# # Display the image with highlighted contours using pyplot
# plt.figure(figsize=(48, 80)) # Optional: Set the figure size
# plt.imshow(cv2.cvtColor(masked_image_vertical, cv2.COLOR_BGR2RGB))
# plt.title('Image with Highlighted Contours')
# plt.axis('off') # Optional: Turn off the axis labels
# plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
return masked_image_vertical
# Display the image with highlighted contours using pyplot
# plt.figure(figsize=(48, 80)) # Optional: Set the figure size
# plt.imshow(cv2.cvtColor(masked_image_vertical, cv2.COLOR_BGR2RGB))
# plt.title('Image with Highlighted Contours')
# plt.axis('off') # Optional: Turn off the axis labels
# plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
return masked_image_vertical
def combine_both_clear_contours(masked_image_vertical, masked_image_horizontal, output_path):
contours_image_gray = cv2.cvtColor(masked_image_vertical, cv2.COLOR_BGR2GRAY)
# Create a mask from the contour image
_, mask = cv2.threshold(contours_image_gray, 1, 255, cv2.THRESH_BINARY)
# Find contours in the mask
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Iterate through the contours
for i, contour in enumerate(contours):
# Create a mask for the current contour
contour_mask = np.zeros_like(mask)
cv2.drawContours(contour_mask, [contour], -1, (255), thickness=cv2.FILLED)
# Apply the inverse mask to extract the background
vertical_and_horizontal_wiped_combined = cv2.bitwise_and(masked_image_horizontal, masked_image_horizontal, mask=mask)
# # Display the image with highlighted contours using pyplot
# plt.figure(figsize=(48, 80)) # Optional: Set the figure size
# plt.imshow(cv2.cvtColor(vertical_and_horizontal_wiped_combined, cv2.COLOR_BGR2RGB))
# plt.title('Image with Highlighted Contours')
# plt.axis('off') # Optional: Turn off the axis labels
# plt.show()
# Save the modified image
cv2.imwrite(output_path, vertical_and_horizontal_wiped_combined)
cv2.waitKey(0)
cv2.destroyAllWindows()
return vertical_and_horizontal_wiped_combined
def contours_combine_both_clear_contours(masked_image_vertical, masked_image_horizontal,vertical_and_horizontal_wiped_combined,output_path):
# Load the contour image
contours_image_gray = cv2.cvtColor(masked_image_vertical, cv2.COLOR_BGR2GRAY)
#target_image_gray = cv2.cvtColor(masked_image_horizontal, cv2.COLOR_BGR2GRAY)
image1 = masked_image_vertical.copy()
# Create a mask from the contour image
_, mask = cv2.threshold(contours_image_gray, 1, 255, cv2.THRESH_BINARY)
# Find contours in the mask
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Iterate through the contours
for i, contour in enumerate(contours):
# Create a mask for the current contour
contour_mask = np.zeros_like(mask)
cv2.drawContours(contour_mask, [contour], -1, (255), thickness=cv2.FILLED)
# Apply the contour mask to extract the ROI from the original image
roi = cv2.bitwise_and(masked_image_horizontal, masked_image_horizontal, mask=contour_mask)
# Convert the ROI to grayscale
roi_gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
# Find contours within the ROI
roi_contours, _ = cv2.findContours(roi_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Draw the contours found within the ROI on the original target image
cv2.drawContours(image1, roi_contours, -1, (0, 0, 255), 2)
# Display the image with highlighted contours using pyplot
# plt.figure(figsize=(48, 80)) # Optional: Set the figure size
# plt.imshow(cv2.cvtColor(image1, cv2.COLOR_BGR2RGB))
# plt.title('Image with Highlighted Contours')
# plt.axis('off') # Optional: Turn off the axis labels
# plt.show()
# Save the modified image
cv2.imwrite(output_path, image1)
cv2.waitKey(0)
cv2.destroyAllWindows()
def intensity_of_each_lane_each_box(masked_image_vertical, masked_image_horizontal,image):
# height, width,_ = image.shape
# Print the height and width
# print(f"Height: {height}, Width: {width}")
# contour_3_boundaries = []
# Load the contour image
contours_image_gray = cv2.cvtColor(masked_image_vertical, cv2.COLOR_BGR2GRAY)
#target_image_gray = cv2.cvtColor(masked_image_horizontal, cv2.COLOR_BGR2GRAY)
# Create a mask from the contour image
_, mask = cv2.threshold(contours_image_gray, 1, 255, cv2.THRESH_BINARY)
# Find contours in the mask
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#define empty list for roi, counter and intensity
roi_list = [] # List for ROI
contour_list = [] # List for Contour
mean_intensity_list = [] # List for Mean Intensity
sum_intensity_list = [] # List for sum Intensity
# Iterate through the contours
for i, contour in enumerate(contours):
if i < 2 or i >= len(contours) - 2:
continue
# Create a mask for the current contour
contour_mask = np.zeros_like(mask)
cv2.drawContours(contour_mask, [contour], -1, (255), thickness=cv2.FILLED)
# Apply the contour mask to extract the ROI from the original image
roi = cv2.bitwise_and(masked_image_horizontal, masked_image_horizontal, mask=contour_mask)
# Convert the ROI to grayscale
roi_gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
# Find contours within the ROI
roi_contours, _ = cv2.findContours(roi_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Create a copy of the ROI image for visualization
roi_image_with_contours = roi.copy()
# Draw the contours found within the ROI on the ROI image
cv2.drawContours(roi_image_with_contours, roi_contours, -1, (0, 0, 255), 2)
# Initialize lists to store intensity values and x-coordinates
intensity_values = []
x_coordinates = []
# Calculate the intensity values and x-coordinates for each contour
for j, roi_contour in enumerate(roi_contours):
if j == 0 or j == len(roi_contours) - 1:
continue
#Mask to extract pixels within the contour
mask_within_contour = np.zeros_like(roi_gray)
cv2.drawContours(mask_within_contour, [roi_contour], -1, 255, thickness=cv2.FILLED)
# Extract the pixels within the contour
pixels_within_contour = cv2.bitwise_and(roi_gray, roi_gray, mask=mask_within_contour)
pixels_within_contour = 255-pixels_within_contour
# #pixels_within_contour = 255 - pixels_within_contour
# print("length")
# print(len(pixels_within_contour))
# print("pixel with in contour")
# print(pixels_within_contour)
# print("minimum and maximum pixel in a range")
# print(np.min(pixels_within_contour), np.max(pixels_within_contour))
# print("data_type")
# print(pixels_within_contour.dtype)
# Get the height and width of the pixels_within_contour array
# height, width = pixels_within_contour.shape
# Print the height and width
# print(f"Height: {height}, Width: {width}")
# # # Count the number of 0 values
# num_white_pixels = np.sum(pixels_within_contour == 255)
# # # Count the number of pixels greater than 0
# num_non_white_pixels = np.sum(pixels_within_contour < 255)
# x= np.count_nonzero(mask_within_contour)
# y = num_white_pixels + num_non_white_pixels
# print(f"total pixel = {y}")
# # # Print the results
# print(f"Number of pixels with value 255: {num_white_pixels}")
# print(f"Number of pixels with value less than 255: {num_non_white_pixels}")
# print(f"Number of pixels within the countour: {x}")
# print("new_intensity_mean")
mean_intensity_new = np.mean(pixels_within_contour[pixels_within_contour < 255])
sum_intensity_new = np.sum(pixels_within_contour[pixels_within_contour < 255])
# print(mean_intensity_new)
# Calculate the mean intensity of pixels within the contour
#mean_intensity = np.mean(pixels_within_contour)
# Print the mean intensity of the contour
# print(f"ROI {i+1}, Contour {j+1}: Mean Intensity = {mean_intensity_new}")
# Append the intensity value to the list
intensity_values.append(mean_intensity_new)
# Append the x-coordinate (position within the contour) to the list
x_coordinates.append(j)
# Append the information to the respective lists
roi_list.append(f'ROI {i+1}')
contour_list.append(f'Contour {j+1}')
mean_intensity_list.append(round(mean_intensity_new, 4))
sum_intensity_list.append(sum_intensity_new)
#create a dataframe #.round(4)
data_mean = {
'ROI': roi_list,
'Contour': contour_list,
'Intensity': mean_intensity_list
}
df_mean = pd.DataFrame(data_mean)
# print(df_mean)
# df.to_csv(output_intensity, index=False)
#create a dataframe #.round(4)
data_sum = {
'ROI': roi_list,
'Contour': contour_list,
'Intensity': sum_intensity_list
}
df_sum = pd.DataFrame(data_sum)
# print(df_sum)
# # Plot the intensity graph for the current ROI
# plt.figure(figsize=(24, 12)) # Set the figure size
# plt.plot(x_coordinates, intensity_values, color='blue', marker='o', linestyle='-')
# plt.title(f'Intensity Graph for ROI {i+1}')
# plt.xlabel('Contour Position')
# plt.ylabel('Intensity')
# plt.grid(True)
# plt.show()
# # Display the ROI image with its contours
# plt.figure(figsize=(6, 6)) # Set the figure size
# plt.imshow(cv2.cvtColor(roi_image_with_contours, cv2.COLOR_BGR2RGB))
# plt.title(f'ROI {i+1} with Contours')
# plt.axis('off')
# plt.show()
# Return the DataFrame
return df_mean, df_sum
#define a function to sort the intensity file
def intensity_sorting(df,output_pivoted_intensity_sorted):
print("intensity_sorting_step")
print(df)
# Pivot the DataFrame
pivot_df = df.pivot(index='Contour', columns='ROI', values='Intensity')
# Optionally, reset the index if you want 'Contour' to be a regular column
pivot_df.reset_index(inplace=True)
# Display the pivoted DataFrame
print(pivot_df)
# Save the pivoted DataFrame to a new CSV file
# pivot_df.to_csv(output_pivoted_intensity, index=False)
# Extract the numeric part from the 'Contour' column
pivot_df['Contour_Num'] = pivot_df['Contour'].str.extract('(\d+)').astype(int)
# Sort the DataFrame based on the numeric part of 'Contour'
pivot_df_sorted = pivot_df.sort_values(by='Contour_Num')
# Drop the temporary 'Contour_Num' column
pivot_df_sorted.drop('Contour_Num', axis=1, inplace=True)
# Display the sorted DataFrame
print(pivot_df_sorted)
#save the sorted_intensity as csv file
pivot_df_sorted.to_csv(output_pivoted_intensity_sorted, index=False)
def get_contour_number(contour):
"""Extracts the numerical part of the contour for sorting."""
return int(contour.split()[-1])
def process_and_save_csv_files(file_pattern='sample_name_*.csv', input_dir='intensity_of_samples'):
"""
Processes each CSV file in the given subfolder matching the pattern, sorts, transposes, and saves them.
"""
# Ensure input_dir is a Path
input_dir = Path(input_dir)
# Build the glob pattern (folder + file pattern)
csv_files = list(input_dir.glob(file_pattern))
processed_dfs = [] # List to store processed DataFrames
for file in csv_files:
# Read CSV file into a DataFrame
df = pd.read_csv(file)
# Apply sorting logic
df['Contour_Num'] = df['Contour'].apply(get_contour_number)
df = df.sort_values(by='Contour_Num').drop(columns=['Contour_Num'])
# Set 'Contour' column as the index
df.set_index('Contour', inplace=True)
# Transpose the DataFrame
df_transposed = df.transpose()
# Reset the index to keep the original column names
df_transposed = df_transposed.reset_index()
# Append the processed DataFrame to the list
processed_dfs.append(df_transposed)
print(f"Processed DataFrame from: {file}")
return processed_dfs # Return the list of processed DataFrames
def merge_and_sort_files(processed_dfs):
"""Merges and sorts all DataFrames from the list of processed DataFrames."""
# Concatenate all DataFrames into a single DataFrame
merged_df = pd.concat(processed_dfs, ignore_index=True)
# Sort the merged DataFrame by contour number
merged_df["Contour_Num"] = merged_df["index"].apply(get_contour_number)
sorted_df = merged_df.sort_values(by="Contour_Num").drop(columns=["Contour_Num"])
# Save the sorted DataFrame
sorted_df.to_csv("ready_for_normalization.csv", index=False)
return sorted_df
def quantile_normalize_rowwise(final_sorted_df, tie_method: str = "average"):
"""
Quantile normalization row-wise with proper tie handling
"""
df = final_sorted_df.copy()
# Separate numeric and non-numeric columns
num_cols = df.select_dtypes(include=np.number).columns
non_num_cols = [col for col in df.columns if col not in num_cols]
# Process only numeric columns
df_num = df[num_cols].copy()
X = df_num.to_numpy(dtype=float)
# 1) Sort values within each row
sorted_vals = np.sort(X, axis=1)
# 2) Reference distribution
mean_vector = sorted_vals.mean(axis=0)
# 3) Calculate ranks with tie handling
ranks = df_num.rank(axis=1, method=tie_method, ascending=True).to_numpy()
# 4) Interpolate onto reference distribution
positions = np.arange(1, df_num.shape[1] + 1, dtype=float)
normalized_vals = np.interp(ranks, positions, mean_vector)
# Rebuild DataFrame
df_norm_num = pd.DataFrame(normalized_vals,
index=df_num.index,
columns=df_num.columns).round(2)
# Reattach non-numeric columns
if non_num_cols:
df_norm = pd.concat(
[df[non_num_cols].reset_index(drop=True),
df_norm_num.reset_index(drop=True)],
axis=1
)
df_norm.index = df.index
else:
df_norm = df_norm_num
df_norm.to_csv("normalized_df.csv", index=False)
return df_norm
def ready_for_ml(combined_df,classification_file):
df= combined_df
# Get the directory where the script/notebook is running
output_dir = Path.cwd()
# Filter rows where the 'index' column starts with 'val'
val_df = df[df['index'].str.startswith('val')]
# Filter rows where the 'index' column does not start with 'val'
non_val_df = df[~df['index'].str.startswith('val')]
# Save each subset to separate CSV files
val_df.to_csv(output_dir / 'normalized_validation.csv', index=False)
non_val_df.to_csv(output_dir / 'normalized_samples.csv', index=False)
df2 = non_val_df
df1 = classification_file
print(df1)
# Remove duplicates and handle missing values in df2
df2 = df2.drop_duplicates(subset='index').dropna(subset=['index'])
print(df2)
# Create a new "cluster" column in df2 and assign corresponding values from df1
df2['cluster'] = df2['index'].map(df1.set_index('sample')['cluster'])
# Drop rows where 'cluster' has NaN values
df2 = df2.dropna(subset=['cluster'])
# Convert the 'ok' column values to integers
df2['cluster'] = df2['cluster'].astype(int)
# Print the modified df2
print(df2)
# Extract the numeric part from the "index" column
df2['sort_key'] = df2['index'].str.extract('(\d+)').astype(int)
# Sort the DataFrame by the numeric part
df2_sorted = df2.sort_values(by='sort_key')
# Drop the temporary "sort_key" column (optional)
df2_sorted = df2_sorted.drop(columns=['sort_key'])
df2_sorted.to_csv(output_dir / 'ready_for_ml_class_1.csv', index=False)