Ejemplo n.º 1
0
def estimate_svm(textlines):
    svc = LinearSVC(C=10, random_state=1, class_weight={1:0.35})
    
    data = []
    for line in textlines:

        dat = np.r_[line.in_word_distances, line.between_word_distances]
        if dat.shape[0] < 2:
            continue

        _, _, centroids = cv2.kmeans(data=np.asarray([dat]).transpose().astype(np.float32), K=2, bestLabels=None,
            criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001), attempts=5, 
            flags=cv2.KMEANS_PP_CENTERS) 

        diff = abs(centroids[0] - centroids[1])

        if line.n_words == 1:
            # single word
            data.append([1] + [diff / np.mean(line.heights), diff / (np.median(dat) + 1e-10)])
            continue

        #multi word
        data.append([-1] + [diff / np.mean(line.heights), diff / (np.median(dat) + 1e-10)])

        if len(line.in_word_distances) < 2:
            continue
        # create an artificial single word
        _, _, centroids = cv2.kmeans(data=np.asarray([line.in_word_distances]).transpose().astype(np.float32), K=2, bestLabels=None,
            criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001), attempts=5, 
            flags=cv2.KMEANS_PP_CENTERS) 
        diff = abs(centroids[0] - centroids[1])
        data.append([1] + [diff / np.mean(line.heights), diff / (np.median(line.in_word_distances) + 1e-10)])
    data = np.array(data)
    svc.fit(data[:,1:], data[:,0])
    return svc
Ejemplo n.º 2
0
def mergeCenters(nCenters):
	"""
	This function loads the cluster centers from ./Centers/ 
	directory and stores final centers in centerFinal.p file 
	in current directory. The centers are clubed together in 
	single numpy array and then kmeans is applied over the 
	clubed centers.
	"""
	path = os.getcwd()
	os.chdir('Centers/')
	center = np.zeros((0,128))		#: Populator for centers

	for i in os.listdir(os.getcwd()):
	    Center = open(i,"rb")		#: File pointer for centers file
	    center = np.vstack((center, pickle.load(Center)))	#Populate centers
	    Center.close()

	center = np.float32(center)
	criteria = (cv2.TERM_CRITERIA_MAX_ITER, 10,0.0001)
	#Checking version of opencv..
	if __verison__[0] == '3':
		ret,label,center=cv2.kmeans(center,int(nCenters),None,criteria,50,cv2.KMEANS_PP_CENTERS)
	else:
		ret,label,center=cv2.kmeans(center,int(nCenters),criteria,50,cv2.KMEANS_PP_CENTERS)

	CenterFinal = open(path+'/centerFinal.p',"wb")#: File pointer for final centers file
	pickle.dump(center, CenterFinal)	#Dump centers to file
	CenterFinal.close()
Ejemplo n.º 3
0
def kmeans(K, inputText):
	inputText.destroy()
	global orgImg

	t0 = time.time()
	# color quantization
	orgImg = cv2.cvtColor(orgImg, cv2.COLOR_BGR2LAB)
	orgImg = orgImg.reshape((orgImg.shape[0] * orgImg.shape[1], 3))
	clt = MiniBatchKMeans(n_clusters = 8)
	labels = clt.fit_predict(orgImg)
	quant = clt.cluster_centers_.astype("uint8")[labels]

	quant = quant.reshape((h, w, 3))
	orgImg = orgImg.reshape((h, w, 3))

	quant = cv2.cvtColor(quant, cv2.COLOR_LAB2RGB)
	orgImg = cv2.cvtColor(orgImg, cv2.COLOR_LAB2RGB)

	cv2.imshow("orgImg", np.hstack([orgImg, quant]))
	#cv2.waitkey(0)
	#orgImg = np.hstack([orgImg, quant])
	quant = cv2.cvtColor(quant, cv2.COLOR_RGB2BGR)
	orgImg = quant
	print(time.time() - t0)
	
	# kmeans algorithm
	img = orgImg
	height, width, channels = img.shape
	cv2.imwrite('curr_img.jpg', img)

	z = img.reshape((-1, 3))
	z = np.float32(z)

	criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
	min_sse = sys.maxint
	min_k = 100
	print("Pre-Optimal value of K: %d" % min_k)
	for i in range(1, 16):
		ret,pixel,center = cv2.kmeans(z, i, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
		if ret < min_sse:
			min_k = i
			min_sse = ret
	print("Optimal value of K: %d" % min_k)

	ret, pixel, center = cv2.kmeans(z, min_k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

	global coordinates
	coordinates = [[] for _ in range(len(center))]
	for i in range(len(pixel)):
		coordinates[pixel[i][0]].append([i%width, int(i/width)])

	center2 = sorted(center, key=step, reverse=True)

	indices = []
	for i in range(len(center2)):
		for j in range(len(center)):
			if list(center[j]) == list(center2[i]):
				indices.append(j)
	generateUI(center2, pixel, indices)
Ejemplo n.º 4
0
def kmeans(samples, nclusters, max_iter=100, attempts=20):
    criteria = (cv2.TERM_CRITERIA_MAX_ITER, max_iter, 1.0)
    flags = cv2.KMEANS_PP_CENTERS

    if context.OPENCV3:
        return cv2.kmeans(samples, nclusters, None, criteria, attempts, flags)
    else:
        return cv2.kmeans(samples, nclusters, criteria, attempts, flags)
Ejemplo n.º 5
0
def kMeans(nCenters):
	"""
	This function takes descriptors stored in ./Desc/ 
	directory one file at time. Loads the first half 
	and the runs kmeans from cv2 library to find out 
	cluster centers. Similary it does for the other half. 
	Finally both the cluster centers are merged and again 
	runs kmeans to find final cluster centers for that 
	particular object descriptors.

	@param nCenters: Number of cluster centers.
	"""
	path = os.getcwd()

	#Create directory Center if it does not exists
	if not os.path.exists('Centers'):
	    os.makedirs('Centers')

	os.chdir('Desc/')

	criteria = (cv2.TERM_CRITERIA_MAX_ITER, 10, 0.0001)

	for i in os.listdir(os.getcwd()):
	    Desc = open(i,"rb") #: File pointer for descriptor file
	    center = np.zeros((0,128))	#: Populator for cluster centers

	    while 1:
	        try:                    
	            des = pickle.load(Desc)		#: Read descriptor into des(numpy array)
	            print(np.shape(des))

	            #Checking the version of opencv..
	            if __version__[0] == '3':
	            	ret,label,center1=cv2.kmeans(des,int(nCenters),None,criteria,50,cv2.KMEANS_PP_CENTERS)
	            else:
	            	ret,label,center1=cv2.kmeans(des,int(nCenters),criteria,50,cv2.KMEANS_PP_CENTERS)
	            del des
	            center = np.vstack((center,center1))	#: Append cluster centers
	            print(np.shape(center))
	        except EOFError:
	            break		#: Detect End of file and break while loop
	    
	    del center1
	    des = np.float32(center)	#Convert to float, required by kmeans
	    print(np.shape(des))
	    
	    #Checking the version of opencv..
	    if __version__[0] == '3':
	    	ret,label,center1=cv2.kmeans(des,int(nCenters),None,criteria,50,cv2.KMEANS_PP_CENTERS)
	    else:
	        ret,label,center1=cv2.kmeans(des,int(nCenters),criteria,50,cv2.KMEANS_PP_CENTERS)

	    Center = open(path+"/Centers/"+i,"wb")	#: File pointer for centers file
	    pickle.dump(center,Center)  #: Save cluster centers to file
	    Center.close()
	    Desc.close()
	del path
Ejemplo n.º 6
0
def cluster_resources_for_host(host, rindex):
    # find all resources needed by all pages under the host
    host_ruris = set(
        rl[0]
        for page in host.pages
        for rl in page.resource_loads
    )

    host_resources = [rindex[ruri] for ruri in host_ruris]
    fvs = [r.get_fv_for_host(host) for r in host_resources]

    kmeans_criteria = (cv2.TERM_CRITERIA_MAX_ITER, 100, 0) # 100 iterations

    # top-level clustering
    if len(fvs) < K:
        return None

    top_distortion, top_clusters, top_means = cv2.kmeans(
        np.array(fvs, dtype = 'float32'),
        K = K,
        criteria = kmeans_criteria,
        attempts = 20,
        flags = cv2.KMEANS_RANDOM_CENTERS)

    clusters = build_clusters(top_means, top_clusters, host_resources)

    # subclusters
    subclusters = []
    for mean, cluster_resources in clusters:
        fvs = [r.get_fv_for_host(host) for r in cluster_resources]

        sub_distortion, sub_clusters, sub_means = cv2.kmeans(
            np.array(fvs, dtype = 'float32'),
            K = SUBK if SUBK < len(fvs) else 1,
            criteria = kmeans_criteria,
            attempts = 20,
            flags = cv2.KMEANS_RANDOM_CENTERS)

        subclusters.append(
            build_clusters(sub_means, sub_clusters, cluster_resources)
        )

    host.clusters = clusters
    host.subclusters = subclusters

    # print clusters and subclusters for debugging
    for i, (mean, resources) in enumerate(host.clusters):
        print '{} {} {}:'.format(i, mean, len(resources)),
        for mean, resources in host.subclusters[i]:
            print len(resources),
        print

    return top_distortion
Ejemplo n.º 7
0
	def quantize(self, img):
		Z = img.reshape((-1, 3))
		Z = np.float32(Z)

		criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
		K = 3

		if cv2.__version__.find("2.4.6") > -1:
			ret, label, center = cv2.kmeans(Z, K, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
		else:
			ret, label, center = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

		dst = np.uint8(center)
		dst = dst[label.flatten()]
		return dst.reshape((img.shape))
Ejemplo n.º 8
0
def Color_Features_Extract(img_folder):
    print "Color_Features_Extract Start"
    starttime = datetime.datetime.now()

    back = np.array([255,128,128])

    image_num = len(os.listdir(seg_img_folder))

    Color_Features = []

    for index, image_name in enumerate(os.listdir(img_folder)):
        image_path = img_folder + str("/") +image_name
        image = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2LAB)
        rows, columns, lab = image.shape

        # Make densely-sampling color features
        pixel_index = 0

        for x in range(rows):
            for y in range(columns):
                if pixel_index % 9 == 0 and np.array_equal(image[x][y],back) == False:
                    Color_Features.append(image[x][y].tolist())
                pixel_index += 1

    # Get CodeBook of Color_Features
    Color_Features = np.float32(Color_Features)

    # Define criteria = ( type, max_iter = 10 , epsilon = 1.0 )
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)

    # Set flags (Just to avoid line break in the code)
    flags = cv2.KMEANS_RANDOM_CENTERS

    # Apply KMeans
    compactness,labels,centers = cv2.kmeans(Color_Features,800,None,criteria,10,flags)

    Image_Color_Features = [[0 for x in range(800)] for y in range(image_num)]

    color_index = 0

    for image_index, image_name in enumerate(os.listdir(img_folder)):
        image_path = img_folder + str("/") +image_name
        image = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2LAB)
        rows, columns, lab = image.shape

        pixel_index = 0

        for x in range(rows):
            for y in range(columns):
                if pixel_index % 9 == 0 and np.array_equal(image[x][y],back) == False:
                    Image_Color_Features[image_index][labels[color_index]] += 1
                    color_index += 1
                pixel_index += 1
        print image_name

    endtime = datetime.datetime.now()
    print "Time: " + str((endtime - starttime).seconds) + "s"
    print "Color_Features_Extract End"

    return Image_Color_Features
