def rgb2gray(rgbFram): #grayFram = rgbFram #sensor.set_pixformat(sensor.GRAYSCALE) img_gray = sensor.snapshot() for x in range(0, rgbFram.width()): for y in range(0, rgbFram.height()): rgb = rgbFram.get_pixel(x, y) gray = image.rgb_to_grayscale(rgb) img_gray.set_pixel(x, y, gray) #grayFram[x,y] = gray a = img_gray.get_pixel(10, 10) print(a) return img_gray
img.draw_rectangle(blob.rect()) for blob in img.find_blobs([yellow_thresholds], area_threshold=150, merge=False): if(max(blob.w(), blob.h())/min(blob.w()+blob.w(), blob.h())<1.5 and blob.cx()+(blob.w()/2)<ImageX and blob.cx()-(blob.w()/2)>0 and blob.cy()-(blob.h()/2)>0 and blob.cy()+(blob.h()/2)<ImageY and math.sqrt(((blob.cx()-(ImageX/2))*(blob.cx()-(ImageX/2)))+((blob.cy()-(ImageY/2))*(blob.cy()-(ImageY/2))))<(ImageY/3) and img.get_statistics(roi=blob.rect()).stdev()<30): yellow_blobfound=True img.draw_rectangle(blob.rect()) for blob in img.find_blobs([green_thresholds], area_threshold=150, merge=False): if(max(blob.w(), blob.h())/min(blob.w()+blob.w(), blob.h())<1.5 and blob.cx()+(blob.w()/2)<ImageX and blob.cx()-(blob.w()/2)>0 and blob.cy()-(blob.h()/2)>0 and blob.cy()+(blob.h()/2)<ImageY and math.sqrt(((blob.cx()-(ImageX/2))*(blob.cx()-(ImageX/2)))+((blob.cy()-(ImageY/2))*(blob.cy()-(ImageY/2))))<(ImageY/3) and img.get_statistics(roi=blob.rect()).stdev()<30): green_blobfound=True img.draw_rectangle(blob.rect()) img.cartoon(size=1, seed_threshold=1) img.binary([letter_thresholds], zero=True) letter_blobfound=False for blob in img.find_blobs([letter_thresholds], area_threshold=150, merge=False): if(max(blob.w(), blob.h())/min(blob.w()+blob.w(), blob.h())<1.5 and blob.cx()+(blob.w()/2)<ImageX and blob.cx()-(blob.w()/2)>0 and blob.cy()-(blob.h()/2)>0 and blob.cy()+(blob.h()/2)<ImageY and math.sqrt(((blob.cx()-(ImageX/2))*(blob.cx()-(ImageX/2)))+((blob.cy()-(ImageY/2))*(blob.cy()-(ImageY/2))))<(ImageY/3) and img.get_statistics(roi=blob.rect()).stdev()>30): letter_blobfound=True SimilarityH = 255-((abs(image.rgb_to_grayscale(img.get_pixel(blob.cx(), blob.y()))-blobHtop)+abs(image.rgb_to_grayscale(img.get_pixel(blob.x(), blob.cy()))-blobHleft)+abs(image.rgb_to_grayscale(img.get_pixel(blob.x(), blob.y()))-blobHtopleft)+abs(image.rgb_to_grayscale(img.get_pixel(blob.cx(), blob.cy()))-blobHcenter))/4) SimilarityS = 255-((abs(image.rgb_to_grayscale(img.get_pixel(blob.cx(), blob.y()))-blobStop)+abs(image.rgb_to_grayscale(img.get_pixel(blob.x(), blob.cy()))-blobSleft)+abs(image.rgb_to_grayscale(img.get_pixel(blob.x(), blob.y()))-blobStopleft)+abs(image.rgb_to_grayscale(img.get_pixel(blob.cx(), blob.cy()))-blobScenter))/4) SimilarityU = 255-((abs(image.rgb_to_grayscale(img.get_pixel(blob.cx(), blob.y()))-blobUtop)+abs(image.rgb_to_grayscale(img.get_pixel(blob.x(), blob.cy()))-blobUleft)+abs(image.rgb_to_grayscale(img.get_pixel(blob.x(), blob.y()))-blobUtopleft)+abs(image.rgb_to_grayscale(img.get_pixel(blob.cx(), blob.cy()))-blobUcenter))/4) img.draw_rectangle(blob.rect()) if(red_blobfound==True): print('Red') uart.write('R') elif(yellow_blobfound==True): print('Yellow') uart.write('Y') elif(green_blobfound==True): print('Green') uart.write('G') elif(letter_blobfound==True): if(SimilarityS>=SimilarityH and SimilarityS>=SimilarityU): print('S')
def unittest(data_path, temp_path): import image gs = image.rgb_to_grayscale((120, 200, 120)) return (gs == 182)
REGRESSION_GRAY = False REGRESSION_SEGMENTED = True # Setting upp thresholds function # find_line_segments instead of get_line_regression? while True: clock.tick() fps = clock.fps() img = sensor.snapshot().histeq() if REGRESSION_COLOR: line = img.get_regression((COLOR_THRESHOLD), pixels_threshold = PIXELS_THRESHOLD, area_threshold = AREA_THRESHOLD, robust = True, roi = LANE_ROI) elif REGRESSION_GRAY: line = image.rgb_to_grayscale(img).get_regression((GRAYSCALE_THRESHOLDS), pixels_threshold = PIXELS_THRESHOLD, area_threshold = AREA_THRESHOLD, robust = True, roi = LANE_ROI) elif REGRESSION_SEGMENTED: Lines = [] for l in img.find_line_segments(merge_distance=0 max_theta_difference=15): img.draw_line(l.line(), color=(255, 0, 0), thickness=2) parameters = np.polyfit((l.line(x1),l.line(x2)), (l.line(y1),l.line(y2)), 1) slope = parameters[0] intercept = parameters[1] Lines.append((slope, intercept)) LinesAverage = np.average(Lines, axis=0) # slope, intercept = l.line() # # y1 = IMG_HEIGHT # # y2 = int(IMG_HEIGHT*(3.25/5)) # x1 = int((IMG_HEIGHT-intercept)/slope) # x2 = int((0-intercept)/slope)
def unittest(data_path, temp_path): import image gs = image.rgb_to_grayscale((120, 200, 120)) return (gs == 182)
#time.sleep(100) #roi = (10,10,10,10) #thres = (49,174) while (True): #clock.tick() #img_gray = sensor.snapshot() #a = img.get_pixel(10,10) #i = rgb2gray(img) #time.sleep(100) #img.draw_rectangle(roi) #img = image.Image(path,copy_to_fb = True) #a = img.get_pixel(10,10) #b = image.rgb_to_grayscale(a) #bloc = img.find_blobs(thres) #roi = blob.rect() #img.draw_rectangle(roi) #a = img.width() for i in range(0, img.width()): for j in range(0, img.height()): a = img.get_pixel(i, j) g = image.rgb_to_grayscale(a) img_gray.set_pixel(i, j, g) iii = image.copy(img_gray) b = img_gray.get_pixel(10, 10) print(b)