def kmeansUsingIntensityAndLocation(im, k, iterations, epsilon): #create the samples and labels vector col = cv.Reshape(im, 1, im.width * im.height) samples = cv.CreateMat(col.height, 3, cv.CV_32FC1) count = 0 for j in xrange(0, im.height): for i in xrange(0, im.width): samples[count, 0] = im[j, i] samples[count, 1] = i samples[count, 2] = j count += 1 labels = cv.CreateMat(col.height, 1, cv.CV_32SC1) crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, iterations, epsilon) cv.KMeans2(samples, k, labels, crit) clusters = getClusters(col, samples, labels) #calculate the means means = {} for c in clusters: means[c] = sum(clusters[c]) / len(clusters[c]) for m in means: print m, means[m], len(clusters[m]) #apply clustering to the image for i in xrange(0, col.rows): lbl = labels[i, 0] col[i, 0] = means[lbl]
def kmeansUsingRGB(im, k, iterations, epsilon): #create the samples and labels vector col = cv.Reshape(im, 3, im.width * im.height) samples = cv.CreateMat(col.height, 1, cv.CV_32FC3) cv.Scale(col, samples) labels = cv.CreateMat(col.height, 1, cv.CV_32SC1) crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, iterations, epsilon) cv.KMeans2(samples, k, labels, crit) #calculate the means clusters = getClusters(col, samples, labels) means = {} for c in clusters: means[c] = [0.0, 0.0, 0.0] for v in clusters[c]: means[c][0] += v[0] means[c][1] += v[1] means[c][2] += v[2] means[c][0] /= len(clusters[c]) means[c][1] /= len(clusters[c]) means[c][2] /= len(clusters[c]) for m in means: print m, means[m], len(clusters[m]) #apply clustering to the image for i in xrange(0, col.rows): lbl = labels[i, 0] col[i, 0] = means[lbl]
def main(): img = cv.LoadImage("latex/Pictures/IMG_7324.CR2.jpg") img = normalize_rgb(img, aggressive=0.005) mask, seqs, time = get_mask_with_contour(img, ret_cont=True, ret_img=True, with_init_mask=False, time_took=True) boxes, min_rects = get_skin_rectangles(seqs) draw_boxes(boxes, img) center = [(c[0],c[1]) for c,_,_ in min_rects] verticies = [] for x,y,w,h in boxes: verticies+=[(x,y), (x+w,y),(x,y+h),(x+w,y+h)] # verticies = [(x+w/2, y+h/2) for x,y,w,h in boxes] polys = map(cv.BoxPoints, min_rects) cv.PolyLine(img, polys, True, cv.RGB(50,50,255), 2) sample_count = len(verticies) samples = cv.CreateMat(sample_count, 1, cv.CV_32FC2) clusters = cv.CreateMat(sample_count, 1, cv.CV_32SC1) [cv.Set1D(samples, i, verticies[i]) for i in range(sample_count)] cv.KMeans2(samples, 3, clusters, (cv.CV_TERMCRIT_EPS + cv.CV_TERMCRIT_ITER, 10, 1.0)) color = [cv.RGB(255,10,10), cv.RGB(255,255,10), cv.RGB(10,255,255), cv.RGB(255,10,255)] for i, xy in enumerate(verticies): cv.Circle(img, xy, 5, color[int(clusters[i,0])], thickness=-1) # np_centers = np.asarray(verticies) # result = cv.kmeans(verticies, 2, 0, 5, cv.CV_TERMCRIT_ITER) show_image(img)
def kmeansUsingLM(im, k, iterations, epsilon): #array of filter kernels filterBank = lmfilters.loadLMFilters() #create the samples and labels vector col = cv.Reshape(im, 3, im.width * im.height) samples = cv.CreateMat(col.height, 48, cv.CV_32FC1) for f in xrange(0, 48): filter = filterBank[f] dst = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_32F, 3) cv.Filter2D(im, dst, filter) count = 0 for j in xrange(0, im.height): for i in xrange(0, im.width): #take the maximum response from the 3 channels maxRes = max(dst[j, i]) if math.isnan(maxRes): maxRes = 0.0 samples[count, f] = maxRes count += 1 labels = cv.CreateMat(col.height, 1, cv.CV_32SC1) crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, iterations, epsilon) cv.KMeans2(samples, k, labels, crit) clusters = getClusters(col, samples, labels) means = {} for c in clusters: means[c] = [0.0, 0.0, 0.0] for v in clusters[c]: means[c][0] += v[0] means[c][1] += v[1] means[c][2] += v[2] means[c][0] /= len(clusters[c]) means[c][1] /= len(clusters[c]) means[c][2] /= len(clusters[c]) for m in means: print m, means[m], len(clusters[m]) #apply clustering to the image for i in xrange(0, col.rows): lbl = labels[i, 0] col[i, 0] = means[lbl]
def kmeansUsingPCA(im, k, iterations, epsilon): #convert image to YUV color space cv.CvtColor(im, im, cv.CV_BGR2YCrCb) #array of filter kernels filterBank = lmfilters.loadLMFilters() #create the samples and labels vector col = cv.Reshape(im, 3, im.width * im.height) #samples = cv.CreateMat(col.height, 51, cv.CV_32FC1) samples = zeros([col.height, 51]) for f in xrange(0, 48): filter = filterBank[f] dst = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_32F, 3) cv.Filter2D(im, dst, filter) count = 0 for j in xrange(0, im.height): for i in xrange(0, im.width): #take the maximum response from the 3 channels maxRes = max(dst[j, i]) if math.isnan(maxRes): maxRes = 0.0 samples[count, f] = maxRes count += 1 #YUV features count = 0 for j in xrange(0, im.height): for i in xrange(0, im.width): samples[count, 48] = im[j, i][0] samples[count, 49] = im[j, i][1] samples[count, 50] = im[j, i][2] count += 1 #get the first 4 primary components using pca pca = PCA(samples) pcaSamples = zeros([col.height, 4]) for i in xrange(0, col.height): pcaSamples[i] = pca.getPCA(samples[i], 4) samples = cv.fromarray(pcaSamples) samplesMat = cv.CreateMat(col.height, 4, cv.CV_32FC1) cv.Scale(samples, samplesMat) labels = cv.CreateMat(col.height, 1, cv.CV_32SC1) crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, iterations, epsilon) cv.KMeans2(samplesMat, k, labels, crit) clusters = getClusters(col, samplesMat, labels) means = {} for c in clusters: means[c] = [0.0, 0.0, 0.0] for v in clusters[c]: means[c][0] += v[0] means[c][1] += v[1] means[c][2] += v[2] means[c][0] /= len(clusters[c]) means[c][1] /= len(clusters[c]) means[c][2] /= len(clusters[c]) for m in means: print m, means[m], len(clusters[m]) #apply clustering to the image for i in xrange(0, col.rows): lbl = labels[i, 0] col[i, 0] = means[lbl]
# initialize data & label matrix samples = cv.CreateMat(numRows, numCols, cv.CV_32F) labels = cv.CreateMat(numRows, 1, cv.CV_32S) # remove row name from data for j in range(0, numRows): data[j].pop(0) # fill data matrix samples = cv.fromarray(np.array(data, np.float32)) # set ten iterations of the k-means algorithm criteria = (cv.CV_TERMCRIT_EPS + cv.CV_TERMCRIT_ITER, 10, 1.0) # k-means algorithm (implementation in OpenCV) cv.KMeans2(samples, clusters, labels, criteria) # get the cluster info into an array for j in range(0, numRows): cluster.append(int(cv.Get1D(labels, j)[0])) # prep output file output = open(outFile, "wb") writer = csv.writer(output, dialect='excel-tab') # write the first row firstRow.insert(1, "Cluster") writer.writerow(firstRow) for j in range(0, numRows): row = rows[j]
first = k*sample_count/cluster_count last = sample_count if k != cluster_count: last = (k+1)*sample_count/cluster_count point_chunk = cv.GetRows(points, first, last) cv.RandArr(rng, point_chunk, cv.CV_RAND_NORMAL, cv.Scalar(center[0], center[1], 0, 0), cv.Scalar(img.width*0.1, img.height*0.1, 0, 0)) # shuffle samples cv.RandShuffle(points, rng) cv.KMeans2(points, cluster_count, clusters, (cv.CV_TERMCRIT_EPS + cv.CV_TERMCRIT_ITER, 10, 1.0)) cv.Zero(img) for i in range(sample_count): cluster_idx = int(clusters[i, 0]) pt = (cv.Round(points[i, 0][0]), cv.Round(points[i, 0][1])) cv.Circle(img, pt, 2, color_tab[cluster_idx], cv.CV_FILLED, cv.CV_AA, 0) cv.ShowImage("clusters", img) key = cv.WaitKey(0) % 0x100 if key in [27, ord('q'), ord('Q')]: break cv.DestroyWindow("clusters")