Ejemplo n.º 9
0
    def __predictBB(self, bb, old_pts, new_pts):
        pts = []
        for kp in new_pts:
            pts.append((kp.pt[0], kp.pt[1]))
            cv2.circle(self.kmeans_img, (int(kp.pt[0]), int(kp.pt[1])), 3, (0, 255, 255), -1)

        np_pts = np.asarray(pts)
        t, pts, new_center = cv2.kmeans(np.asarray(np_pts, dtype=np.float32), K=1, bestLabels=None,
                            criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 1, 10), attempts=1, 
                            flags=cv2.KMEANS_RANDOM_CENTERS)
        print new_center

        cv2.circle(self.kmeans_img, (new_center[0][0], new_center[0][1]), 8, (0, 0, 255), 2)
        
        max_x = int(max(np_pts[:, 0]))
        min_x = int(min(np_pts[:, 0]))
        max_y = int(max(np_pts[:, 1]))
        min_y = int(min(np_pts[:, 1]))
        
        rad = ((new_center[0][0]-max_x)**2+(new_center[0][1]-max_y)**2)**0.5
        self.pos.append((new_center[0][0], new_center[0][1]))
        cv2.circle(self.kmeans_img, (new_center[0][0], new_center[0][1]), int(rad) , (0, 0, 255), 2)
        cv2.imshow("K-Means", self.kmeans_img)
        new_bb =  (min_x-5, min_y-5, max_x-min_x+5, max_y-min_y+5)
        print new_bb, new_center
        #new_center[0][0] += new_bb[0]
        #new_center[0][1] += new_bb[1]
        self._setKalman(new_center[0][0], new_center[0][1], self.predict_pt[0], self.predict_pt[1])
        self._predictKalman()
        self._changeMeasure(new_center[0][0], new_center[0][1])
        self._correctKalman()
        print self.state_pt, self.predict_pt
        
        return new_bb, new_center
Ejemplo n.º 10
0
def kmeans_quantization(img, n_clusters):
    """ Quantizises the image into a given number of clusters (n_clusters). 
        This can work with images (4 channel) that have an alpha channel, this gets ignored,
        but it will "spend" one cluster for that 
    """
    has_mask = img.shape[2] == 4

    color = img[:, :, 0:3] if has_mask else img
    lab = cv2.cvtColor(color, cv2.COLOR_BGR2LAB)    # Convert to lab for better perceptual accuracy
    lab = lab.reshape((color.shape[0] * color.shape[1], 3))

    flab = np.float32(lab)

    if has_mask:
        mask = img[:, :, 3]
        mask = mask.reshape((mask.shape[0] * mask.shape[1], 1))
        flab[np.where(mask == 0)[0]] = (-255, -255, -255)  # Mask off alpha areas

    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)

    ret, labels, centers = cv2.kmeans(flab, n_clusters, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

    exclude_idx = np.where(centers[:, -1] < 0)[0]
    center = np.uint8(centers)

    res = center[labels.flatten()]

    hist = centroid_histogram(labels)
    hist[exclude_idx] = 0.0

    dominant = center[np.argmax(hist)]

    return dominant, hist, res
Ejemplo n.º 11
0
def unsupervised_classification_opencv(input_band_list,n_classes,n_iterations):
    
    '''Unsupervised K-Means classification using OpenCV library.
    
    Tool used to recall the K-Means unsupervised classification algorithm implemented by the OpenCV library. Main difference in respect of the Orfeo Toolbox implementation is in the input type
    (matrices instead of a tiff file)

    Example: unsupervised_classification_opencv(input_band_list=(band1,band2,band3,band4),n_classes=5,n_iterations=10)

    :param input_band_list: list of 2darrays corresponding to bands (band 1: blue) (list of numpy arrays)
    :param n_classes: number of classes to extract (integer)
    :param n_iterations: number of iterations of the classifier (integer)
    :returns:  an output 2darray is created with the results of the classifier
    :raises: AttributeError, KeyError
    
    Author: Daniele De Vecchi - Mostapha Harb
    Last modified: 20/03/2014
    '''
    
    img = np.dstack((input_band_list[0],input_band_list[1],input_band_list[2],input_band_list[3])) #stack the 4 bands together
    Z = img.reshape((-1,4)) #reshape for the classifier
    
    Z = np.float32(Z) #convert to np.float32
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, n_iterations, 0.0001) #definition of the criteria
    ret,label,center=cv2.kmeans(Z,n_classes,criteria,n_iterations,cv2.KMEANS_RANDOM_CENTERS) #kmeans classification
    center = np.uint8(center) 
    res = center[label.flatten()]
    res2 = res[:,0] #extraction of the desired row
    output_array = res2.reshape(input_band_list[0].shape) #reshape to original raster dimensions
    
    return output_array
Ejemplo n.º 12
0
def predict_textline(svc, textline):
    data = np.r_[textline.in_word_distances, textline.between_word_distances]

    if len(data) <= 1:
        return 1 # single word
    _, _, centroids = cv2.kmeans(data=np.asarray([data]).transpose().astype(np.float32), 
            K=2, bestLabels=None,
        criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 100, 0.01), 
        attempts=5, 
        flags=cv2.KMEANS_PP_CENTERS) 
    diff = abs(centroids[0] - centroids[1])
    v = np.r_[diff / np.mean(textline.heights), diff / np.median(np.r_[textline.in_word_distances,
        textline.between_word_distances])]
    label = svc.predict(v)
    if label == 1:
        return 1 # single word

    # multi-word
    max_centroid = max(centroids[0], centroids[1])
    min_centroid = min(centroids[0], centroids[1])

    results = []
    for x in data:
        if abs(max_centroid - x) < abs(min_centroid - x):
            results.append(True)
        else:
            results.append(False)
    return results
Ejemplo n.º 13
0
def clustering2(boxes, k, direction):
    """
    box のリストをうけとり、それらをk個に分類して返す
    分類にはk-means法を使う
    クラスタの重心からの距離は1次元ではかる
      縦書きなら、y 軸方向の距離
      横書きなら、x 軸方向の距離
    :param boxes: list of boxes
    :param k: demanded number for output clusters
    :param direction: 0 or 1 : 0 = 縦書き, 1 = 横書き
    :return: list of list of boxes
      この外側のlistの要素数はk となる
    """
    if direction == 0:
        data = [box[1]+box[3]/2 for box in boxes]
    else:
        data = [box[0]+box[2]/2 for box in boxes]
    data = np.float32(np.array(data))

    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    flags = cv2.KMEANS_RANDOM_CENTERS
    compactness, labels, centers = cv2.kmeans(data, k, None, criteria, 10, flags)

    ret_list = []
    for i in range(0, k):
        ret_list.append([])
    for i in range(0, len(boxes)):
        ret_list[labels[i]].append(boxes[i])
    return ret_list
