コード例 #1
0
def preparePatches(pad, pxCount):
    """
    generates pixel patches for each of the pixels in each of the images already read
    :param pad: determines the size of the pixel patch, where patch width=((2*pad)+1)
    :param pxCount: the number of pixels to use per image when training and testing
    """
    
    train_input_patches[:]=[]
    train_labels_pixels[:]=[]
    test_input_patches[:]=[]
    test_labels_pixels[:]=[]
    #creates pixel patches for training set
    for i in range(len(train_input)):
        for j in range(pxCount):
            x=random.randint(0,1023)
            y=random.randint(0,1023)
            train_labels_pixels.append(train_labels[i][x][y])
            train_input[i]=border.createBorder(pad,train_input[i])
            ppatch=border.pixelPatch(pad, train_input[i],x,y).flatten()
            #ppatch=test.noisyPatch(5,train_labels[i],x,y).flatten() #creates patches with random noise isntead, for testing purposes
            train_input_patches.append(ppatch)
                  
    
    #creates pixel patches for test set
    for i in range(len(test_input)):
        for j in range(pxCount):
            x=random.randint(0,1023)
            y=random.randint(0,1023)
            test_labels_pixels.append(test_labels[i][x][y])
            test_input[i]=border.createBorder(pad,test_input[i])
            ppatch=border.pixelPatch(pad, test_input[i],x,y).flatten()
            #ppatch=test.noisyPatch(5,test_labels[i],x,y).flatten() #creates patches with random noise instead, for testing purposes
            test_input_patches.append(ppatch)
コード例 #2
0
def sortClasses(pad, num_points, data, seed=None, equalClasses=True):
    """
    sorts the data into an equal number of white and black pixels
    :param pad: defines the pixel patch size, where the side length = 2*pad+1
    :param num_points: defines the number of points to use per image
    :param data: is the list of [input images, label images]
    :param seed: defines the seed to use for the random number generator
    :return: a list of [pixel patch data matrix, label points]
    """
    if not seed == None:
        random.seed(seed)
    input = data[0]
    label = data[1]
    merged_input=np.zeros(shape=((num_points)*len(input),(2*pad+1)**2))
    merged_label=np.zeros(shape=((num_points)*len(input)))
    x=0
    if equalClasses:
        for img in range(len(input)):
            membrane_indices = np.nonzero(label[img])
            nonmembrane_indices = np.nonzero(255-label[img])
            bordered=border.createBorder(pad, input[img])
            white=True
            
            for i in range(num_points):
                if white:
                    randindex=random.randint(0,len(membrane_indices[0])-1)
                    merged_input[x]=(border.pixelPatch(pad, bordered, membrane_indices[0][randindex], membrane_indices[1][randindex]).flatten())
                    merged_label[x]=(label[img][membrane_indices[0][randindex]][membrane_indices[1][randindex]])
                elif not white:
                    randindex=random.randint(0,len(nonmembrane_indices[0])-1)
                    merged_input[x]=(border.pixelPatch(pad, bordered, nonmembrane_indices[0][randindex], nonmembrane_indices[1][randindex]).flatten())
                    merged_label[x]=(label[img][nonmembrane_indices[0][randindex]][nonmembrane_indices[1][randindex]]) 
                white = not white
                x+=1
    else:
        for img in range(len(input)):
            bordered=border.createBorder(pad, input[img])
            for i in range(num_points):
                randx=random.randint(0,len(input[img])-255)
                randy=random.randint(0,len(input[img][0])-255)
                merged_input[x]=border.pixelPatch(pad,bordered,randx,randy).flatten()
                merged_label[x]=label[img][randx][randy]
                x+=1
    return merged_input, merged_label
コード例 #3
0
#generates 100 random pixel coordinates to use from each image when testing/training/validating
for i in range(100):
    x_coord.append(random.randint(0,1023))
    y_coord.append(random.randint(0,1023))

for i in range(209):
    train_input.append(mahotas.imread("input/train/"+str(i)+".tif"))
    train_labels.append(mahotas.imread("labels/train/"+str(i)+".tif"))

#creates pixel patches for training set
for i in range(len(train_input)):
    for j in range(len(x_coord)):
        train_labels_pixels.append(train_labels[i][x_coord[j]][y_coord[j]])
        train_input[i]=border.createBorder(pad,train_input[i])
        ppatch=border.pixelPatch(pad, train_input[i], x_coord[j], y_coord[j]).flatten()
        train_input_patches.append(ppatch)
              
print 1
knn = sklearn.neighbors.KNeighborsClassifier(n_neighbors=k, weights='uniform')
print 2     
#fit the data
knn.fit(train_input_patches, train_labels_pixels)
print 3
img_patches=border.outputMatrix(pad, img)
print 4
predicted_img_flat = knn.predict(img_patches)
print 5
predicted_img = np.zeros(shape=(1024,1024), dtype=np.double)
print 6
x=0