def main(): """ change the parameter in sp_label20 , sp_label19 , a and b . You can show the seg tracking from previous frame you want. """ mat = scipy.io.loadmat( "/Users/zhufucai/Documents/GitHub/TSP_matlab/results/sp_labels_banana.mat" ) # image no. 100 ; mat no. 99 : because mat start at 0 n image start at 1 !!!! sp_labels2 = mat['sp_labels'][:, :, 94] sp_labels2 = np.array(sp_labels2, dtype=np.int32) # sp_labels1 = mat['sp_labels'][:,:,18] sp_labels1 = mat['sp_labels'][:, :, 81] a = Joe_segmentation('./frame/0008s2.jpg', './svmfile.pkl') sp20 = np.array(tranpose(sp_labels2), dtype=np.int32) sp19 = np.array(tranpose(sp_labels1), dtype=np.int32) [result, list_seg19, list_seg19_proba] = a.segmentation_tsp(sp19) result19 = np.array(result, dtype=np.uint8) seg = tracking(list_seg19, sp_labels1, sp_labels2) contours_seg = slic.contours(seg, sp20) b = Joe_segmentation('./frame/00095.jpg', './svmfile.pkl') [result, list_seg20, list_seg20_proba] = b.segmentation_tsp(sp20) result20 = np.array(result, dtype=np.uint8) # sp20 = np.array(tranpose(sp_labels2),dtype = np.int32) contours20 = slic.contours(result20, sp20) # print np.unique(sp19) # print "\n" # print list_seg19_proba # print "\n" # print list_seg20_proba # combine labels combination_result = label_combination(np.unique(sp19), list_seg19_proba, list_seg20_proba, sp20) contours_combination = slic.contours(combination_result, sp20) cv2.imshow("current_image", b.original_image) cv2.imshow("result_track", seg) cv2.imshow("result_seg", result20) cv2.imshow("combination_result", combination_result) cv2.waitKey(0)
def color_point(img, index): temp = np.ones([img.shape[0], img.shape[1]], dtype=np.uint16) for i in range(500): temp[index[i][0], index[i][1]] = 255 out = cv2.merge((temp, temp, temp)) out = np.array(out, dtype=np.uint8) result = slic.contours(out, seg.slic_result) return result
def show_label(self, pyr): ''' Update map when click Given QPixmap self.query ''' try: self.npimg except: # self.npimg not exist qimage = self.query.toImage() imgarr = rgb_view(qimage) self.npimg = imresize(imgarr, (self.h, self.w), interp='bicubic') contours = slic.contours(self.npimg, self.label0, 10) draw0 = contours[:, :, :-1] self.drawMask(draw0, self.label0, self.mergeSegs[0], 0) if pyr is not 0: contours = slic.contours(self.npimg, self.auxlabel, 10) draw = contours[:, :, :-1] self.drawMask(draw, self.auxlabel, self.mergeSegs[pyr], pyr) # show reference label draw = self.npimg.copy() xx = np.tile(np.asarray(range(self.w)), (self.h, 1)) yy = np.tile(np.asarray(range(self.h)), (self.w, 1)).transpose() idx_x = abs(xx - self.label[0].cx) < 5 idx_y = abs(yy -self.label[0].cy) < 5 for i in range(3): draw[:, :, i][idx_x & idx_y] = 255 qimage = array2qimage(draw) qpixmap = QPixmap.fromImage(qimage) self.reflabel.setPixmap(qpixmap.scaled(self.reflabel.size(), Qt.KeepAspectRatio))
def test_tsp(sp_labels): output_loc = "./segmentation_result/" a = Joe_segmentation('./frame/00020.jpg', './svmfile.pkl') result = np.array(a.segmentation_tsp(sp_labels), dtype=np.uint8) # contours = slic.contours(result, a.slic_result) sp = np.array(tranpose(sp_labels), dtype=np.int32) contours = slic.contours(result, sp) cv2.imshow("original", a.original_image) cv2.imshow("result", contours) cv2.waitKey(0)
def generate_semantic_segmantation_result(): output_loc = "./segmentation_result/" try: os.mkdir(output_loc) except OSError: pass range(100) for i in range(100): print i a = Joe_segmentation('./frame/%#05d.jpg' % (i + 1), './svmfile.pkl') result = np.array(a.segmentation(), dtype=np.uint8) region_labels = a.slic_result contours = slic.contours(result, region_labels) cv2.imwrite(output_loc + "/%#05d.jpg" % (i + 1), contours)
def color_point(self, img, index): ''' color_point(img,index): img: the canvas you want to draw the points on index: index (x,y) of centroids of superpixels ''' temp = np.ones([img.shape[0], img.shape[1]], dtype=np.uint16) for i in range(self.number_regions): temp[int(index[i][0]), int(index[i][1])] = 255 out = cv2.merge((temp, temp, temp)) out = np.array(out, dtype=np.uint8) result = slic.contours(out, self.slic_result) return result
def test_something_wrong(): # c = svm_training('./feature/outfile_sk.npy', './feature/outfile_fl.npy', './feature/outfile_ot.npy') a = Joe_segmentation('./frame/00020.jpg', './svmfile.pkl') result = np.array(a.segmentation(), dtype=np.uint8) print result # slic labels dtype will cuase buffer mismatch if it's changed into other type i.e. uint8 rather than original uint8_t(according to .dtype = int32) region_labels = a.slic_result im = np.array(a.original_image, dtype=np.uint16) sum_temp = np.add(result, im) im[np.where(sum_temp > 255)] = 255 im = np.array(im, dtype=np.uint8) contours = slic.contours(result, region_labels, 1) cv2.imshow("result", contours) cv2.waitKey(0) return True
def generate_tsp_seg_result(): output_loc = "./segmentation_result/" try: os.mkdir(output_loc) except OSError: pass range(100) for i in range(100): print i a = Joe_segmentation('./frame/%#05d.jpg'%(i+1), './svmfile.pkl') mat = scipy.io.loadmat("/Users/zhufucai/Documents/GitHub/TSP_matlab/results/sp_labels_banana.mat") sp_labels = np.array(mat['sp_labels'][:,:,i], dtype = np.int32) print sp_labels.shape result = np.array(a.segmentation_tsp(sp_labels),dtype=np.uint8) # fix the problem of mat & C++ dont share the same indexing way sp = np.array(tranpose(sp_labels,sp_labels.shape[0],sp_labels.shape[1]),dtype = np.int32) contours = slic.contours(result, sp) cv2.imwrite(output_loc + "/%#05d.jpg"%(i+1), contours)
import numpy as np import PIL.Image import matplotlib.pyplot as plt import slic im = np.array(PIL.Image.open("grass.jpg")) region_labels = slic.slic_n(im, 1000, 10) print type(region_labels) contours = slic.contours(im, region_labels, 10) plt.imshow(contours[:, :, :-1].copy()) plt.show()
import numpy as np import Image import matplotlib.pyplot as plt import slic im = np.array(Image.open("grass.jpg")) image_argb = np.dstack([im[:, :, :1], im]).copy("C") region_labels = slic.slic_n(image_argb, 1000, 10) slic.contours(image_argb, region_labels, 10) plt.imshow(image_argb[:, :, 1:].copy()) plt.show()
import numpy as np import Image import matplotlib.pyplot as plt import slic im = np.array(Image.open("tennis.png")) region_labels = slic.slic_n(im, 100, 10) contours = slic.contours(im, region_labels, 10) plt.imshow(contours[:, :, :-1].copy()) plt.show()