Ejemplo n.º 14
0
    def execute_kfold_training(self, matrix, music_dict):
        current_accuracy = 0.

        for i in range(0,len(self.kfold_trainings)):
            
            # Constroi a matriz de treinamento obtendo o posicionamento das 
            # musicas conseguido do calculo do SVD
            train_matrix = [matrix[music_dict[x]['pos']] for x in self.kfold_trainings[i]]
            train_matrix = np.float32(train_matrix)

            # Constroi a matriz de treinamento obtendo o posicionamento das 
            # musicas conseguido do calculo do SVD
            test_matrix = [matrix[music_dict[x]['pos']] for x in self.kfold_tests[i]]
            test_matrix = np.float32(test_matrix)

            # Executa o Kmeans com os critérios definidos no construtor da classe KMeans
            ret,label,center=cv2.kmeans(train_matrix, self.num_clusters,None, self.criteria, self.attempts,cv2.KMEANS_RANDOM_CENTERS)

            # Obtem a proporção de cada grupo/centroide e o classifica de acordo
            # com a maioria
            group_labels = get_proportion(label, music_dict, self.kfold_trainings[i])

            # Executa o método execute_teste para testar o treinamento atual
            self.execute_test(music_dict, test_matrix, train_matrix, center, group_labels, i)

            # Guarda as informações dos K Means
            self.labels.append(label)
            self.centers.append(center)
            self.retValues.append(ret)
            self.centers_labels.append(group_labels)
Ejemplo n.º 15
0
    def calcKmeans(self, img):
        """Calculate mask based on k-means

        Don't do any checks.

        Args:
          img: 3D structure where x,y are image axis and z represents
               different features.
        """
        oShape = img.shape
        img = np.float32(img)
        img = np.reshape(img, (oShape[0] * oShape[1], oShape[2]), order="F")

        # k-means. max 10 iters. Stop if diff < 1. Init centers at random
        compactness, labels, centers = cv2.kmeans(
            img,
            2,
            (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, self.maxIter,
                self.epsilon),
            self.attempts,
            cv2.KMEANS_RANDOM_CENTERS)

        labels = np.reshape(labels, (oShape[0], oShape[1]), order="F")

        labels = labels.astype(np.float64)
        # FIXME: do this check if we don't have mean centers.
        FG = sum(sum(labels == 1))
        BG = sum(sum(labels == 0))
        if BG < FG:
            labels = -(labels - 1)

        return (labels.astype(np.float64))
def kmeans_color_quant(img, k):
    """Performs color quantization on an image.

    Uses the OpenCV implementation of k-means clustering to perform color 
    quantization on an image with a specified number of clusters.

    Args:
        img: The image on which to perform k-means clustering.
        k: The number of clusters.
    Returns:
        (clustered, label, center): A tuple containing three arrays for the 
        clustered image, pixel labels (coded as 0, 1, 2, ...), and the centers 
        of clusters.
    """
    #Reshape into list of pixels
    Z = np.float32(img.reshape((-1, 3)))
    
    #Define criteria and perform clustering
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    _, label, center = cv2.kmeans(Z, k, None, criteria, 10, 
                                  cv2.KMEANS_RANDOM_CENTERS)
    
    #Convert back into uint8 and reshape to shape of original image
    center = np.uint8(center)
    clustered = center[label.flatten()].reshape((img.shape))
    return (clustered, label, center)
Ejemplo n.º 17
0
def find_sample_clusters(pos_reg_generator, window_dims, hog, num_clusters):
    regions = list(pos_reg_generator)
    descriptors = trainhog.compute_hog_descriptors(hog, regions, window_dims, 1)

    # convert to np.float32
    descriptors = [rd.descriptor for rd in descriptors]
    Z = np.float32(descriptors)

    # define criteria and apply kmeans()
    K = num_clusters
    print 'find_label_clusters,', 'kmeans:', K
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    attempts = 10
    ret,label,center=cv2.kmeans(Z,K,None,criteria,attempts,cv2.KMEANS_RANDOM_CENTERS)
    # ret,label,center=cv2.kmeans(Z,2,criteria,attempts,cv2.KMEANS_PP_CENTERS)

    print 'ret:', ret
    # print 'label:', label
    # print 'center:', center

    # # Now separate the data, Note the flatten()
    # A = Z[label.ravel()==0]
    # B = Z[label.ravel()==1]

    clusters = partition(regions, label)
    return clusters
Ejemplo n.º 18
0
def kmeans(Z, STO):  # pg please make Z a np array like the one described below :P
    # Z = np.array([[a1,b1],[x1,y1],[x2,y2],[a3,b3],[a2,b2]])
    # convert to np.float32
    # plt.clf()
    Z = np.float32(Z)
    # define criteria and apply kmeans()
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    ret, label, center = cv2.kmeans(Z, 1, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
    # pls add corresponding entries for each cluster
    # Now separate the data, Note the flatten()
    A = Z[label.ravel() == 0]
    B = Z[label.ravel() == 1]

    # Plot the data
    # """
    # rempove for debug
    plt.scatter(A[:, 0], A[:, 1])
    plt.scatter(B[:, 0], B[:, 1], c="r")
    plt.scatter([x[1], x[2], x[3], x[0], x[4]], [y[1], y[2], y[3], y[0], y[4]], s=40, c="red")
    plt.scatter(center[:, 0], center[:, 1], s=80, c="y", marker="s")
    plt.xlabel("X"), plt.ylabel("Y")
    plotfences(plt)
    plt.draw()
    pt = [center[:, 0], center[:, 1]]
    print(checkfences(pt))

    STO = center
Ejemplo n.º 19
0
Archivo: util.py Proyecto: hdidx/hdidx
 def kmeans(vs, ks, niter):
     criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,
                 niter, 0.01)
     flags = cv2.KMEANS_RANDOM_CENTERS
     compactness, labels, centers = cv2.kmeans(
         vs, ks, criteria, 1, flags)
     return centers
Ejemplo n.º 20
0
def main():
    cluster_n = 5
    img_size = 512

    # generating bright palette
    colors = np.zeros((1, cluster_n, 3), np.uint8)
    colors[0,:] = 255
    colors[0,:,0] = np.arange(0, 180, 180.0/cluster_n)
    colors = cv.cvtColor(colors, cv.COLOR_HSV2BGR)[0]

    while True:
        print('sampling distributions...')
        points, _ = make_gaussians(cluster_n, img_size)

        term_crit = (cv.TERM_CRITERIA_EPS, 30, 0.1)
        ret, labels, centers = cv.kmeans(points, cluster_n, None, term_crit, 10, 0)

        img = np.zeros((img_size, img_size, 3), np.uint8)
        for (x, y), label in zip(np.int32(points), labels.ravel()):
            c = list(map(int, colors[label]))

            cv.circle(img, (x, y), 1, c, -1)

        cv.imshow('kmeans', img)
        ch = cv.waitKey(0)
        if ch == 27:
            break

    print('Done')
def main():
	train = pd.read_csv('../../data/raw/train.csv')
	print train.shape
	
	uniq = train['place_id'].nunique()
	print uniq

	col_headers = list(train.columns.values)
	print col_headers
	train[col_headers[1:-1]] = train[col_headers[1:-1]].apply(lambda x: (x - x.min()) / (x.max() - x.min()))
	train['accuracy'] = 1 - train['accuracy']
	train_X_norm = train.values[:,:-1]
	print train_X_norm.shape

	K = uniq
	clusters = range(0,K)
	batch_size = 500
	n_init = 10

	train_X_norm = train_X_norm.astype(np.float32)
	print train_X_norm.dtype
	print train_X_norm.shape


	# define criteria and apply kmeans()
	criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
	ret, label, center = cv2.kmeans(train_X_norm, K, criteria, n_init, cv2.KMEANS_RANDOM_CENTERS)

	print center.shape
Ejemplo n.º 22
0
def kmeans(im,K,show=False):
    '''
    documentation:
    http://docs.opencv.org/trunk/doc/py_tutorials/py_ml/py_kmeans/py_kmeans_opencv/py_kmeans_opencv.html#kmeans-opencv 
    '''

    Z = im.reshape((-1,3))

    # convert to np.float32
    Z = np.float32(Z)

    # define criteria, number of clusters(K) and apply kmeans()
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)

    # Now convert back into uint8, and make original image
    center = np.uint8(center)
    res = center[label.flatten()]
    res2 = res.reshape((im.shape))

    if show:
        plt.subplot(111),plt.imshow(res2),plt.title('center blurred h')
        plt.show()

    return res2
Ejemplo n.º 23
0
def segment_by_angle_kmeans(lines, k=2, **kwargs):
    """Groups lines based on angle with k-means.

    Uses k-means on the coordinates of the angle on the unit circle 
    to segment `k` angles inside `lines`.
    """

    # Define criteria = (type, max_iter, epsilon)
    default_criteria_type = cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER
    criteria = kwargs.get('criteria', (default_criteria_type, 10, 1.0))
    flags = kwargs.get('flags', cv2.KMEANS_RANDOM_CENTERS)
    attempts = kwargs.get('attempts', 10)

    # returns angles in [0, pi] in radians
    angles = np.array([line[0][1] for line in lines])
    # multiply the angles by two and find coordinates of that angle
    pts = np.array([[np.cos(2*angle), np.sin(2*angle)]
                    for angle in angles], dtype=np.float32)

    # run kmeans on the coords
    labels, centers = cv2.kmeans(pts, k, None, criteria, attempts, flags)[1:]
    labels = labels.reshape(-1)  # transpose to row vec

    # segment lines based on their kmeans label
    segmented = defaultdict(list)
    for i, line in zip(range(len(lines)), lines):
        segmented[labels[i]].append(line)
    segmented = list(segmented.values())
    return segmented
