def ISO_Tune(amount): gain = sensor.get_gain_db() # 感光度自动调节 范围11-16左右 if amount == 0 and gain > 11: sensor.set_auto_gain(False, gain_db=sensor.get_gain_db() * 0.9) # 画面明亮时调低gain elif amount >= 3 and gain < 16: sensor.set_auto_gain(False, gain_db=sensor.get_gain_db() * 1.3) # 画面过暗时调高gain,稳定性差
def init(self, robot_): self.robot = robot_ if self.robot == self.ROBOT_O: #O_bot self.thresholds = [ (35, 100, 26, 78, 22, 72), #Ball (68, 100, -19, 27, 41, 81), #Yellow Goal (20, 41, -6, 15, -55, -15) ] # Blue Goal self.window = (59, 18, 184, 181) elif self.robot == self.ROBOT_P2: #P2_bot self.thresholds = [ (39, 100, 26, 78, 22, 72), #Ball (68, 100, -19, 27, 41, 81), #Yellow Goal (20, 41, -6, 25, -55, -15) ] # Blue Goal self.window = (71, 4, 193, 191) # sensor setup sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) #Resolution sensor.set_windowing(self.window) sensor.skip_frames(time=1000) sensor.set_auto_exposure(False) sensor.set_auto_whitebal(False) # Need to let the above settings get in... sensor.skip_frames(time=250) # === GAIN === curr_gain = sensor.get_gain_db() sensor.set_auto_gain(False, gain_db=curr_gain) # === EXPOSURE === curr_exposure = sensor.get_exposure_us() sensor.set_auto_exposure(False, exposure_us=int(curr_exposure * 0.5)) # === WHITE BAL === sensor.set_auto_whitebal( False, rgb_gain_db=(-6.02073, -3.762909, 3.33901)) #Must remain false for blob tracking sensor.set_brightness(2) sensor.set_contrast(2) sensor.set_saturation(2) sensor.skip_frames(time=500)
def sensor_config(data): global processing sensor_regs = struct.unpack("<16I", data) reg_list = [ 0x00, 0x01, 0x02, 0x03, 0x08, 0x10, 0x2d, 0x2e, 0x2f, 0x33, 0x34, 0x35, 0x36, 0x37, 0x37, 0x38 ] i = 0 for sr in sensor_regs: sensor.__write_reg(reg_list[i], sr) i += 1 gain_db = sensor.get_gain_db() exposure_us = sensor.get_exposure_us() rgb_gain_db = sensor.get_rgb_gain_db() processing = False return struct.pack("<fIfff", gain_db, exposure_us, rgb_gain_db[0], rgb_gain_db[1], rgb_gain_db[2])
x_diff1 = 0 x_err1 = [0, 0, 0, 0] dist2center = 0 blobs1 = [] motor = 0 # Configure camera sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat( sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QQVGA) # Set frame size to QVGA (320x240) sensor.skip_frames(time=2000) # Wait for settings take effect. sensor.set_auto_gain(False, gain_db=9) # must be turned off for color tracking sensor.set_auto_whitebal(False) # must be turned off for color tracking clock = time.clock() # Create a clock object to track the FPS. print(sensor.get_gain_db()) ## Control Values # PID#2.5 Kp_max_s = 2.0 Kp = Kp_max_s Kd_s = 0.012 Kd = Kd_s turbo_pwm = 50 max_pwm = 35 min_pwm = 35 # brake control straight_counter = 0 brake_wait_counter = 0 brake_wait = 50 brake_counter = 0
# here we always choose the QVGA format (320x240) inside a VGA image img_width = 320 img_height = 240 sensor.reset() sensor_format = sensor.GRAYSCALE # Grayscale is enough to find a chessboard sensor_size = sensor.VGA sensor.set_pixformat(sensor_format) sensor.set_framesize(sensor_size) if img_width != sensor.width() or img_height != sensor.height(): sensor.set_windowing((int((sensor.width()-img_width)/2),int((sensor.height()-img_height)/2),img_width,img_height)) sensor.skip_frames(time = 2000) # get the current the current exposure and gains and send them to the remote cam so that the # 2 cams have the same image settings sensor.snapshot() gain_db = sensor.get_gain_db() exposure_us = sensor.get_exposure_us() print("exposure is " + str(exposure_us)) rgb_gain_db = sensor.get_rgb_gain_db() sensor.set_auto_gain(False, gain_db) sensor.set_auto_exposure(False, exposure_us) sensor.set_auto_whitebal(False, rgb_gain_db) result = interface.call("sensor_config", struct.pack("<fIfff", gain_db, exposure_us, rgb_gain_db[0], rgb_gain_db[1], rgb_gain_db[2])) if result is not None: gain_db, exposure_us, r_gain_db, g_gain_db, b_gain_db = struct.unpack("<fIfff", result) print("ret is " + str(exposure_us)) else: # apparently something went wrong with the remote cam # stopping there in this case
sensor.snapshot().save("QR_%s.jpg" % NUM, quallity=100) NUM += 1 else: ch.pulse_width_percent(0) state_flag = -1 NUM = 0 ################ # 图像信息打印 img.draw_rectangle(roi) img.draw_cross(WINDOW_CENTER_X, WINDOW_CENTER_Y) img.draw_string( 2, 0 * SCALE, "GAIN: %s" % sensor.get_gain_db(), color=128, scale=SCALE, mono_space=False, ) img.draw_string(2, 8 * SCALE, "FPS: %s" % clock.fps(), color=128, scale=SCALE, mono_space=False) img.draw_string( 2, 16 * SCALE, "STATE_FLAG: %s" % state_flag, color=128,
yellowGoal = [(65, 85, -14, 21, 46, 83)] curr_wbal = (-6.02073, -3.454361, 5.986629) vwin_val = (40, 0, 249,240) # ||| UART SETUP ||| uart = UART(3, 9600, timeout_char = 1000) # ||| SENSOR SETUP ||| sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 1000) # ||| GAIN ||| curr_gain = sensor.get_gain_db() sensor.set_auto_gain(False, gain_db=curr_gain) # ||| EXPOSURE ||| curr_exposure = sensor.get_exposure_us() sensor.set_auto_exposure(False, exposure_us = int(curr_exposure)) # ||| WHITE BAL ||| sensor.set_auto_whitebal(False, rgb_gain_db=curr_wbal) # ||| SET VALUES & WINDOWING ||| sensor.set_windowing(vwin_val) sensor.set_saturation(3) sensor.set_brightness(3) #Change to -3 sensor.set_contrast(3)
# higher exposure time. Gain control allows you to increase the output per # pixel using analog and digital multipliers... however, it also amplifies # noise. So, it's best to let the exposure increase as much as possible # and then use gain control to make up any remaining ground. import sensor, image, time # Change this value to adjust the gain. Try 10.0/0/0.1/etc. GAIN_SCALE = 1.0 sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) # Print out the initial gain for comparison. print("Initial gain == %f db" % sensor.get_gain_db()) sensor.skip_frames(time = 2000) # Wait for settings take effect. clock = time.clock() # Create a clock object to track the FPS. # You have to turn automatic exposure control and automatic white blance off # otherwise they will change the image exposure to undo any gain settings # that you put in place... sensor.set_auto_exposure(False) sensor.set_auto_whitebal(False) # Need to let the above settings get in... sensor.skip_frames(time = 500) current_gain_in_decibels = sensor.get_gain_db() print("Current Gain == %f db" % current_gain_in_decibels)
sensor.set_pixformat( sensor.GRAYSCALE ) # set camera format to grayscale (color not important in this example) sensor.set_framesize(sensor.QVGA) # set camera resolution to QVGA 320 x 240 sensor.set_auto_whitebal(False) # Turn off white balance sensor.set_auto_exposure( False, exposure_us=user_exposure_time ) # set exposure time, user changable (user_exposure_time variable) #these setting are manually set in order to prevent camera to change it during use (no automatic setting in machine vision!) sensor.set_brightness(0) #set camera brightness sensor.set_contrast(2) #set camera contrast sensor.set_saturation(0) #set camera saturation # Print out the initial gain for comparison. print("Initial gain == %f db" % sensor.get_gain_db()) sensor.skip_frames( time=2000) #small 2 seconds pause, so camera can update its settings # calculating sensor gain # user can change GAIN_SCALE factor in order to obtain more bright image current_gain_in_decibels = sensor.get_gain_db() #current sensor gain print("Current Gain == %f db" % current_gain_in_decibels) #reporting current gain sensor.set_auto_gain(False, gain_db=current_gain_in_decibels * GAIN_SCALE) #new gain print("New gain == %f db" % sensor.get_gain_db()) #reporting new sensor gain sensor.skip_frames( time=2000) #small 2 seconds pause, so camera can update its settings #please note that this secod skip_frames is not an error
GAIN_SCALE = 0.1 #color = [[58, 87, 18, 65, -10, 52]] sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.CIF) sensor.skip_frames(time=2000) clock = time.clock() sensor.set_auto_whitebal(False) sensor.set_auto_exposure(False) sensor.skip_frames(time=500) current_gain_in_decibels = sensor.get_gain_db() sensor.set_auto_gain(False, \ gain_db = current_gain_in_decibels * GAIN_SCALE) def cal(): tres = [0, 0, 0, 0, 0, 0] tres[0] = image.rgb_to_lab(img.get_pixel(177, 144))[0] tres[1] = image.rgb_to_lab(img.get_pixel(177, 144))[0] tres[2] = image.rgb_to_lab(img.get_pixel(177, 144))[1] tres[3] = image.rgb_to_lab(img.get_pixel(177, 144))[1] tres[4] = image.rgb_to_lab(img.get_pixel(177, 144))[2] tres[5] = image.rgb_to_lab(img.get_pixel(177, 144))[2] nub = tres
from pyb import UART pixie = frc_pixie.frc_pixie() can = frc_can.frc_can(1) # Set the configuration for our OpenMV frcCAN device. can.set_config(2, 0, 0, 0) # Set the mode for our OpenMV frcCAN device. can.set_mode(1) sensor.reset() sensor.set_pixformat(sensor.RGB565) # Modify as you like. sensor.set_framesize(sensor.QVGA) # Modify as you like. sensor.skip_frames(time=3000) sensor.set_auto_gain(False, gain_db=int(sensor.get_gain_db() - 50)) sensor.set_auto_whitebal(False) original_exposure = sensor.get_exposure_us() sensor.set_auto_exposure(False, int(0.5 * original_exposure)) clock = time.clock() uart = UART(1, 115200) color = bytearray(3) color[0] = 0x00 color[1] = 0xFF color[2] = 0x80 roi = (0, 0, 332, 190)
# and then use gain control to make up any remaining ground. # We can achieve the above by setting a gain ceiling on the automatic # gain control algorithm. Once this is set the algorithm will have to # increase the exposure time to meet any gain needs versus using gain # to do so. However, this comes at the price of the exposure time varying # more when the lighting changes versus the exposure being constant and # the gain changing. import sensor, image, time sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) # The gain db ceiling maxes out at about 24 db for the OV7725 sensor. sensor.set_auto_gain(True, gain_db_ceiling = 16.0) # Default gain. # Note! If you set the gain ceiling to low without adjusting the exposure control # target value then you'll just get a lot of oscillation from the exposure # control if it's on. sensor.skip_frames(time = 2000) # Wait for settings take effect. clock = time.clock() # Create a clock object to track the FPS. while(True): clock.tick() # Update the FPS clock. img = sensor.snapshot() # Take a picture and return the image. print("FPS %f, Gain %f dB, Exposure %d us" % \ (clock.fps(), sensor.get_gain_db(), sensor.get_exposure_us()))
# pixel using analog and digital multipliers... however, it also amplifies # noise. So, it's best to let the exposure increase as much as possible # and then use gain control to make up any remaining ground. import sensor, image, time # Change this value to adjust the gain. Try 10.0/0/0.1/etc. GAIN_SCALE = 1.0 sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat( sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) # Print out the initial gain for comparison. print("Initial gain == %f db" % sensor.get_gain_db()) sensor.skip_frames(time=2000) # Wait for settings take effect. clock = time.clock() # Create a clock object to track the FPS. # You have to turn automatic exposure control and automatic white blance off # otherwise they will change the image exposure to undo any gain settings # that you put in place... sensor.set_auto_exposure(False) # Need to let the above settings get in... sensor.skip_frames(time=500) current_gain_in_decibels = sensor.get_gain_db() print("Current Gain == %f db" % current_gain_in_decibels) # Auto gain control (AGC) is enabled by default. Calling the below function
# and then use gain control to make up any remaining ground. # We can achieve the above by setting a gain ceiling on the automatic # gain control algorithm. Once this is set the algorithm will have to # increase the exposure time to meet any gain needs versus using gain # to do so. However, this comes at the price of the exposure time varying # more when the lighting changes versus the exposure being constant and # the gain changing. import sensor, image, time sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) # The gain db ceiling maxes out at about 24 db for the OV7725 sensor. sensor.set_auto_gain(True, gain_db_ceiling = 16.0) # Default gain. # Note! If you set the gain ceiling to low without adjusting the exposure control # target value then you'll just get a lot of oscillation from the exposure # control if it's on. sensor.skip_frames(time = 2000) # Wait for settings take effect. clock = time.clock() # Create a clock object to track the FPS. while(True): clock.tick() # Update the FPS clock. img = sensor.snapshot() # Take a picture and return the image. print("FPS %f, Gain %f dB, Exposure %d us" % \ (clock.fps(), sensor.get_gain_db(), sensor.get_exposure_us()))
def findCubeCenter(): """ find the cube roi position """ global roi sensor.set_auto_whitebal(False) sensor.set_contrast(2) cnt = 1 LAB_THRESHOLD = ( (0, 60, -40, 50, -40, 10), # blue (0, 40, -50, 40, -60, 30), # yellow orange red white (0, 50, -40, 15, -25, 70)) #(0, 70, -25, 15, -60, 30)) # green CENTER_THRESHOLD = roi[2] / 3 / 2 gain = 0 while (True): if cnt > 12: sensor.set_auto_gain(gain) img = sensor.snapshot() if cnt % 60 == 0: cnt = 1 gain += 10 if (int(cnt / 24)) % 2 == 1: lab_threshold = LAB_THRESHOLD[int(cnt / 12) - 2] img = img.binary([lab_threshold]) img = img.dilate(2) img = img.erode(2) lcd.display(img) center_roi = list( map(int, [ roi[0] + roi[2] / 2 - CENTER_THRESHOLD * 2, roi[1] + roi[3] / 2 - CENTER_THRESHOLD * 2, CENTER_THRESHOLD * 4, CENTER_THRESHOLD * 4 ])) squares = [] for r in img.find_rects(roi=center_roi, threshold=500): if (isSquare(r)): squares.append(r) img = img.draw_rectangle(r.rect()) for p in r.corners(): img = img.draw_circle(p[0], p[1], 5, color=(0, 255, 0)) lcd.display(img) #time.sleep_ms(5000) if not squares: cnt += 1 print(cnt) else: roi = findCenter(squares, roi, CENTER_THRESHOLD * math.sqrt(2)) center_roi = list( map(int, [ roi[0] + roi[2] / 2 - CENTER_THRESHOLD * 2, roi[1] + roi[3] / 2 - CENTER_THRESHOLD * 2, CENTER_THRESHOLD * 4, CENTER_THRESHOLD * 4 ])) img = img.draw_rectangle(center_roi) img = img.draw_rectangle(roi) lcd.display(img) sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQVGA) sensor.set_auto_whitebal(False) sensor.skip_frames(time=60) gain = sensor.get_gain_db() sensor.set_auto_gain(0, gain) sensor.skip_frames(time=60) sensor.set_auto_exposure(0, 80000) sensor.skip_frames(time=60) return 1