def meanshiftUsingPCA(path): # Load original image given the image path im = cv.LoadImageM(path) #convert image to YUV color space cv.CvtColor(im, im, cv.CV_BGR2YCrCb) # Load bank of filters filterBank = lmfilters.loadLMFilters() # Resize image to decrease dimensions during clustering resize_factor = 1 thumbnail = cv.CreateMat(im.height / resize_factor, im.width / resize_factor, cv.CV_8UC3) cv.Resize(im, thumbnail) # now work with resized thumbnail image response = np.zeros(shape=((thumbnail.height) * (thumbnail.width), 51), dtype=float) for f in xrange(0, 48): filter = filterBank[f] # Resize the filter with the same factor for the resized image dst = cv.CreateImage(cv.GetSize(thumbnail), cv.IPL_DEPTH_32F, 3) resizedFilter = cv.CreateMat(filter.height / resize_factor, filter.width / resize_factor, filter.type) cv.Resize(filter, resizedFilter) # Apply the current filter cv.Filter2D(thumbnail, dst, resizedFilter) for j in xrange(0, thumbnail.height): for i in xrange(0, thumbnail.width): # Select the max. along the three channels maxRes = max(dst[j, i]) if math.isnan(maxRes): maxRes = 0.0 if maxRes > response[thumbnail.width * j + i, f]: # Store the max. response for the given feature index response[thumbnail.width * j + i, f] = maxRes #YUV features count = 0 for j in xrange(0, thumbnail.height): for i in xrange(0, thumbnail.width): response[count, 48] = thumbnail[j, i][0] response[count, 49] = thumbnail[j, i][1] response[count, 50] = thumbnail[j, i][2] count += 1 #get the first 4 primary components using pca pca = PCA(response) pcaResponse = zeros([thumbnail.height * thumbnail.width, 4]) for i in xrange(0, thumbnail.height * thumbnail.width): pcaResponse[i] = pca.getPCA(response[i], 4) # Create new mean shift instance ms = MeanShift(bandwidth=10, bin_seeding=True) # Apply the mean shift clustering algorithm ms.fit(pcaResponse) labels = ms.labels_ n_clusters_ = np.unique(labels) print "Number of clusters: ", len(n_clusters_) repaintImage(thumbnail, labels) cv.Resize(thumbnail, im) return im
def meanshiftUsingPCA(path): # Load original image given the image path im = cv.LoadImageM(path) #convert image to YUV color space cv.CvtColor(im,im,cv.CV_BGR2YCrCb) # Load bank of filters filterBank = lmfilters.loadLMFilters() # Resize image to decrease dimensions during clustering resize_factor = 1 thumbnail = cv.CreateMat(im.height / resize_factor, im.width / resize_factor, cv.CV_8UC3) cv.Resize(im, thumbnail) # now work with resized thumbnail image response = np.zeros(shape=((thumbnail.height)*(thumbnail.width),51), dtype=float) for f in xrange(0,48): filter = filterBank[f] # Resize the filter with the same factor for the resized image dst = cv.CreateImage(cv.GetSize(thumbnail), cv.IPL_DEPTH_32F, 3) resizedFilter = cv.CreateMat(filter.height / resize_factor, filter.width / resize_factor, filter.type) cv.Resize(filter, resizedFilter) # Apply the current filter cv.Filter2D(thumbnail,dst,resizedFilter) for j in xrange(0,thumbnail.height): for i in xrange(0,thumbnail.width): # Select the max. along the three channels maxRes = max(dst[j,i]) if math.isnan(maxRes): maxRes = 0.0 if maxRes > response[thumbnail.width*j+i,f]: # Store the max. response for the given feature index response[thumbnail.width*j+i,f] = maxRes #YUV features count = 0 for j in xrange(0,thumbnail.height): for i in xrange(0,thumbnail.width): response[count,48] = thumbnail[j,i][0] response[count,49] = thumbnail[j,i][1] response[count,50] = thumbnail[j,i][2] count+=1 #get the first 4 primary components using pca pca = PCA(response) pcaResponse = zeros([thumbnail.height*thumbnail.width,4]) for i in xrange(0,thumbnail.height*thumbnail.width): pcaResponse[i] = pca.getPCA(response[i],4) # Create new mean shift instance ms = MeanShift(bandwidth=10,bin_seeding=True) # Apply the mean shift clustering algorithm ms.fit(pcaResponse) labels = ms.labels_ n_clusters_ = np.unique(labels) print "Number of clusters: ", len(n_clusters_) repaintImage(thumbnail,labels) cv.Resize(thumbnail, im) return im
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 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 meanshiftUsingILM(path): # Load original image given the image path im = cv2.LoadImageM(path) # Load bank of filters filterBank = lmfilters.loadLMFilters() # Resize image to decrease dimensions during clustering resize_factor = 1 thumbnail = cv2.CreateMat(im.height / resize_factor, im.width / resize_factor, cv2.CV_8UC3) cv2.Resize(im, thumbnail) # now work with resized thumbnail image response = np.zeros(shape=((thumbnail.height) * (thumbnail.width), 4), dtype=float) for f in range(0, 48): filter = filterBank[f] # Resize the filter with the same factor for the resized image dst = cv2.CreateImage(cv2.GetSize(thumbnail), cv2.IPL_DEPTH_32F, 3) resizedFilter = cv2.CreateMat(filter.height / resize_factor, filter.width / resize_factor, filter.type) cv2.Resize(filter, resizedFilter) # Apply the current filter cv2.Filter2D(thumbnail, dst, resizedFilter) featureIndex = getFilterTypeIndex(f) for j in range(0, thumbnail.height): for i in range(0, thumbnail.width): # Select the max. along the three channels maxRes = max(dst[j, i]) if math.isnan(maxRes): maxRes = 0.0 if maxRes > response[thumbnail.width * j + i, featureIndex]: # Store the max. response for the given feature index response[thumbnail.width * j + i, featureIndex] = maxRes # Create new mean shift instance ms = MeanShift(bandwidth=10, bin_seeding=True) # Apply the mean shift clustering algorithm ms.fit(response) labels = ms.labels_ n_clusters_ = np.unique(labels) print("Number of clusters: ", len(n_clusters_)) repaintImage(thumbnail, labels) cv2.Resize(thumbnail, im) return im
def meanshiftUsingILM(path): # Load original image given the image path im = cv.LoadImageM(path) # Load bank of filters filterBank = lmfilters.loadLMFilters() # Resize image to decrease dimensions during clustering resize_factor = 1 thumbnail = cv.CreateMat(im.height / resize_factor, im.width / resize_factor, cv.CV_8UC3) cv.Resize(im, thumbnail) # now work with resized thumbnail image response = np.zeros(shape=((thumbnail.height)*(thumbnail.width),4), dtype=float) for f in xrange(0,48): filter = filterBank[f] # Resize the filter with the same factor for the resized image dst = cv.CreateImage(cv.GetSize(thumbnail), cv.IPL_DEPTH_32F, 3) resizedFilter = cv.CreateMat(filter.height / resize_factor, filter.width / resize_factor, filter.type) cv.Resize(filter, resizedFilter) # Apply the current filter cv.Filter2D(thumbnail,dst,resizedFilter) featureIndex = getFilterTypeIndex(f) for j in xrange(0,thumbnail.height): for i in xrange(0,thumbnail.width): # Select the max. along the three channels maxRes = max(dst[j,i]) if math.isnan(maxRes): maxRes = 0.0 if maxRes > response[thumbnail.width*j+i,featureIndex]: # Store the max. response for the given feature index response[thumbnail.width*j+i,featureIndex] = maxRes # Create new mean shift instance ms = MeanShift(bandwidth=10,bin_seeding=True) # Apply the mean shift clustering algorithm ms.fit(response) labels = ms.labels_ n_clusters_ = np.unique(labels) print "Number of clusters: ", len(n_clusters_) repaintImage(thumbnail,labels) cv.Resize(thumbnail, im) return im
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]
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]
def constructFeatureVector(image, feature_type): v = None if feature_type == "INTENSITY": v = zeros((image.height,image.width,1)) elif feature_type == "INTENSITY+LOC": v = zeros((image.height,image.width,3)) elif feature_type == "RGB": v = zeros((image.height,image.width,3)) elif feature_type == "YUV": v = zeros((image.height,image.width,3)) elif feature_type == "ILM": v = zeros((image.height,image.width,4)) elif feature_type == "PCA": v = zeros((image.height,image.width,4)) def getFilterTypeIndex(index): if index >=0 and index <= 17: return 0 elif index >= 18 and index <= 35: return 1 elif index >= 36 and index <= 45: return 2 else: return 3 if feature_type in ["INTENSITY","INTENSITY+LOC","RGB","YUV"]: for y in range(0,image.height): for x in range(0, image.width): if feature_type == "INTENSITY": v[y,x,0] = image[y,x] elif feature_type == "INTENSITY+LOC": v[y,x,0] = image[y,x] v[y,x,1] = y v[y,x,2] = x elif feature_type == "RGB" or feature_type == "YUV": v[y,x,0] = image[y,x][0] v[y,x,1] = image[y,x][1] v[y,x,2] = image[y,x][2] elif feature_type == "ILM": filterBank = lmfilters.loadLMFilters() for f in xrange(0,48): filter = filterBank[f] dst = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 3) cv.Filter2D(image,dst,filter) featureIndex = getFilterTypeIndex(f) for j in xrange(0,image.height): for i in xrange(0,image.width): #take the maximum response from the 3 channels maxRes = max(dst[j,i]) if math.isnan(maxRes): maxRes = 0.0 #take the maximun over this feature if maxRes > v[j,i,featureIndex]: v[j,i,featureIndex] = maxRes elif feature_type == "PCA": filterBank = lmfilters.loadLMFilters() samples = zeros([image.width*image.height,51]) #lm filters features for f in xrange(0,48): filter = filterBank[f] dst = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 3) cv.Filter2D(image,dst,filter) count = 0 for j in xrange(0,image.height): for i in xrange(0,image.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,image.height): for i in xrange(0,image.width): samples[count,48] = image[j,i][0] samples[count,49] = image[j,i][1] samples[count,50] = image[j,i][2] count+=1 #get the first 4 primary components using pca count = 0 pca = PCA(samples) for j in xrange(0,image.height): for i in xrange(0,image.width): v[j,i] = pca.getPCA(samples[count],4) count+=1 return v