Ejemplo n.º 24
0
    def get_idx_from_contours_kmeans(self, contour, n):
        '''
        @param contour: contour in points, assumed to be convex
        @param n: number of sides
        '''
        from research.util import Plotter

        assert isinstance(contour, np.ndarray)
        # calculate direction vectors
        k = []
        for i in range(contour.shape[0]):
            ki = contour[(i + 1) % contour.shape[0]] - contour[i]
            ki = ki.astype(np.float32)
            ki_mod = np.linalg.norm(ki, 2)
            ki[0] = ki[0] / ki_mod
            ki[1] = ki[1] / ki_mod
            k.append(ki)
        k = np.squeeze(np.vstack(k))

        # plot for debug
        image = np.ones((300, 300, 3)) * 255
        Plotter.plot_points(image, (k * 100 + 150),
                            getFuncName() + ": plot_points", (0, 0, 0))

        # use k-means
        termination_criteria = (cv2.TERM_CRITERIA_EPS, 30, 0.1)
        _, labels, _ = cv2.kmeans(k, n,
                                  termination_criteria, 10,
                                  cv2.KMEANS_RANDOM_CENTERS)
        labels = list(np.squeeze(labels))
        return labels
Ejemplo n.º 25
0
def Cluster(Z, groups = 3):
    import cv2
    from math import sqrt
    
    # define criteria and apply kmeans()
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    ret,label,center = cv2.kmeans(Z,groups,criteria,10,cv2.KMEANS_RANDOM_CENTERS)

    # Now separate the data, Note the flatten()
    segregated = []
    centers = []
    distFromCenter = []
    
    for i in xrange(groups):
        segregated.append(Z[label.flatten()==i])
        distFromCenter.append([])
        centers.append((int(center[i][0]), int(center[i][1])))

    # Create a distance from centroid list
    for j in xrange(groups):
        x1 = centers[j][0]
        y1 = centers[j][1]
        for i in range(len(segregated[j])):
            x2 = segregated[j][i][0]
            y2 = segregated[j][i][1]
            distFromCenter[j].append( sqrt( (x2 - x1)**2 + (y2 - y1)**2 ))

    # Create an average distance from centroid list
    distFromCenterAve = []
    for j in xrange(groups):
        distFromCenterAve.append(sum(distFromCenter[j])/len(distFromCenter[j]))

    return segregated, centers, distFromCenter, distFromCenterAve
Ejemplo n.º 26
0
def kmeans(Z,STO):# pg please make Z a np array like the one described below :P
	#Z = np.array([[a1,b1],[x1,y1],[x2,y2],[a3,b3],[a2,b2]])
	# convert to np.float32
	# plt.clf()
	Z = np.float32(Z)
	# define criteria and apply kmeans()
	criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
	ret,label,center=cv2.kmeans(Z,1,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
	# pls add corresponding entries for each cluster
	# Now separate the data, Note the flatten()
	A = Z[label.ravel()==0]
	B = Z[label.ravel()==1]

	# Plot the data
	#""" 
	#rempove for debug 
	plt.scatter(A[:,0],A[:,1])
	plt.scatter(B[:,0],B[:,1],c = 'r')
	plt.scatter([x1,x2,x3],[y1,y2,y3],s = 40, c = 'red')
	plt.scatter(center[:,0],center[:,1],s = 80,c = 'y', marker = 's')
	plt.xlabel('X'),plt.ylabel('Y')
	plt.draw()
	#plt.show()
	#"""
	STO = center
Ejemplo n.º 27
0
def auto_correlogram(img):
    """
    The functions for computing color correlogram.
    To improve the performance, we consider to utilize
    color quantization to reduce image into 64 colors.
    So the K value of k-means should be 64.

    img:
     The numpy ndarray that describe an image in 3 channels.
    """
    z = img.reshape((-1, 3))

    # convert to np.float32
    z = np.float32(z)

    # define criteria, number of clusters(k) and apply kmeans()
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    k = 64
    ret, label, center = cv2.kmeans(z, k, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

    # Now convert back into uint8, and make original image
    center = np.uint8(center)
    res = center[label.flatten()]
    res2 = res.reshape((img.shape))

    # according to "Image Indexing Using Color Correlograms" paper
    k = [i for i in range(1, 9, 2)]

    colors64 = unique(np.array(res))

    result = correlogram(res2, colors64, k)
    return result
Ejemplo n.º 28
0
    def train(self, data, ksize, max_samples=0, iterations=1000):
        assert len(data) > ksize
        assert iterations > 0

        logger.debug(
            'Started training kmeans with ksize={ksize}'.format(
                ksize=ksize
            ))
        if max_samples > 0:
            assert max_samples >= ksize
            data = random.sample(data, max_samples)

        logger.debug('Training kmeans with {samples} samples'.format(
                     samples=len(data)
                     ))

        criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,
                    iterations,
                    1.0)

        ret, _, centers = cv2.kmeans(
            numpy.array(data),
            ksize,
            criteria,
            10,  # attempts
            cv2.KMEANS_PP_CENTERS
        )
        self.clusters = centers
        logger.debug(
            'Completed training kmeans with return={ret}'.format(
                ret=ret
            ))
Ejemplo n.º 29
0
def main(path, K):
	kLabels = []
	wholeK = np.zeros([1,153])
	labelLines = 0
	for u in os.listdir(path): 
		if u[-4:] == 'List':
			labelLines += 1
			filePath = path+u
			kPoints = pickle.load(open(filePath, 'rb'))
			kPoints = np.asarray(kPoints)
			kLabels.append(u[0:6])
			wholeK = np.append(wholeK, kPoints, axis=0)
		
			


	Z = np.float32(wholeK)
	criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
	ret,label,center=cv2.kmeans(Z,K,criteria,10,cv2.KMEANS_PP_CENTERS)

	cen = open(path+'centers', 'wb')
	pickle.dump(center, cen)

	for y in range(0,labelLines):
		l = label[0+(1000*y):999+(1000*y)]
		ll = l.tolist()
		lll = [item for sublist in ll for item in sublist] 
		
		wordcount = []
		for x in range(K):
			wordcount.append(lll.count(x))
		stringSave = path+kLabels[y] + '_WC'
		
		pickle.dump(wordcount, open(stringSave, 'wb'))	
Ejemplo n.º 30
0
    def get_idx_from_contours_spectral_clustering(self, contours):
        '''
        @todo: 还没有完全实现
        @param contours:
        '''
        cv2.drawContours(self.image, [contours], 0, (0, 0, 255), 2)
        k = []
        for i in range(contours.shape[0]):
            # cv2.circle(image, tuple(contours[i][0]), 2, (255,255,0))
            ki = contours[(i + 1) % contours.shape[0]] - contours[i]
            ki = ki[0]
            ki = ki.astype(float)
            ki_mod = (ki[0] ** 2 + ki[1] ** 2) ** 0.5
            ki[0] = ki[0] / ki_mod
            ki[1] = ki[1] / ki_mod
            ki.append(contours[0], contours[1])
            k.append(ki)

        termination_criteria = (cv2.TERM_CRITERIA_EPS, 30, 0.1)
        k_array = np.float32(k)
        centers = cv2.kmeans(k_array, 4,
                             termination_criteria, 10,
                             cv2.KMEANS_RANDOM_CENTERS)
        idx = centers[1]
        return contours, idx

        # build an array of direction+position ->
        #     what are the possible better choice?
        # get the index of each point
            # cluster the array, spectral clustering is a good choice
            # find lines in the 4d space
        # smooth the result using a 1*5 median filter
        raise Exception('To be implemented')
Ejemplo n.º 31
0
import cv2 as cv
import time

folder = "/Users/ahmedbingol/Desktop/Hw2/"
img = cv.imread('/Users/ahmedbingol/Desktop/SunnyLake.bmp')
Z = img.reshape((-1, 3))

# convert to np.float32
Z = np.float32(Z)

# define criteria, number of clusters(K) and apply kmeans() for 4 different K
start_time = time.time()

criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K1 = 4
ret, label, center = cv.kmeans(Z, K1, None, criteria, 10,
                               cv.KMEANS_RANDOM_CENTERS)
labels_unique = np.unique(label)
print("Cluster no for ", K1, "is = ", len(labels_unique))

K2 = 8
ret2, label2, center2 = cv.kmeans(Z, K2, None, criteria, 10,
                                  cv.KMEANS_RANDOM_CENTERS)
labels_unique = np.unique(label2)
print("Cluster no for ", K2, "is = ", len(labels_unique))

K3 = 16
ret3, label3, center3 = cv.kmeans(Z, K3, None, criteria, 10,
                                  cv.KMEANS_RANDOM_CENTERS)

labels_unique = np.unique(label3)
print("Cluster no for ", K3, "is = ", len(labels_unique))
Ejemplo n.º 32
0
input_file = sys.argv[2]
output_file = sys.argv[3]
img = cv2.imread(input_file)
Z = img.reshape((-1, 3))

# In[3]:

Z = np.float32(Z)

# In[4]:

#Here the criteria defines the argument we want to pass to the open_cv
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)

#Run open_cv kmeans method to cluster the given image.
ret, label, center = cv2.kmeans(Z, K_value, criteria, 10,
                                cv2.KMEANS_RANDOM_CENTERS)

