def handle_read(self): try: data = self.recv(1024) if not data: return datalist = data.split(',') timestamp = datalist[2] for i in range( (len(datalist) - 3 )/4): if str(self.order) == datalist[ 3 + i*4]: x = np.double(datalist[4 + i*4]) y = np.double(datalist[5 + i*4]) """ create and pubish tranmerc """ gps_message = gpgga_tranmerc() gps_message.time = timestamp gps_message.northing = y/100 gps_message.easting = x/100 gps_message.fix = np.uint8(4) gps_message.sat = np.uint8(6) gps_message.hdop = np.double(1.0) self.gps_pub.publish(gps_message) except: pass
def CLQFilterDemo(self, img): # Constrained least square filter img = np.int16(img) noise = self.noiseGenerator.GuassNoise(img, 0, 10, np.int32(img.size)) nImg = img + noise H = self.GenerateHDemo(img.shape) fImg = np.fft.fftshift(np.fft.fft2(img)) gImg = np.fft.ifft2(np.fft.ifftshift(fImg * H)) gImg += noise fgImg = np.fft.fftshift(np.fft.fft2(gImg)) gamma = 0.3 l = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]]) p = np.zeros(img.shape) p[0:3, 0:3] = l P = np.fft.fftshift(np.fft.fft2(p)) N = 512 * 512 * (np.std(noise) ** 2.0 - np.mean(noise) ** 2.0) a = 0.1 ggImg = self.CLSFilterOptimal(fgImg, gamma, H, P, N, a) #ggImg = self.CLSFilter(fgImg, gamma, H, P) cv2.namedWindow("orig") cv2.imshow("orig", np.uint8(img)) cv2.namedWindow("g") cv2.imshow("g", np.uint8(gImg)) cv2.namedWindow("CLQ filter restore") cv2.imshow("CLQ filter restore", np.uint8(ggImg)) cv2.waitKey(0)
def updateWave(): global data, fData, time, ptList, freqList, line freqList = [] if len(ptList) < 2: fData = np.zeros(CHUNK, dtype=float) data = np.uint8(fData) return elif len(ptList) == 2: dist = np.sqrt((ptList[0]['xPos'] - ptList[1]['xPos'])**2. + (ptList[0]['yPos'] - ptList[1]['yPos'])**2. + (ptList[0]['zPos'] - ptList[1]['zPos'])**2.) freqList.append(int(BASEFREQ/dist)) else: for point1 in ptList: for point2 in ptList: if point1 is not point2: dist = np.sqrt((point1['xPos'] - point2['xPos'])**2. + (point1['yPos'] - point2['yPos'])**2. + (point1['zPos'] - point2['zPos'])**2.) freqList.append(int(BASEFREQ/dist)) fData = np.zeros(CHUNK, dtype=float) for freq in freqList: iFreq = float(int(freq/10.)) fData += np.sin(time*iFreq) fData = fData / np.max(np.abs(fData)) * 127 + 128 yData = np.abs(np.fft.fft(fData[:PLOTWIDTH])) yData /= 100. # yData /= yData.max() # yData = np.log(yData) yDataSwap = np.fft.fftshift(yData) line.set_ydata(yDataSwap) fig.canvas.draw() data = np.uint8(fData)
def write(self, struct_name, data_dict): """write data_dict under the group struct_name in the open hdf5 file :param struct_name: the identificatioon of the structure to write in the hdf5 :param data_dict: The python dictionnary containing the informations to write """ if self.file is None: info = "No file currently open" logger.info(info) return group_l1 = self.file.create_group(struct_name) group_l1.attrs['OCTAVE_GLOBAL'] = np.uint8(1) group_l1.attrs['OCTAVE_NEW_FORMAT'] = np.uint8(1) group_l1.create_dataset("type", data=np.string_('scalar struct'), dtype="|S14") group_l2 = group_l1.create_group('value') for ftparams in data_dict: group_l3 = group_l2.create_group(ftparams) group_l3.attrs['OCTAVE_NEW_FORMAT'] = np.uint8(1) if type(data_dict[ftparams]) == str: group_l3.create_dataset("type", (), data=np.string_('sq_string'), dtype="|S10") if self.octave_targetted_version < 3.8: group_l3.create_dataset("value", data=np.string_(data_dict[ftparams] + '0')) else: group_l3.create_dataset("value", data=np.string_(data_dict[ftparams])) else: group_l3.create_dataset("type", (), data=np.string_('scalar'), dtype="|S7") group_l3.create_dataset("value", data=data_dict[ftparams])
def process(self): # capture ret, frame = self.cam.read() #frame = self.first frame = cv.resize(frame, None, fx=0.5, fy=0.5) frame = cv.flip(frame, 1) self.orig_frame = frame for click in (c for c in list(zip(self.col_defs, self.col_poss, self.col_disps)) if (c[0] != self.undefined_col).all() and (c[1] != self.undefined_pos).all()): col = click[0] pos = click[1] disp = click[2] thresh = self.thresh_slide.get() hlower = (col[0, 0, 0] - thresh) % 180 hupper = (col[0, 0, 0] + thresh) % 180 # in case we have gone under 0 or over 180 invert = hlower > hupper if invert: hlower, hupper = hupper, hlower lower = np.uint8([[[hlower, 20, 20]]]) upper = np.uint8([[[hupper, 255, 255]]]) hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) mask = cv.inRange(hsv, lower, upper) if not invert: mask = cv.bitwise_not(mask) frame = cv.bitwise_and(frame, frame, mask=mask) self.frame = frame self.root.after(50, self.process)
def applyMorphologicalCleaning(self, image): """ Applies a variety of morphological operations to improve the detection of worms in the image. Takes 0.030 s on MUSSORGSKY for a typical frame region Takes 0.030 s in MATLAB too """ # start with worm == 1 image = image.copy() segmentation.clear_border(image) # remove objects at edge (worm == 1) # fix defects in the thresholding by closing with a worm-width disk # worm == 1 wormSE = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (self.wormDiskRadius+1, self.wormDiskRadius+1)) imcl = cv2.morphologyEx(np.uint8(image), cv2.MORPH_CLOSE, wormSE) imcl = np.equal(imcl, 1) # fix defects by filling holes imholes = ndimage.binary_fill_holes(imcl) imcl = np.logical_or(imholes, imcl) # fix barely touching regions # majority with worm pixels == 1 (median filter same?) imcl = nf.median_filter(imcl, footprint=[[1, 1, 1], [1, 0, 1], [1, 1, 1]]) # diag with worm pixels == 0 imcl = np.logical_not(bwdiagfill(np.logical_not(imcl))) # open with worm pixels == 1 openSE = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1)) imcl = cv2.morphologyEx(np.uint8(imcl), cv2.MORPH_OPEN, openSE) return np.equal(imcl, 1)
def __init__(self, mode='dummy', address=None, high_duration=0.001, verbose=None): self._stamp_trigger = self._parallel_trigger if mode == 'parallel': if 'Linux' in platform.system(): address = '/dev/parport0' if address is None else address import parallel as _p self._port = _p.Parallel(address) self._set_data = self._port.setData elif 'Windows' in platform.system(): from ctypes import windll if not hasattr(windll, 'inpout32'): raise SystemError('Must have inpout32 installed') addr = 0x0378 if address is None else address base = int(addr, 16) if addr[:2] == '0x' else addr self._port = windll.inpout32 mask = np.uint8(1 << 5 | 1 << 6 | 1 << 7) # Use ECP to put the port into byte mode val = int((self._port.Inp32(base + 0x402) & ~mask) | (1 << 5)) self.port.Out32(base + 0x402, val) # Now to make sure the port is in output mode we need to make # sure that bit 5 of the control register is not set val = int(self._port.Inp32(base + 2) & ~np.uint8(1 << 5)) self._port.Out32(base + 2, val) def _set_data(data): return self._port.Out32(base, data) self._set_data = _set_data else: raise NotImplementedError else: # mode == 'dummy': self._stamp_trigger = self._dummy_trigger self.high_duration = high_duration
def testIntMax(self): num = np.int(np.iinfo(np.int).max) self.assertEqual(np.int(ujson.decode(ujson.encode(num))), num) num = np.int8(np.iinfo(np.int8).max) self.assertEqual(np.int8(ujson.decode(ujson.encode(num))), num) num = np.int16(np.iinfo(np.int16).max) self.assertEqual(np.int16(ujson.decode(ujson.encode(num))), num) num = np.int32(np.iinfo(np.int32).max) self.assertEqual(np.int32(ujson.decode(ujson.encode(num))), num) num = np.uint8(np.iinfo(np.uint8).max) self.assertEqual(np.uint8(ujson.decode(ujson.encode(num))), num) num = np.uint16(np.iinfo(np.uint16).max) self.assertEqual(np.uint16(ujson.decode(ujson.encode(num))), num) num = np.uint32(np.iinfo(np.uint32).max) self.assertEqual(np.uint32(ujson.decode(ujson.encode(num))), num) if platform.architecture()[0] != '32bit': num = np.int64(np.iinfo(np.int64).max) self.assertEqual(np.int64(ujson.decode(ujson.encode(num))), num) # uint64 max will always overflow as it's encoded to signed num = np.uint64(np.iinfo(np.int64).max) self.assertEqual(np.uint64(ujson.decode(ujson.encode(num))), num)
def crop_waffle(img): hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) greyscale = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) lower_yellow = np.array([0,50,50]) upper_yellow = np.array([70,255,255]) mask = cv2.inRange(hsv, np.uint8(lower_yellow), np.uint8(upper_yellow)) kernel = np.ones((9,9),np.uint8) closed_mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) masked_img = cv2.bitwise_and(greyscale,greyscale,mask = closed_mask) [contours,hiearchy] = cv2.findContours(masked_img,cv.CV_RETR_EXTERNAL,cv.CV_CHAIN_APPROX_SIMPLE) #now find the largest contour max_area = 0 max_contour = None for c in contours: #we change datatypes from numpy arrays to cv arrays and back because contour area only takes cv arrays. c = cv.fromarray(c) if cv.ContourArea(c) > max_area: max_contour = c max_area = cv.ContourArea(c) max_contour = np.asarray(max_contour) shape = img.shape largest_blob_mask = np.zeros((shape[0],shape[1],1),np.uint8) cv2.fillPoly(largest_blob_mask, pts =[max_contour], color=(255,255,255)) print_rgb_hist(img,largest_blob_mask) return cv2.bitwise_and(img,img, mask= largest_blob_mask)
def calcThreshold(img): """ Calculates a good estimate of the threshold value for a given image. """ # Initial T = mean of img T = mean(img) # delta T dT = 0.1 # Loop until acceptable T found while True: # Generate masks split on the given T mask1 = np.uint8(np.where(img < int(T), 1, 0)) mask2 = np.uint8(np.where(img > int(T), 1, 0)) # Find the mean of the two groups m1 = mean(img, mask1) m2 = mean(img, mask2) # Save old T old_T = T # Calculate new T T = (m1 + m2) / 2.0 # Check if acceptable if abs(T - old_T) < dT: break # Return threshold found return T
def testInt(self): num = np.int(2562010) self.assertEqual(np.int(ujson.decode(ujson.encode(num))), num) num = np.int8(127) self.assertEqual(np.int8(ujson.decode(ujson.encode(num))), num) num = np.int16(2562010) self.assertEqual(np.int16(ujson.decode(ujson.encode(num))), num) num = np.int32(2562010) self.assertEqual(np.int32(ujson.decode(ujson.encode(num))), num) num = np.int64(2562010) self.assertEqual(np.int64(ujson.decode(ujson.encode(num))), num) num = np.uint8(255) self.assertEqual(np.uint8(ujson.decode(ujson.encode(num))), num) num = np.uint16(2562010) self.assertEqual(np.uint16(ujson.decode(ujson.encode(num))), num) num = np.uint32(2562010) self.assertEqual(np.uint32(ujson.decode(ujson.encode(num))), num) num = np.uint64(2562010) self.assertEqual(np.uint64(ujson.decode(ujson.encode(num))), num)
def pred(): keep_probability = tf.placeholder(tf.float32, name="keep_probabilty") image = tf.placeholder(tf.float32, shape=[None, IMAGE_HEIGHT, IMAGE_WIDTH, 3], name="input_image") annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_HEIGHT, IMAGE_WIDTH, 1], name="annotation") pred_annotation, logits = inference(image, keep_probability) test_dataset_reader = TestDataset('data/testlist.mat') with tf.Session() as sess: sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir) saver = tf.train.Saver() if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print("Model restored...") itr = 0 test_images, test_annotations, test_orgs = test_dataset_reader.next_batch() #print('getting', test_annotations[0, 200:210, 200:210]) if len(test_annotations) > 0: feed_dict = {image: test_images, annotation: test_annotations, keep_probability: 0.5} preds = sess.run(pred_annotation, feed_dict=feed_dict) org0_im = Image.fromarray(np.uint8(test_orgs[0])) org0_im.save('res/org0.jpg') org1_im = Image.fromarray(np.uint8(test_orgs[1])) org1_im.save('res/org1.jpg') save_alpha_img(test_orgs[0], test_annotations[0], 'res/ann0') save_alpha_img(test_orgs[1], test_annotations[1], 'res/ann1') save_alpha_img(test_orgs[0], preds[0], 'res/pre0') save_alpha_img(test_orgs[1], preds[1], 'res/pre1')
def partition_FOV_KMeans(self,tradeoff_weight=.5,fx=.25,fy=.25,n_clusters=4,max_iter=500): """ Partition the FOV in clusters that are grouping pixels close in space and in mutual correlation Parameters ------------------------------ tradeoff_weight:between 0 and 1 will weight the contributions of distance and correlation in the overall metric fx,fy: downsampling factor to apply to the movie n_clusters,max_iter: KMeans algorithm parameters Outputs ------------------------------- fovs:array 2D encoding the partitions of the FOV mcoef: matric of pairwise correlation coefficients distanceMatrix: matrix of picel distances Example """ _,h1,w1=self.shape self.resize(fx,fy) T,h,w=self.shape Y=np.reshape(self,(T,h*w)) mcoef=np.corrcoef(Y.T) idxA,idxB = np.meshgrid(list(range(w)),list(range(h))); coordmat=np.vstack((idxA.flatten(),idxB.flatten())) distanceMatrix=euclidean_distances(coordmat.T); distanceMatrix=old_div(distanceMatrix,np.max(distanceMatrix)) estim=KMeans(n_clusters=n_clusters,max_iter=max_iter); kk=estim.fit(tradeoff_weight*mcoef-(1-tradeoff_weight)*distanceMatrix) labs=kk.labels_ fovs=np.reshape(labs,(h,w)) fovs=cv2.resize(np.uint8(fovs),(w1,h1),old_div(1.,fx),old_div(1.,fy),interpolation=cv2.INTER_NEAREST) return np.uint8(fovs), mcoef, distanceMatrix
def draw_mask_on_image_array(image, mask, color='red', alpha=0.4): """Draws mask on an image. Args: image: uint8 numpy array with shape (img_height, img_height, 3) mask: a uint8 numpy array of shape (img_height, img_height) with values between either 0 or 1. color: color to draw the keypoints with. Default is red. alpha: transparency value between 0 and 1. (default: 0.4) Raises: ValueError: On incorrect data type for image or masks. """ if image.dtype != np.uint8: raise ValueError('`image` not of type np.uint8') if mask.dtype != np.uint8: raise ValueError('`mask` not of type np.uint8') if np.any(np.logical_and(mask != 1, mask != 0)): raise ValueError('`mask` elements should be in [0, 1]') if image.shape[:2] != mask.shape: raise ValueError('The image has spatial dimensions %s but the mask has ' 'dimensions %s' % (image.shape[:2], mask.shape)) rgb = ImageColor.getrgb(color) pil_image = Image.fromarray(image) solid_color = np.expand_dims( np.ones_like(mask), axis=2) * np.reshape(list(rgb), [1, 1, 3]) pil_solid_color = Image.fromarray(np.uint8(solid_color)).convert('RGBA') pil_mask = Image.fromarray(np.uint8(255.0*alpha*mask)).convert('L') pil_image = Image.composite(pil_solid_color, pil_image, pil_mask) np.copyto(image, np.array(pil_image.convert('RGB')))
def write_sequence_file(awgData, fileName, miniLLRepeat=1): ''' Main function to pack channel LLs into an APS h5 file. ''' #Preprocess the sequence data to handle APS restrictions LLs12, repeat12, wfLib12 = preprocess(awgData['ch12']['linkList'], awgData['ch12']['wfLib'], awgData['ch12']['correctionT']) LLs34, repeat34, wfLib34 = preprocess(awgData['ch34']['linkList'], awgData['ch34']['wfLib'], awgData['ch34']['correctionT']) assert repeat12 == repeat34, 'Failed to unroll sequence' if repeat12 != 0: miniLLRepeat *= repeat12 #Merge the the marker data into the IQ linklists merge_APS_markerData(LLs12, awgData['ch1m1']['linkList'], 1) merge_APS_markerData(LLs12, awgData['ch2m1']['linkList'], 2) merge_APS_markerData(LLs34, awgData['ch3m1']['linkList'], 1) merge_APS_markerData(LLs34, awgData['ch4m1']['linkList'], 2) #Open the HDF5 file if os.path.isfile(fileName): os.remove(fileName) with h5py.File(fileName, 'w') as FID: #List of which channels we have data for #TODO: actually handle incomplete channel data channelDataFor = [1,2] if LLs12 else [] channelDataFor += [3,4] if LLs34 else [] FID['/'].attrs['Version'] = 2.1 FID['/'].attrs['channelDataFor'] = np.uint16(channelDataFor) FID['/'].attrs['miniLLRepeat'] = np.uint16(miniLLRepeat - 1) #Create the waveform vectors wfInfo = [] for wfLib in (wfLib12, wfLib34): wfInfo.append(create_wf_vector({key:wf.real for key,wf in wfLib.items()})) wfInfo.append(create_wf_vector({key:wf.imag for key,wf in wfLib.items()})) LLData = [LLs12, LLs34] repeats = [0, 0] #Create the groups and datasets for chanct in range(4): chanStr = '/chan_{0}'.format(chanct+1) chanGroup = FID.create_group(chanStr) chanGroup.attrs['isIQMode'] = np.uint8(1) #Write the waveformLib to file FID.create_dataset('{0}/waveformLib'.format(chanStr), data=wfInfo[chanct][0]) #For A channels (1 & 3) we write link list data if we actually have any if (np.mod(chanct,2) == 0) and LLData[chanct//2]: groupStr = chanStr+'/linkListData' LLGroup = FID.create_group(groupStr) LLDataVecs, numEntries = create_LL_data(LLData[chanct//2], wfInfo[chanct][1], os.path.basename(fileName)) LLGroup.attrs['length'] = numEntries for key,dataVec in LLDataVecs.items(): FID.create_dataset(groupStr+'/' + key, data=dataVec) else: chanGroup.attrs['isLinkListData'] = np.uint8(0)
def endDrawing(self, pos): has_moved = self._hasMoved # _hasMoved will change after calling moveTo if has_moved: self.moveTo(pos) else: assert(self.pos == pos) self.moveTo(QPointF(pos.x()+0.0001, pos.y()+0.0001)) # move a little tempi = QImage(QSize(self.bb.width(), self.bb.height()), QImage.Format_ARGB32_Premultiplied) #TODO: format tempi.fill(0) painter = QPainter(tempi) self.scene.render(painter, target=QRectF(), source=QRectF(QPointF(self.bb.x(), self.bb.y()), QSizeF(self.bb.width(), self.bb.height()))) painter.end() ndarr = qimage2ndarray.rgb_view(tempi)[:,:,0] labels = numpy.where(ndarr>0,numpy.uint8(self.drawnNumber),numpy.uint8(0)) labels = labels.swapaxes(0,1) assert labels.shape[0] == self.bb.width() assert labels.shape[1] == self.bb.height() ## ## ensure that at least one pixel is label when the brush size is 1 ## ## this happens when the user just clicked without moving ## in that case the lineitem will be so tiny, that it won't be rendered ## into a single pixel by the code above if not has_moved and self.brushSize <= 1 and numpy.count_nonzero(labels) == 0: labels[labels.shape[0]//2, labels.shape[1]//2] = self.drawnNumber self.brushStrokeAvailable.emit(QPointF(self.bb.x(), self.bb.y()), labels)
def array_colorkey (surface): """pygame.numpyarray.array_colorkey (Surface): return array copy the colorkey values into a 2d array Create a new array with the colorkey transparency value from each pixel. If the pixel matches the colorkey it will be fully tranparent; otherwise it will be fully opaque. This will work on any type of Surface format. If the image has no colorkey a solid opaque array will be returned. This function will temporarily lock the Surface as pixels are copied. """ colorkey = surface.get_colorkey () if colorkey == None: # No colorkey, return a solid opaque array. array = numpy.empty (surface.get_width () * surface.get_height (), numpy.uint8) array.fill (0xff) array.shape = surface.get_width (), surface.get_height () return array # Taken from from Alex Holkner's pygame-ctypes package. Thanks a # lot. array = array2d (surface) # Check each pixel value for the colorkey and mark it as opaque or # transparent as needed. val = surface.map_rgb (colorkey) array = numpy.choose (numpy.equal (array, val), (numpy.uint8 (0xff), numpy.uint8 (0))) array.shape = surface.get_width (), surface.get_height () return array
def unpack_4x8(unpack_1x8_func, u, func_opts): """Evaluate a GLSL unpack4x8 function. :param unpack_1x8_func: the component-wise function of the GLSL unpack4x8 function :param u: a uint32 :return: a 4-tuple of float32 """ assert(isinstance(u, uint32)) ux = uint8(u & 0xff) uy = uint8((u >> 8) & 0xff) uz = uint8((u >> 16) & 0xff) uw = uint8((u >> 24) & 0xff) x = unpack_1x8_func(ux) y = unpack_1x8_func(uy) z = unpack_1x8_func(uz) w = unpack_1x8_func(uw) assert(isinstance(x, float32)) assert(isinstance(y, float32)) assert(isinstance(z, float32)) assert(isinstance(w, float32)) return (x, y, z, w)
def draw_roi(self, filename, roi, expend_border=0): try: image = self.load_image(filename) roi_image = self.wrap_roi(image, roi, expend_border) roi_image = caffe.io.resize_image(roi_image, (384, 384)) roi_pil = roi_image * 255. image_pil= image * 255. image_pil= PIL.Image.fromarray(np.uint8(image_pil)) roi_pil = PIL.Image.fromarray(np.uint8(roi_pil)) dr_image = PIL.ImageDraw.Draw(image_pil) dr_image.rectangle( [roi['x'], roi['y'], roi['x']+roi['width'], roi['y']+roi['height']], outline=(0,0,255) ) dr_image.text([roi['x'], roi['y']], roi['confidence'], fill=(0,0,255)) dr_image.text([roi['x'], roi['y']+40], roi['category'], fill=(0,0,255)) return roi_pil, image_pil except Exception as err: print 'ERROR in agent.draw_roi ', err return None
def shift2(im): w,h=im.shape w2=np.uint8(w/2) h2=np.uint8(h/2) im1=np.vstack((im[w2:,:],im[:w2,:])) im2=np.hstack((im1[:,h2:],im1[:,:h2])) return im2
def apply_mask(frame, bg, key_color, tolerance): # open files fg_img = frame.convert('YCbCr') bg_img = Image.open(bg).convert('RGB') img_size = 400, 300 # resize image to fit homer.gif pixel size if bg_img.size != img_size: bg_img = bg_img.resize((img_size), Image.ANTIALIAS) bg_img = bg_img.crop((0, 0)+img_size) [Y_key, Cb_key, Cr_key] = key_color [tol_a, tol_b]= tolerance (x,y) = fg_img.size fg_data = numpy.array(fg_img.getdata()) mask_vector = numpy.vectorize(color_close) alpha_mask = mask_vector(fg_data[:,1], fg_data[:,2], Cb_key, Cr_key, tol_a, tol_b) alpha_mask.shape = (y,x) img_mask = Image.fromarray(numpy.uint8(alpha_mask)) invert_mask = Image.fromarray(numpy.uint8(255-255*(alpha_mask/255))) # create images for color mask color_mask = Image.new('RGB', (x,y), (0,0,0)) all_green = Image.new('YCbCr', (x,y), key_color) color_mask.paste(all_green, invert_mask) fg_img = fg_img.convert('RGB') cleaned = ImageChops.subtract(fg_img, color_mask) bg_img.paste(cleaned, img_mask) return bg_img
def esquelet (image): img=np.uint8(image) mask = np.zeros(img.shape, dtype=np.uint8) img1 = np.uint8(img) size = np.size(img1) skel = np.zeros(img1.shape,np.uint8) element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3)) done=False #skeletonization while(not done): eroded = cv2.erode(img1,element) temp = cv2.dilate(eroded,element) temp = cv2.subtract(img1,temp) skel = cv2.bitwise_or(skel,temp) img1 = eroded.copy() zeros = size - cv2.countNonZero(img1) if zeros==size: done = True return [skel,img]
def on_epoch_end(self, callback_data, model, epoch): # convert to numpy arrays data_batch = model.data_batch.get() noise_batch = model.noise_batch.get() # value transform data_batch = self._value_transform(data_batch) noise_batch = self._value_transform(noise_batch) # shape transform data_canvas = self._shape_transform(data_batch) noise_canvas = self._shape_transform(noise_batch) # plotting options im_args = dict(interpolation="nearest", vmin=0., vmax=1.) if self.nchan == 1: im_args['cmap'] = plt.get_cmap("gray") fname = self.filename+'_data_'+'{:03d}'.format(epoch)+'.png' Image.fromarray(np.uint8(data_canvas*255)).convert('RGB').save(fname) fname = self.filename+'_noise_'+'{:03d}'.format(epoch)+'.png' Image.fromarray(np.uint8(noise_canvas*255)).convert('RGB').save(fname) # plot logged WGAN costs if logged if model.cost.costfunc.func == 'wasserstein': giter = callback_data['gan/gen_iter'][:] nonzeros = np.where(giter) giter = giter[nonzeros] cost_dis = callback_data['gan/cost_dis'][:][nonzeros] w_dist = medfilt(np.array(-cost_dis, dtype='float64'), kernel_size=101) plt.figure(figsize=(400/self.dpi, 300/self.dpi), dpi=self.dpi) plt.plot(giter, -cost_dis, 'k-', lw=0.25) plt.plot(giter, w_dist, 'r-', lw=2.) plt.title(self.filename, fontsize=self.font_size) plt.xlabel("Generator Iterations", fontsize=self.font_size) plt.ylabel("Wasserstein estimate", fontsize=self.font_size) plt.margins(0, 0, tight=True) plt.savefig(self.filename+'_training.png', bbox_inches='tight') plt.close()
def explain(model, img, topLabels, numSamples, numFeatures, hideRest, hideColor, positiveOnly): img, oldImg = transform_img_fn(img) img = img*(1./255) prediction = model.predict(img) explainer = lime_image.LimeImageExplainer() img = np.squeeze(img) explanation = explainer.explain_instance(img, model.predict, top_labels=topLabels, hide_color=hideColor, num_samples=numSamples) temp, mask = explanation.get_image_and_mask(getTopPrediction(prediction[0]), positive_only=positiveOnly, num_features=numFeatures, hide_rest=hideRest) tempMask = mask * 255 temp = Image.fromarray(np.uint8(tempMask)) temp = temp.resize((oldImg.width, oldImg.height)) temp = image.img_to_array(temp) temp = temp * 1./255 temp = temp.astype(np.int64) temp = np.squeeze(temp) oldImgArr = image.img_to_array(oldImg) oldImgArr = oldImgArr * (1./255) oldImgArr = oldImgArr.astype(np.float64) imgExplained = mark_boundaries(oldImgArr, temp) imgFinal = np.uint8(imgExplained*255) img = Image.fromarray(imgFinal) imgByteArr = io.BytesIO() img.save(imgByteArr, format='JPEG') imgByteArr = imgByteArr.getvalue() return imgByteArr
def grabCut(img, rect=None, mask=None, ite=5): height, width, channels = img.shape # if no arguments, try to segment using a large rectangle if rect == None and mask == None: rect = (int(width*0.15), 15, int(width*0.85), height-15) initOpt = cv2.GC_INIT_WITH_RECT # if rectangle argument but no mask, init mask with rectangle elif mask == None: mask = np.zeros((height, width), np.uint8) initOpt = cv2.GC_INIT_WITH_RECT # if mask argument but no rectangle, use mask and let rect to None elif rect == None: initOpt = cv2.GC_INIT_WITH_MASK rect = (0, 0, width, height) mask = np.uint8(mask) # if mask argument and rectangle, set pixels outside the mask as background else: mask = np.uint8(mask) rect = rectangleutil.checkRectangleBounds(rect, mask.shape) maskRect = rectangleutil.rectangle2mask(rect, mask.shape) mask[maskRect == 0] = cv2.GC_BGD initOpt = cv2.GC_INIT_WITH_MASK #imageblured = np.zeros(img.shape, img.dtype) #cv2.smooth(img, imageblured, cv.CV_GAUSSIAN, 5) tmp1 = np.zeros((1, 13 * 5)) tmp2 = np.zeros((1, 13 * 5)) cv2.grabCut(img, mask, rect, tmp1, tmp2, ite, initOpt) mask[mask == cv2.GC_BGD] = 0 mask[mask == cv2.GC_PR_BGD] = 0 mask[mask == cv2.GC_FGD] = 255 mask[mask == cv2.GC_PR_FGD] = 255 return mask
def get_depth(): """ Returns numpy ndarrays representing the raw and ranged depth images. Outputs: dmap:= distancemap in mm, 1L ndarray, dtype=uint16, min=0, max=2**12-1 d4d := depth for dislay, 3L ndarray, dtype=uint8, min=0, max=255 Note1: fromstring is faster than asarray or frombuffer Note2: .reshape(120,160) #smaller image for faster response OMAP/ARM default video configuration .reshape(240,320) # Used to MATCH RGB Image (OMAP/ARM) Requires .set_video_mode """ dmap = np.fromstring(depth_stream.read_frame().get_buffer_as_uint16(),dtype=np.uint16).reshape(h,w) # Works & It's FAST d4d = dmap.astype(float) *255/ 2**12-1 # Correct the range. Depth images are 12bits d4d = cv2.cvtColor(np.uint8(d4d),cv2.COLOR_GRAY2RGB) temp1 = np.zeros(dmap.shape,dtype=np.uint8) temp1 = dmap - 2**8 #most significant: 2**8-2**15 temp2 = dmap.copy() #least significant: 2**0-2**7 temp1[temp1<0]=0 temp2[temp2>255] = 0 dmap = np.uint8(np.dstack((temp2,temp1,temp2))) #print dmap.shape, type(dmap), dmap.dtype return dmap, d4d
def __init__(self): self.stack = np.zeros(STACK_SIZE, dtype=np.uint8) self.sp = np.uint8(0) self.pc = np.uint8(0) self.s_regs = np.zeros(8, dtype=np.uint8) self.m_regs = np.zeros(8, dtype=np.uint8) self.progmem = np.zeros(PROG_SIZE, dtype=np.uint8)
def calc_chans(self): """Calculate lockout and inclusion chan neighbourhoods, max number of chans to use, and define the spike record dtype""" sort = self.sort self.enabledSiteLoc = {} for chan in self.chans: # for all enabled chans self.enabledSiteLoc[chan] = sort.stream.probe.SiteLoc[chan] # get its (x, y) # distance matrix for the chans enabled for this search, sorted by chans: self.dm = DistanceMatrix(self.enabledSiteLoc) # dict of neighbourhood of chanis for each chani self.locknbhdi = {} # for lockout around a spike self.inclnbhdi = {} # for inclusion of wavedata as part of a spike maxnchansperspike = 0 for chani, distances in enumerate(self.dm.data): # iterate over rows of distances # at what col indices does the returned row fall within lockr?: lockchanis, = np.uint8(np.where(distances <= self.lockr)) # at what col indices does the returned row fall within inclr?: inclchanis, = np.uint8(np.where(distances <= self.inclr)) self.locknbhdi[chani] = lockchanis self.inclnbhdi[chani] = inclchanis maxnchansperspike = max(maxnchansperspike, len(inclchanis)) self.maxnchansperspike = maxnchansperspike self.SPIKEDTYPE = [('id', np.int32), ('nid', np.int16), ('chan', np.uint8), ('nchans', np.uint8), ('chans', np.uint8, self.maxnchansperspike), ('chani', np.uint8), ('t', np.int64), ('t0', np.int64), ('t1', np.int64), ('dt', np.int16), # time between peaks, in us ('tis', np.uint8, (self.maxnchansperspike, 2)), # peak positions ('aligni', np.uint8), ('V0', np.float32), ('V1', np.float32), ('Vpp', np.float32), ('x0', np.float32), ('y0', np.float32), ('sx', np.float32), ('sy', np.float32), ]
def save_frame_and_response_map(frame, bbox, fig_n, crop_x, score, writer, fig): # fig = plt.figure(fig_n) plt.clf() ax = fig.add_subplot(131) ax.set_title('Tracked sequence') r = patches.Rectangle((bbox[0],bbox[1]), bbox[2], bbox[3], linewidth=2, edgecolor='r', fill=False) ax.imshow(np.uint8(frame)) ax.add_patch(r) ax2 = fig.add_subplot(132) ax2.set_title('Context region') ax2.imshow(np.uint8(crop_x)) ax2.spines['left'].set_position('center') ax2.spines['right'].set_color('none') ax2.spines['bottom'].set_position('center') ax2.spines['top'].set_color('none') ax2.set_yticklabels([]) ax2.set_xticklabels([]) ax3 = fig.add_subplot(133) ax3.set_title('Response map') ax3.spines['left'].set_position('center') ax3.spines['right'].set_color('none') ax3.spines['bottom'].set_position('center') ax3.spines['top'].set_color('none') ax3.set_yticklabels([]) ax3.set_xticklabels([]) ax3.imshow(np.uint8(score)) # ax3.grid() writer.grab_frame()
def test_valid(self): prop = bcpp.Int() assert prop.is_valid(None) assert prop.is_valid(0) assert prop.is_valid(1) assert prop.is_valid(np.int8(0)) assert prop.is_valid(np.int8(1)) assert prop.is_valid(np.int16(0)) assert prop.is_valid(np.int16(1)) assert prop.is_valid(np.int32(0)) assert prop.is_valid(np.int32(1)) assert prop.is_valid(np.int64(0)) assert prop.is_valid(np.int64(1)) assert prop.is_valid(np.uint8(0)) assert prop.is_valid(np.uint8(1)) assert prop.is_valid(np.uint16(0)) assert prop.is_valid(np.uint16(1)) assert prop.is_valid(np.uint32(0)) assert prop.is_valid(np.uint32(1)) assert prop.is_valid(np.uint64(0)) assert prop.is_valid(np.uint64(1)) # TODO (bev) should fail assert prop.is_valid(False) assert prop.is_valid(True)
def print_blue_hvs(self): blue = np.uint8([[[0, 0, 255]]]) hsv_blue = cv2.cvtColor(blue, cv2.COLOR_BGR2HSV)
def forward(self, bottom, top): # print "first : ",int(np.prod(bottom[0].data.shape[1:])) from time import time t0 = time() D = bottom[0].channels from ccnn import constraintloss self.diff = [] loss, w = 0, 0 for i in range(bottom[0].num): # print '-------------------------------------' # print 'Image Number : ',self.counter if self.semi_supervised: assert ( len(bottom) > 4 ), "Semi Supervised Flag ON, but full supervised images not supplied as additional bottom !" if (not self.semi_supervised) or ( bottom[3].data[i] == 0): # weakly-supervised downsampled training # Setup bottoms f = np.ascontiguousarray(bottom[0].data[i].reshape( (D, -1)).T) # f : height*width x channels q = np.exp(f - np.max( f, axis=1)[:, None]) # expAndNormalize across channels q /= np.sum(q, axis=1)[:, None] # Setup the constraint softmax csm = constraintloss.ConstraintSoftmax(self.hardness) # Add Negative Label constraints if self.apply_size_constraint: L = bottom[2].data[i].flatten() > 0.5 else: L = bottom[1].data[i].flatten() > 0.5 csm.addZeroConstraint((~L).astype(np.float32)) # Add Small Object Size constraints # L_up = 0*L # if self.apply_size_constraint: # assert (len(bottom)>2),"Size constraint ON, but size information not supplied as additional bottom !" # L_up = 1*L # L = bottom[2].data[i].flatten() > 0.5 # for l in np.flatnonzero(L_up): # if l>0 and not L[l]: # v = np.zeros(D).astype(np.float32); v[l] = 1 # csm.addLinearConstraint( -v, -self.fg_upper_small, self.fg_slack ) # Apply Positive Label Constraints for l in np.flatnonzero(L): if l > 0: v = np.zeros(D).astype(np.float32) v[l] = 1 # if self.apply_size_constraint: # csm.addLinearConstraint( v, self.fg_lower_hard ) csm.addLinearConstraint(v, self.fg_lower, self.fg_slack) # Add Background Constraints v = np.zeros(D).astype(np.float32) v[0] = 1 csm.addLinearConstraint(v, self.bg_lower, self.bg_slack) # lower bound if (np.sum(L[1:]) > 0): # i.e. image is not all background csm.addLinearConstraint(-v, -self.bg_upper) # upper bound # Run constrained optimization p = csm.compute(f) if self.normalization: self.diff.append( ((q - p).T.reshape(bottom[0].data[i].shape)) / np.float32(f.shape[0])) # normalize by (f.shape[0]) else: self.diff.append(((q - p).T.reshape( bottom[0].data[i].shape))) # unnormalize # Debugging Code --------- # temp = 1 # for l in np.flatnonzero(L_up): # if l>0 and not L[l]: # if p[:,l].sum() > self.fg_upper_small: # print 'Small Object Class Index=',temp,' sumP=',p[:,l].sum(),' sumQ=',q[:,l].sum() # print '\tP=',repr(p[:,l]) # print '\tQ=',repr(q[:,l]) # temp += 1 # print '' # np.savez('./debug/debug_im'+str(self.counter)+'.npz', hardness=self.hardness, bg_lower = self.bg_lower, bg_upper=self.bg_upper, L=L, L_up=L_up, fg_lower = self.fg_lower, fg_slack=self.fg_slack, fg_upper_small=self.fg_upper_small, f=f,p=p,q=q ) # self.counter += 1 # ----------------------- else: # fully-supervised upsample training f = np.ascontiguousarray(bottom[5].data[i].reshape( (D, -1)).T) # f : height*width x channels q = np.exp(f - np.max( f, axis=1)[:, None]) # expAndNormalize across channels q /= np.sum(q, axis=1)[:, None] gt = bottom[4].data[i] # print '\t q : ',q.shape # print '\t cnn_output_Shape : ',bottom[0].data[i].shape # print '\t gt_Shape : ',gt.shape # print '\t gt_resized_Shape : ', (np.float32(self.DS(np.uint8(gt[0,...])))).shape gt = np.uint8( gt[0, ...] ) # For downsampling the gt use this : self.DS(np.uint8(gt[0,...])) gt = np.ascontiguousarray(gt.reshape( (1, -1)).T) # gt : height*width x 1 gt = gt.squeeze() p = np.zeros(q.shape).astype( np.float32) # q,p,f : height*width x channels ind = np.where(gt != 255) p[ind, gt[ind]] = 1 ind = np.where(gt == 255) p[ind, :] = q[ ind, :] # so that q-p=0 at this position because it is ignore label if self.normalization: self.diff.append( ((q - p).T.reshape(bottom[5].data[i].shape)) / np.float32(f.shape[0])) # normalize by (f.shape[0]) else: self.diff.append( (q - p).T.reshape(bottom[5].data[i].shape)) if self.normalization: loss += (np.sum(p * np.log(np.maximum(p, 1e-10))) - np.sum( p * np.log(np.maximum(q, 1e-10)))) / np.float32( f.shape[0]) # normalize by (f.shape[0]) else: loss += (np.sum(p * np.log(np.maximum(p, 1e-10))) - np.sum(p * np.log(np.maximum(q, 1e-10))) ) # unnormalize # print( np.min(f), np.max(f) ) # np.set_printoptions(linewidth=150) # print( L.astype(bool) ) # print( np.bincount(np.argmax(f,axis=1),minlength=21) ) # print( np.sum(p[:,~L]), 'P', np.sum(p,axis=0).astype(int)[L], 'H', np.bincount(np.argmax(p,axis=1),minlength=L.size)[L] ) #print( "===== %f ====="%(time()-t0) ) top[0].data[0, 0, 0, 0] = loss self.diff = np.array(self.diff)
# -*- coding: utf-8 -*- """ Created on Wed Sep 30 17:53:24 2020 @author: malen """ import numpy as np import cv2 import matplotlib as plt img = cv2.imread('image/bananaPDI.jpg') I = cv2.cvtColor(img, cv2.COLOR_BG2GRAY) binary = np.uint8((I<200)*255)
def undo_normalize_image(normalized_image): """Convert to a numpy array that can be read by PIL.""" # Convert from NHWC to HWC. normalized_image = np.squeeze(normalized_image, axis=0) return np.uint8(normalized_image * 127.5 + 127.5)
def img_show(img): pil_img = Image.fromarray(np.uint8(img)) pil_img.show()
import numpy as np from shutil import copyfile from tqdm import trange data_root = "/home/ziheng/YorkUrbanDB" out_root = "/home/ziheng/YorkUrbanDB_new/test" list_file = io.loadmat(os.path.join(data_root, "Manhattan_Image_DB_Names.mat")) name_list = [e[0][0].strip("\\") for e in list_file["Manhattan_Image_DB_Names"]] test_set = io.loadmat(os.path.join(data_root, "ECCV_TrainingAndTestImageNumbers.mat")) test_set_id = test_set["testSetIndex"].flatten().tolist() imgs = [os.path.join(data_root, name_list[i - 1], name_list[i - 1] + ".jpg") for i in test_set_id] labels = [io.loadmat(os.path.join(data_root, name_list[i - 1], name_list[i - 1] + "LinesAndVP.mat")) for i in test_set_id] lines = [np.float32(lab["lines"]).reshape((-1, 4)) for lab in labels] maps = [np.uint8(lab["finalImg"]) for lab in labels] os.makedirs(out_root, exist_ok=True) max_juncs = 512 for i in trange(len(imgs)): img, line = imgs[i], lines[i] fname = os.path.basename(img)[:-4] hm = maps[i] lg = LineGraph(eps_junction=1., eps_line_deg=np.pi / 30, verbose=False) for x1, y1, x2, y2 in line: lg.add_junction((x1, y1)) lg.add_junction((x2, y2)) lg.freeze_junction() for x1, y1, x2, y2 in line: lg.add_line_seg((x1, y1), (x2, y2)) lg.freeze_line_seg()
def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, lock=None): """Open a file with rasterio (experimental). This should work with any file that rasterio can open (most often: geoTIFF). The x and y coordinates are generated automatically from the file's geoinformation, shifted to the center of each pixel (see `"PixelIsArea" Raster Space <http://web.archive.org/web/20160326194152/http://remotesensing.org/geotiff/spec/geotiff2.5.html#2.5.2>`_ for more information). You can generate 2D coordinates from the file's attributes with:: from affine import Affine da = xr.open_rasterio('path_to_file.tif') transform = Affine(*da.attrs['transform']) nx, ny = da.sizes['x'], da.sizes['y'] x, y = np.meshgrid(np.arange(nx)+0.5, np.arange(ny)+0.5) * transform Parameters ---------- filename : str Path to the file to open. parse_coordinates : bool, optional Whether to parse the x and y coordinates out of the file's ``transform`` attribute or not. The default is to automatically parse the coordinates only if they are rectilinear (1D). It can be useful to set ``parse_coordinates=False`` if your files are very large or if you don't need the coordinates. chunks : int, tuple or dict, optional Chunk sizes along each dimension, e.g., ``5``, ``(5, 5)`` or ``{'x': 5, 'y': 5}``. If chunks is provided, it used to load the new DataArray into a dask array. cache : bool, optional If True, cache data loaded from the underlying datastore in memory as NumPy arrays when accessed to avoid reading from the underlying data- store multiple times. Defaults to True unless you specify the `chunks` argument to use dask, in which case it defaults to False. lock : False, True or threading.Lock, optional If chunks is provided, this argument is passed on to :py:func:`dask.array.from_array`. By default, a global lock is used to avoid issues with concurrent access to the same file when using dask's multithreaded backend. Returns ------- data : DataArray The newly created DataArray. """ import rasterio riods = rasterio.open(filename, mode='r') if cache is None: cache = chunks is None coords = OrderedDict() # Get bands if riods.count < 1: raise ValueError('Unknown dims') coords['band'] = np.asarray(riods.indexes) # Get coordinates if LooseVersion(rasterio.__version__) < '1.0': transform = riods.affine else: transform = riods.transform if transform.is_rectilinear: # 1d coordinates parse = True if parse_coordinates is None else parse_coordinates if parse: nx, ny = riods.width, riods.height # xarray coordinates are pixel centered x, _ = (np.arange(nx) + 0.5, np.zeros(nx) + 0.5) * transform _, y = (np.zeros(ny) + 0.5, np.arange(ny) + 0.5) * transform coords['y'] = y coords['x'] = x else: # 2d coordinates parse = False if (parse_coordinates is None) else parse_coordinates if parse: warnings.warn( "The file coordinates' transformation isn't " "rectilinear: xarray won't parse the coordinates " "in this case. Set `parse_coordinates=False` to " "suppress this warning.", RuntimeWarning, stacklevel=3) # Attributes attrs = dict() # Affine transformation matrix (always available) # This describes coefficients mapping pixel coordinates to CRS # For serialization store as tuple of 6 floats, the last row being # always (0, 0, 1) per definition (see https://github.com/sgillies/affine) attrs['transform'] = tuple(transform)[:6] if hasattr(riods, 'crs') and riods.crs: # CRS is a dict-like object specific to rasterio # If CRS is not None, we convert it back to a PROJ4 string using # rasterio itself attrs['crs'] = riods.crs.to_string() if hasattr(riods, 'res'): # (width, height) tuple of pixels in units of CRS attrs['res'] = riods.res if hasattr(riods, 'is_tiled'): # Is the TIF tiled? (bool) # We cast it to an int for netCDF compatibility attrs['is_tiled'] = np.uint8(riods.is_tiled) if hasattr(riods, 'transform'): # Affine transformation matrix (tuple of floats) # Describes coefficients mapping pixel coordinates to CRS attrs['transform'] = tuple(riods.transform) if hasattr(riods, 'nodatavals'): # The nodata values for the raster bands attrs['nodatavals'] = tuple([ np.nan if nodataval is None else nodataval for nodataval in riods.nodatavals ]) # Parse extra metadata from tags, if supported parsers = {'ENVI': _parse_envi} driver = riods.driver if driver in parsers: meta = parsers[driver](riods.tags(ns=driver)) for k, v in meta.items(): # Add values as coordinates if they match the band count, # as attributes otherwise if isinstance(v, (list, np.ndarray)) and len(v) == riods.count: coords[k] = ('band', np.asarray(v)) else: attrs[k] = v data = indexing.LazilyIndexedArray(RasterioArrayWrapper(riods)) # this lets you write arrays loaded with rasterio data = indexing.CopyOnWriteArray(data) if cache and (chunks is None): data = indexing.MemoryCachedArray(data) result = DataArray(data=data, dims=('band', 'y', 'x'), coords=coords, attrs=attrs) if chunks is not None: from dask.base import tokenize # augment the token with the file modification time try: mtime = os.path.getmtime(filename) except OSError: # the filename is probably an s3 bucket rather than a regular file mtime = None token = tokenize(filename, mtime, chunks) name_prefix = 'open_rasterio-%s' % token if lock is None: lock = RASTERIO_LOCK result = result.chunk(chunks, name_prefix=name_prefix, token=token, lock=lock) # Make the file closeable result._file_obj = riods return result
# roi_eye = roi_face[int(1.2*ey):int(0.8*(ey+eh)), int(1.2*ex):int(0.8*(ex+ew))] roi_eye = roi_face[ey:ey + eh, ex:ex + ew] center = 0 roi_eye = cv2.GaussianBlur(roi_eye, (3, 3), 0) roi_eye = cv2.addWeighted(roi_eye, 1.5, roi_eye, -0.5, 0) roi_eye_canny = cv2.Canny(roi_eye, 100, 200) cv2.imwrite('./data/canny' + str(counter) + '.png', roi_eye_canny) laplacian = cv2.Laplacian(roi_eye, cv2.CV_64F) cv2.imwrite('./data/lapla' + str(counter) + '.png', laplacian) # res = cv2.resize(roi_eye,(int(ew/2), int(eh/2)), interpolation = cv2.INTER_AREA) roi_eyex = cv2.Sobel(roi_eye, cv2.CV_64F, 1, 0, ksize=3) roi_eyey = cv2.Sobel(roi_eye, cv2.CV_64F, 0, 1, ksize=3) roi_eyex = np.absolute(roi_eyex) roi_eyey = np.absolute(roi_eyey) roi_eyex = np.uint8(roi_eyex) roi_eyey = np.uint8(roi_eyey) # sobelx64f = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5) # abs_sobel64f = np.absolute(sobelx64f) # sobel_8u = np.uint8(abs_sobel64f) cv2.imwrite('./data/zsobely' + str(counter) + '.png', roi_eyey) cv2.imwrite('./data/zsobelx' + str(counter) + '.png', roi_eyex) ret, tmp = cv2.threshold(roi_eyex, 0, 255, cv2.THRESH_OTSU) tmp = cv2.erode(tmp, kernel, iterations=1) cv2.imwrite('./data/zsobelxt' + str(counter) + '.png', tmp) mag = np.hypot(roi_eyex, roi_eyey) # magnitude mag *= 255.0 / np.max(mag) # normalize (Q&D) roi_eye_sobel = mag.astype(np.uint8) # roi_eye_sobel = cv2.morphologyEx(roi_eye_sobel, cv2.MORPH_OPEN, kernel)
sum_current = x_histogram[0] x_cdf[0] = sum_current for i in range(1, l): sum_current += x_histogram[i] x_cdf[i] = sum_current y_cdf = (l-1)*x_cdf/(X.shape[0] * X.shape[1]) for i in range(X.shape[0]): for j in range(X.shape[1]): X[i, j] = y_cdf[X[i, j]] print('Time Consumption: [%.3f] Second ' % (time() - init_time)) return X ########################## Support code below from os.path import normpath as fn # Fixes window/linux path conventions import warnings warnings.filterwarnings('ignore') img = imread(fn('inputs/p2_inp.jpg')) out = histeq(img) out = np.maximum(0,np.minimum(255,out)) out = np.uint8(out) imsave(fn('outputs/prob2.jpg'),out)
import numpy as np from matplotlib import cm base_path = sys.argv[1] LINE_NO = int(sys.argv[2]) CLIP = 0.2 SCALE = 1.0 / (2.0 * CLIP) with SgzReader(os.path.join(base_path, '0.sgz')) as reader: t0 = time.time() slice_sgz = reader.read_zslice(LINE_NO) print("SgzReader took", time.time() - t0) im = Image.fromarray( np.uint8(cm.seismic((slice_sgz.T.clip(-CLIP, CLIP) + CLIP) * SCALE) * 255)) im.save(os.path.join(base_path, 'out_zslice-sgz.png')) with segyio.open(os.path.join(base_path, '0.sgy')) as segyfile: t0 = time.time() slice_segy = segyfile.depth_slice[LINE_NO] print("segyio took", time.time() - t0) im = Image.fromarray( np.uint8( cm.seismic((slice_segy.T.clip(-CLIP, CLIP) + CLIP) * SCALE) * 255)) im.save(os.path.join(base_path, 'out_zslice-sgy.png')) im = Image.fromarray( np.uint8( cm.seismic(
def main(): try: os.makedirs(args.output_directory) except: pass # Set GPU device rtx.set_device(args.gpu_device) # Initialize colors colors = [] for n in range(args.num_colors): hue = n / args.num_colors saturation = 1 lightness = 1 red, green, blue = colorsys.hsv_to_rgb(hue, saturation, lightness) colors.append((red, green, blue, 1)) screen_width = args.image_size screen_height = args.image_size # Setting up a raytracer rt_args = rtx.RayTracingArguments() rt_args.num_rays_per_pixel = 1024 rt_args.max_bounce = 3 rt_args.supersampling_enabled = args.anti_aliasing rt_args.next_event_estimation_enabled = True rt_args.ambient_light_intensity = 0.1 cuda_args = rtx.CUDAKernelLaunchArguments() cuda_args.num_threads = 64 cuda_args.num_rays_per_thread = 32 renderer = rtx.Renderer() render_buffer = np.zeros((screen_height, screen_width, 3), dtype=np.float32) archiver = Archiver( directory=args.output_directory, total_scenes=args.total_scenes, num_scenes_per_file=min(args.num_scenes_per_file, args.total_scenes), image_size=(args.image_size, args.image_size), num_observations_per_scene=args.num_observations_per_scene, initial_file_number=args.initial_file_number) camera = rtx.PerspectiveCamera(fov_rad=math.pi / 3, aspect_ratio=screen_width / screen_height) camera_distance = 2 for _ in tqdm(range(args.total_scenes)): scene = build_scene(floor_textures, wall_textures, fix_light_position=args.fix_light_position) place_objects(scene, colors, max_num_objects=args.max_num_objects, discrete_position=args.discrete_position, rotate_object=args.rotate_object) scene_data = SceneData((args.image_size, args.image_size), args.num_observations_per_scene) for _ in range(args.num_observations_per_scene): # Sample camera position rand_position_xz = np.random.normal(size=2) rand_position_xz = camera_distance * rand_position_xz / np.linalg.norm( rand_position_xz) camera_position = np.array( (rand_position_xz[0], wall_height / 2, rand_position_xz[1])) center = np.array((0, wall_height / 2, 0)) # Compute yaw and pitch camera_direction = camera_position - center yaw, pitch = compute_yaw_and_pitch(camera_direction) camera.look_at(tuple(camera_position), tuple(center), up=(0, 1, 0)) renderer.render(scene, camera, rt_args, cuda_args, render_buffer) # Convert to sRGB image = np.power(np.clip(render_buffer, 0, 1), 1.0 / 2.2) image = np.uint8(image * 255) image = cv2.bilateralFilter(image, 3, 25, 25) scene_data.add(image, camera_position, math.cos(yaw), math.sin(yaw), math.cos(pitch), math.sin(pitch)) if args.visualize: plt.clf() plt.imshow(image) plt.pause(1e-10) archiver.add(scene_data)
blankFrame = np.zeros(np.shape(frame), np.uint8) emptyFrame = blankFrame emptyFrame32 = np.float32(blankFrame) cv2.namedWindow('frame') cv2.setMouseCallback('frame', setEmpty) while(True): _, frame = cap.read() frame32 = np.float32(frame) diff32 = np.absolute(frame32 - emptyFrame32) norm32 = np.sqrt(diff32[:,:,0]**2 + diff32[:,:,1]**2 + diff32[:,:,2]**2)/np.sqrt(255**2 + 255**2 + 255**2) diff = np.uint8(norm32*255) _, thresh = cv2.threshold(diff, 100, 255, 0) kernel = np.ones((20,20), np.uint8) blobby = cv2.dilate(thresh, kernel, iterations= 4) # buffer pastBuff = currBuff currBuff = ( (currBuff << 1) | (np.any(blobby)) ) & buffMask if currBuff == buffMask: cv2.imshow('frame', blobby) else: cv2.imshow('frame', blankFrame) if cv2.waitKey(1) & 0xFF == 27: break
import cv2 import numpy as np from matplotlib import pyplot as plt image = cv2.imread('E:\Images\Kmeans\Test images\star.ppm') Z = image.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 = 8 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((image.shape)) cv2.imshow('res2', res2) cv2.waitKey(0) cv2.destroyAllWindows()
def main(model_path, exp_config): # Make and restore vagan model segvae_model = segvae(exp_config=exp_config) segvae_model.load_weights(model_path, type='best_ged') data_loader = data_switch(exp_config.data_identifier) data = data_loader(exp_config) lat_lvls = exp_config.latent_levels # RANDOM IMAGE # x_b, s_b = data.test.next_batch(1) # FIXED IMAGE # Cardiac: 100 normal image # LIDC: 200 large lesion, 203, 1757 complicated lesion # Prostate: 165 nice slice index = 165 # x_b = data.test.images[index, ...].reshape([1] + list(exp_config.image_size)) if exp_config.data_identifier == 'lidc': s_b = data.test.labels[index, ...] if np.sum(s_b[..., 0]) > 0: s_b = s_b[..., 0] elif np.sum(s_b[..., 1]) > 0: s_b = s_b[..., 1] elif np.sum(s_b[..., 2]) > 0: s_b = s_b[..., 2] else: s_b = s_b[..., 3] s_b = s_b.reshape([1] + list(exp_config.image_size[0:2])) elif exp_config.data_identifier == 'uzh_prostate': s_b = data.test.labels[index, ...] s_b = s_b[..., 0] s_b = s_b.reshape([1] + list(exp_config.image_size[0:2])) else: s_b = data.test.labels[index, ...].reshape([1] + list(exp_config.image_size[0:2])) # # print(x_b.shape) # print(s_b.shape) # x_b[:,30:64+10,64:64+10,:] = np.mean(x_b) # # x_b = utils.add_motion_artefacts(np.squeeze(x_b), 15) # x_b = x_b.reshape([1]+list(exp_config.image_size)) x_b_d = utils.convert_to_uint8(np.squeeze(x_b)) x_b_d = utils.resize_image(x_b_d, video_target_size) s_b_d = np.squeeze(np.uint8((s_b / exp_config.nlabels) * 255)) s_b_d = utils.resize_image(s_b_d, video_target_size, interp=cv2.INTER_NEAREST) _, mu_list_init, _ = segvae_model.generate_prior_samples( x_b, return_params=True) if SAVE_VIDEO: fourcc = cv2.VideoWriter_fourcc(*'XVID') outfile = os.path.join(model_path, 'samplevid_id%d.avi' % index) out = cv2.VideoWriter(outfile, fourcc, 10.0, (3 * video_target_size[1], video_target_size[0])) for lvl in reversed(range(lat_lvls)): samps = 50 if lat_lvls > 1 else 200 for _ in range(samps): # z_list, mu_list, sigma_list = segvae_model.generate_prior_samples(x_b, return_params=True) print('doing level %d/%d' % (lvl, lat_lvls)) # fix all below current level # for jj in range(lvl,lat_lvls-1): # z_list[jj+1] = mu_list_init[jj+1] # fix jj's level to mu # sample only current level # z_list_new = z_list.copy() # for jj in range(lat_lvls): # z_list_new[jj] = mu_list_init[jj] # z_list_new[lvl] = z_list[lvl] # z_list = z_list_new # # print('z means') # for jj, z in enumerate(z_list): # print('lvl %d: %.3f' % (jj, np.mean(z))) # # # feed_dict = {i: d for i, d in zip(segvae_model.prior_z_list_gen, z_list)} # feed_dict[segvae_model.training_pl] = False # # fix all below current level (the correct implementation) feed_dict = {} for jj in range(lvl, lat_lvls - 1): feed_dict[segvae_model.prior_z_list_gen[jj + 1]] = mu_list_init[jj + 1] feed_dict[segvae_model.training_pl] = False feed_dict[segvae_model.x_inp] = x_b s_p, s_p_list = segvae_model.sess.run( [segvae_model.s_out_eval, segvae_model.s_out_eval_list], feed_dict=feed_dict) s_p = np.argmax(s_p, axis=-1) print(np.unique(s_p)) # print('mean logits for myo cardium per level') # fig = plt.figure() # # cumsum = np.zeros((128,128)) # cumsum_all = np.zeros((128,128,4)) # for i, s in enumerate(reversed(s_p_list)): # # cumsum += s[0,:,:,2] # cumsum_all += s[0,:,:,:] # # fig.add_subplot(4,4,i+1) # plt.imshow(s[0,:,:,2]) # # fig.add_subplot(4,4,i+1+4) # plt.imshow(cumsum) # # fig.add_subplot(4,4,i+1+8) # plt.imshow(1./(1+np.exp(-cumsum))) # # fig.add_subplot(4,4,i+1+12) # plt.imshow(np.argmax(cumsum_all, axis=-1)) # # # plt.show() # DEUBG # cum_img = np.squeeze(s_p_list[lat_lvls-1]) # cum_img_disp = softmax(cum_img) # # indiv_img = np.squeeze(s_p_list[lat_lvls-1]) # indiv_img_disp = softmax(indiv_img) # # for ii in reversed(range(lat_lvls-1)): # cum_img += np.squeeze(s_p_list[ii]) # indiv_img = np.squeeze(s_p_list[ii]) # # cum_img_disp = np.concatenate([cum_img_disp, softmax(cum_img)], axis=1) # indiv_img_disp = np.concatenate([indiv_img_disp, softmax(indiv_img)], axis=1) # # # cum_img_disp = utils.convert_to_uint8(np.argmax(cum_img_disp, axis=-1)) # indiv_img_disp = utils.convert_to_uint8(indiv_img_disp[:,:,2]) # # cum_img_disp = np.concatenate([cum_img_disp, indiv_img_disp], axis=0) # # # print('cum img shape') # print(cum_img_disp.shape) # cv2.imshow('debug', cum_img_disp) # END DEBUG # s_p_d = utils.convert_to_uint8(np.squeeze(s_p)) s_p_d = np.squeeze(np.uint8((s_p / exp_config.nlabels) * 255)) s_p_d = utils.resize_image(s_p_d, video_target_size, interp=cv2.INTER_NEAREST) img = np.concatenate([x_b_d, s_b_d, s_p_d], axis=1) img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) img = histogram_equalization(img) if exp_config.data_identifier == 'acdc': # labels (0 85 170 255) rv = cv2.inRange(s_p_d, 84, 86) my = cv2.inRange(s_p_d, 169, 171) rv_cnt, hierarchy = cv2.findContours(rv, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) my_cnt, hierarchy = cv2.findContours(my, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(img, rv_cnt, -1, (0, 255, 0), 1) cv2.drawContours(img, my_cnt, -1, (0, 0, 255), 1) if exp_config.data_identifier == 'uzh_prostate': # labels (0 85 170 255) print(np.unique(s_p_d)) s1 = cv2.inRange(s_p_d, 84, 86) s2 = cv2.inRange(s_p_d, 169, 171) # s3 = cv2.inRange(s_p_d, 190, 192) s1_cnt, hierarchy = cv2.findContours(s1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) s2_cnt, hierarchy = cv2.findContours(s2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # s3_cnt, hierarchy = cv2.findContours(s3, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(img, s1_cnt, -1, (0, 255, 0), 1) cv2.drawContours(img, s2_cnt, -1, (0, 0, 255), 1) # cv2.drawContours(img, s3_cnt, -1, (255, 0, 255), 1) elif exp_config.data_identifier == 'lidc': thresh = cv2.inRange(s_p_d, 127, 255) lesion, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(img, lesion, -1, (0, 255, 0), 1) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img, 'Sampling level %d/%d' % (lvl + 1, lat_lvls), (30, 256 - 30), font, 1, (255, 255, 255), 1, cv2.LINE_AA) print('actual size') print(img.shape) if SAVE_VIDEO: out.write(img) cv2.imshow('frame', img) if cv2.waitKey(1) & 0xFF == ord('q'): break if SAVE_VIDEO: out.release() cv2.destroyAllWindows()
def predict_on_images(input_dir, graph, sess, output_dir, tmp_dir, score_threshold, categories, num_imgs, inference_times, delete_input, output_polygons, mask_threshold, mask_nth, output_minrect, view_margin, fully_connected, fit_bbox_to_polygon, output_width_height, bbox_as_fallback, output_mask_image): """ Method performing predictions on all images ony by one or combined as specified by the int value of num_imgs. :param input_dir: the directory with the images :type input_dir: str :param graph: the graph to use :type graph: tf.Graph() :param sess: the tensorflow session :type sess: tf.Session :param output_dir: the output directory to move the images to and store the predictions :type output_dir: str :param tmp_dir: the temporary directory to store the predictions until finished :type tmp_dir: str :param score_threshold: the minimum score predictions have to have :type score_threshold: float :param categories: the label map :param num_imgs: the number of images to combine into one before presenting to graph :type num_imgs: int :param inference_times: whether to output a CSV file with the inference times :type inference_times: bool :param delete_input: whether to delete the input images rather than moving them to the output directory :type delete_input: bool :param output_polygons: whether the model predicts masks and polygons should be stored in the CSV files :type output_polygons: bool :param mask_threshold: the threshold to use for determining the contour of a mask :type mask_threshold: float :param mask_nth: to speed up polygon computation, use only every nth row and column from mask :type mask_nth: int :param output_minrect: when predicting polygons, whether to output the minimal rectangles around the objects as well :type output_minrect: bool :param view_margin: the margin in pixels to use around the masks :type view_margin: int :param fully_connected: whether regions of 'high' or 'low' values should be fully-connected at isthmuses :type fully_connected: str :param fit_bbox_to_polygon: whether to fit the bounding box to the polygon :type fit_bbox_to_polygon: bool :param output_width_height: whether to output x/y/w/h instead of x0/y0/x1/y1 :type output_width_height: bool :param bbox_as_fallback: if ratio between polygon-bbox and bbox is smaller than this value, use bbox as fallback polygon, ignored if < 0 :type bbox_as_fallback: float :param output_mask_image: generates PNGs with indexed palette with the detected objects :type output_mask_image: float """ # Iterate through all files present in "test_images_directory" total_time = 0 if inference_times: times = list() times.append("Image(s)_file_name(s),Total_time(ms),Number_of_images,Time_per_image(ms)\n") # counter for keeping track of images that cannot be processed incomplete_counter = dict() while True: start_time = datetime.now() im_list = [] # Loop to pick up images equal to num_imgs or the remaining images if less for image_path in os.listdir(input_dir): # Load images only ext_lower = os.path.splitext(image_path)[1] if ext_lower in SUPPORTED_EXTS: full_path = os.path.join(input_dir, image_path) if auto.is_image_complete(full_path): im_list.append(full_path) else: if not full_path in incomplete_counter: incomplete_counter[full_path] = 1 else: incomplete_counter[full_path] = incomplete_counter[full_path] + 1 # remove images that cannot be processed remove_from_blacklist = [] for k in incomplete_counter: if incomplete_counter[k] == MAX_INCOMPLETE: print("%s - %s" % (str(datetime.now()), os.path.basename(k))) remove_from_blacklist.append(k) try: if delete_input: print(" flagged as incomplete {} times, deleting\n".format(MAX_INCOMPLETE)) os.remove(k) else: print(" flagged as incomplete {} times, skipping\n".format(MAX_INCOMPLETE)) os.rename(k, os.path.join(output_dir, os.path.basename(k))) except: print(traceback.format_exc()) for k in remove_from_blacklist: del incomplete_counter[k] if len(im_list) == num_imgs: break if len(im_list) == 0: time.sleep(1) break else: print("%s - %s" % (str(datetime.now()), ", ".join(os.path.basename(x) for x in im_list))) try: # Combining picked up images i = len(im_list) combined = [] comb_img = None if i > 1: while i != 0: if comb_img is None: img2 = Image.open(im_list[i-1]) img1 = Image.open(im_list[i-2]) i -= 1 combined.append(os.path.join(output_dir, "combined.png")) else: img2 = comb_img img1 = Image.open(im_list[i-1]) i -= 1 # Remove alpha channel if present img1 = remove_alpha_channel(img1) img2 = remove_alpha_channel(img2) w1, h1 = img1.size w2, h2 = img2.size comb_img = np.zeros((h1+h2, max(w1, w2), 3), np.uint8) comb_img[:h1, :w1, :3] = img1 comb_img[h1:h1+h2, :w2, :3] = img2 comb_img = Image.fromarray(comb_img) if comb_img is None: im_name = im_list[0] image = Image.open(im_name) image = remove_alpha_channel(image) else: im_name = combined[0] image = remove_alpha_channel(comb_img) image_np = image_to_numpyarray(image) output_dict = inference_for_image(image_np, graph, sess) # Loading results boxes = output_dict['detection_boxes'] scores = output_dict['detection_scores'] classes = output_dict['detection_classes'] # Code for splitting rois to multiple csv's, one csv per image before combining max_height = 0 prev_min = 0 for i in range(len(im_list)): img = Image.open(im_list[i]) img_height = img.height min_height = prev_min max_height += img_height prev_min = max_height roi_path = "{}/{}-rois.csv".format(output_dir, os.path.splitext(os.path.basename(im_list[i]))[0]) img_path = "{}/{}-mask.png".format(output_dir, os.path.splitext(os.path.basename(im_list[i]))[0]) if tmp_dir is not None: roi_path_tmp = "{}/{}-rois.tmp".format(tmp_dir, os.path.splitext(os.path.basename(im_list[i]))[0]) img_path_tmp = "{}/{}-mask.tmp".format(tmp_dir, os.path.splitext(os.path.basename(im_list[i]))[0]) else: roi_path_tmp = "{}/{}-rois.tmp".format(output_dir, os.path.splitext(os.path.basename(im_list[i]))[0]) img_path_tmp = "{}/{}-mask.tmp".format(output_dir, os.path.splitext(os.path.basename(im_list[i]))[0]) roiobjs = [] for index in range(output_dict['num_detections']): score = scores[index] # Ignore this roi if the score is less than the provided threshold if score < score_threshold: continue y0n, x0n, y1n, x1n = boxes[index] # Translate roi coordinates into combined image coordinates x0 = x0n * image.width y0 = y0n * image.height x1 = x1n * image.width y1 = y1n * image.height if y0 > max_height or y1 > max_height: continue elif y0 < min_height or y1 < min_height: continue label = classes[index] label_str = categories[label - 1]['name'] px = None py = None pxn = None pyn = None bw = None bh = None mask_comb = None if output_polygons: px = [] py = [] pxn = [] pyn = [] bw = "" bh = "" if 'detection_masks'in output_dict: poly = mask_to_polygon(output_dict['detection_masks'][index], mask_threshold=mask_threshold, mask_nth=mask_nth, view=(x0, y0, x1, y1), view_margin=view_margin, fully_connected=fully_connected) if len(poly) > 0: px, py = polygon_to_lists(poly[0], swap_x_y=True, normalize=False) pxn, pyn = polygon_to_lists(poly[0], swap_x_y=True, normalize=True, img_width=image.width, img_height=image.height) if output_minrect: bw, bh = polygon_to_minrect(poly[0]) if bbox_as_fallback >= 0: if len(px) >= 3: p_x0n, p_y0n, p_x1n, p_y1n = polygon_to_bbox(lists_to_polygon(pxn, pyn)) p_area = (p_x1n - p_x0n) * (p_y1n - p_y0n) b_area = (x1n - x0n) * (y1n - y0n) if (b_area > 0) and (p_area / b_area < bbox_as_fallback): px = [float(i) for i in [x0, x1, x1, x0]] py = [float(i) for i in [y0, y0, y1, y1]] pxn = [float(i) for i in [x0n, x1n, x1n, x0n]] pyn = [float(i) for i in [y0n, y0n, y1n, y1n]] else: px = [float(i) for i in [x0, x1, x1, x0]] py = [float(i) for i in [y0, y0, y1, y1]] pxn = [float(i) for i in [x0n, x1n, x1n, x0n]] pyn = [float(i) for i in [y0n, y0n, y1n, y1n]] if output_minrect: bw = x1 - x0 + 1 bh = y1 - y0 + 1 if fit_bbox_to_polygon: if len(px) >= 3: x0, y0, x1, y1 = polygon_to_bbox(lists_to_polygon(px, py)) x0n, y0n, x1n, y1n = polygon_to_bbox(lists_to_polygon(pxn, pyn)) if output_mask_image and 'detection_masks'in output_dict: mask_img = output_dict['detection_masks'][index].copy() mask_img[mask_img < mask_threshold] = 0 mask_img[mask_img >= mask_threshold] = label+1 # first label is 0 if mask_comb is None: mask_comb = mask_img else: tmp = np.where(mask_comb==0, mask_img, mask_comb) mask_comb = tmp roiobj = ROIObject(x0, y0, x1, y1, x0n, y0n, x1n, y1n, label, label_str, score=score, poly_x=px, poly_y=py, poly_xn=pxn, poly_yn=pyn, minrect_w=bw, minrect_h=bh) roiobjs.append(roiobj) info = ImageInfo(os.path.basename(im_list[i])) roiext = (info, roiobjs) options = ["--output", str(tmp_dir if tmp_dir is not None else output_dir), "--no-images"] if output_width_height: options.append("--size-mode") roiwriter = ROIWriter(options) roiwriter.save([roiext]) if tmp_dir is not None: os.rename(roi_path_tmp, roi_path) if mask_comb is not None: im = Image.fromarray(np.uint8(mask_comb), 'P') im.save(img_path_tmp, "PNG") os.rename(img_path_tmp, img_path) except: print("Failed processing images: {}".format(",".join(im_list))) print(traceback.format_exc()) # Move finished images to output_path or delete it for i in range(len(im_list)): if delete_input: os.remove(im_list[i]) else: os.rename(im_list[i], os.path.join(output_dir, os.path.basename(im_list[i]))) end_time = datetime.now() inference_time = end_time - start_time inference_time = int(inference_time.total_seconds() * 1000) time_per_image = int(inference_time / len(im_list)) if inference_times: l = "" for i in range(len(im_list)): l += ("{}|".format(os.path.basename(im_list[i]))) l += ",{},{},{}\n".format(inference_time, len(im_list), time_per_image) times.append(l) print(" Inference + I/O time: {} ms\n".format(inference_time)) total_time += inference_time if inference_times: with open(os.path.join(output_dir, "inference_time.csv"), "w") as time_file: for l in times: time_file.write(l) with open(os.path.join(output_dir, "total_time.txt"), "w") as total_time_file: total_time_file.write("Total inference and I/O time: {} ms\n".format(total_time))
train_dateset = NpDataset('train') for imgs, lables in train_dateset: img,lab = modelTest(imgs/255.0) cv2.imshow('imgsSrc', imgs[3] / 255.0) cv2.imshow('lab', np.sum(lab[3].numpy(), -1)) cv2.imshow('lablesSrc', np.sum(lables[3], -1) / 255) blank = lab[3].numpy() labTem = np.zeros([cfg.TRAIN.INPUT_SIZE,cfg.TRAIN.INPUT_SIZE]) for i in range(4): tem = blank[:,:,i] ret, binary = cv2.threshold(tem, 0.7, 1, cv2.THRESH_BINARY) labTem += binary gray = np.uint8(binary * 255.0) contours, hier = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for c in contours: # area = cv2.contourArea(c) # 面积 Rectx, Recty, Rectw, Recth = cv2.boundingRect(c) # 矩形框 imgTem = tem[Recty:Recty + Recth, Rectx:Rectx + Rectw] # 利用cv2.minMaxLoc寻找到图像中最亮和最暗的点 (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(imgTem) print(maxLoc) print(imgTem) # cv2.imshow('%d'%(i+3), binary) cv2.imshow('labTem', labTem) cv2.waitKey(0)
hsvImageCopy = np.float32(hsvImageCopy) #initialize desaturation Scale value saturationScale = 0.01 #split the channels H, S, V = cv2.split(hsvImageCopy) #Desaturation. Multiply S channel by scaling factor and make sure the values are between 0 and 255 S = np.clip(S * saturationScale, 0, 255) #merge the channels hsvImageCopy = cv2.merge([H, S, V]) #convert back to uint8 hsvImageCopy = np.uint8(hsvImageCopy) #convert back to bgr color space hsvImageCopy = cv2.cvtColor(hsvImageCopy, cv2.COLOR_HSV2BGR) #create windows to show image cv2.namedWindow("image", cv2.WINDOW_NORMAL) cv2.namedWindow("hsv", cv2.WINDOW_NORMAL) cv2.namedWindow("desaturated", cv2.WINDOW_NORMAL) #display images cv2.imshow("image", image) cv2.imshow("hsv", hsvImage) cv2.imshow("desaturated", hsvImageCopy) #press esc to exit the program
def track(self, sess, first_bbox, frames, logdir='/tmp'): """Runs tracking on a single image sequence.""" # Get initial target bounding box and convert to center based bbox = convert_bbox_format(first_bbox, 'center-based') print(frames) # Feed in the first frame image to set initial state. bbox_feed = [bbox.y, bbox.x, bbox.height, bbox.width] input_feed = [frames[0], bbox_feed] frame2crop_scale = self.siamese_model.initialize(sess, input_feed) # Storing target state original_target_height = bbox.height original_target_width = bbox.width search_center = np.array([get_center(self.x_image_size), get_center(self.x_image_size)]) current_target_state = TargetState(bbox=bbox, search_pos=search_center, scale_idx=int(get_center(self.num_scales))) include_first = get(self.track_config, 'include_first', False) logging.info('Tracking include first -- {}'.format(include_first)) # Run tracking loop reported_bboxs = [] output_json={} #dump all bboxes in this output file for i, filename in enumerate(frames): if i > 0 or include_first: # We don't really want to process the first image unless intended to do so. bbox_feed = [current_target_state.bbox.y, current_target_state.bbox.x, current_target_state.bbox.height, current_target_state.bbox.width] input_feed = [filename, bbox_feed] outputs, metadata = self.siamese_model.inference_step(sess, input_feed) search_scale_list = outputs['scale_xs'] response = outputs['response'] response_size = response.shape[1] # Choose the scale whole response map has the highest peak if self.num_scales > 1: response_max = np.max(response, axis=(1, 2)) penalties = self.track_config['scale_penalty'] * np.ones((self.num_scales)) current_scale_idx = int(get_center(self.num_scales)) penalties[current_scale_idx] = 1.0 response_penalized = response_max * penalties best_scale = np.argmax(response_penalized) else: best_scale = 0 response = response[best_scale] #print(response) with np.errstate(all='raise'): # Raise error if something goes wrong response = response - np.min(response) response = response / np.sum(response) if self.window is None: window = np.dot(np.expand_dims(np.hanning(response_size), 1), np.expand_dims(np.hanning(response_size), 0)) self.window = window / np.sum(window) # normalize window window_influence = self.track_config['window_influence'] response = (1 - window_influence) * response + window_influence * self.window # Find maximum response srtd=response.argsort(axis=None) v = response.argmax() r_max, c_max = np.unravel_index(v, response.shape) if not osp.exists(osp.join(logdir,"Intermediate")): os.mkdir(osp.join(logdir,"Intermediate")) to_save = np.interp(response,(response.min(),response.max()),(0,255)) cv2.imwrite(osp.join(logdir,"Intermediate",f"response_{i}.png"),to_save) to_save = to_save.reshape(to_save.shape[0],to_save.shape[1],1) ret,thresh1 = cv2.threshold(to_save,185,255,cv2.THRESH_BINARY) cv2.imwrite(osp.join(logdir,"Intermediate",f"response_{i}_thresh.png"),thresh1) image = np.uint8(thresh1.copy()) cnts = cv2.findContours(image, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) backtorgb = cv2.cvtColor(image,cv2.COLOR_GRAY2RGB) image = cv2.drawContours(backtorgb, cnts, -1, (0, 255, 0), 2) cv2.imwrite(osp.join(logdir,"Intermediate",f"response_{i}_cntrs.png"),image) centres=[] for c in cnts: M = cv2.moments(c) cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) centres.append((cY,cX,False)) centres.append((r_max,c_max,True)) #print(centres) #cts_copy = copy(current_target_state) #cts_copy2 = copy(current_target_state) output_json[filename]=[] for (r_max,c_max,to_deep_copy) in centres: if to_deep_copy: cts_copy = deepcopy(current_target_state) else: cts_copy = copy(current_target_state) # Convert from crop-relative coordinates to frame coordinates p_coor = np.array([r_max, c_max]) # displacement from the center in instance final representation ... disp_instance_final = p_coor - get_center(response_size) # ... in instance feature space ... upsample_factor = self.track_config['upsample_factor'] disp_instance_feat = disp_instance_final / upsample_factor # ... Avoid empty position ... r_radius = int(response_size / upsample_factor / 2) disp_instance_feat = np.maximum(np.minimum(disp_instance_feat, r_radius), -r_radius) # ... in instance input ... disp_instance_input = disp_instance_feat * self.model_config['embed_config']['stride'] # ... in instance original crop (in frame coordinates) disp_instance_frame = disp_instance_input / search_scale_list[best_scale] # Position within frame in frame coordinates y = cts_copy.bbox.y x = cts_copy.bbox.x y += disp_instance_frame[0] x += disp_instance_frame[1] # Target scale damping and saturation target_scale = cts_copy.bbox.height / original_target_height search_factor = self.search_factors[best_scale] scale_damp = self.track_config['scale_damp'] # damping factor for scale update target_scale *= ((1 - scale_damp) * 1.0 + scale_damp * search_factor) target_scale = np.maximum(0.2, np.minimum(5.0, target_scale)) # Some book keeping height = original_target_height * target_scale width = original_target_width * target_scale cts_copy.bbox = Rectangle(x, y, width, height) cts_copy.scale_idx = best_scale cts_copy.search_pos = search_center + disp_instance_input assert 0 <= cts_copy.search_pos[0] < self.x_image_size, \ 'target position in feature space should be no larger than input image size' assert 0 <= cts_copy.search_pos[1] < self.x_image_size, \ 'target position in feature space should be no larger than input image size' if self.log_level > 0 and to_deep_copy: np.save(osp.join(logdir, 'num_frames.npy'), [i + 1]) # Select the image with the highest score scale and convert it to uint8 image_cropped = outputs['image_cropped'][best_scale].astype(np.uint8) # Note that imwrite in cv2 assumes the image is in BGR format. # However, the cropped image returned by TensorFlow is RGB. # Therefore, we convert color format using cv2.cvtColor imwrite(osp.join(logdir, 'image_cropped{}.jpg'.format(i)), cv2.cvtColor(image_cropped, cv2.COLOR_RGB2BGR)) np.save(osp.join(logdir, 'best_scale{}.npy'.format(i)), [best_scale]) np.save(osp.join(logdir, 'response{}.npy'.format(i)), response) y_search, x_search = cts_copy.search_pos search_scale = search_scale_list[best_scale] target_height_search = height * search_scale target_width_search = width * search_scale bbox_search = Rectangle(x_search, y_search, target_width_search, target_height_search) bbox_search = convert_bbox_format(bbox_search, 'top-left-based') np.save(osp.join(logdir, 'bbox{}.npy'.format(i)), [bbox_search.x, bbox_search.y, bbox_search.width, bbox_search.height]) reported_bbox = convert_bbox_format(cts_copy.bbox, 'top-left-based') #print(f"reported bbox {reported_bbox}") if to_deep_copy: reported_bboxs.append(reported_bbox) else: rect_str = '{},{},{},{}\n'.format(reported_bbox.x + 1, reported_bbox.y + 1, reported_bbox.width, reported_bbox.height) arr = output_json[filename] arr.append(rect_str) with open(osp.join(logdir,'bboxes.json'),'w') as f: json.dump(output_json,f,indent=4) return reported_bboxs
def heatmap_overlay(bg_img, overlay_img, cmap='jet'): img = np.uint8(bg_img[..., :3] * 255) if len(overlay_img.shape) == 2: overlay_img = cm.get_cmap(cmap)(overlay_img) heatmap = np.uint8(overlay_img[..., :3] * 255) return cv2.addWeighted(img, 0.5, heatmap, 0.5, 0)
def pre_processing(observe): processed_observe = np.uint8( resize(rgb2gray(observe), (84, 84), mode='constant') * 255) return processed_observe
def write(filename, mesh, binary=True): # noqa: C901 for key in mesh.cells: if not any(c.type in ["triangle", "quad"] for c in mesh.cells): raise WriteError("Can only deal with triangular and quadrilateral faces") with open_file(filename, "wb") as fh: fh.write(b"ply\n") fh.write(b"comment Created by meshio\n") if binary: fh.write( "format binary_{}_endian 1.0\n".format(sys.byteorder).encode("utf-8") ) else: fh.write(b"format ascii 1.0\n") # counts fh.write("element vertex {:d}\n".format(mesh.points.shape[0]).encode("utf-8")) # dim_names = ["x", "y", "z"] # From <https://en.wikipedia.org/wiki/PLY_(file_format)>: # # > The type can be specified with one of char uchar short ushort int uint float # > double, or one of int8 uint8 int16 uint16 int32 uint32 float32 float64. # # We're adding [u]int64 here. type_name_table = { numpy.dtype(numpy.int8): "int8", numpy.dtype(numpy.int16): "int16", numpy.dtype(numpy.int32): "int32", numpy.dtype(numpy.int64): "int64", numpy.dtype(numpy.uint8): "uint8", numpy.dtype(numpy.uint16): "uint16", numpy.dtype(numpy.uint32): "uint32", numpy.dtype(numpy.uint64): "uint64", numpy.dtype(numpy.float32): "float", numpy.dtype(numpy.float64): "double", } for k in range(mesh.points.shape[1]): type_name = type_name_table[mesh.points.dtype] fh.write("property {} {}\n".format(type_name, dim_names[k]).encode("utf-8")) for key, value in mesh.point_data.items(): type_name = type_name_table[value.dtype] fh.write("property {} {}\n".format(type_name, key).encode("utf-8")) num_cells = 0 for cell_type, c in mesh.cells: if cell_type in ["triangle", "quad"]: num_cells += c.data.shape[0] fh.write("element face {:d}\n".format(num_cells).encode("utf-8")) # possibly cast down to int32 cells = mesh.cells has_cast = False for k, (cell_type, data) in enumerate(mesh.cells): if data.dtype == numpy.int64: has_cast = True mesh.cells[k] = CellBlock(cell_type, data.astype(numpy.int32)) if has_cast: warnings.warn( "PLY doesn't support 64-bit integers. Casting down to 32-bit." ) # assert that all cell dtypes are equal cell_dtype = None for _, cell in cells: if cell_dtype is None: cell_dtype = cell.dtype if cell.dtype != cell_dtype: raise WriteError() if cell_dtype is not None: ply_type = numpy_to_ply_dtype[cell_dtype] fh.write( "property list {} {} vertex_indices\n".format("uint8", ply_type).encode( "utf-8" ) ) # TODO other cell data fh.write(b"end_header\n") if binary: # points and point_data out = numpy.rec.fromarrays( [coord for coord in mesh.points.T] + list(mesh.point_data.values()) ) fh.write(out.tobytes()) # cells for cell_type, data in cells: if cell_type not in ["triangle", "quad"]: continue # prepend with count out = numpy.rec.fromarrays( [ numpy.broadcast_to(numpy.uint8(data.shape[1]), data.shape[0]), *data.T, ] ) fh.write(out.tobytes()) else: # vertices # numpy.savetxt(fh, mesh.points, "%r") # slower # out = numpy.column_stack([mesh.points] + list(mesh.point_data.values())) out = numpy.rec.fromarrays( [coord for coord in mesh.points.T] + list(mesh.point_data.values()) ) fmt = " ".join(["{}"] * len(out[0])) out = "\n".join([fmt.format(*row) for row in out]) + "\n" fh.write(out.encode("utf-8")) # cells for cell_type, data in cells: if cell_type not in ["triangle", "quad"]: continue out = numpy.column_stack( [numpy.full(data.shape[0], data.shape[1], dtype=data.dtype), data] ) # savetxt is slower # numpy.savetxt(fh, out, "%d %d %d %d") fmt = " ".join(["{}"] * out.shape[1]) out = "\n".join([fmt.format(*row) for row in out]) + "\n" fh.write(out.encode("utf-8"))
def __getitem__(self, i): idx = self.ids[i] mask_file = glob(self.masks_dir + idx + '.*') img_file = glob(self.imgs_dir + idx + '.*') # create new image of desired size for padding ww = 550 hh = 550 assert len(mask_file) == 1, \ f'Either no mask or multiple masks found for the ID {idx}: {mask_file}' assert len(img_file) == 1, \ f'Either no image or multiple images found for the ID {idx}: {img_file}' mask = Image.open(mask_file[0]) wd_m, ht_m = mask.size # color = (255 ,0, 0) result_mask = np.full((hh,ww,3), 0, dtype=np.uint8) # compute center offset xx = (ww - wd_m) // 2 yy = (hh - ht_m) // 2 # copy img image into center of result image try: result_mask[yy:yy+ht_m, xx:xx+wd_m] = mask except: # with open('bad_files.txt', 'a') as f: # f.write('Img file: \n') # f.write(mask_file[0]) # f.write('\n') print(mask_file[0]) print('error in result_mask[yy:yy+ht_m, xx:xx+wd_m] = mask!!!!') # print(mask) # print(result_mask) img = Image.open(img_file[0]).convert('L') wd_im, ht_im = img.size result_img = np.full((hh,ww), 0, dtype=np.uint8) xx = (ww - wd_im) // 2 yy = (hh - ht_im) // 2 try: result_img[yy:yy+ht_im, xx:xx+wd_im] = img except: # with open('bad_files.txt', 'a') as f: # f.write('Mask file: \n') # f.write(mask_file[0]) # f.write('\n') print(img_file[0]) print('error in result_img[yy:yy+ht_im, xx:xx+wd_im] = img!!!!') # print(img) # print(result_img) assert img.size == mask.size, \ f'Image and mask {idx} should be the same size, but are {img.size} and {mask.size}' result_img = Image.fromarray(np.uint8(result_img)) result_mask = Image.fromarray(np.uint8(result_mask)) result_img = self.preprocess(result_img, self.scale) result_mask = self.preprocess(result_mask, self.scale, mask) return { 'image': torch.from_numpy(result_img).type(torch.FloatTensor), 'mask': torch.from_numpy(result_mask).type(torch.FloatTensor) }
sess.run(tf.global_variables_initializer()) print('Loading model checkpoint weights') saver = tf.train.Saver(max_to_keep=1000) saver.restore(sess, args.checkpoint_path) print("Testing image " + args.image) loaded_image = utils.load_image(args.image) resized_image = cv2.resize(loaded_image, (args.crop_width, args.crop_height)) input_image = np.expand_dims(np.float32( resized_image[:args.crop_height, :args.crop_width]), axis=0) / 255.0 st = time.time() output_image = sess.run(network, feed_dict={net_input: input_image}) run_time = time.time() - st output_image = np.array(output_image[0, :, :, :]) output_image = helpers.reverse_one_hot(output_image) out_vis_image = helpers.colour_code_segmentation(output_image, label_values) file_name = utils.filepath_to_name(args.image) cv2.imwrite("%s_pred.png" % (file_name), cv2.cvtColor(np.uint8(out_vis_image), cv2.COLOR_RGB2BGR)) print("") print("Finished!") print("Wrote image " + "%s_pred.png" % (file_name))
u = u[:, :chi] vt = vt[:chi, :] st = s[:chi] # In[8]: array_truncated = np.dot(np.dot(u, np.diag(st)), vt) ## make truncated array normalized_distance = np.sqrt(np.sum( (array - array_truncated)**2)) / np.sqrt(np.sum(array**2)) print("Low rank approximation with chi=" + repr(chi)) print("Normalized distance:" + repr(normalized_distance)) ## print normalized distance # In[9]: img_gray_truncated = Image.fromarray(np.uint8(np.clip( array_truncated, 0, 255))) ## convert to grayscale image # In[10]: #img_gray_truncated.show(title="Truncated") ## show image in external window img_gray_truncated.save("./gray_truncated.png") ## save compressed image #img_gray_truncated.save("./gray_truncated.jpg") ## save compressed image in jpg # In[11]: plt.figure(figsize=(array.shape[1] * 0.01, array.shape[0] * 0.01)) plt.axis("off") plt.title("Original") plt.imshow(img_gray, cmap='gray') plt.figure(figsize=(array_truncated.shape[1] * 0.01,
def main(): p = argparse.ArgumentParser() p.add_argument('--gpu', '-g', type=int, default=-1) p.add_argument('--model', '-m', type=str, default='models/baseline.pth') p.add_argument('--input', '-i', required=True) p.add_argument('--sr', '-r', type=int, default=44100) p.add_argument('--hop_length', '-l', type=int, default=1024) p.add_argument('--window_size', '-w', type=int, default=512) p.add_argument('--out_mask', '-M', action='store_true') p.add_argument('--postprocess', '-p', action='store_true') args = p.parse_args() print('loading model...', end=' ') device = torch.device('cpu') model = nets.CascadedASPPNet() model.load_state_dict(torch.load(args.model, map_location=device)) if torch.cuda.is_available() and args.gpu >= 0: device = torch.device('cuda:{}'.format(args.gpu)) model.to(device) print('done') print('loading wave source...', end=' ') X, sr = librosa.load(args.input, args.sr, False, dtype=np.float32, res_type='kaiser_fast') print('done') print('stft of wave source...', end=' ') X = spec_utils.calc_spec(X, args.hop_length) X, phase = np.abs(X), np.exp(1.j * np.angle(X)) coeff = X.max() X /= coeff print('done') offset = model.offset l, r, roi_size = dataset.make_padding(X.shape[2], args.window_size, offset) X_pad = np.pad(X, ((0, 0), (0, 0), (l, r)), mode='constant') X_roll = np.roll(X_pad, roi_size // 2, axis=2) model.eval() with torch.no_grad(): masks = [] masks_roll = [] for i in tqdm(range(int(np.ceil(X.shape[2] / roi_size)))): start = i * roi_size X_window = torch.from_numpy( np.asarray([ X_pad[:, :, start:start + args.window_size], X_roll[:, :, start:start + args.window_size] ])).to(device) pred = model.predict(X_window) pred = pred.detach().cpu().numpy() masks.append(pred[0]) masks_roll.append(pred[1]) mask = np.concatenate(masks, axis=2)[:, :, :X.shape[2]] mask_roll = np.concatenate(masks_roll, axis=2)[:, :, :X.shape[2]] mask = (mask + np.roll(mask_roll, -roi_size // 2, axis=2)) / 2 if args.postprocess: vocal = X * (1 - mask) * coeff mask = spec_utils.mask_uninformative(mask, vocal) inst = X * mask * coeff vocal = X * (1 - mask) * coeff basename = os.path.splitext(os.path.basename(args.input))[0] print('inverse stft of instruments...', end=' ') wav = spec_utils.spec_to_wav(inst, phase, args.hop_length) print('done') sf.write('{}_Instruments.wav'.format(basename), wav.T, sr) print('inverse stft of vocals...', end=' ') wav = spec_utils.spec_to_wav(vocal, phase, args.hop_length) print('done') sf.write('{}_Vocals.wav'.format(basename), wav.T, sr) if args.out_mask: norm_mask = np.uint8((1 - mask) * 255).transpose(1, 2, 0) norm_mask = np.concatenate( [np.max(norm_mask, axis=2, keepdims=True), norm_mask], axis=2)[::-1] _, bin_mask = cv2.imencode('.png', norm_mask) with open('{}_Mask.png'.format(basename), mode='wb') as f: bin_mask.tofile(f)
xmin = target_bboxes[i, j, k, 0] * wandhG[k, 0] + anchor_xmin ymin = target_bboxes[i, j, k, 1] * wandhG[k, 1] + anchor_ymin xmax = tf.exp(target_bboxes[i, j, k, 2]) * wandhG[k, 0] + xmin ymax = tf.exp(target_bboxes[i, j, k, 3]) * wandhG[k, 1] + ymin if target_scores[i, j, k, 1] > 0: # it is a positive sample print("=> Decoding positive sample: %d, %d, %d" %(i, j, k)) cv2.circle(decode_image, center=(int(0.5*(xmin+xmax)), int(0.5*(ymin+ymax))), radius=1, color=[255,0,0], thickness=4) pred_boxes.append(np.array([xmin, ymin, xmax, ymax])) pred_score.append(target_scores[i, j, k, 1]) pred_boxes = np.array(pred_boxes) plot_boxes_on_image(decode_image, pred_boxes, color=[0, 255, 0]) Image.fromarray(np.uint8(decode_image)).show() ############################## FASTER DECODE OUTPUT ############################### faster_decode_image = np.copy(raw_image) # pred_bboxes = target_bboxes # pred_scores = target_scores.astype(np.float32) pred_bboxes = np.expand_dims(target_bboxes, 0).astype(np.float32) pred_scores = np.expand_dims(target_scores, 0).astype(np.float32) pred_scores, pred_bboxes = decode_output(pred_bboxes, pred_scores) plot_boxes_on_image(faster_decode_image, pred_bboxes, color=[255, 0, 0]) # red boundig box Image.fromarray(np.uint8(faster_decode_image)).show() ## bboxes
def MergeImg(cats_order, scatts, styles, rend_dt, output_queue): _rendDtFilesToMemmaps(rend_dt) init = True merged_img = None merge_tmp = grass.tempfile() for cat_id in cats_order: if cat_id not in scatts: continue scatt = scatts[cat_id] # print "color map %d" % cat_id # TODO make more general if cat_id != 0 and (styles[cat_id]["opacity"] == 0.0 or not styles[cat_id]["show"]): if cat_id in rend_dt and not rend_dt[cat_id]: del rend_dt[cat_id] continue if init: b2_i = scatt["bands_info"]["b1"] b1_i = scatt["bands_info"]["b2"] full_extend = ( b1_i["min"] - 0.5, b1_i["max"] + 0.5, b2_i["min"] - 0.5, b2_i["max"] + 0.5, ) # if it does not need to be updated and was already rendered if not _renderCat(cat_id, rend_dt, scatt, styles): # is empty - has only zeros if rend_dt[cat_id] is None: continue else: masked_cat = np.ma.masked_less_equal(scatt["np_vals"], 0) vmax = np.amax(masked_cat) # totally empty -> no need to render if vmax == 0: rend_dt[cat_id] = None continue cmap = _getColorMap(cat_id, styles) masked_cat = np.uint8(masked_cat * (255.0 / float(vmax))) cmap = np.uint8(cmap._lut * 255) sh = masked_cat.shape rend_dt[cat_id] = {} if cat_id != 0: rend_dt[cat_id]["color"] = styles[cat_id]["color"] rend_dt[cat_id]["dt"] = np.memmap(grass.tempfile(), dtype="uint8", mode="w+", shape=(sh[0], sh[1], 4)) # colored_cat = np.zeros(dtype='uint8', ) ApplyColormap(masked_cat, masked_cat.mask, cmap, rend_dt[cat_id]["dt"]) # colored_cat = np.uint8(cmap(masked_cat) * 255) del masked_cat del cmap # colored_cat[...,3] = np.choose(masked_cat.mask, (255, 0)) if init: merged_img = np.memmap(merge_tmp, dtype="uint8", mode="w+", shape=rend_dt[cat_id]["dt"].shape) merged_img[:] = rend_dt[cat_id]["dt"] init = False else: MergeArrays(merged_img, rend_dt[cat_id]["dt"], styles[cat_id]["opacity"]) """ #c_img_a = np.memmap(grass.tempfile(), dtype="uint16", mode='w+', shape = shape) c_img_a = colored_cat.astype('uint16')[:,:,3] * styles[cat_id]['opacity'] #TODO apply strides and there will be no need for loop #b = as_strided(a, strides=(0, a.strides[3], a.strides[3], a.strides[3]), shape=(3, a.shape[0], a.shape[1])) for i in range(3): merged_img[:,:,i] = (merged_img[:,:,i] * (255 - c_img_a) + colored_cat[:,:,i] * c_img_a) / 255; merged_img[:,:,3] = (merged_img[:,:,3] * (255 - c_img_a) + 255 * c_img_a) / 255; del c_img_a """ _rendDtMemmapsToFiles(rend_dt) merged_img = {"dt": merged_img.filename, "sh": merged_img.shape} output_queue.put((merged_img, full_extend, rend_dt))
import cv2 import numpy as np import imutils if __name__ == "__main__": img_path = "test_imgs/frame_0214.jpg" img = cv2.imread(img_path) box = cv2.selectROI("Frame", img, fromCenter=False, showCrosshair=True) x, y, w, h = box roi_img = img[y:y + h, x:x + w] roi_img = imutils.resize(roi_img, width=64, height=128) norm_roi = np.float32(roi_img) / 255.0 gx = cv2.Sobel(norm_roi, cv2.CV_32F, 1, 0, ksize=1) gy = cv2.Sobel(norm_roi, cv2.CV_32F, 0, 1, ksize=1) mag, angle = cv2.cartToPolar(gx, gy, angleInDegrees=True) mag *= 255.0 mag = np.uint8(mag) cv2.imshow('frame', mag) while True: k = cv2.waitKey(30) & 0xff if k == 27: break
def main(image_filename): ''' Returns: 1 - List of tuples which is the coordinates for occupied grid. 2 - Dictionary with information of path. ''' occupied_grids = [] # List to store coordinates of occupied grid planned_path = {} # Dictionary to store information regarding path planning # load the image and define the window width and height image = cv2.imread(image_filename) (winW, winH) = (60, 60) # Size of individual cropped images obstacles = [] # List to store obstacles (black tiles) index = [1,1] #starting point #create blank image, initialized as a matrix of 0s the width and height blank_image = np.zeros((60,60,3), np.uint8) #create an array of 100 blank images list_images = [[blank_image for i in xrange(20)] for i in xrange(20)] #array of list of images #empty #matrix to represent the grids of individual cropped images maze = [[0 for i in xrange(20)] for i in xrange(20)] #traversal for each square for (x, y, window) in traversal.sliding_window(image, stepSize=60, windowSize=(winW, winH)): # if the window does not meet our desired window size, ignore it if window.shape[0] != winH or window.shape[1] != winW: continue # print index, image is our iterator, it's where were at returns image matrix clone = image.copy() #format square for openCV cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2) crop_img = image[x:x + winW, y:y + winH] #crop the image list_images[index[0]-1][index[1]-1] = crop_img.copy() #Add it to the array of images #we want to print occupied grids, need to check if white or not average_color_per_row = np.average(crop_img, axis=0) average_color = np.average(average_color_per_row, axis=0) average_color = np.uint8(average_color) #Average color of the grids #iterate through color matrix, if (any(i <= 176 for i in average_color)): #Check if grids are colored maze[index[1]-1][index[0]-1] = 1 #ie not majorly white occupied_grids.append(tuple(index)) #These grids are termed as occupied_grids if (any(i <= 43 for i in average_color)): #Check if grids are black in color # print ("black obstacles") obstacles.append(tuple(index)) #add to obstacles list #show this iteration #cv2.namedWindow("Window", cv2.WINDOW_NORMAL) #cv2.resizeWindow('Window', 600,600) #cv2.imshow("Window", clone) #cv2.waitKey(1) #time.sleep(0.005) #Iterate index[1] = index[1] + 1 if(index[1]>20): index[0] = index[0] + 1 index[1] = 1 #get object list list_colored_grids = [i for i in occupied_grids if i not in obstacles] #Grids with objects (not black obstacles) #Compare each image in the list of objects with every other image in the same list #Most similar images return a ssim score of > 0.9 #Find the min path from the startimage to this similar image u=by calling astar function for startimage in list_colored_grids: key_startimage = startimage #start image img1 = list_images[startimage[0]-1][startimage[1]-1] for grid in [i for i in list_colored_grids if i != startimage]: #next image img = list_images[grid[0]-1][grid[1]-1] #convert to grayscale image = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) image2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #compare structural similarity s = ssim(image, image2) #if they are similar if s > 0.75: #perform a star search between both result = astarsearch.astar(maze,(startimage[0]-1,startimage[1]-1),(grid[0]-1,grid[1]-1)) # print result list2=[] for t in result: x,y = t[0],t[1] list2.append(tuple((x+1,y+1))) #Contains min path + startimage + endimage result = list(list2[1:-1]) #Result contains the minimum path required if not result: #If no path is found; planned_path[startimage] = list(["NO PATH",[], 0]) planned_path[startimage] = list([str(grid),result,len(result)+1]) #for obj in list_colored_grids: # if not(planned_path.has_key(obj)): #If no matched object is found; # planned_path[obj] = list(["NO MATCH",[],0]) #saveas text #f = open('output/path.txt', 'w') #for t in result: # line = ' '.join(str(x) for x in t) # f.write(line + '\n') #f.close() print list return occupied_grids, planned_path
output_unit = [] for i in range(num_top): feature_map = feature_maps[i][unitID] if max_value == 0: max_value = np.max(feature_map) feature_map = feature_map / max_value mask = cv2.resize(feature_map, segment_size) mask[mask < threshold_scale] = 0.0 # binarize the mask mask[mask > threshold_scale] = 1.0 img = cv2.imread(paths[i]) img = cv2.resize(img, segment_size) img = cv2.normalize(img.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX) img_mask = np.multiply(img, mask[:, :, np.newaxis]) img_mask = np.uint8(img_mask * 255) output_unit.append(img_mask) output_unit.append( np.uint8(np.ones((segment_size[0], margin, 3)) * 255)) montage_unit = np.concatenate(output_unit, axis=1) cv2.imwrite( os.path.join(output_folder, 'image', '%s-unit%03d.jpg' % (layer, unitID)), montage_unit) if flag_crop == 1: # load the library to crop image import tightcrop montage_unit_crop = tightcrop.crop_tiled_image( montage_unit, margin) cv2.imwrite( os.path.join(output_folder, 'image', '%s-unit%03d_crop.jpg' % (layer, unitID)),