@@ -21,50 +21,41 @@ class IndiAllskyDetectLines(object):
2121 min_line_length = 40 # minimum number of pixels making up a line
2222 max_line_gap = 20 # maximum gap in pixels between connectable line segments
2323
24- def __init__ (self , config , bin_v ):
24+ mask_blur_kernel_size = 75
25+
26+
27+ def __init__ (self , config , bin_v , mask = None ):
2528 self .config = config
2629 self .bin_v = bin_v
2730
28- self .x_offset = 0
29- self .y_offset = 0
30-
31+ self ._sqm_mask = mask
32+ self ._sqm_gradient_mask = None
3133
32- def detectLines (self , img ):
33- image_height , image_width = img .shape [:2 ]
3434
35- sqm_roi = self .config .get ('SQM_ROI' , [])
35+ def detectLines (self , original_img ):
36+ if isinstance (self ._sqm_mask , type (None )):
37+ # This only needs to be done once if a mask is not provided
38+ self ._generateSqmMask (original_img )
3639
37- try :
38- x1 = int (sqm_roi [0 ] / self .bin_v .value )
39- y1 = int (sqm_roi [1 ] / self .bin_v .value )
40- x2 = int (sqm_roi [2 ] / self .bin_v .value )
41- y2 = int (sqm_roi [3 ] / self .bin_v .value )
42- except IndexError :
43- logger .warning ('Using central ROI for line detection' )
44- x1 = int ((image_width / 2 ) - (image_width / 3 ))
45- y1 = int ((image_height / 2 ) - (image_height / 3 ))
46- x2 = int ((image_width / 2 ) + (image_width / 3 ))
47- y2 = int ((image_height / 2 ) + (image_height / 3 ))
40+ if isinstance (self ._sqm_gradient_mask , type (None )):
41+ # This only needs to be done once
42+ self ._generateSqmGradientMask (original_img )
4843
4944
50- self . x_offset = x1
51- self . y_offset = y1
45+ # apply the gradient to the image
46+ masked_img = ( original_img * self . _sqm_gradient_mask ). astype ( numpy . uint8 )
5247
53- roi_img = img [
54- y1 :y2 ,
55- x1 :x2 ,
56- ]
5748
58- if len (img .shape ) == 2 :
59- img_gray = roi_img
49+ if len (original_img .shape ) == 2 :
50+ img_gray = masked_img
6051 else :
61- img_gray = cv2 .cvtColor (roi_img , cv2 .COLOR_BGR2GRAY )
52+ img_gray = cv2 .cvtColor (masked_img , cv2 .COLOR_BGR2GRAY )
6253
6354
6455
6556 lines_start = time .time ()
6657
67- blur_gray = cv2 .GaussianBlur (img_gray , (self .blur_kernel_size , self .blur_kernel_size ), 0 )
58+ blur_gray = cv2 .GaussianBlur (img_gray , (self .blur_kernel_size , self .blur_kernel_size ), cv2 . BORDER_DEFAULT )
6859
6960
7061 edges = cv2 .Canny (blur_gray , self .canny_low_threshold , self .canny_high_threshold )
@@ -91,35 +82,74 @@ def detectLines(self, img):
9182
9283 logger .info ('Detected %d lines' , len (lines ))
9384
94- self ._drawLines (img , lines , ( x1 , y1 , x2 , y2 ) )
85+ self ._drawLines (original_img , lines )
9586
9687 return lines
9788
9889
99- def _drawLines (self , img , lines , box ):
90+ def _generateSqmMask (self , img ):
91+ logger .info ('Generating mask based on SQM_ROI' )
92+
93+ image_height , image_width = img .shape [:2 ]
94+
95+ # create a black background
96+ mask = numpy .zeros ((image_height , image_width ), dtype = numpy .uint8 )
97+
98+ sqm_roi = self .config .get ('SQM_ROI' , [])
99+
100+ try :
101+ x1 = int (sqm_roi [0 ] / self .bin_v .value )
102+ y1 = int (sqm_roi [1 ] / self .bin_v .value )
103+ x2 = int (sqm_roi [2 ] / self .bin_v .value )
104+ y2 = int (sqm_roi [3 ] / self .bin_v .value )
105+ except IndexError :
106+ logger .warning ('Using central ROI for blob calculations' )
107+ x1 = int ((image_width / 2 ) - (image_width / 3 ))
108+ y1 = int ((image_height / 2 ) - (image_height / 3 ))
109+ x2 = int ((image_width / 2 ) + (image_width / 3 ))
110+ y2 = int ((image_height / 2 ) + (image_height / 3 ))
111+
112+ # The white area is what we keep
113+ cv2 .rectangle (
114+ img = mask ,
115+ pt1 = (x1 , y1 ),
116+ pt2 = (x2 , y2 ),
117+ color = (255 ), # mono
118+ thickness = cv2 .FILLED ,
119+ )
120+
121+ # mask needs to be blurred so that we do not detect it as an edge
122+ self ._sqm_mask = mask
123+
124+
125+ def _generateSqmGradientMask (self , img ):
126+ # blur the mask to prevent mask edges from being detected as lines
127+ blur_mask = cv2 .blur (self ._sqm_mask , (self .mask_blur_kernel_size , self .mask_blur_kernel_size ), cv2 .BORDER_DEFAULT )
128+
129+ if len (img .shape ) == 2 :
130+ # mono
131+ mask = blur_mask
132+ else :
133+ # color
134+ mask = cv2 .cvtColor (blur_mask , cv2 .COLOR_GRAY2BGR )
135+
136+ self ._sqm_gradient_mask = mask / 255
137+
138+
139+ def _drawLines (self , img , lines ):
100140 if not self .config .get ('DETECT_DRAW' ):
101141 return
102142
103143 color_bgr = list (self .config ['TEXT_PROPERTIES' ]['FONT_COLOR' ])
104144 color_bgr .reverse ()
105145
106- ### box drawn in star detection
107- #logger.info('Draw box around ROI')
108- #cv2.rectangle(
109- # img=img,
110- # pt1=(box[0], box[1]),
111- # pt2=(box[2], box[3]),
112- # color=(128, 128, 128),
113- # thickness=1,
114- #)
115-
116146
117147 for line in lines :
118148 for x1 , y1 , x2 , y2 in line :
119149 cv2 .line (
120150 img ,
121- (x1 + self . x_offset , y1 + self . y_offset ),
122- (x2 + self . x_offset , y2 + self . y_offset ),
151+ (x1 , y1 ),
152+ (x2 , y2 ),
123153 tuple (color_bgr ),
124154 3 ,
125155 )
0 commit comments