# In[5]:

center = np.uint8(center)
res = center[label.flatten()]
img_out = res.reshape((img.shape))

#This will save the output to a file
cv2.imwrite(output_file, img)

#This will show the clustered image in a window
cv2.imshow('image', img_out)
cv2.waitKey(0)
cv2.destroyAllWindows()
Ejemplo n.º 33
0
from matplotlib import pyplot as plt
from sklearn.cluster import KMeans

K = 4

image = cv2.imread('inputFileImages//image1.jpg')
Z = image.reshape((-1, 3))

# convert to np.float32
Z = np.float32(Z)

# define criteriaOfimages, number of clusters(K) and apply kmeans()
criteriaOfimages = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10,
                    1.0)

ret, criteriaLabel, center = cv2.kmeans(Z, K, None, criteriaOfimages, 10,
                                        cv2.KMEANS_RANDOM_CENTERS)

center = np.uint8(center)
res = center[criteriaLabel.flatten()]
output_image = res.reshape((image.shape))
cv2.imwrite('outputClusteredImages//output1_' + str(K) + '.png', output_image)

print("Image1 has been clustered")

image = cv2.imread('inputFileImages//image2.jpg')
Z = image.reshape((-1, 3))

# convert to np.float32
Z = np.float32(Z)

# define criteriaOfimages, number of clusters(K) and apply kmeans()
#读取原始图像
img = cv2.imread('scenery.png')
print(img.shape)

#图像二维像素转换为一维
data = img.reshape((-1, 3))
data = np.float32(data)

#定义中心 (type,max_iter,epsilon)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)

#设置标签
flags = cv2.KMEANS_RANDOM_CENTERS

#K-Means聚类 聚集成2类
compactness, labels2, centers2 = cv2.kmeans(data, 2, None, criteria, 10, flags)

#K-Means聚类 聚集成4类
compactness, labels4, centers4 = cv2.kmeans(data, 4, None, criteria, 10, flags)

#K-Means聚类 聚集成8类
compactness, labels8, centers8 = cv2.kmeans(data, 8, None, criteria, 10, flags)

#K-Means聚类 聚集成16类
compactness, labels16, centers16 = cv2.kmeans(data, 16, None, criteria, 10,
                                              flags)

#K-Means聚类 聚集成64类
compactness, labels64, centers64 = cv2.kmeans(data, 64, None, criteria, 10,
                                              flags)
Ejemplo n.º 35
0
def selectElements(img):
    # Kmeans segmentation
    image_copy = img.copy()
    #inicializacion de los parametros para usar kmeans
    pixel_values = image_copy.reshape((-1, 3))
    pixel_values = np.float32(pixel_values)
    stop_criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10,
                     1.0)
    centroid_initialization_strategy = cv2.KMEANS_RANDOM_CENTERS
    print("getting kmeans information")
    #se genera el kmeans de la imagen
    _, labels, centers = cv2.kmeans(pixel_values, 5, None, stop_criteria, 1,
                                    centroid_initialization_strategy)
    centers = np.uint8(centers)
    segmented_data = centers[labels.flatten()]
    segmented_image = segmented_data.reshape(image_copy.shape)

    # Identify objects
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #Generación de bordes de la imagen
    edges = cv2.Canny(gray, 80, 200)
    edges_final = edges.copy()

    #aplicación de close y open para eliminar ruido
    kernel = np.ones((3, 3), np.uint8)
    kernel_closi = np.ones((5, 5), np.uint8)
    closing = cv2.morphologyEx(edges,
                               cv2.MORPH_CLOSE,
                               kernel_closi,
                               iterations=4)
    opening = cv2.morphologyEx(closing,
                               cv2.MORPH_OPEN,
                               kernel_closi,
                               iterations=3)

    img_copy = img.copy()
    final_mask_c = img.copy()
    final_mask_c[:, :, :] = 0
    final_mask_cH = final_mask_c.copy()
    final_mask_centers = final_mask_c.copy()

    contours, hierarchy = cv2.findContours(opening, 1, cv2.CHAIN_APPROX_NONE)

    areas = []
    hull = []
    font = cv2.FONT_HERSHEY_SIMPLEX
    print("creating contours, convex hull")
    for i, contour in enumerate(contours):
        #Obtencion de area por los contornos
        area = cv2.contourArea(contour)
        areas.append(area)
        #Obtencion de convex hull por los contornos
        convex = cv2.convexHull(contours[i], False)
        hull.append(convex)

    max_val = np.argmax(areas)
    areas[max_val] = 0

    print("Drawing contours, convex hull")
    contours_lengh = len(contours)
    for i in range(contours_lengh):
        #Dibujado de los contornos por cada contorno
        cv2.drawContours(final_mask_c, contours, i, (10, 108, 28), 2,
                         cv2.LINE_8, hierarchy, 100)
        #Dibujado de los convex hull por cada contorno
        cv2.drawContours(final_mask_cH, hull, i, (255, 1, 1), 3, 8)

        #calculo de los momentos de cada contorno
        M = cv2.moments(contours[i])
        m00 = M['m00'] if M['m00'] else 1
        # Obtencion de los centroides de cada elemento por su contorno
        cx = int(M['m10'] / m00)
        cy = int(M['m01'] / m00)
        # Dibujar un circulo en cada centro obtenido
        final_mask_centers = cv2.circle(final_mask_centers, (cx, cy), 10,
                                        (255, 254, 0), -1)

    #Dibujado de la cantidad de elementos detectados
    cv2.putText(final_mask_centers, f"Objects: {len(contours)}", (150, 250),
                font, 5, (0, 255, 0), 4)

    arr1 = np.array(final_mask_c)
    arr2 = np.array(final_mask_cH)
    arr3 = np.array(final_mask_centers)
    final_mask_c = cv2.cvtColor(final_mask_c, cv2.COLOR_BGR2RGBA)
    final_mask_cH = cv2.cvtColor(final_mask_cH, cv2.COLOR_BGR2RGBA)
    final_mask_centers = cv2.cvtColor(final_mask_centers, cv2.COLOR_BGR2RGBA)
    segmented_image = cv2.cvtColor(segmented_image, cv2.COLOR_BGR2RGBA)

    final_mask_c[:, :, 3] = 0
    final_mask_cH[:, :, 3] = 0
    final_mask_centers[:, :, 3] = 0
    result = np.where(arr1 == 10)
    for i in range(len(result[0]) - 1):
        final_mask_c[result[0][i], result[1][i], 3] = 255
    result = np.where(arr2 == 255)
    for i in range(len(result[0]) - 1):
        final_mask_cH[result[0][i], result[1][i], 3] = 255
    result = np.where(arr3 == 255)
    for i in range(len(result[0]) - 1):
        final_mask_centers[result[0][i], result[1][i], 3] = 255

    return final_mask_c, final_mask_cH, final_mask_centers, segmented_image, edges_final
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
X = np.random.randint(25, 50, (25, 2))
Y = np.random.randint(60, 85, (25, 2))
Z = np.vstack((X, Y))
# convert to np.float32
Z = np.float32(Z)
# define criteria and apply kmeans()
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 10, 1.0)
ret, label, center = cv.kmeans(Z, 2, None, criteria, 10,
                               cv.KMEANS_RANDOM_CENTERS)
# Now separate the data, Note the flatten()
A = Z[label.ravel() == 0]
B = Z[label.ravel() == 1]
# Plot the data
plt.scatter(A[:, 0], A[:, 1])
plt.scatter(B[:, 0], B[:, 1], c='r')
plt.scatter(center[:, 0], center[:, 1], s=80, c='y', marker='s')
plt.xlabel('Height'), plt.ylabel('Weight')
plt.show()
Ejemplo n.º 37
0
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

x = np.random.randint(25, 100, 25)
y = np.random.randint(175, 255, 25)
z = np.hstack((x, y))
z = z.reshape((50, 1))
z = np.float32(z)
# plt.hist(z, 256, [0, 256]), plt.show()

criteria = (cv.TERM_CRITERIA_MAX_ITER + cv.TERM_CRITERIA_EPS, 10, 1.0)
flags = cv.KMEANS_RANDOM_CENTERS
# The fifth argument means the times doing the algorithm.
# And this function will return the best result with best compactness.
compactness, labels, centers = cv.kmeans(z, 2, None, criteria, 10, flags)
A = z[labels == 0]
B = z[labels == 1]

plt.hist(A, 256, [0, 256], color='r')
plt.hist(B, 256, [0, 256], color='b')
plt.hist(centers, 32, [0, 256], color='y')
plt.show()
Ejemplo n.º 38
0
 def kmeans(vs, ks, niter):
     criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, niter,
                 0.01)
     flags = cv2.KMEANS_RANDOM_CENTERS
     compactness, labels, centers = cv2.kmeans(vs, ks, criteria, 1, flags)
     return centers
Ejemplo n.º 39
0
import cv2
import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import itemfreq

img = cv2.imread('satellite.jpg')

average_color = [img[:, :, i].mean for i in range(img.shape[-1])]

arr = np.float32(img)
pixels = arr.reshape((-1, 3))

n_colors = 5
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .1)
flags = cv2.KMEANS_RANDOM_CENTERS
_, labels, centroids = cv2.kmeans(pixels, n_colors, None, criteria, 10, flags)

