def imagetestplt(thetainput, doubleopponencyinput): """Summary Display function that generates the final output images using MatplotLib windows Args: thetainput (float): a threshold value for perception doubleopponencyinput (bool): A boolean toggle for changing the opponency mode """ theta = thetainput rgcMode = doubleopponencyinput C = retina.sample(img, x, y, coeff[i], loc[i], rgb=True) # CENTRE(sharp retina) S = retina.sample(img, x, y, dcoeff[i], dloc[i], rgb=True) # SURROUND(blurred retina) if rgcMode == 0: pV, nV = rgc.opponency(C, S, theta) else: pV, nV = rgc.doubleopponency(C, S, theta) rIntensity, cIntensity = showNonOpponency(C, theta) # Construct window plots plt.subplot(3, 1, 1), plt.imshow(cv2.cvtColor( img, cv2.COLOR_BGR2RGB)), plt.title('Original test image') plt.xticks([]), plt.yticks([]) plt.subplot(3, 1, 2), plt.imshow( cv2.cvtColor(rIntensity, cv2.COLOR_BGR2RGB)), plt.title( 'Backprojected R+G Intensity Response') plt.xticks([]), plt.yticks([]) plt.subplot(3, 1, 3), plt.imshow( cv2.cvtColor( cIntensity, cv2.COLOR_BGR2RGB)), plt.title('Cortical R+G Intensity Response') plt.xticks([]), plt.yticks([]) # format float to string thetastring = "%.2f" % theta plt.suptitle('Rectified DoG Intensity Images. Threshold:' + thetastring, fontsize=16) plt.show() #Generate backprojected images if showInverse: rOpponent = showBPImg(pV, nV) plt.imshow(cv2.cvtColor(rOpponent, cv2.COLOR_BGR2RGB)), plt.title( 'Backprojected Opponent Cells Output') plt.xticks([]), plt.yticks([]) plt.show() # Cortex if showCortex: cOpponent = showCortexImg(pV, nV) plt.imshow(cv2.cvtColor( cOpponent, cv2.COLOR_BGR2RGB)), plt.title('Cortex Opponent Cells Output') plt.xticks([]), plt.yticks([]) plt.show()
def speedup(loc, coeff, img, rgb, show_res): ''' This test measures the performance of the two implementation from initialisation to the end of the cortical transform ''' init_p = time.time() GI = retina.gauss_norm_img(int(img.shape[1] / 2), int(img.shape[0] / 2), coeff, loc, img.shape, rgb) init_c = time.time() ret = retina_cuda.create_retina( loc, coeff, img.shape, (int(img.shape[1] / 2), int(img.shape[0] / 2))) sample_p = time.time() V_p = retina.sample(img, img.shape[1] / 2, img.shape[0] / 2, coeff, loc, rgb) sample_c = time.time() V_c = ret.sample(img) invert_p = time.time() inv_p = retina.inverse(V_p, img.shape[1] / 2, img.shape[0] / 2, coeff, loc, GI, img.shape, rgb) invert_c = time.time() inv_c = ret.inverse(V_c) retina_end = time.time() cort_init_p = time.time() L, R = cortex.LRsplit(loc) L_loc, R_loc = cortex.cort_map(L, R) L_loc, R_loc, G, cort_size = cortex.cort_prepare(L_loc, R_loc) cort_init_c = time.time() cort = cortex_cuda.create_cortex_from_fields(loc, rgb=rgb) cort_img_p = time.time() l_p, r_p = cortex.cort_img(V_p, L, L_loc, R, R_loc, cort_size, G) cort_img_c = time.time() l_c = cort.cort_image_left(V_c) r_c = cort.cort_image_right(V_c) cort_end = time.time() print '%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,' % (init_c - init_p, sample_p - init_c, sample_c - sample_p, \ invert_p - sample_c, invert_c - invert_p, retina_end - invert_c,\ cort_init_c - cort_init_p, cort_img_p - cort_init_c, cort_img_c - cort_img_p, cort_end - cort_img_c) if show_res: cv2.namedWindow("inverse CUDA", cv2.WINDOW_NORMAL) cv2.imshow("inverse CUDA", inv_c) cv2.namedWindow("inverse Piotr", cv2.WINDOW_NORMAL) cv2.imshow("inverse Piotr", inv_p) c_c = np.concatenate((np.rot90(l_c), np.rot90(r_c, k=3)), axis=1) c_p = np.concatenate((np.rot90(l_p), np.rot90(r_p, k=3)), axis=1) cv2.namedWindow("cortex CUDA", cv2.WINDOW_NORMAL) cv2.imshow("cortex CUDA", c_c) cv2.namedWindow("cortex Piotr", cv2.WINDOW_NORMAL) cv2.imshow("cortex Piotr", c_p)
def showNonOpponency(C, theta): """Summary This function encapsulates the routine to generate backprojected and cortical views for the magnocellular pathway retinal ganglion cells Args: C (vector): The sharp retina is passed to the function theta (float): A threshold value is passed to the function Returns: merged: Return a merged image of the backprojected view as a numpy image array mergecort: Return a merged image of the cortical view as a numpy image array """ GI = retina.gauss_norm_img(x, y, dcoeff[i], dloc[i], imsize=imgsize, rgb=False) # Sample using the other recepetive field, note there is no temporal response with still images S = retina.sample(img, x, y, dcoeff[i], dloc[i], rgb=True) #backproject the imagevectors ncentreV, nsurrV = rgc.nonopponency(C, S, theta) ninverse = retina.inverse(ncentreV, x, y, dcoeff[i], dloc[i], GI, imsize=imgsize, rgb=True) ninv_crop = retina.crop(ninverse, x, y, dloc[i]) ninverse2 = retina.inverse(nsurrV, x, y, dcoeff[i], dloc[i], GI, imsize=imgsize, rgb=True) ninv_crop2 = retina.crop(ninverse2, x, y, dloc[i]) # place descriptive text onto generated images cv2.putText(ninv_crop, "R+G + ", (xx, yy), font, 1, (255, 255, 255), 2) cv2.putText(ninv_crop2, "R+G - ", (xx, yy), font, 1, (255, 255, 255), 2) merged = np.concatenate((ninv_crop, ninv_crop2), axis=1) # create cortical maps of the imagevectors lposnon, rposnon = cortex.cort_img(ncentreV, L, L_loc, R, R_loc, cort_size, G) lnegnon, rnegnon = cortex.cort_img(nsurrV, L, L_loc, R, R_loc, cort_size, G) pos_cort_img = np.concatenate((np.rot90(lposnon), np.rot90(rposnon, k=3)), axis=1) neg_cort_img = np.concatenate((np.rot90(lnegnon), np.rot90(rnegnon, k=3)), axis=1) mergecort = np.concatenate((pos_cort_img, neg_cort_img), axis=1) return merged, mergecort
def imagetest(thetainput, doubleopponencyinput): """Summary Display function that generates the final output images using opencv windows Args: thetainput (float): a threshold value for perception doubleopponencyinput (bool): A boolean toggle for changing the opponency mode """ theta = thetainput rgcMode = doubleopponencyinput C = retina.sample(img, x, y, coeff[i], loc[i], rgb=True) # CENTRE S = retina.sample(img, x, y, dcoeff[i], dloc[i], rgb=True) # SURROUND if rgcMode == 0: pV, nV = rgc.opponency(C, S, theta) else: pV, nV = rgc.doubleopponency(C, S, theta) cv2.namedWindow("Input", cv2.WINDOW_NORMAL) cv2.imshow("Input", img) rIntensity, cIntensity = showNonOpponency(C, theta) cv2.namedWindow("Intensity Responses", cv2.WINDOW_NORMAL) cv2.imshow("Intensity Responses", rIntensity) cv2.namedWindow("Intensity Responses Cortex", cv2.WINDOW_NORMAL) cv2.imshow("Intensity Responses Cortex", cIntensity) cv2.waitKey(0) #Generate backprojected images if showInverse: rOpponent = showBPImg(pV, nV) cv2.namedWindow("Backprojected Opponent Cells Output", cv2.WINDOW_NORMAL) cv2.imshow("Backprojected Opponent Cells Output", rOpponent) cv2.waitKey(0) # Cortex if showCortex: cOpponent = showCortexImg(pV, nV) cv2.namedWindow("Cortex Opponent Cells Output", cv2.WINDOW_NORMAL) cv2.imshow("Cortex Opponent Cells Output", cOpponent) cv2.waitKey(0)
def showNonOpponency(C, theta): """Summary This function encapsulates the routine to generate backprojected and cortical views for the magnocellular pathway retinal ganglion cells Args: C (vector): The sharp retina is passed to the function theta (float): A threshold value is passed to the function Returns: merged: Return a merged image of the backprojected view as a numpy image array mergecort: Return a merged image of the cortical view as a numpy image array """ # Sample using the other recepetive field, but with a temporally different image, lateimg S = retina.sample(lateimg, x, y, dcoeff[i], dloc[i], rgb=True) ncentreV, nsurrV = rgc.nonopponency(C, S, theta) ninverse = retina.inverse(ncentreV, x, y, dcoeff[i], dloc[i], GI, imsize=imgsize, rgb=False) ninv_crop = retina.crop(ninverse, x, y, dloc[i]) ninverse2 = retina.inverse(nsurrV, x, y, dcoeff[i], dloc[i], GI, imsize=imgsize, rgb=False) ninv_crop2 = retina.crop(ninverse2, x, y, dloc[i]) merged = np.concatenate((ninv_crop, ninv_crop2), axis=1) lposnon, rposnon = cortex.cort_img(ncentreV, L, L_loc, R, R_loc, cort_size, G) lnegnon, rnegnon = cortex.cort_img(nsurrV, L, L_loc, R, R_loc, cort_size, G) pos_cort_img = np.concatenate((np.rot90(lposnon), np.rot90(rposnon, k=3)), axis=1) neg_cort_img = np.concatenate((np.rot90(lnegnon), np.rot90(rnegnon, k=3)), axis=1) mergecort = np.concatenate((pos_cort_img, neg_cort_img), axis=1) return merged, mergecort
def preview(): stdimg_dir = os.getcwd() + os.sep + 'testimage\\' print "Using " + os.listdir(stdimg_dir)[0] name = os.listdir(stdimg_dir)[0] standard_image = cv2.imread(stdimg_dir + name, ) img = cv2.normalize(standard_image.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX) img = cv2.cvtColor(standard_image, cv2.COLOR_BGR2GRAY) x, y = img.shape[1] / 2, img.shape[0] / 2 size = img.shape oz_V = retina.sample(img, x, y, ozimek_coeff, ozimek_loc, rgb=False) oz_GI = retina.gauss_norm_img(x, y, ozimek_coeff, ozimek_loc, imsize=size) oz_I = retina.inverse(oz_V, x, y, ozimek_coeff, ozimek_loc, oz_GI, imsize=size, rgb=False) oz_I_crop = retina.crop(oz_I, x, y, ozimek_loc) oz_GI_crop = retina.crop(oz_GI, x, y, ozimek_loc) # test application of retinal receptive field plt.figure(figsize=(6, 6), num="Test application of retinal field") plt.axis('off') plt.imshow(oz_I_crop, cmap='gray') plt.show() #heatmap of retinal receptive field plt.figure(figsize=(6, 6), num="Heatmap of retina") plt.axis('off') plt.imshow(oz_GI_crop, cmap='RdBu') plt.show()
# repeat for every new frame while True: ret, img = cap.read() ret, lateimg = cap.read() # for temporal responses if ret is True: # get image frame properties x = int(img.shape[1] / 2) y = int(img.shape[0] / 2) imgsize = (img.shape[0], img.shape[1]) theta = cv2.getTrackbarPos('theta', 'Input') / 100.0 rgcMode = cv2.getTrackbarPos(switch, 'Input') # sample images C = retina.sample(img, x, y, coeff[i], loc[i], rgb=True) # CENTRE(sharp retina) S = retina.sample(img, x, y, dcoeff[i], dloc[i], rgb=True) # SURROUND(blurred retina) # generate rectified imagevectors based on the type of opponency if rgcMode == 0: pV, nV = rgc.opponency(C, S, theta) else: pV, nV = rgc.doubleopponency(C, S, theta) # Display functions are called cv2.imshow("Input", img) rIntensity, cIntensity = showNonOpponency(C, theta) cv2.imshow("Intensity Responses", rIntensity) cv2.namedWindow("Intensity Responses Cortex", cv2.WINDOW_NORMAL)
def correctness_test(loc, coeff, cap, rgb=False): ''' CUDA code uses the minimal initialisation from the host, all tracatable values are computed on the GPU Get an image from the camera, generate inverse and cortical image with both implementation and subtract the results ''' r, img = cap.read() if not rgb: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # create CUDA objects to pass to evaluation ret = retina_cuda.create_retina( loc, coeff, img.shape, (int(img.shape[1] / 2), int(img.shape[0] / 2)), None) cort = cortex_cuda.create_cortex_from_fields(loc, rgb=rgb) while ord('q') != cv2.waitKey(10): r, img = cap.read() if not rgb: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if r: ''' Sample the image img with CUDA retina ret, inverse transform it with ret and create the cortical image with CUDA cortex cort Sample and generate retina and cortical images from img with Piotrs's code Visually compare the results by showing the subtraction of the generatd images ''' V_c = ret.sample(img) # sample with CUDA inv_c = ret.inverse(V_c) # inverse with CUDA l_c = cort.cort_image_left(V_c) # left cortical image CUDA r_c = cort.cort_image_right(V_c) # right cortical image CUDA c_c = np.concatenate( (np.rot90(l_c), np.rot90(r_c, k=3)), axis=1) #concatenate the results into one image # create Piotr's retina and cortical images GI = retina.gauss_norm_img(int(img.shape[1] / 2), int(img.shape[0] / 2), coeff, loc, img.shape, rgb) L, R = cortex.LRsplit(loc) L_loc, R_loc = cortex.cort_map(L, R) L_loc, R_loc, G, cort_size = cortex.cort_prepare(L_loc, R_loc) V_p = retina.sample(img, img.shape[1] / 2, img.shape[0] / 2, coeff, loc, rgb) inv_p = retina.inverse(V_p, img.shape[1] / 2, img.shape[0] / 2, coeff, loc, GI, img.shape, rgb) l_p, r_p = cortex.cort_img(V_p, L, L_loc, R, R_loc, cort_size, G) c_p = np.concatenate((np.rot90( l_p[:l_c.shape[0], :]), np.rot90(r_p[:r_c.shape[0], :], k=3)), axis=1) # show CUDA results cv2.namedWindow("inverse CUDA", cv2.WINDOW_NORMAL) cv2.imshow("inverse CUDA", inv_c) cv2.namedWindow("cortex CUDA", cv2.WINDOW_NORMAL) cv2.imshow("cortex CUDA", c_c) # show Piotr's results cv2.namedWindow("inverse Piotr", cv2.WINDOW_NORMAL) cv2.imshow("inverse Piotr", inv_p) cv2.namedWindow("cortex Piotr", cv2.WINDOW_NORMAL) cv2.imshow("cortex Piotr", c_p) # show the difference of the images cv2.namedWindow("inverse diff", cv2.WINDOW_NORMAL) cv2.imshow("inverse diff", np.power((inv_c - inv_p), 2) * 255) cv2.namedWindow("cortex diff", cv2.WINDOW_NORMAL) cv2.imshow("cortex diff", np.power((c_c - c_p), 2) * 255)
# repeat for every new frame while True: ret, img = cap.read() ret, lateimg = cap.read() if ret is True: # get image frame properties x = int(img.shape[1] / 2) y = int(img.shape[0] / 2) imgsize = (img.shape[0], img.shape[1]) theta = cv2.getTrackbarPos('theta', 'Input') / 100.0 rgcMode = cv2.getTrackbarPos(switch, 'Input') # get index of the species type t = cv2.getTrackbarPos(species, 'Input') # sample images C = retina.sample(img, x, y, coeff[i], loc[i], rgb=True) # CENTRE S = retina.sample(img, x, y, dcoeff[i], dloc[i], rgb=True) # SURROUND # generate rectified imagevectors based on the type of opponency if rgcMode == 0: pV, nV = rgc.opponency(C, S, theta) else: pV, nV = rgc.doubleopponency(C, S, theta) # Display functions cv2.imshow("Input", img) rIntensity, cIntensity = showNonOpponency(C, theta) cv2.imshow("Intensity Responses", rIntensity) cv2.namedWindow("Intensity Responses Cortex", cv2.WINDOW_NORMAL) cv2.imshow("Intensity Responses Cortex", cIntensity)