def extract_bright(grey_img, histogram=False): """ Extracts brightest part of the image. Expected to be the LEDs (provided that there is a dark background) Returns a Thresholded image histgram defines if we use the hist calculation to find the best margin """ ## Searches for image maximum (brightest pixel) # We expect the LEDs to be brighter than the rest of the image [minVal, maxVal, minLoc, maxLoc] = cv2.minMaxLoc(grey_img) # print "Brightest pixel val is %d" %(maxVal) state = "" if maxVal == 255: state = "=" else: state = "." return state # We retrieve only the brightest part of the image # Here is use a fixed margin (80%), but you can use hist to enhance this one if 0: ## Histogram may be used to wisely define the margin # We expect a huge spike corresponding to the mean of the background # and another smaller spike of bright values (the LEDs) hist = grey_histogram(grey_img, nBins=64) [hminValue, hmaxValue, hminIdx, hmaxIdx] = cv2.getMinMaxHistValue(hist) margin = 0 # statistics to be calculated using hist data else: margin = 0.8 thresh = int(maxVal * margin) # in pix value to be extracted # print "Threshold is defined as %d" %(thresh) return thresh
def run(self): hist = cv2.createHist([180], cv2.CV_HIST_ARRAY, [(0,180)], 1 ) backproject_mode = True while True: frame = cv2.QueryFrame( self.capture ) # Convert to HSV and keep the hue hsv = cv2.createImage(cv2.GetSize(frame), 8, 3) cv2.cvtColor(frame, hsv, cv2.CV_BGR2HSV) self.hue = cv2.createImage(cv2.GetSize(frame), 8, 1) cv2.split(hsv, self.hue, None, None, None) # Compute back projection backproject = cv2.createImage(cv2.GetSize(frame), 8, 1) cv2.calcArrBackProject( [self.hue], backproject, hist ) # Run the cam-shift (if the a window is set and != 0) if self.track_window and is_rect_nonzero(self.track_window): crit = ( cv2.CV_TERMCRIT_EPS | cv2.CV_TERMCRIT_ITER, 10, 1) (iters, (area, value, rect), track_box) = cv2.camShift(backproject, self.track_window, crit) #Call the camshift !! self.track_window = rect #Put the current rectangle as the tracked area # If mouse is pressed, highlight the current selected rectangle and recompute histogram if self.drag_start and is_rect_nonzero(self.selection): sub = cv2.getSubRect(frame, self.selection) #Get specified area #Make the effect of background shadow when selecting a window save = cv2.cloneMat(sub) cv2.convertScale(frame, frame, 0.5) cv2.copy(save, sub) #Draw temporary rectangle x,y,w,h = self.selection cv2.rectangle(frame, (x,y), (x+w,y+h), (255,255,255)) #Take the same area but in hue image to calculate histogram sel = cv2.getSubRect(self.hue, self.selection ) cv2.calcArrHist( [sel], hist, 0) #Used to rescale the histogram with the max value (to draw it later on) (_, max_val, _, _) = cv2.getMinMaxHistValue( hist) if max_val != 0: cv2.convertScale(hist.bins, hist.bins, 255. / max_val) elif self.track_window and is_rect_nonzero(self.track_window): #If window set draw an elipseBox cv2.ellipseBox( frame, track_box, cv2.CV_RGB(255,0,0), 3, cv2.CV_AA, 0 ) cv2.showImage( "CamShiftDemo", frame ) cv2.showImage( "Backprojection", backproject) cv2.showImage( "Histogram", self.hue_histogram_as_image(hist)) c = cv2.waitKey(7) % 0x100 if c == 27: break
hist = 0 maxVal = 0 thresh = 0 success, image = vidcap.read() print('Read a new frame: ', success) cv2.imwrite(os.path.join(Folder, "frame{:d}.jpg".format(count)), image) # save frame as JPEG file image1 = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) cv2.imwrite(os.path.join(Folder, "frameg{:d}.jpg".format(count)), image1) #hist = cv2.calcHist([image1],[0],None,[255],[0,256]) [minVal, maxVal, minLoc, maxLoc] = cv2.minMaxLoc(image1) print "Brightest pixel val is %d" % (maxVal) if 0: ## Histogram may be used to wisely define the margin # We expect a huge spike corresponding to the mean of the background # and another smaller spike of bright values (the LEDs) hist = cv2.calcHist([image1], [0], None, [64], [0, 256]) [hminValue, hmaxValue, hminIdx, hmaxIdx] = cv2.getMinMaxHistValue(hist) margin = 0 # statistics to be calculated using hist data else: margin = 0.71 thresh = int(maxVal * margin) print "Threshold is defined as %d" % (thresh) _, thresh1 = cv2.threshold(image1, thresh, 255, cv2.THRESH_BINARY) cv2.imwrite(os.path.join(Folder, "framet{:d}.jpg".format(count)), thresh1) count += 1 time_end = time.time() print time_end - time_start print fps