palette = np.uint8(centroids)
quantized = palette[labels.flatten()]
quantized = quantized.reshape(img.shape)

dominant_color = palette[np.argmax(itemfreq(labels)[:, -1])]

res = np.zeros((200, 200, 3), np.uint8)


res[:, :100] = average_color

for i in range(n_colors):
	res[40 * i:40 * i + 20, 100:] = tuple(dominant_color)
Ejemplo n.º 40
0
    def draw_brush(self, row_f: float, col_f: float):
        row = int(round(row_f))
        col = int(round(col_f))

        # Erase old tool mask
        self.tool_mask.array.fill(self.tool_background_class)

        rr, cc = skimage.draw.ellipse(  # we can use rounded row, col and radii,
            # but float values give more precise resulting ellipse indexes
            row_f,
            col_f,
            *self.tool_mask.spatial_size_to_indexed(
                np.array([self.radius, self.radius])),
            shape=self.tool_mask.array.shape)

        if self.mode == Mode.ERASE:
            self.erase_region(rr, cc)
            return

        mask_circle_pixels = self.mask.array[rr, cc]
        background_or_mask_class_indexes = \
            (mask_circle_pixels == self.mask_background_class) | (mask_circle_pixels == self.mask_foreground_class)
        # Do not use pixels, which already painted to another mask class
        rr = rr[background_or_mask_class_indexes]
        cc = cc[background_or_mask_class_indexes]

        samples = self.image.array[rr, cc]
        if len(
                samples.shape
        ) == 2:  # if there is an axis with channels (multichannel image)
            samples = samples[:, 0]  # use only the first channel
        samples = samples.astype(np.float32)
        number_of_clusters = 2
        if number_of_clusters > samples.size:
            return

        criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10,
                    1.0)
        ret, label, centers = cv2.kmeans(samples, number_of_clusters, None,
                                         criteria, 10,
                                         cv2.KMEANS_RANDOM_CENTERS)
        label = label.ravel()  # 2D array (one column) to 1D array without copy
        centers = centers.ravel()

        if self.paint_central_pixel_cluster:
            center_pixel_indexes = np.where((rr == row) & (cc == col))[0]
            if center_pixel_indexes.size != 1:  # there are situations, when the center pixel is out of image
                return
            center_pixel_index = center_pixel_indexes[0]
            painted_cluster_label = label[center_pixel_index]
        else:
            # Label of light cluster
            painted_cluster_label = 0 if centers[0] > centers[1] else 1
            if self.paint_dark_cluster:
                # Swapping 1 with 0 and 0 with 1
                painted_cluster_label = 1 - painted_cluster_label

        tool_mask_circle_pixels = self.tool_mask.array[rr, cc]
        tool_mask_circle_pixels[
            label == painted_cluster_label] = self.tool_foreground_class
        self.tool_mask.array[rr, cc] = tool_mask_circle_pixels

        if self.paint_central_pixel_cluster and self.paint_connected_component:
            labeled_tool_mask = skimage.measure.label(
                self.tool_mask.array, background=self.tool_background_class)
            label_under_mouse = labeled_tool_mask[row, col]
            self.tool_mask.array[
                (self.tool_mask.array == self.tool_foreground_class)
                & (labeled_tool_mask != label_under_mouse
                   )] = self.tool_unconnected_component_class

        if self.mode == Mode.DRAW:
            self.mask.array[self.tool_mask.array == self.
                            tool_foreground_class] = self.mask_foreground_class
            self.mask.emit_pixels_modified()

        self.tool_mask.emit_pixels_modified()
Ejemplo n.º 41
0
def image_decolor(img, N):
    #減色を用いたレーン検出処理
    H = img.shape[0]
    W = img.shape[1]
    R = 6
    h = H // R
    w = W // R

    ximg = cv2.resize(img, (w, h), cv2.INTER_LANCZOS4)

    t = 0.25
    if t > 0:
        op = np.array([[-t, -t, -t], [-t, 1 + 8 * t, -t], [-t, -t, -t]])
        ximg = cv2.filter2D(ximg, ddepth=-1, kernel=op)

    img_src = ximg.copy()
    #img_src = cv2.fastNlMeansDenoisingColored(ximg,None,10,10,7,21)

    Z = img_src.reshape((-1, 3))

    # float32に変換
    Z = np.float32(Z)
    # K-Means法
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    K = N
    ret, label, center = cv2.kmeans(
        Z,
        K,
        None,
        criteria,
        10,
        #cv2.KMEANS_RANDOM_CENTERS
        cv2.KMEANS_PP_CENTERS)

    ##検出ラベルごとの画素数確認
    result = []
    for i in range(K):
        result.append(np.sum(label == i))
        #print(result[i])

    #print(img.shape,np.sum(result)/img.shape[0])
    #行と列数にずれなきこと確認
    # print(center)

    #面積と輝度を確認して排除
    for i in range(K):
        delsize = 50000 // (R * R)
        if (result[i] > delsize):
            center[i] *= 0
            continue

        g = center[i][0] * 0.299 + center[i][1] * 0.587 + center[i][2] * 0.114
        if (g < 80):
            center[i] *= 0

    #print(label.shape)
    test = label.copy()
    test = test.reshape(h, w)

    #print(test.shape)
    #横方向の連なりを確認して排除
    for i in range(K):
        #sliding windows
        if (all(center[i] == 0)):
            continue

        mysliding = 32
        half = mysliding // 2

        xtest = np.sum(test == i, axis=1)

        for j in range(half, h, half):
            t = j - half
            b = j + half
            if (t < 0):
                continue
            if (b >= h):
                continue
            cnt = (np.sum(xtest[t:b]))
            if (cnt > ((w * 7)) // 10):
                center[i] *= 0
                break

    #print(center)

    # UINT8に変換
    center = np.uint8(center)
    res = center[label.flatten()]
    ximg_dst = res.reshape((img_src.shape))

    img_dst = cv2.resize(ximg_dst, (W, H))
    #plt.figure(figsize=(15, 5))
    #plt.imshow(img_dst)

    return img_dst
Ejemplo n.º 42
0
    image_lengths.append(des.shape[0])

    all_descriptors = np.concatenate((all_descriptors, des), axis=0)
    all_keypoints = np.concatenate((all_keypoints, kp), axis=0)

    print "Number of keypoints:", all_descriptors.shape

# Remove the first dummy entry
all_descriptors = all_descriptors[1:]
all_descriptors = np.float32(all_descriptors)

# Preform k-means
flags = cv2.KMEANS_RANDOM_CENTERS
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 50, 1.0)
NUM_RESTARTS = 3
compactness, labels, centers = cv2.kmeans(all_descriptors, VOCAB_SIZE,
                                          criteria, NUM_RESTARTS, flags)
print 'Kmeans finished with compactness:', compactness

np.savetxt(os.path.join(datapath,
                        'cluster-centers-' + str(VOCAB_SIZE) + '.txt'),
           centers,
           delimiter=",")

# Write results to file: <index> <cluster id> <keypoint size>
word_index = 0
for index, doc_length in enumerate(image_lengths):
    doc = open(os.path.join(savepath, 'doc' + str(index + 1) + '.txt'), 'w+')
    print "Writing to document", index, "with length", doc_length
    for i in xrange(doc_length):
        doc.write(
            str(i) + "," + str(labels[word_index + i, 0]) + "," +
Ejemplo n.º 43
0
filedesc = open('sift.pickle', 'wb')
ft = np.array(feat)
print ft.shape
pickle.dump(feat, filedesc)
descriptor = feat[0]

#stacking all the descriptors into a single array
for i in range(0, len(feat)):
    print feat[i].shape
    descriptor = np.vstack((descriptor, feat[i]))

#Perform Kmeans on descriptor array to get the cluster centers
print "K Means clustering"
k = 500
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
retval, bestLabels, centers = cv2.kmeans(descriptor, k, criteria, 10, 0)

#dumping into kmeans
filekmeans = open('kmeans.pickle', 'wb')
pickle.dump(centers, filekmeans)

#Creating Histogram of clustered features
print "Creating Histogram"
X = np.zeros((n, k), dtype=np.float32)
for i in xrange(n):
    print feat[i]
    words, distance = vq(feat[i], centers)  #vector quantization
    for w in words:
        X[i][w] += 1  #bag-of-visual-words representation[]

filedoc = open('doc-word.pickle', 'wb')
Ejemplo n.º 44
0
img = cv2.imread('extracted3.jpg')
#Z = img.reshape((-1,3))
centroids = img.copy()
#MeanShiftFiltering (Trying)
meanShiftImg = img.copy()
cv2.pyrMeanShiftFiltering(img, 20, 45, meanShiftImg, 1)
cv2.imshow('Mean Shift', meanShiftImg)
img = meanShiftImg.copy()

Z = img.reshape((-1, 3))
Z = np.float32(Z)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 5
#while(K<101):
ret, label, center = cv2.kmeans(Z, K, None, criteria, 10,
                                cv2.KMEANS_RANDOM_CENTERS, centroids)

# Now convert back into uint8, and make original image
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))
cv2.imshow('KMC', res2)
cv2.imshow('centroids', centroids)
print('Centres:')
print(center)

print('Labels:')
print(label)

