def majorityseg(original, mask): if hsvsegflag: hsvimg = cv2.cvtColor(original, cv2.COLOR_BGR2HSV) segmented_image, labels = seg.getSegments(hsvimg, md=MIN_DENSITY) else: segmented_image, labels = seg.getSegments(original, md=MIN_DENSITY) unique_labels = np.unique(labels) blank1 = original - original blank2 = original - original for label in unique_labels: #randomly paint blank1 b = random.randint(0, 255) g = random.randint(0, 255) r = random.randint(0, 255) blank1[labels == label] = [b, g, r] #find majority category and paint blank2 majority = -1 for cat in CATS: tmp = mask[labels == label] count = np.count_nonzero(np.all(tmp == cat, axis=1)) if count > majority: classification = cat majority = count blank2[labels == label] = classification cv2.imshow('ms_segmentation', blank1) cv2.imshow('majority segmentation', blank2) cv2.waitKey(0) return blank1, blank2
def extractTestingBlobs(img, gt, hsv=False): instances = [] labels = [] if hsv: print('HSV SEGMENTATION') tmp = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) seg_img, markers = seg.getSegments(tmp, False, sr=5, rr=5, md=1000) else: print('BGR SEGMENTATION') seg_img, markers = seg.getSegments(img, False, sr=1, rr=1, md=1000) #go through each segment marks = np.unique(markers) for uq_mark in marks: #get the segment and append it to inputs region = img.copy() region[markers != uq_mark] = [0, 0, 0] gtregion = gt.copy() gtregion[markers != uq_mark] = [0, 0, 0] #opencv bounding rect only works on single channel images... blank = img.copy() blank = blank - blank blank[markers == uq_mark] = [255, 255, 255] grey = cv2.cvtColor(blank, cv2.COLOR_BGR2GRAY) x, y, w, h = cv2.boundingRect(grey) #crop the colored region cropped = region[y:y + h, x:x + w] mask = markers[y:y + h, x:x + w] #tile the cropped region segment = seg.getTiledSegment(cropped, mask == uq_mark) #find out what the label is majority = -1 for i, cat in enumerate(constants.CATS): tmp = gtregion[markers == uq_mark] count = np.count_nonzero(np.all(tmp == cat, axis=1)) if count > majority: classification = constants.CATS_ONEHOT[i] majority = count #append the instance and its classification label labels.append(classification) instances.append(segment.astype(np.uint8)) #return the testing instances and labels return instances, labels, markers
def extractBlobs(img, fout="unsupervised_segmentation.png", hsv=False): blobs = [] if hsv: print('HSV SEGMENTATION') hsvimg = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) seg_img, markers = seg.getSegments(hsvimg, sr=5, rr=5, md=1000) else: print('BGR SEGMENTATION') seg_img, markers = seg.getSegments(img, sr=1, rr=1, md=1000) labels = np.unique(markers) canvas = img.copy() for uq_mark in labels[1:]: #get the segment and append it to inputs region = img.copy() region[markers != uq_mark] = [0, 0, 0] #opencv bounding rect only works on single channel images... blank = img.copy() blank = blank - blank blank[markers == uq_mark] = [255, 255, 255] grey = cv2.cvtColor(blank, cv2.COLOR_BGR2GRAY) x, y, w, h = cv2.boundingRect(grey) cropped = region[y:y + h, x:x + w] cropped = np.uint8(cropped) blobs.append(cropped) b = randint(0, 255) g = randint(0, 255) r = randint(0, 255) canvas[markers == uq_mark] = [b, g, r] cv2.imwrite(fout, canvas) print("Unsupervised segmentation saved! %s" % fout) return blobs, markers, labels
def saveTiledSegments(directory, dirout='', bg=False): cat1_list = os.listdir(directory) for f1 in cat1_list: full_dir1 = directory + f1 img = cv2.imread(full_dir1, cv2.IMREAD_COLOR) original = cv2.resize(img, (1000, 1000), interpolation=cv2.INTER_CUBIC) segimg, segmask = segmentModule.getSegments(original, sr=1, rr=1, md=1000) segmentModule.saveTiledSegments(original, segmask, category=f1, outdir=dirout)
def saveMSSegmentsHSV(directory, dirout='', bg=False): cat1_list = os.listdir(directory) for f1 in cat1_list: full_dir1 = directory + f1 fileout = os.path.splitext(f1)[0] + '.png' image1 = cv2.imread(full_dir1, cv2.IMREAD_COLOR) original = cv2.cvtColor(image1, cv2.COLOR_BGR2HSV) seg_img, labels = segmentModule.getSegments(original, False, rr=5, sr=5) segmentModule.saveSegments(original, labels, dirout, fileout, showbg=bg)
def saveMSSegments(directory, dirout='', bg=False): cat1_list = os.listdir(directory) for f1 in cat1_list: full_dir1 = directory + f1 fileout = os.path.splitext(f1)[0] + '.png' tmp = cv2.imread(full_dir1, cv2.IMREAD_COLOR) original = cv2.resize(tmp, (1000, 1000), interpolation=cv2.INTER_CUBIC) segmented_image, labels = segmentModule.getSegments(original, False, md=1000, rr=1, sr=1) segmentModule.saveSegments(original, labels, dirout, fileout, showbg=bg)
def saveMSSegmentsBGRHSV(directory, dirout='', bg=False): cat1_list = os.listdir(directory) for f1 in cat1_list: full_dir1 = directory + f1 fileout = os.path.splitext(f1)[0] + '.png' bgr_img = cv2.imread(full_dir1, cv2.IMREAD_COLOR) bgr_small = cv2.resize(bgr_img, (1000, 1000), interpolation=cv2.INTER_CUBIC) hsv_img = cv2.cvtColor(bgr_small, cv2.COLOR_BGR2HSV) seg_img, labels = segmentModule.getSegments(hsv_img, False, md=1000, rr=5, sr=5) segmentModule.saveSegments(bgr_small, labels, dirout, fileout, showbg=bg)
def display(original, labels=None, SHOWFLAG=True): #if mode is meanshift, apply meanshift if msflag: image, labels = analyze.meanshift(original) print(labels) if SHOWFLAG: seg.showSegments(image, labels) #if mode is plt raws elif prawflag: analyze.plotRawPixelOutput(original) return 1 #if mode is meanshiftbin, convert 2d image to 3d using bin method and apply meanshift elif msbinflag: image, labels = analyze.meanshift(original, binning=True) print(labels) if SHOWFLAG: seg.showSegments(image, labels) #if mode is fjmeanshift, do fjmeanshift elif fjmsflag: if SHOWFLAG: image, labels = seg.getSegments(original, True) else: image, labels = seg.getSegments(original, False) print(labels) #if mode is fjmeanshift, do fjmeanshift elif qsflag: labels = analyze.quickmeanshift(original) if SHOWFLAG: seg.showSegments(original, labels) #if mode is meanshiftbin, convert 2d image to 3d using bin method and apply meanshift elif dbscanflag: image, labels = analyze.dbscan(original, binning=True) print(labels) if SHOWFLAG: seg.showSegments(image, labels) #if mode is size elif sizeflag: combined_filename = sys.argv[1] # Generate and save blob size for this blob we assume black as background size = analyze.extractBlobSize(original) print('--------------SIZE---------------') if SHOWFLAG: print(size) return size #if mode is hog, show hog feature vector of image elif hogflag: #hist = analyze.extractHOG(original,False) #featurevector = hist.flatten() #norm = analyze.normalize(featurevector) analyze.visualizeHOG(original) #print('-------------HOG----------------') #if SHOWFLAG: # analyze.displayHistogram(featurevector) return 1 #if mode is wavelettransform elif wtflag: analyze.visualizeWT(original, show=True) return 1 #if mode is gabor, extract gabor feature from image using several orientations elif gaborflag: orientations = 16 filters = gabor.build_filters(orientations) combined_filename = sys.argv[1] # Generate and save ALL hogs for this image result = gabor.run_gabor(original, filters, combined_filename, orientations, mode='training') featurevector = result.flatten()[1:] norm = analyze.normalize(featurevector) print('--------------Gabor---------------') if SHOWFLAG: analyze.displayHistogram(featurevector, 'r--') return norm #if mode is color, show color histogram of image elif colorflag: hist = analyze.extractColorHist(original, False) print('-------------Color----------------') if SHOWFLAG: analyze.displayHistogram(hist) return hist elif bifflag: r = original[:, :, 2] g = original[:, :, 1] b = original[:, :, 0] y, x = np.where(r >= 0) out1 = analyze.bilinear_interpolate(r, x, y) y, x = np.where(g >= 0) out2 = analyze.bilinear_interpolate(g, x, y) y, x = np.where(b >= 0) out3 = analyze.bilinear_interpolate(b, x, y) cv2.namedWindow('bilinear filtering red channel', cv2.WINDOW_NORMAL) cv2.namedWindow('bilinear filtering green channel', cv2.WINDOW_NORMAL) cv2.namedWindow('bilinear filtering blue channel', cv2.WINDOW_NORMAL) cv2.imshow('bilinear filtering red channel', out1.astype(np.uint8)) cv2.imshow('bilinear filtering green channel', out2.astype(np.uint8)) cv2.imshow('bilinear filtering blue channel', out3.astype(np.uint8)) cv2.waitKey(0) return 1 elif binflag: hist = analyze.extractbinHist(original, False) norm = analyze.normalize(hist) if SHOWFLAG: analyze.displayHistogram(norm) return norm elif hsvflag: hsvimg = cv2.cvtColor(original, cv2.COLOR_BGR2HSV) hist = analyze.extractHSVHist(hsvimg, False) if SHOWFLAG: analyze.displayHistogram(hist) return hist
if __name__ == '__main__': if len(sys.argv) >= 2: #if user input is doing lda or pca if os.path.isfile(sys.argv[1]) and (ldaflag or pcaflag or qdaflag): if os.path.splitext(sys.argv[1])[1] == '.npy': tmp = np.load(sys.argv[1], mmap_mode='r') instances = tmp[:, :-1].astype(float) labels = tmp[:, -1:].astype(int) elif os.path.isfile(sys.argv[2]): img = cv2.imread(sys.argv[1], cv2.IMREAD_COLOR) mask = cv2.imread(sys.argv[2], cv2.IMREAD_COLOR) seg_image, labels = seg.getSegments(img, False) scatter(instances, labels) #if user input to visuzlize is an image file else: #evaluate single image #check if the image was read in correctly if os.path.isfile(sys.argv[1]): if os.path.splitext(sys.argv[1])[1] == '.npy' and prawflag: original = np.load(sys.argv[1]) else: original = cv2.imread(sys.argv[1], cv2.IMREAD_COLOR) display(original) else: print('invalid image! Could not open: %s' % sys.argv[1])
index = i tmp = a return index #main program #When using a single classification if len(sys.argv) == 3: imageFileIn = sys.argv[1] classificationsIn = sys.argv[2] #initialize image, markers using segmentModule #initialize classifications using classificationsIn #segmentModule.getSegments always produces the same result so this works. Since classification for each segment is known using same function in execute.py. original = segmentModule.normalizeImage(imageFileIn) image, markers = segmentModule.getSegments(original, False) uniquemarkers = np.unique(markers) classifications = [] with open(classificationsIn,'r') as cin: lines = cin.read().splitlines() for l in lines: classifications.append(float(l)) if len(classifications) == len(uniquemarkers): blank = image.copy() blank = blank - blank blank[markers == -1] = UNKNOWN for c,um in zip(classifications,uniquemarkers): if c > 0: blank[markers == um] = INGROUP
def display(original, labels=None, SHOWFLAG=True): #if mode is meanshift, apply meanshift if msflag: image, labels = analyze.meanshift(original) print(labels) if SHOWFLAG: seg.showSegments(image, labels) #if mode is meanshiftbin, convert 2d image to 3d using bin method and apply meanshift elif msbinflag: image, labels = analyze.meanshift(original, binning=True) print(labels) if SHOWFLAG: seg.showSegments(image, labels) #if mode is fjmeanshift, do fjmeanshift elif fjmsflag: if SHOWFLAG: image, labels = seg.getSegments(original, True) else: image, labels = seg.getSegments(original, False) print(labels) #if mode is fjmeanshift, do fjmeanshift elif qsflag: labels = analyze.quickmeanshift(original) if SHOWFLAG: seg.showSegments(original, labels) #if mode is meanshiftbin, convert 2d image to 3d using bin method and apply meanshift elif dbscanflag: image, labels = analyze.dbscan(original, binning=True) print(labels) if SHOWFLAG: seg.showSegments(image, labels) #if mode is size elif sizeflag: combined_filename = sys.argv[1] # Generate and save blob size for this blob we assume black as background size = analyze.extractBlobSize(original) print('--------------SIZE---------------') if SHOWFLAG: print(size) return size #if mode is dominant colors: elif domcolorflag: canvas = np.zeros(original.shape) h, w, d = canvas.shape for i in range(5, h - 6): for j in range(5, w - 6): centers = f.k_means_color(original[i - 5:i + 5, j - 5:j + 5, :]) canvas[i, j] = centers[0] cv2.imshow('original image', original) cv2.imshow('dominant color image', canvas.astype(np.uint8)) cv2.waitKey(0) ''' centers = f.k_means_color(original) cv2.imshow('original image',original) for i,c in enumerate(centers): tmp = np.full((100,100,3),c) cv2.imshow('dom color ' + str(i),tmp) cv2.waitKey(0) ''' return 1 #if mode is texture elif textureflag: return 1 #if mode is hog, show hog feature vector of image elif hogflag: hist = analyze.extractHOG(original, False) featurevector = hist.flatten() norm = analyze.normalize(featurevector) analyze.visualizeHOG(original) print('-------------HOG----------------') if SHOWFLAG: analyze.displayHistogram(featurevector) return norm #if mode is wavelettransform elif wtflag: analyze.visualizeWT(original, show=True) return 1 #if mode is gabor, extract gabor feature from image using several orientations elif gaborflag: orientations = 16 filters = gabor.build_filters(orientations) combined_filename = sys.argv[1] # Generate and save ALL hogs for this image result = gabor.run_gabor(original, filters, combined_filename, orientations, mode='training') featurevector = result.flatten()[1:] norm = analyze.normalize(featurevector) print('--------------Gabor---------------') if SHOWFLAG: analyze.displayHistogram(featurevector, 'r--') return norm #if mode is color, show color histogram of image elif colorflag: hist = analyze.extractColorHist(original, False) print('-------------Color----------------') if SHOWFLAG: analyze.displayHistogram(hist) return hist elif binflag: hist = analyze.extractbinHist(original, False) norm = analyze.normalize(hist) if SHOWFLAG: analyze.displayHistogram(norm) return norm elif bifflag: gray = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY) y, x = np.where(gray >= 0) out = analyze.bilinear_interpolate(gray, x, y).reshape((gray.shape)) cv2.namedWindow('bilinear filtering', cv2.WINDOW_NORMAL) cv2.imshow('bilinear filtering', out.astype(np.uint8)) cv2.waitKey(0) return 1 elif blurflag: analyze.blurErode(original) return 1 elif hsvflag: hsvimg = cv2.cvtColor(original, cv2.COLOR_BGR2HSV) hist = analyze.extractHSVHist(hsvimg, False) if SHOWFLAG: analyze.displayHistogram(hist) return hist
def threshseg2(original, raw_values, thresh_val=1): #segment the image if hsvsegflag: hsvimg = cv2.cvtColor(original, cv2.COLOR_BGR2HSV) segmented_image, labels = seg.getSegments(hsvimg, md=MIN_DENSITY) else: segmented_image, labels = seg.getSegments(original, md=MIN_DENSITY) #get the threshold matrix. #1. take sum of all positive predictions divided by the max value. The closer to 1 the better h, w, d = raw_values.shape raw_values += np.abs(np.min(raw_values, axis=2)).reshape((h, w, 1)) raw_values /= np.max(raw_values, axis=2).reshape((h, w, 1)) raw_values = np.nan_to_num(raw_values) #means = np.mean(raw_values,axis=2).reshape((h,w,1)) #raw_values[raw_values < means] = 0 thresh_matrix = np.sum(raw_values, axis=2) #get the mask mask = raw_values.argmax(axis=2) #obscure the low threshold pixels mask[thresh_matrix > thresh_val] = -1 mask[thresh_matrix <= 0] = -1 #display histogram h, w = thresh_matrix.shape[:2] hist = thresh_matrix.reshape(h * w) hist.sort() plt.plot(hist) plt.show() #paint a rgb mask rgb_mask = original rgb_mask[mask == -1] = [0, 0, 0] rgb_mask[mask == 0] = [0, 0, 255] rgb_mask[mask == 1] = [0, 255, 0] rgb_mask[mask == 2] = [255, 0, 0] rgb_mask[mask == 3] = [0, 255, 255] rgb_mask[mask == 4] = [255, 0, 255] rgb_mask[mask == 5] = [255, 255, 0] #create the segmented image with majority rule using classification categories unique_labels = np.unique(labels) blank1 = original - original blank2 = original - original for label in unique_labels: #randomly paint blank1 according to meanshift labels b = random.randint(0, 255) g = random.randint(0, 255) r = random.randint(0, 255) blank1[labels == label] = [b, g, r] #find majority category and paint blank2 majority = -1 for cat in CATS: tmp = rgb_mask[labels == label] count = np.count_nonzero(np.all(tmp == cat, axis=1)) if count > majority: classification = cat majority = count blank2[labels == label] = classification #show the results cv2.imshow('ms_segmentation', blank1) cv2.imshow('majority segmentation', blank2) cv2.imshow('thresholding mask', rgb_mask) cv2.waitKey(0) return blank1, blank2, rgb_mask
def threshseg(original, raw_values, thresh_val='median'): #segment the image if hsvsegflag: hsvimg = cv2.cvtColor(original, cv2.COLOR_BGR2HSV) segmented_image, labels = seg.getSegments(hsvimg, md=MIN_DENSITY) else: segmented_image, labels = seg.getSegments(original, md=MIN_DENSITY) #get the threshold matrix. #1. take sum of all positive predictions divided by the max value. The closer to 1 the better h, w, d = raw_values.shape raw_values += abs(raw_values.min()) raw_values /= float(raw_values.max()) #get the mask mask = raw_values.argmax(axis=2) #get the thresholded matrix raw_values.sort(axis=2) thresh_matrix = raw_values[:, :, -1] - raw_values[:, :, -2] #get threshold value medval = np.median(thresh_matrix[30:-30, 30:-30]) minval = thresh_matrix.min() maxval = thresh_matrix.max() meanval = np.mean(thresh_matrix) print('mean: %.4f' % meanval) print('min: %.4f' % minval) print('med: %.4f' % medval) print('max: %.4f' % maxval) if (thresh_val == 'median'): thresh_val = medval elif (thresh_val == 'mean'): thresh_val = meanval print('threshval: %.4f' % thresh_val) #obscure the low threshold pixels mask[thresh_matrix < thresh_val] = -1 #paint a rgb mask rgb_mask = original rgb_mask[mask == -1] = [0, 0, 0] rgb_mask[mask == 0] = [0, 0, 255] rgb_mask[mask == 1] = [0, 255, 0] rgb_mask[mask == 2] = [255, 0, 0] rgb_mask[mask == 3] = [0, 255, 255] rgb_mask[mask == 4] = [255, 0, 255] rgb_mask[mask == 5] = [255, 255, 0] #create the segmented image with majority rule using classification categories unique_labels = np.unique(labels) blank1 = original - original blank2 = original - original for label in unique_labels: #randomly paint blank1 according to meanshift labels b = random.randint(0, 255) g = random.randint(0, 255) r = random.randint(0, 255) blank1[labels == label] = [b, g, r] #find majority category and paint blank2 majority = -1 for cat in CATS: tmp = rgb_mask[labels == label] count = np.count_nonzero(np.all(tmp == cat, axis=1)) if count > majority: classification = cat majority = count blank2[labels == label] = classification #show the results cv2.imshow('ms_segmentation', blank1) cv2.imshow('majority segmentation', blank2) cv2.imshow('thresholding mask', rgb_mask) cv2.waitKey(0) return blank1, blank2, rgb_mask