cv2.waitKey(0)
x=pd.DataFrame(np.matrix(x))
pca = PCA(n_components=500, whiten=True).fit(x)
pca_x=pca.transform(x)
'''

# k means
for file in labels.filename:
    img = cv2.imread('500_enhanced_augmentated/' + file)
    Z = img.reshape((-1, 3))
    Z = np.float32(Z)
    k = cv2.KMEANS_PP_CENTERS
    # define criteria, number of clusters(K) and apply kmeans()
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    K = 10
    ret, label, center = cv2.kmeans(Z, K, None, criteria, 10, k)
    # Now convert back into uint8, and make original image
    center = np.uint8(center)
    res = center[label.flatten()]
    res2 = res.reshape((img.shape))
    x.append(np.array(res2).flatten())

# Training Test Spilt
x_train, x_test = x[:1200], x[1200:]
y_train, y_test = y[:1200], y[1200:]
y_train_bi, y_test_bi = y_new[:1200], y_new[1200:]
# SVM
clf = svm.SVC(C=0.01,
              kernel='rbf',
              decision_function_shape='ovr',
              gamma='auto')
Ejemplo n.º 46
0
cv2.imshow("binary", binary)
# Detect black blobs.
keypoints = detector.detect(binary)
print(keypoints)
tong_cham = len(keypoints)  # so luong cham den

tam = np.zeros(shape=(len(keypoints), 2))  # Tim tam cua nhung cham
for i in range(len(keypoints)):
    tam[i][0] = keypoints[i].pt[0]  # Toa do x
for i in range(len(keypoints)):
    tam[i][1] = keypoints[i].pt[1]  # Toa do y
tam = np.float32(tam)  # Chuyen float 32bit
# Su dung Kmean
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
k = 6
ret, label, center = cv2.kmeans(tam, k, None, criteria, 10,
                                cv2.KMEANS_RANDOM_CENTERS)
print(tam)
print(label)
count = np.zeros((6, 1),
                 dtype=int)  # Ma tran count luu so cham den cung 1 vung
font = cv2.FONT_HERSHEY_SIMPLEX
for i in label:  # Ma tran chua so vung cua tung xuc xuat
    count[i] = count[i] + 1  # count chu so cham trong tung vung
for i in range(len(count)):
    cv2.putText(image, str(count[i]), (center[i][0], center[i][1]), font, 0.5,
                (0, 0, 255), 1)

cv2.putText(image, 'Tong cham:' + str(tong_cham), (300, 50), font, 1,
            (0, 0, 255), 1)
cv2.imshow("ket qua", image)
cv2.waitKey(0)
''' Reshape into 2D array of pixels and 3 color values RGB'''
''' it shoul be  m x 3 dimension, where m - number of pixels, 3 - numb.of colors'''

pixel_vals = image_copy.reshape((-1,3)) 

'''convert to float'''
pixel_vals = np.float32(pixel_vals)

''' input:  m x 3 array pixel_vals'''
''' None - no labels, sto p criteria, 10 steps '''
''' previous val k = 2'''
k = 6 
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
            
retval, labels, centers = \
   cv2.kmeans(pixel_vals, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
   

f, axes = plt.subplots(4, 3, figsize=(25,15))

axes[0,0].set_title('Original')
axes[0,0].imshow(image_copy)

''' convert back into 8-bit image data values '''
''' flatten : Return a copy of the array collapsed into one dimension'''
centers = np.uint8(centers)   
segmented_data = centers[labels.flatten()]  
segmented_data = segmented_data.reshape(image_copy.shape)   

print('image shape: ', image.shape, ' image len: ',len(image_copy))
print('centers: ', centers)
Ejemplo n.º 48
0
    data = np.array(image)
    a2D = data.reshape(-1, data.shape[-1])
    #R G and B values of the image captured
    r = a2D[:, 0]
    g = a2D[:, 1]
    b = a2D[:, 2]

    #print(a2D)
    pixels = np.float32(image.reshape(-1, 3))

    #Use Kmeans to find the dominant colors in the picture
    n_colors = 5
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .1)
    flags = cv2.KMEANS_RANDOM_CENTERS

    _, labels, palette = cv2.kmeans(pixels, n_colors, None, criteria, 10,
                                    flags)
    _, counts = np.unique(labels, return_counts=True)

    #Pick the RGB color with the highest count (the dominant)

    dominant = palette[np.argmax(counts)]
    print(dominant)

    #Uncomment to show the color in matplotlib
    #plt.imshow([[[int(dominant[0]),int(dominant[1]),int(dominant[2])]]])
    #plt.show()


def signal_handler(signal, frame):
    videoCaptureObject.release()
    cv2.destroyAllWindows()
Ejemplo n.º 49
0
img = cv2.resize(img, (480, 360))
### Reshaping the image array into a column vector
temp = img.reshape(-1, 3)
### Converting the dataype of pixels as float 32
features = np.float32(temp)

### RUNNING K-MEANS
### Specifying the criteria of intented accuracy and iterations
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 1.0)

### Flags to specify how initial centers are taken
flags = cv2.KMEANS_RANDOM_CENTERS

### Number of clusters
K = 3
compactness, labels, centers = cv2.kmeans(features, K, None, criteria, 30,
                                          flags)

### Reforming the image
centers = np.uint8(centers)
out = centers[labels.flatten()]
output = out.reshape(img.shape)

### Gaussian Blur
blur = cv2.GaussianBlur(output, (3, 3), 0)

### Canny Edge Detection
seg = cv2.Canny(blur, 50, 50)

### Creating the duplicate copy of the image
temp = img.copy()
Ejemplo n.º 50
0
if __name__ == '__main__':
    cluster_n = 5
    img_size = 512

    print(__doc__)

    # generating bright palette
    colors = np.zeros((1, cluster_n, 3), np.uint8)
    colors[0, :] = 255
    colors[0, :, 0] = np.arange(0, 180, 180.0 / cluster_n)
    colors = cv.cvtColor(colors, cv.COLOR_HSV2BGR)[0]

    while True:
        print('sampling distributions...')
        points, _ = make_gaussians(cluster_n, img_size)

        term_crit = (cv.TERM_CRITERIA_EPS, 30, 0.1)
        ret, labels, centers = cv.kmeans(points, cluster_n, None, term_crit,
                                         10, 0)

        img = np.zeros((img_size, img_size, 3), np.uint8)
        for (x, y), label in zip(np.int32(points), labels.ravel()):
            c = list(map(int, colors[label]))

            cv.circle(img, (x, y), 1, c, -1)

        cv.imshow('gaussian mixture', img)
        ch = cv.waitKey(0)
        if ch == 27:
            break
        cv.destroyAllWindows()
from matplotlib import pyplot as plt
from os.path import expanduser

home = expanduser("~")
prefix = home + '/gabor/'

# /home/drishi/Kaggle Dataset/imgs_head/

# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.1)
K = int(sys.argv[1])

root = home + '/gabor/'
j = 1
for subdir, dirs, files in os.walk(root + 'Masked Files'):
    print 'subdie is ', subdir
    for file1 in files:
        print 'file name is ', j
        j = j + 1
        img = cv2.imread(root + 'Masked Files/' + file1, 0)
        Z = img.reshape((-1, 1))
        Z = np.float32(Z)
        #K=4
        ret, label, center = cv2.kmeans(Z, K, criteria, 25,
                                        cv2.KMEANS_RANDOM_CENTERS)
        center = np.uint8(center)
        res = center[label.flatten()]
        res2 = res.reshape((img.shape))
        des = root + 'KmeansOnMasked/' + file1
        cv2.imwrite(des, res2)
def descriptors():

    ksize = 100
    des = None
    img_descs = None
    word = np.full(ksize, 0)

    idf = np.full(ksize, 0)
    np.array(img_descs)
    image_path = '../data/'
    img_paths = glob.glob(image_path + '*.jpg')
    #img_paths=np.asarray(img_paths)
    img_paths = sorted(
        img_paths, key=lambda name: int(name.split('/')[-1].split('.')[-2]))

    step = len(img_paths) / 100
    for image in img_paths:
        print(image)

        trans_image_path = image_path + image.split('/')[-1].split(
            '.')[-2] + '/'
        trans_img_path = glob.glob(trans_image_path + '*.jpg')
        img = cv.imread(image)
        #resize_to = 640
        h, w, channels = img.shape
        kp, des = sift(image)
        '''
        for trans_image in trans_img_path:

            trans_img = trans_image+'*.py'
            kp2, des2 = sift(trans_img)
        '''

        if img_descs is not None:
            img_descs = np.vstack((img_descs, des))
        else:
            img_descs = des
            #print(img_descs)

        criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 10, 1.0)
        ret, label, center = cv.kmeans(img_descs, ksize, None, criteria, 10,
                                       cv.KMEANS_RANDOM_CENTERS)

    all_representation, all_labels = representation(img_paths)
    word2 = np.full(ksize, 0)
    for labels in all_labels:
        for i in range(0, ksize):
            #print word2[i]
            word2[i] = np.sum(labels.ravel() == i)
            x = any(labels.ravel() == i)
            #print(word2[i])
            #print(any(label.ravel()==i))
            if x == False:
                print("attention!")
            if word2[i] != 0:
                #print(word2[i],i)
                word[i] = word[i] + 1
        print(sum(word2))
    idf[i] = math.log(len(img_paths) / word[i])
    print(word)

    return center, label
Ejemplo n.º 53
0
image  = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# reshape the image to a 2D array of pixels and 3 color values (RGB)
pixel_values = image.reshape((-1, 3))
# convert to float
pixel_values = np.float32(pixel_values)

print(pixel_values.shape)

# define stopping criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)

# number of clusters (K)
k = 14
_, labels, (centers) = cv2.kmeans(pixel_values, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

# convert back to 8 bit values
centers = np.uint8(centers)

# flatten the labels array
labels = labels.flatten()

# convert all pixels to the color of the centroids
segmented_image = centers[labels.flatten()]

# reshape back to the original image dimension
segmented_image = segmented_image.reshape(image.shape)
# show the image
plt.imshow(segmented_image)
plt.show()
Ejemplo n.º 54
0
import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('./imgs/Lena.jpg').astype(np.float32) / 255

image_lab = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)

data = image_lab.reshape((-1, 3))

num_classes = 4

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 50, 0.1)
_, labels, centers = cv2.kmeans(data, num_classes, None, criteria, 10,
                                cv2.KMEANS_RANDOM_CENTERS)

segmented_lab = centers[labels.flatten()].reshape(image.shape)
segmented = cv2.cvtColor(segmented_lab, cv2.COLOR_Lab2RGB)

plt.subplot(121)
plt.axis('off')
plt.title('original')
plt.imshow(image[:, :, [2, 1, 0]])
plt.subplot(122)
plt.axis('off')
plt.title('semented')
plt.imshow(segmented)
plt.show()
Ejemplo n.º 55
0
                    feat = feat / cv2.norm(feat, cv2.NORM_L2)

            all_feats.append(feat)

        label_count = label_count + 1

    all_feats = np.concatenate(all_feats, axis=0)
    all_feats = np.array(all_feats)
    all_feats = np.float32(all_feats)
    all_feats = np.ascontiguousarray(all_feats)
    print all_feats.shape

    print "Clustering ", all_feats.shape, " features..."
    outkmeans = cv2.kmeans(data=all_feats,
                           K=num_clusters,
                           bestLabels=None,
                           criteria=criteria,
                           attempts=10,
                           flags=flags)
    print "Done!"
    class_dict = outkmeans[2]
    print outkmeans[2].shape
    print outkmeans[2]

    iocsv.writeNPY(np.asarray(class_dict), dictionary_output)
    exit()

    #class_dict = iocsv.readNPY(dictionary_output + '.bkp.npz')

    with_dl = True
    if with_dl == True:
Ejemplo n.º 56
0
def refine_sky(bopt, image):
    sky_mask = make_mask(bopt, image)

    ground = np.ma.array(
        image,
        mask=cv2.cvtColor(cv2.bitwise_not(sky_mask), cv2.COLOR_GRAY2BGR)
    ).compressed()
    sky = np.ma.array(
        image,
        mask=cv2.cvtColor(sky_mask, cv2.COLOR_GRAY2BGR)
    ).compressed()
    ground.shape = (ground.size//3, 3)
    sky.shape = (sky.size//3, 3)

    ret, label, center = cv2.kmeans(
        np.float32(sky),
        2,
        None,
        (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0),
        10,
        cv2.KMEANS_RANDOM_CENTERS
    )

    sigma_s1, mu_s1 = cv2.calcCovarMatrix(
        sky[label.ravel() == 0],
        None,
        cv2.COVAR_NORMAL | cv2.COVAR_ROWS | cv2.COVAR_SCALE
    )
    ic_s1 = cv2.invert(sigma_s1, cv2.DECOMP_SVD)[1]

    sigma_s2, mu_s2 = cv2.calcCovarMatrix(
        sky[label.ravel() == 1],
        None,
        cv2.COVAR_NORMAL | cv2.COVAR_ROWS | cv2.COVAR_SCALE
    )
    ic_s2 = cv2.invert(sigma_s2, cv2.DECOMP_SVD)[1]

    sigma_g, mu_g = cv2.calcCovarMatrix(
        ground,
        None,
        cv2.COVAR_NORMAL | cv2.COVAR_ROWS | cv2.COVAR_SCALE
    )
    icg = cv2.invert(sigma_g, cv2.DECOMP_SVD)[1]

    if cv2.Mahalanobis(mu_s1, mu_g, ic_s1) > cv2.Mahalanobis(mu_s2, mu_g, ic_s2):
        mu_s = mu_s1
        sigma_s = sigma_s1
        ics = ic_s1
    else:
        mu_s = mu_s2
        sigma_s = sigma_s2
        ics = ic_s2

    for x in range(image.shape[1]):
        cnt = np.sum(np.less(
            spatial.distance.cdist(
                image[0:bopt[x], x],
                mu_s,
                'mahalanobis',
                VI=ics
            ),
            spatial.distance.cdist(
                image[0:bopt[x], x],
                mu_g,
                'mahalanobis',
                VI=icg
            )
        ))

        if cnt < (bopt[x] / 2):
            bopt[x] = 0

    return bopt
Ejemplo n.º 57
0
size_list_new = []
for size in size_list:
    if sizeMax * 0.9 >= size >= sizeMin * 0.9:
        size_list_new.append(size)

size_list = np.float32(size_list_new)
# plt.hist(size_list, 50), plt.show()

# K_Means
# Define criteria = ( type, max_iter = 10 , epsilon = 1.0 )
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
# Set flags (Just to avoid line break in the code)
flags = cv2.KMEANS_RANDOM_CENTERS
# Apply KMeans
compactness, labels, centers = cv2.kmeans(size_list, 3, None, criteria, 10,
                                          flags)
# print(labels)
# plt show
A = []
B = []
C = []
for x, y in zip(size_list, labels):
    if y == 0:
        A.append(x)
    elif y == 1:
        B.append(x)
    else:
        C.append(x)
# Now plot 'A' in red, 'B' in blue, 'centers' in yellow
plt.hist(A, 100, color='r')
plt.hist(B, 100, color='g')
#matplotlib.use('Qt5Agg', warn=False)


image = cv2.imread(inputName)

start=timer()


# reshape data to 1D BGR vector
Z = image.reshape((-1,3))
Z = np.float32(Z)

# clustering pixels:  define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, iterations, 1.0)
compactness,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
# compactness : It is the sum of squared distance from each point to their corresponding centers.
# En center bbtenemos K valores, tantos como etiquetas hemos elegido  

if 0:
   # NOTA:     
    #otro operador que haace algo parecido a kmeans es meanshift, que devuelve para cada región el máximo local 
    # (dependerá de la configuración de parámetros las características de la región asociada a cada máximo local)
    # Con este operador se generan más etiquetas distintas (todos los máximos locales detectados) pero todavía puede 
    # haber varios blobs con la misma etiqueta (puede haber dos máximos locales exactamente iguales en toda la imagen)
    # Explicación: http://seiya-kumada.blogspot.com/2013/05/mean-shift-filtering-practice-by-opencv.html
    # USO: 
    imagenClusterizadaConMeanShift= cv2.pyrMeanShiftFiltering(img, 20, 55)

# Convert back into uint8, and make original image
center = np.uint8(center)
Ejemplo n.º 59
0
            new_pixel[:, :, 1] = new_avg_color[1]
            new_pixel[:, :, 2] = new_avg_color[2]
            # transform to HSV color
            hsv_avg_color = cv2.cvtColor(new_pixel, cv2.COLOR_BGR2HSV_FULL)
            # get h channel
            h_avg_color = hsv_avg_color[:, :, 0].astype(np.float32)

            ###
            # get dominant color from tag and transfer to HSV colors for kNN
            ###
            # get dominant color
            pixels = np.float32(tag.reshape(-1, 3))
            n_colors = 5
            criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,
                        200, .1)
            _, labels, palette = cv2.kmeans(pixels, n_colors, None, criteria,
                                            10, cv2.KMEANS_RANDOM_CENTERS)
            _, counts = np.unique(labels, return_counts=True)
            new_dominant_color = palette[np.argmax(counts)]

            # opencv friendly array
            new_pixel = np.zeros((1, 1, 3), dtype=np.uint8)
            new_pixel[:, :, 0] = new_dominant_color[0]
            new_pixel[:, :, 1] = new_dominant_color[1]
            new_pixel[:, :, 2] = new_dominant_color[2]
            # transform to HSV color
            hsv_dominant_color = cv2.cvtColor(new_pixel,
                                              cv2.COLOR_BGR2HSV_FULL)
            # get h channel
            h_dominant_color = hsv_dominant_color[:, :, 0].astype(np.float32)

            # print("fruit:", fruit_list[hue_index], "average:", h_avg_color[0], "dominant:", h_dominant_color[0])
plt.imshow(image_copy)
plt.show()



#prepare data for k means
pixel_vals=image_copy.reshape((-1,3))
pixel_vals=np.float32(pixel_vals)

#implement the k Means clustring 
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.1)

## TODO: Select a value for k
# then perform k-means clustering
k = 2
retval, labels, centers = cv2.kmeans(pixel_vals, k, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

# convert data into 8-bit values
centers = np.uint8(centers)
segmented_data = centers[labels.flatten()]

# reshape data into the original image dimensions
segmented_image = segmented_data.reshape((image.shape))
labels_reshape = labels.reshape(image.shape[0], image.shape[1])

plt.imshow(segmented_image)

plt.show()

plt.imshow(labels_reshape==1,cmap='gray')
plt.show()