def display(params,query_id,ranking,relnotrel): ''' Display the first elements of the ranking ''' # Read query image query_im = cv2.imread(os.path.join(params['root'],params['database'],params['split'], 'images',query_id.split('.')[0] + '.jpg')) # Handling the duality in file terminations. I know it's not pretty, but it works... if query_im is None: query_im = cv2.imread(os.path.join(params['root'],params['database'],params['split'], 'images',query_id.split('.')[0] + '.JPG')) # Blue contour for the query query_im = cv2.cvtColor(query_im,cv2.COLOR_BGR2RGB) query_im = cv2.copyMakeBorder(query_im,100,100,100,100,cv2.BORDER_CONSTANT,value=[0,0,255]) # Init figure fig = plt.figure(figsize=(20,10)) ax = fig.add_subplot(4, 4, 1) # Display ax.imshow(query_im) ax.axes.get_xaxis().set_visible(False) ax.axes.get_yaxis().set_visible(False) # We will show the first 15 elements of the ranking for i in range(15): # Read image im = cv2.imread(os.path.join(params['root'],params['database'],'train','images',ranking[0].tolist()[i] + '.jpg')) # Handling the duality in file terminations. I know it's not pretty, but it works... if im is None: im = cv2.imread(os.path.join(params['root'],params['database'],'train', 'images',ranking[0].tolist()[i] + '.JPG')) # Switch to RGB to display with matplotlib im = cv2.cvtColor(im,cv2.COLOR_BGR2RGB) # Paint the boundaries with the ground truth # If it was correctly selected if relnotrel[i] == 1: # Put green contour im = cv2.copyMakeBorder(im,100,100,100,100,cv2.BORDER_CONSTANT,value= [0,255,0]) # If it was not else: # Put red contour im = cv2.copyMakeBorder(im,100,100,100,100,cv2.BORDER_CONSTANT,value= [255,0,0]) # Show in figure ax = fig.add_subplot(4, 4, i+2) ax.imshow(im) ax.axes.get_xaxis().set_visible(False) ax.axes.get_yaxis().set_visible(False) print "Displaying..." plt.show()
def fix_rotation(qr_rect, image, aux_image): """Fixes the rotation of the image using the qrcode rectangle. -> cv2.image""" actual_down = np.array( [ float(qr_rect[1][0]-qr_rect[0][0]) , float(qr_rect[1][1]-qr_rect[0][1]) ] ) actual_down = actual_down/np.linalg.norm(actual_down) real_down = np.array([0,1]) angle = np.arccos(np.dot(actual_down, real_down)) if np.isnan(angle): if (actual_down == real_down).all(): angle = 0.0 else: angle = np.pi if actual_down[0]>0: angle = 2*np.pi-angle #calculate the size of the borders to make it squared w, h = image.shape[::-1] if w>h: top, bott, left, right = (w-h)/2, (w-h)/2, 0, 0 else: top, bott, left, right = (h-w)/2, (h-w)/2, 0, 0 #add the borders bigger_img = cv2.copyMakeBorder(image,top,bott,left,right,cv2.BORDER_CONSTANT,value=[0,0,0]) bigger_aux = cv2.copyMakeBorder(aux_image,top,bott,left,right,cv2.BORDER_CONSTANT,value=[0,0,0]) #calculate the tramsformation w, h = bigger_img.shape[::-1] M = cv2.getRotationMatrix2D((w/2,h/2),180*angle/np.pi,1.0) #TODO o not use the variable margin here, try to find the real w, h that accounts for the new transformation #margin = doc_parameters["margin"] return ( cv2.warpAffine(bigger_img,M,(w,h)), cv2.warpAffine(bigger_aux,M,(w,h)) )
def calculate_viewport_info(coordinates): """takes a coordinate array and returns a list of the edges of all viewports""" # the coordinates array contains four channels: u, v, x, y u = coordinates[:,:,0] labeled_array, num_labels = scipy.ndimage.measurements.label(u >= 0.0) # shapes arr_shape = u.shape padded_arr_shape = np.add(u.shape, [2, 2]) # 1px border # generate edges in both coordinates OUT = [] for idx in range(1, num_labels + 1): # detect contour of viewport in display coordinates. # we assume that the viewport has no holes. viewport_mask = np.array(labeled_array == idx, dtype=np.uint8) padded_viewport_mask = np.zeros(padded_arr_shape, np.uint8) cv2.copyMakeBorder(viewport_mask, 1, 1, 1, 1, cv2.BORDER_CONSTANT, padded_viewport_mask, 0) cnts, _ = cv2.findContours(padded_viewport_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE, offset=(-1, -1)) assert len(cnts) == 1, "There should only be one contour per detected viewport." # draw the detected contour in a mask to select the viewport edge viewport_contour_mask = np.zeros(arr_shape, dtype=np.uint8) cv2.drawContours(viewport_contour_mask, cnts, -1, 1, 1) edge = coordinates[viewport_contour_mask > 0, :] # sort the edge by walking along the contour: idxs_y, idxs_x = np.where(viewport_contour_mask > 0) idx_order = [0] idx_max = len(idxs_x) directions = np.array([(-1, 0), (-1, 1), ( 0, 1), ( 1, 1), ( 1, 0), ( 1,-1), ( 0,-1), (-1,-1)], dtype=np.int) while len(idx_order) < idx_max: cur_i = idx_order[-1] cur_y, cur_x = idxs_y[cur_i], idxs_x[cur_i] for dir_, (dy, dx) in enumerate(directions): new_i = np.where((idxs_x == (cur_x + dx)) & (idxs_y == (cur_y + dy)))[0] if len(new_i) > 0 and (new_i[0] not in idx_order): idx_order.append(new_i[0]) directions = np.roll(directions, dir_, axis=0) break else: # if we are stuck, but are missing only a few (10) pixels, # we ignore the missing ones... if len(idx_order) > idx_max - 10: # FIXME: improve this... break edge = edge[idx_order,:] # sorted. uv_edge = edge[:, 0:2] uv_coords = coordinates[viewport_mask > 0, 0:2] xy_edge = edge[:, 2:4] xy_coords = coordinates[viewport_mask > 0, 2:4] # append viewport coordinates and detected edges for uv and xy OUT.append((uv_edge, uv_coords, xy_edge, xy_coords)) return OUT
def compute(self,source): #p1 = X[0:-2,1:-1] #n #q1 = X[1:-1,2: ] #e #pm1 = X[2: ,1:-1] #s #qm1 = X[1:-1,0:-2] #w pm1_0 = cv2.copyMakeBorder(self._data[2:,1:-1,0],1,1,1,1,cv2.BORDER_WRAP) qm1_1 = cv2.copyMakeBorder(self._data[1:-1,0:-2,1],1,1,1,1,cv2.BORDER_WRAP) p1_2 = cv2.copyMakeBorder(self._data[0:-2,1:-1,2],1,1,1,1,cv2.BORDER_WRAP) q1_3 = cv2.copyMakeBorder(self._data[1:-1,2:,3],1,1,1,1,cv2.BORDER_WRAP) #colision rule psiX = ((pm1_0 * p1_2 * (1-qm1_1) * (1-q1_3)) - ((1-pm1_0) * (1-p1_2) * qm1_1 * q1_3)) #psiX = 0 #update of the map x = self._data x[...] = 0 x[:,:,0] = pm1_0 - psix x[:,:,1] = qm1_1 + psix x[:,:,2] = p1_2 - psix x[:,:,3] = q1_3 + psix self._data[np.nonzero(source)] |= 1
def do_center_pad_to_factor_edgeYreflectX(image, factor=32): H,W = image.shape[:2] dy0, dy1, dx0, dx1 = compute_center_pad(H,W, factor) image = cv2.copyMakeBorder(image, 0, 0, dx0, dx1, cv2.BORDER_REFLECT101) image = cv2.copyMakeBorder(image, dy0, dy1, 0, 0, cv2.BORDER_REPLICATE) return image
def predict(dataset_name, input_path, output_path): dataset = Dataset(dataset_name) label_margin = 186 # Create theano graph input_var = T.tensor4('input') net = build_model(input_var) outputs = lasagne.layers.get_output(net['prob'], deterministic=True) fn = theano.function([input_var], outputs) # Set the parameters from lasagne f = open(dataset.pretrained_path, 'rb') params = pickle.load(f) [p.set_value(pval) for (p, pval) in zip(lasagne.layers.get_all_params(net['prob']), params)] # Image processing input_dims = dataset.shape batch_size, num_channels, input_height, input_width = input_dims image = cv2.imread(input_path, 1).astype(np.float32) - dataset.mean_pixel image_size = image.shape output_height = input_height - 2 * label_margin output_width = input_width - 2 * label_margin image = cv2.copyMakeBorder(image, label_margin, label_margin, label_margin, label_margin, cv2.BORDER_REFLECT_101) num_tiles_h = image_size[0] // output_height + \ (1 if image_size[0] % output_height else 0) num_tiles_w = image_size[1] // output_width + \ (1 if image_size[1] % output_width else 0) prediction = [] for h in range(num_tiles_h): col_prediction = [] for w in range(num_tiles_w): offset = [output_height * h, output_width * w] tile = image[offset[0]:offset[0] + input_height, offset[1]:offset[1] + input_width, :] margin = [0, input_height - tile.shape[0], 0, input_width - tile.shape[1]] tile = cv2.copyMakeBorder(tile, margin[0], margin[1], margin[2], margin[3], cv2.BORDER_REFLECT_101) lasagne_in = tile.transpose([2, 0, 1]) # Get theano graph prediction prob = fn(np.asarray([lasagne_in])) col_prediction.append(prob) col_prediction = np.concatenate(col_prediction, axis=1) prediction.append(col_prediction) prob = np.concatenate(prediction, axis=1).transpose().reshape((21, 66, 66)) if dataset.zoom > 1: prob = interp_map(prob, dataset.zoom, image_size[1], image_size[0]) prediction = np.argmax(prob.transpose([1, 2, 0]), axis=2) # Save the segmentation prediction color_image = dataset.palette[prediction.ravel()].reshape(image_size) color_image = cv2.cvtColor(color_image, cv2.COLOR_RGB2BGR) print('Writing', output_path) cv2.imwrite(output_path, color_image)
def main(name): bound = 5 data = pd.read_csv(PWD+name, names=HEADERS, index_col=[0,1]) data = data[(data.x_pos < 105 - bound) & (data.x_pos > 0 + bound) & (data.y_pos>0 + bound) & (data.y_pos<68-bound)] #import ipdb; ipdb.set_trace() #out = cv2.VideoWriter('%s.avi'%name,fourcc, 20.0, (700,462)) pIndex = data.index.get_level_values(0) bg = court() pic = bg.copy() flag = True model = None for time in pIndex[::10]: cv2.copyMakeBorder(bg,0,0,0,0,cv2.BORDER_REPLICATE, dst=pic) d = data.loc[time] print time cv2.putText(pic, "%s"%time, (15,15), font, 1, (255,255,255), 1) #mapInfo(d, pic) model = MapMetric(d, pic, model) #out.write(pic) cv2.imshow('frame', pic) if cv2.waitKey(25) & 0xFF == ord('q'): break out.release() cv2.destroyAllWindows()
def normalize_img(img_path): img = cv2.imread(img_path, 0) h,w = img.shape[:2] max_dim = max(h,w) ratio = 128.0/max_dim top = 0 bottom = 0 left = 0 right = 0 if w == max_dim: w = 128 h = int(h*ratio) padding = int((128 - h)/2.0) top = padding bottom = padding else: h = 128 w = int(w*ratio) padding = int((128 - w)/2.0) left = padding right = padding img1 = cv2.resize(img, (w, h)) h_borders = np.concatenate((img1[0,:], img1[h-1,:])) v_borders = np.concatenate((img1[:,w-1], img1[:,0])) color = mode(np.concatenate((h_borders, v_borders)))[0][0] output = cv2.copyMakeBorder(img1, top, bottom, right, left, cv2.BORDER_CONSTANT, value=[int(color)]) return cv2.copyMakeBorder(output, 3, 3, 3, 3, cv2.BORDER_CONSTANT, value=[int(color)])
def augScale(I, points=None, scale_rand=None, obj_scale=1.0, target_scale=1.0, pad_value=None, border_color=(0, 0, 0)): if scale_rand is not None: scale_multiplier = scale_rand else: scale_multiplier = randScale(0.8, 1.2) if isinstance(scale_multiplier, tuple) or isinstance(scale_multiplier, list): scale = target_scale / obj_scale * np.asarray(scale_multiplier) I_new = cv2.resize(I, (0, 0), fx=scale[0], fy=scale[1]) else: scale = target_scale / obj_scale * scale_multiplier I_new = cv2.resize(I, (0, 0), fx=scale, fy=scale) target_size = I.shape border_size = (target_size[0] - I_new.shape[0], target_size[1] - I_new.shape[1]) if border_size[0] > 0: I_new = cv2.copyMakeBorder(I_new, int(math.floor(border_size[0] / 2.0)), \ int(math.ceil(border_size[0] / 2.0)), 0, 0, \ cv2.BORDER_CONSTANT, value=border_color) elif border_size[0] < 0: I_new = I_new[ -int(math.floor(border_size[0] / 2.0)):I_new.shape[0] + int(math.ceil(border_size[0] / 2.0))] if border_size[1] > 0: I_new = cv2.copyMakeBorder(I_new, 0, 0, int(math.floor(border_size[1] / 2.0)), \ int(math.ceil(border_size[1] / 2.0)), \ cv2.BORDER_CONSTANT, value=border_color) elif border_size[1] < 0: I_new = I_new[:, -int(math.floor(border_size[1] / 2.0)): I_new.shape[1] + int(math.ceil(border_size[1] / 2.0))] if points is not None: points = points * scale return I_new, points
def disparity_ssd(L, R, ksize=3): """Compute disparity map D(y, x) such that: L(y, x) = R(y, x + D(y, x)) Params: L: Grayscale left image, in range [0.0, 1.0] R: Grayscale right image, same size as L ksize: the x and y shape of the kernel Returns: Disparity map, same size as L, R """ L_h, L_w = L_size = L.shape R_h, R_w = R_size = R.shape if L_size != R_size: return -1 y = 0 border = (ksize-1)/2 padded_l = cv2.copyMakeBorder(L, border,border,border,border,cv2.BORDER_CONSTANT,value=0) padded_r = cv2.copyMakeBorder(R, border,border,border,border,cv2.BORDER_CONSTANT,value=0) disparity = np.zeros((L_h, L_w)) for y in range(border, L_h): L_strip = padded_l[y-border:y+border+1,:] R_strip = padded_r[y-border:y+border+1,:] if np.array_equal(L_strip, R_strip) == False: matches = match_strips(L_strip, R_strip, ksize) disparity[y,:] = matches[:,border:-border] return disparity
def getColorDiscrepancy(self, currentFrame, backgroundClusterCenters, s): bgcBYXa = cv2.copyMakeBorder(backgroundClusterCenters[:,:,0], 1, 1, 1, 1, cv2.BORDER_CONSTANT, value = [0, 0, 0]) bgcBYXb = cv2.copyMakeBorder(backgroundClusterCenters[:,:,1], 1, 1, 1, 1, cv2.BORDER_CONSTANT, value = [0, 0, 0]) diff00a = currentFrame - cv2.resize(bgcBYXa[1:10, 1:17, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diff00b = currentFrame - cv2.resize(bgcBYXb[1:10, 1:17, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diff01a = currentFrame - cv2.resize(bgcBYXa[1:10, 2:18, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diff01b = currentFrame - cv2.resize(bgcBYXb[1:10, 2:18, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diff10a = currentFrame - cv2.resize(bgcBYXa[2:11, 1:17, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diff10b = currentFrame - cv2.resize(bgcBYXb[2:11, 1:17, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diff11a = currentFrame - cv2.resize(bgcBYXa[2:11, 2:18, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diff11b = currentFrame - cv2.resize(bgcBYXb[2:11, 2:18, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diff0m1a = currentFrame - cv2.resize(bgcBYXa[1:10, 0:16, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diff0m1b = currentFrame - cv2.resize(bgcBYXb[1:10, 0:16, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diffm10a = currentFrame - cv2.resize(bgcBYXa[0:9, 1:17, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diffm10b = currentFrame - cv2.resize(bgcBYXb[0:9, 1:17, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diff1m1a = currentFrame - cv2.resize(bgcBYXa[2:11, 0:16, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diff1m1b = currentFrame - cv2.resize(bgcBYXb[2:11, 0:16, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diffm11a = currentFrame - cv2.resize(bgcBYXa[0:9, 2:18, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diffm11b = currentFrame - cv2.resize(bgcBYXb[0:9, 2:18, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diffm1m1a = currentFrame - cv2.resize(bgcBYXa[0:9, 0:16, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) diffm1m1b = currentFrame - cv2.resize(bgcBYXb[0:9, 0:16, :], None, fx = s, fy = s, interpolation = cv2.INTER_NEAREST) stackedFlattenedDifferences = numpy.vstack([diff00a.ravel(), diff00b.ravel(), diff01a.ravel(), diff01b.ravel(), diff10a.ravel(), diff10b.ravel(), diff11a.ravel(), diff11b.ravel(), diff0m1a.ravel(), diff0m1b.ravel(), diffm10a.ravel(), diffm10b.ravel(), diff1m1a.ravel(), diff1m1b.ravel(), diffm11a.ravel(), diffm11b.ravel(), diffm1m1a.ravel(), diffm1m1b.ravel()]) differenceImageFlattened = numpy.min(numpy.absolute(stackedFlattenedDifferences), axis = 0) del stackedFlattenedDifferences differenceImage = numpy.uint8(differenceImageFlattened.reshape(currentFrame.shape)) del differenceImageFlattened return differenceImage
def read_images(fpath): lines = utils.read_image_list(fpath) is_train = os.path.basename(fpath) == 'train.list' logger.info('loading data: {}'.format(fpath)) X_data, y_data = [], [] for inst_path, truth_path in lines: inst, truth = [cv2.imread(p, cv2.IMREAD_GRAYSCALE) for p in (inst_path, truth_path)] assert inst is not None and truth is not None, (inst_path, truth_path) pad_h, pad_w = [x / 2 for x in MODEL_INPUT_SHAPE] # pad input image inst = cv2.copyMakeBorder(inst, pad_h, pad_h, pad_w, pad_w, cv2.BORDER_REFLECT) truth_padded = cv2.copyMakeBorder(truth, pad_h, pad_h, pad_w, pad_w, cv2.BORDER_REFLECT) X_data.append(inst) y_data.append(truth) if is_train: insts = generate_data(truth_padded) for i in insts: # cv2.imshow('', i) # cv2.waitKey(0) X_data.append(i) y_data.append(truth) return X_data, y_data
def transform_size(d,size=(350,350)): file = cStringIO.StringIO(pageGetter(d,random.randint(0,0))) img = Image.open(file) img.thumbnail(size, Image.ANTIALIAS) if img.size[1] == img.size[0]: img = np.array(img) #img = cv2.copyMakeBorder(img,5,5,5,5,cv2.BORDER_CONSTANT,value=[255,255,255]) #either (500,>) #either (<,500) elif img.size[1]<size[0]: #x shape[0] img = np.array(img) val = abs(size[0] - img.shape[0]) new_val = val/2 if new_val*2 < val: add = abs(val-new_val) img = cv2.copyMakeBorder(img,new_val,add,0,0,cv2.BORDER_CONSTANT,value=[255,255,255]) else: img = cv2.copyMakeBorder(img,new_val,new_val,0,0,cv2.BORDER_CONSTANT,value=[255,255,255]) elif img.size[0]<size[0]: #x shape[0] img = np.array(img) val = abs(size[0] - img.shape[1]) new_val = val/2 if new_val*2 < val: add = abs(val-new_val) img = cv2.copyMakeBorder(img,0,0,new_val,add,cv2.BORDER_CONSTANT,value=[255,255,255]) else: img = cv2.copyMakeBorder(img,0,0,new_val,new_val,cv2.BORDER_CONSTANT,value=[255,255,255]) return img
def pad_or_crop_to_shape( I, out_shape, border_color=(255, 255, 255)): if not isinstance(border_color, tuple): n_chans = I.shape[-1] border_color = tuple([border_color] * n_chans) # an out_shape with a dimension value of None means just don't crop or pad in that dim border_size = [out_shape[d] - I.shape[d] if out_shape[d] is not None else 0 for d in range(2)] #border_size = (out_shape[0] - I.shape[0], out_shape[1] - I.shape[1]) if border_size[0] > 0: I = cv2.copyMakeBorder(I, int(math.floor(border_size[0] / 2.0)), int(math.ceil(border_size[0] / 2.0)), 0, 0, cv2.BORDER_CONSTANT, value=border_color) elif border_size[0] < 0: I = I[-int(math.floor(border_size[0] / 2.0)): I.shape[0] + int(math.ceil(border_size[0] / 2.0)), :, :] if border_size[1] > 0: I = cv2.copyMakeBorder(I, 0, 0, int(math.floor(border_size[1] / 2.0)), int(math.ceil(border_size[1] / 2.0)), cv2.BORDER_CONSTANT, value=border_color) elif border_size[1] < 0: I = I[:, -int(math.floor(border_size[1] / 2.0)): I.shape[1] + int(math.ceil(border_size[1] / 2.0)), :] return I
def showBWImage(current_frame,i): #cnt = (389,263),(40,34),0 #box = cv2.cv.BoxPoints(cnt) #box = np.int0(box) if i==1: cap = cap1 label = labelArr1[current_frame] else: cap = cap2 label = labelArr2[current_frame] #current_frame = cv2.getTrackbarPos("Silder1", "Video") cap.set(1,current_frame) ret,image = cap.read() if label[1]!="None": if label[1] == "Right" or label[1] == "Left" or label[1] == "Intersect": cnt = (float(label[2]),float(label[3])),(float(label[4]),float(label[5])),float(label[6]) box = cv2.cv.BoxPoints(cnt) box = np.int0(box) mask = np.zeros((480,640,3), np.uint8) cv2.fillPoly(mask, np.int32([box]), (255,255,255)) cv2.threshold(image,int(label[7]),255,cv2.THRESH_BINARY,image) masked_image = cv2.bitwise_and(image, mask) cv2.drawContours(masked_image ,[box],0,(0,0,255),2) masked_image = cv2.resize(masked_image,(320,240)) masked_image = cv2.copyMakeBorder(masked_image,2,2,2,2,cv2.BORDER_CONSTANT,value=(0,0,255)) #merged_frame[:244, 344:668]= masked_image else: masked_image = np.zeros((240,320,3), np.uint8) masked_image = cv2.copyMakeBorder(masked_image,2,2,2,2,cv2.BORDER_CONSTANT,value=(0,0,255)) if i==1: merged_frame[:244, 344:668]= masked_image else: merged_frame[254:498, 344:668]= masked_image
def __call__(self, data): n = self.n h, w = data['input'].shape[:2] dy0, dy1, dx0, dx1 = compute_padding(h, w, n) data['input'] = cv2.copyMakeBorder(data['input'], dy0, dy1, dx0, dx1, cv2.BORDER_REFLECT_101) data['mask'] = cv2.copyMakeBorder(data['mask'], dy0, dy1, dx0, dx1, cv2.BORDER_REFLECT_101) return data
def load_file(self, fname=None): if fname is not None: self.file_name = fname if self.verbose: print "Loading file ", self.file_name self.image_gray = cv2.copyMakeBorder(cv2.imread(self.file_name, 0), 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=self.WHITE) self.image_color = cv2.copyMakeBorder(cv2.imread(self.file_name), 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=self.WHITE) self.height, self.width = self.image_gray.shape
def do_random_pad_to_factor2(image, mask, limit=(-4,4), factor=32): H,W = image.shape[:2] dy0, dy1, dx0, dx1 = compute_random_pad(H,W, limit, factor) image = cv2.copyMakeBorder(image, dy0, dy1, dx0, dx1, cv2.BORDER_REPLICATE) mask = cv2.copyMakeBorder(mask, dy0, dy1, dx0, dx1, cv2.BORDER_REPLICATE) return image, mask
def get_patch(im, box): # box = box0.copy() # shouldn't change box0! # if spacing is not None and config.NORM_SPACING > 0: # spacing adjust, will overwrite simple scaling # im_scale = float(spacing) / config.NORM_SPACING # box *= im_scale mg = config.BOX_PAD if config.ROI_METHOD == 'FIXED_MARGIN' or config.ROI_METHOD == 'VAR_SIZE_FIXED_MARGIN': # method 1: crop real lesion size + margin. will pad zero for diff size patches box1 = np.round(box).astype(int) box1[0] = np.maximum(0, box1[0] - mg) box1[1] = np.maximum(0, box1[1] - mg) box1[2] = np.minimum(im.shape[1] - 1, box1[2] + mg) box1[3] = np.minimum(im.shape[0] - 1, box1[3] + mg) patch = im[box1[1]:box1[3] + 1, box1[0]:box1[2] + 1] offset_x = np.maximum(box[0] - mg, 0) offset_y = np.maximum(box[1] - mg, 0) box_new = box - np.array([offset_x, offset_y] * 2) max_shape = np.max(patch.shape) patch_scale = 1. if max_shape > config.MAX_PATCH_SIZE: patch_scale = float(config.MAX_PATCH_SIZE) / max_shape patch = cv2.resize(patch, None, None, fx=patch_scale, fy=patch_scale, interpolation=cv2.INTER_LINEAR) box_new *= patch_scale elif config.ROI_METHOD == 'FIXED_CONTEXT': # method 2: crop fixed size context, so no need to pad zeros center = np.round((box[:2] + box[2:]) / 2) box1 = np.zeros((4,), dtype=int) box1[0] = np.maximum(0, center[0] - mg) box1[1] = np.maximum(0, center[1] - mg) box1[2] = np.minimum(im.shape[1] - 1, center[0] + mg - 1) box1[3] = np.minimum(im.shape[0] - 1, center[1] + mg - 1) patch = im[box1[1]:box1[3] + 1, box1[0]:box1[2] + 1] # handle diff size if config.PAD_BORDER: xdiff = mg * 2 - patch.shape[1] ydiff = mg * 2 - patch.shape[0] if xdiff > 0: if center[0] - mg < 0: patch = cv2.copyMakeBorder(patch, 0, 0, xdiff, 0, cv2.BORDER_REPLICATE) else: patch = cv2.copyMakeBorder(patch, 0, 0, 0, xdiff, cv2.BORDER_REPLICATE) if ydiff > 0: if center[1] - mg < 0: patch = cv2.copyMakeBorder(patch, ydiff, 0, 0, 0, cv2.BORDER_REPLICATE) else: patch = cv2.copyMakeBorder(patch, 0, ydiff, 0, 0, cv2.BORDER_REPLICATE) box_new = np.maximum(0, box-np.hstack((center, center))+mg) patch_scale = 1. return patch.copy(), box_new, patch_scale
def compute(self,activation): self.lock.acquire() self._data[:,:,0] |= activation self._data[:,:,1] |= activation self._data[:,:,2] |= activation self._data[:,:,3] |= activation #print("Nb part : %s"%np.sum(self._data)) X = self._data.astype(np.uint8) Y = np.copy(self.obs) #v 0 -> (1,0) -> n #v 1 -> (0,1) -> e #v 2 -> (-1,0) ->s #v 3 -> (0,-1) ->w x0 = cv2.copyMakeBorder(X[:,:,0],1,1,1,1,cv2.BORDER_WRAP).astype(np.bool_) x1 = cv2.copyMakeBorder(X[:,:,1],1,1,1,1,cv2.BORDER_WRAP).astype(np.bool_) x2 = cv2.copyMakeBorder(X[:,:,2],1,1,1,1,cv2.BORDER_WRAP).astype(np.bool_) x3 = cv2.copyMakeBorder(X[:,:,3],1,1,1,1,cv2.BORDER_WRAP).astype(np.bool_) pm1_0 = x0[2:,1:-1] qm1_1 = x1[1:-1,0:-2] p1_2 = x2[0:-2,1:-1] q1_3 = x3[1:-1,2:] #colision rule 1 if collision psiX = (((pm1_0 & p1_2) & ~(qm1_1 | q1_3)) | (~(pm1_0 | p1_2) & (qm1_1 & q1_3)) ) psiX = ( psiX & (~Y)) X = X.astype(np.bool_,copy = False) ##update of the map if collision: 1 ^ 1 -> 0 ##If obstacle collision = 0 life goes on ##if obstacle: we invert the direction of part # part here: 1 ^ 1 -> 0 # oposite direction 0 ^ 1 -> 1 # other direction 0 ^ 0 -> 0 obsVert = (Y & (pm1_0|p1_2)) obsHori = (Y & (qm1_1|q1_3)) X[...] = 0 X[:,:,0] = (pm1_0 ^ psiX) ^ obsVert X[:,:,1] = (qm1_1 ^ psiX) ^ obsHori X[:,:,2] = (p1_2 ^ psiX) ^ obsVert X[:,:,3] = (q1_3 ^ psiX) ^ obsHori #print np.sum(x) self._data = X self.lock.release()
def disparity_ssd(L, R, window_size = 21): """Compute disparity map D(y, x) such that: L(y, x) = R(y, x + D(y, x)) Params: L: Grayscale left image, in range [0.0, 1.0] R: Grayscale right image, same size as L Returns: Disparity map, same size as L, R """ D = np.zeros(L.shape, dtype=np.float) # subtract 1 due to the starting pixel offset = (window_size) / 2 L = cv2.copyMakeBorder(L, offset, offset, offset, offset, cv2.BORDER_CONSTANT,value=0) R = cv2.copyMakeBorder(R, offset, offset, offset, offset, cv2.BORDER_CONSTANT,value=0) shape = L.shape height = shape[0] width = shape[1] r_shape = (R.shape[0]-(window_size-1), R.shape[1]-(window_size-1), window_size, window_size) r_strides = (R.shape[1] * R.itemsize, R.itemsize, R.itemsize * R.shape[1], R.itemsize) r_strips = as_strided(R, r_shape, r_strides) for y in range(offset, height - offset): r_strip = r_strips[y-offset] for x in range(offset, width-offset): l_patch = get_patch(L, offset, offset, offset, offset, y, x) copy_patch = np.copy(l_patch) l_strip = as_strided(copy_patch, r_strip.shape, (0, copy_patch.itemsize*window_size, copy_patch.itemsize)) ssd = ((l_strip - r_strip)**2).sum((1, 2)) x_prime = np.argmin(ssd) D[y-offset][x-offset] = x_prime - x #print D.max() return D # def test_disparity_ssd2(l_image, r_image, problem, window_size = 21): # L = cv2.imread(os.path.join('input', l_image), 0) * (1 / 255.0) # grayscale, scale to [0.0, 1.0] # R = cv2.imread(os.path.join('input', r_image), 0) * (1 / 255.0) # # # Compute disparity (using method disparity_ssd defined in disparity_ssd.py) # start = time.time() # D = disparity_ssd(L, R, window_size) # TODO# : implemenet disparity_ssd() # print "first: " + str(time.time() - start) # start = time.time() # D2 = disparity_ssd_2(L, R, window_size) # print "second: " + str(time.time() - start) #print D == D2 cv2.imwrite(os.path.join("output", "ps3-" + problem + ".png"), np.clip(D2, 0, 255).astype(np.uint8))
def main(leftFilename, rightFilename, ps, maxSearch): limg = cv2.copyMakeBorder( cv2.imread(leftFilename, cv2.IMREAD_COLOR), ps, ps, ps, ps, cv2.BORDER_REPLICATE ) rimg = cv2.copyMakeBorder( cv2.imread(rightFilename, cv2.IMREAD_COLOR), ps, ps, ps, ps, cv2.BORDER_REPLICATE ) print "Loaded left " + repr(leftFilename) + " and right " + repr(rightFilename) disparity = cv2.equalizeHist( scanImages(limg, rimg, ps, maxSearch)[ ps : limg.shape[0] - ps, ps : limg.shape[1] - ps ] ) cv2.imwrite("disparity.png", disparity) plt.imshow(disparity, cmap = 'gray') plt.show()
def threshold(self, img): cv2.cvtColor(img, self.colorspace, dst=self.hsv) cv2.inRange(self.hsv, self.lower, self.upper, dst=self.bin) if False: cv2.split(self.hsv, [self.hue, self.sat, self.val]) # Threshold each component separately # Hue cv2.threshold(self.hue, self.thresh_hue_p, 255, type=cv2.THRESH_BINARY, dst=self.bin) cv2.threshold(self.hue, self.thresh_hue_n, 255, type=cv2.THRESH_BINARY_INV, dst=self.hue) cv2.bitwise_and(self.hue, self.bin, self.hue) if self.draw_hue: # # overlay green where the hue threshold is non-zero self.out[np.dstack((self.zeros, self.hue != 0, self.zeros))] = 255 # Saturation cv2.threshold(self.sat, self.thresh_sat_p, 255, type=cv2.THRESH_BINARY, dst=self.bin) cv2.threshold(self.sat, self.thresh_sat_n, 255, type=cv2.THRESH_BINARY_INV, dst=self.sat) cv2.bitwise_and(self.sat, self.bin, self.sat) if self.draw_sat: # overlay blue where the sat threshold is non-zero self.out[np.dstack((self.sat != 0, self.zeros, self.zeros))] = 255 # Value cv2.threshold(self.val, self.thresh_val_p, 255, type=cv2.THRESH_BINARY, dst=self.bin) cv2.threshold(self.val, self.thresh_val_n, 255, type=cv2.THRESH_BINARY_INV, dst=self.val) cv2.bitwise_and(self.val, self.bin, self.val) if self.draw_val: # overlay red where the val threshold is non-zero self.out[np.dstack((self.zeros, self.zeros, self.val != 0))] = 255 # Combine the results to obtain our binary image which should for the most # part only contain pixels that we care about cv2.bitwise_and(self.hue, self.sat, self.bin) cv2.bitwise_and(self.bin, self.val, self.bin) # Fill in any gaps using binary morphology cv2.morphologyEx(self.bin, cv2.MORPH_CLOSE, self.morphKernel, dst=self.bin, iterations=self.kHoleClosingIterations) if self.draw_thresh: b = (self.bin != 0) cv2.copyMakeBorder(self.black, 0, 0, 0, 0, cv2.BORDER_CONSTANT, value=self.RED, dst=self.out) self.out[np.dstack((b, b, b))] = 255 # return self.bin
def nc_yokoi(src, connectivity=4): if connectivity == 4: img = cv2.copyMakeBorder(src, 1, 1, 1, 1, cv2.BORDER_CONSTANT, 1).astype(int) elif connectivity == 8: img = cv2.copyMakeBorder(1 - src, 1, 1, 1, 1, cv2.BORDER_CONSTANT, 1).astype(int) else: print 'Error. Connectivity = ', connectivity return ret = np.zeros(src.shape) for i in range(len(src)): for j in range(len(src[i])): ret[i][j] = yokoi(img[i:i+3,j:j+3]) return ret
def do_random_pad_to_factor2_edgeYreflectX(image, mask, limit=(-4,4), factor=32): H,W = image.shape[:2] dy0, dy1, dx0, dx1 = compute_random_pad(H,W, limit, factor) # image = cv2.copyMakeBorder(image, dy0, dy1, dx0, dx1, cv2.BORDER_REPLICATE) image = cv2.copyMakeBorder(image, 0, 0, dx0, dx1, cv2.BORDER_REFLECT101) image = cv2.copyMakeBorder(image, dy0, dy1, 0, 0, cv2.BORDER_REPLICATE) # mask = cv2.copyMakeBorder(mask, dy0, dy1, dx0, dx1, cv2.BORDER_REPLICATE) mask = cv2.copyMakeBorder(mask, 0, 0, dx0, dx1, cv2.BORDER_REFLECT101) mask = cv2.copyMakeBorder(mask, dy0, dy1, 0, 0, cv2.BORDER_REPLICATE) return image, mask
def cropStrip(strip, ph, pw): if strip.shape[0]<770: bd = (770-strip.shape[0]+1)/2 strip = cv2.copyMakeBorder(strip,bd,bd,0,0,cv2.BORDER_CONSTANT,value=WHITE) if strip.shape[1]<308: bd = (308-strip.shape[1]+1)/2 strip = cv2.copyMakeBorder(strip,0,0,bd,bd,cv2.BORDER_CONSTANT,value=WHITE) extraV, extraH = strip.shape[0]%ph, strip.shape[1]%pw aV, aH = extraV/2, extraH/2 bV, bH = extraV-aV, extraH-aH return strip[(0+aV):(strip.shape[0]-bV), (0+aH):(strip.shape[0]-bH)]
def showSkeleton(current_frame1,current_frame2,x): frame1 = showMainImage1(current_frame1) frame2 = showMainImage2(current_frame2) skeleton1 = skeletonArr1[current_frame1] skeleton2 = skeletonArr2[current_frame2] point1 =[] point2 =[] if skeleton1[0]!="untracked": for i in range(0,10): #for i in range(0,14) index_x = 3 + i*7 index_y = 4 + i*7 point1.append((int(skeleton1[index_x]),int(skeleton1[index_y]))) cv2.circle(frame1,point1[i],3,(0,0,255),-1) #draw line between points if x==1: cv2.line(frame1,point1[8],point1[6],(0,0,255),1) cv2.line(frame1,point1[6],point1[4],(0,0,255),1) cv2.line(frame1,point1[4],point1[1],(0,0,255),1) cv2.line(frame1,point1[1],point1[2],(0,0,255),1) cv2.line(frame1,point1[0],point1[2],(0,0,255),1) cv2.line(frame1,point1[3],point1[2],(0,0,255),1) cv2.line(frame1,point1[3],point1[5],(0,0,255),1) cv2.line(frame1,point1[5],point1[7],(0,0,255),1) cv2.line(frame1,point1[7],point1[9],(0,0,255),1) if skeleton2[0]!="untracked": for i in range(0,10): #for i in range(0,14) index_x = 3 + i*7 index_y = 4 + i*7 point2.append((int(skeleton2[index_x]),int(skeleton2[index_y]))) cv2.circle(frame2,point2[i],3,(0,0,255),-1) #draw line between points if x==1: cv2.line(frame2,point2[8],point2[6],(0,0,255),1) cv2.line(frame2,point2[6],point2[4],(0,0,255),1) cv2.line(frame2,point2[4],point2[1],(0,0,255),1) cv2.line(frame2,point2[1],point2[2],(0,0,255),1) cv2.line(frame2,point2[0],point2[2],(0,0,255),1) cv2.line(frame2,point2[3],point2[2],(0,0,255),1) cv2.line(frame2,point2[3],point2[5],(0,0,255),1) cv2.line(frame2,point2[5],point2[7],(0,0,255),1) cv2.line(frame2,point2[7],point2[9],(0,0,255),1) rsFrame1 = cv2.resize(frame1,(320,240)) rsFrame1 = cv2.copyMakeBorder(rsFrame1,2,2,2,2,cv2.BORDER_CONSTANT,value=(0,0,255)) rsFrame2 = cv2.resize(frame2,(320,240)) rsFrame2 = cv2.copyMakeBorder(rsFrame2,2,2,2,2,cv2.BORDER_CONSTANT,value=(0,0,255)) merged_frame[:244, 10:334] = rsFrame1 merged_frame[254:498, 10:334] = rsFrame2 cv2.imshow('Video', merged_frame)
def make_img(rect): img = image[rect[1]:rect[1]+rect[3],rect[0]:rect[0]+rect[2],:] img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) height, width = img.shape[:2] border_color = int(round(img.mean())) if height >= width: border_left = int((height - width) / 2) border_right = height - width - border_left img = cv2.copyMakeBorder(img, 0, 0, border_left, border_right, cv2.BORDER_CONSTANT, value=border_color) else: border_top = int((width - height) / 2) border_bottom = width - height - border_top img = cv2.copyMakeBorder(img, border_top, border_bottom, 0, 0, cv2.BORDER_CONSTANT, value=border_color) return cv2.resize(img, (50, 50))
def batch_data_generator(train_idx, batch_size): inputs = [] outputs = [] while True: np.random.shuffle(train_idx) for i in train_idx: for j in range(1): img = skimage.io.imread(all_files[i], plugin='tifffile') img = cv2.resize(img, (520, 520)) pan = skimage.io.imread(all_pan_files[i], plugin='tifffile') pan = cv2.resize(pan, (520, 520)) pan = pan[..., np.newaxis] img = np.concatenate([img, pan], axis=2) msk = cv2.imread(all_masks[i], cv2.IMREAD_UNCHANGED)[..., 0] img = cv2.copyMakeBorder(img, 100, 100, 100, 100, cv2.BORDER_REFLECT_101) msk = cv2.copyMakeBorder(msk, 100, 100, 100, 100, cv2.BORDER_REFLECT_101) if random.random() > 0.5: scale = 0.9 + random.random() * 0.2 angle = random.randint(0, 41) - 24 img = rotate_image(img, angle, scale) msk = rotate_image(msk, angle, scale) x0 = random.randint(0, img.shape[1] - input_shape[1]) y0 = random.randint(0, img.shape[0] - input_shape[0]) img = img[y0:y0+input_shape[0], x0:x0+input_shape[1], :] msk = (msk > 127) * 1 msk = msk[..., np.newaxis] otp = msk[y0:y0+input_shape[0], x0:x0+input_shape[1], :] if random.random() > 0.5: img = img[:, ::-1, ...] otp = otp[:, ::-1, ...] rot = random.randrange(4) if rot > 0: img = np.rot90(img, k=rot) otp = np.rot90(otp, k=rot) inputs.append(img) outputs.append(otp) if len(inputs) == batch_size: inputs = np.asarray(inputs) outputs = np.asarray(outputs, dtype='float') inputs = preprocess_inputs_std(inputs, city_id) yield inputs, outputs inputs = [] outputs = []
def addBorder(self, iim): in_im = copy(iim) im_dim = in_im.shape height = im_dim[0] width = im_dim[1] if height > width: res = height - width add = int(round(res/2, 0)) im_border = cv2.copyMakeBorder(in_im, 0, 0, add, add, cv2.BORDER_CONSTANT, value=[0]) else: res = width - height add = int(round(res/2, 0)) im_border = cv2.copyMakeBorder(in_im, add, add, 0, 0, cv2.BORDER_CONSTANT, value=[0]) return im_border
def split_the_images(X_imgs, Y_imgs, in_hei, in_wid, mag): """ Function that splits images up in to smaller pieces and provides a boundary buffer region. If the sampled pixels are from edge of input image then these are mirrored. Keyword arguments: X_imgs -- Input images. Y_imgs -- Corresponding output images. in_hei -- Desired patch height not inc. boundary. in_wid -- Desired patch width not inc. boundary. mag -- The boundary size in pixels. Returns: train -- An array of images for training which have been normalised for mean and variance. gtdata -- The corresponding ground-truth density representation. images_per_image -- The number of calculated tiles from an input image. """ train = [] gtdata = [] for x_img, y_img in zip(X_imgs, Y_imgs): f_hei, f_wid = x_img.shape images_per_image = 0 rows = 0 for rst in range(0, f_hei, in_hei): rows += 1 cols = 0 for cst in range(0, f_wid, in_wid): cols += 1 top = bottom = left = right = 0 ren = rst + in_hei + 2 * mag cen = cst + in_wid + 2 * mag rst1 = rst cst1 = cst if rst == 0: ren -= mag top = 16 else: rst1 -= mag ren -= mag if cst == 0: cen -= mag left = 16 else: cst1 -= mag cen -= mag if cen > f_wid: right = cen - f_wid + 1 cen = -1 else: right = 0 if ren > f_hei: bottom = ren - f_hei + 1 ren = -1 else: bottom = 0 temp = np.copy(x_img[rst1:ren, cst1:cen]) if top > 0 or bottom > 0 or left > 0 or right > 0: temp = cv2.copyMakeBorder(temp, top, bottom, left, right, borderType=2) temp -= np.mean(temp) temp /= np.std(temp) train.append(temp.reshape(1, temp.shape[0], temp.shape[1])) images_per_image += 1 temp2 = y_img[rst1:ren, cst1:cen] if top > 0 or bottom > 0 or left > 0 or right > 0: temp2 = cv2.copyMakeBorder(temp2, top, bottom, left, right, borderType=2) gtdata.append(temp2.reshape(1, temp2.shape[0], temp2.shape[1])) return train, gtdata, images_per_image
filepath1 = input("please input the first filepath") filepath2 = input("please input the second filepath") else: img_dir = str(sys.argv[1]) names = os.listdir(img_dir) filepath1 = img_dir + '\\' + names[0] filepath2 = img_dir + '\\' + names[1] img1 = cv.imread(filepath1) img2 = cv.imread(filepath2) srcImg = cv.copyMakeBorder(img1, top, bot, left, right, cv.BORDER_CONSTANT, value=(0, 0, 0)) testImg = cv.copyMakeBorder(img2, top, bot, left, right, cv.BORDER_CONSTANT, value=(0, 0, 0)) img1gray = cv.cvtColor(srcImg, cv.COLOR_BGR2GRAY) img2gray = cv.cvtColor(testImg, cv.COLOR_BGR2GRAY) sift = cv.xfeatures2d_SIFT().create() # 利用SIFT算法提取特征点 kp1, des1 = sift.detectAndCompute(img1gray, None)
def draw_test(name, pred, im): monkey = monkey_breeds_dict[str(pred)] BLACK = [0,0,0] expanded_image = cv2.copyMakeBorder(im, 80, 0, 0, 100 ,cv2.BORDER_CONSTANT,value=BLACK) cv2.putText(expanded_image, monkey, (20, 60) , cv2.FONT_HERSHEY_SIMPLEX,1, (0,0,255), 2) cv2.imshow(name, expanded_image)
def reduce_layer(image, kernel=generatingKernel(0.4)): """Convolve the input image with a generating kernel and then reduce its width and height each by a factor of two. For grading purposes, it is important that you use a reflected border (i.e., padding equivalent to cv2.BORDER_REFLECT101) and only keep the valid region (i.e., the convolution operation should return an image of the same shape as the input) for the convolution. Subsampling must include the first row and column, skip the second, etc. Example (assuming 3-tap filter and 1-pixel padding; 5-tap is analogous): fefghg abcd Pad babcdc Convolve ZYXW Subsample ZX efgh -------> fefghg --------> VUTS --------> RP ijkl BORDER jijklk keep RQPO JH mnop REFLECT nmnopo valid NMLK qrst rqrsts JIHG nmnopo A "3-tap" filter means a 3-element kernel; a "5-tap" filter has 5 elements. Please consult the lectures for a more in-depth discussion of how to tackle the reduce function. Parameters ---------- image : numpy.ndarray A grayscale image of shape (r, c). The array may have any data type (e.g., np.uint8, np.float64, etc.) kernel : numpy.ndarray (Optional) A kernel of shape (N, N). The array may have any data type (e.g., np.uint8, np.float64, etc.) Returns ------- numpy.ndarray(dtype=np.float64) An image of shape (ceil(r/2), ceil(c/2)). For instance, if the input is 5x7, the output will be 3x4. """ # WRITE YOUR CODE HERE. # define #print("--------entering reduce_size---------") ker = kernel height_pad = int((kernel.shape[0] - 1) / 2) #print("pad height ",height_pad) width_pad = int((kernel.shape[1] - 1) / 2) #print("pad width ",height_pad) # padding #print("shape before border ",image.shape) image = cv2.copyMakeBorder(image, height_pad, height_pad, width_pad, width_pad, borderType=cv2.BORDER_REFLECT101) #print("shape after border ",image.shape) # Convolve image = cv2.filter2D(image, ddepth=-1, kernel=kernel) #print("shape after filter2D ",image.shape) #print(image) #cv2.imshow("test",image) # remove padding image = image[height_pad:image.shape[0] - height_pad, width_pad:image.shape[1] - width_pad] #print("shape after removing padding ",image.shape) # resize rows_num = int(image.shape[0] / 2 + 0.9) #make sure to get ceiling on divide cols_num = int(image.shape[1] / 2 + 0.9) #make sure to get ceiling on divide image = cv2.resize( image, (cols_num, rows_num), interpolation=cv2.INTER_AREA) #i guess resize is c,r instead of r,c # check size #print("shape after resize ",image.shape) #print("image like this \n", image[0:10,0:10]) # return # needs to be float64 image = image.astype(np.float64) return image
#This is a simple open a picture and display it in gray scale. import cv2 import numpy as np import matplotlib.pyplot as plt img1 = cv2.imread("myTest.jpg") BLUE = [255,0,0] replicate = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REPLICATE) reflect = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT) reflect101 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT_101) wrap = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_WRAP) constant= cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=BLUE) plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL') plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE') plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT') plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101') plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP') plt.subplot(236),plt.imshow(constant),plt.title('CONSTANT') plt.show() cv2.waitKey(0) cv2.destroyAllWindows()
def pad(img, padding, fill=(0, 0, 0), padding_mode='constant'): """Pad the given CV Image on all sides with speficified padding mode and fill value. Args: img (np.ndarray): Image to be padded. padding (int or tuple): Padding on each border. If a single int is provided this is used to pad all borders. If tuple of length 2 is provided this is the padding on left/right and top/bottom respectively. If a tuple of length 4 is provided this is the padding for the left, top, right and bottom borders respectively. fill (int, tuple): Pixel fill value for constant fill. Default is 0. If a tuple of length 3, it is used to fill R, G, B channels respectively. This value is only used when the padding_mode is constant padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant. constant: pads with a constant value, this value is specified with fill edge: pads with the last value on the edge of the image reflect: pads with reflection of image (without repeating the last value on the edge) padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode will result in [3, 2, 1, 2, 3, 4, 3, 2] symmetric: pads with reflection of image (repeating the last value on the edge) padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode will result in [2, 1, 1, 2, 3, 4, 4, 3] Returns: CV Image: Padded image. """ if not _is_numpy_image(img): raise TypeError('img should be CV Image. Got {}'.format(type(img))) if not isinstance(padding, (numbers.Number, tuple)): raise TypeError('Got inappropriate padding arg') if not isinstance(fill, (numbers.Number, str, tuple)): raise TypeError('Got inappropriate fill arg') if not isinstance(padding_mode, str): raise TypeError('Got inappropriate padding_mode arg') if isinstance(padding, collections.Sequence) and len(padding) not in [2, 4]: raise ValueError( "Padding must be an int or a 2, or 4 element tuple, not a " + "{} element tuple".format(len(padding))) assert padding_mode in ['constant', 'edge', 'reflect', 'symmetric'], \ 'Padding mode should be either constant, edge, reflect or symmetric' if isinstance(padding, int): pad_left = pad_right = pad_top = pad_bottom = padding if isinstance(padding, collections.Sequence) and len(padding) == 2: pad_left = pad_right = padding[0] pad_top = pad_bottom = padding[1] if isinstance(padding, collections.Sequence) and len(padding) == 4: pad_left, pad_top, pad_right, pad_bottom = padding if isinstance(fill, numbers.Number): fill = fill, if padding_mode == 'constant': assert (len(fill) == 3 and len(img.shape) == 3) or (len(fill) == 1 and len(img.shape) == 2), \ 'channel of image is {} but length of fill is {}'.format(img.shape[-1], len(fill)) img = cv2.copyMakeBorder(src=img, top=pad_top, bottom=pad_bottom, left=pad_left, right=pad_right, borderType=PAD_MOD[padding_mode], value=fill) return img
def depth_callback(depth_message): global model global graph global prev_mp global ROBOT_Z global fx, cx, fy, cy global crop_size global Input_Res global rgb_crop global grey_crop with TimeIt('prediction'): # with TimeIt('Crop'): # depth = bridge.imgmsg_to_cv2(depth_message) rgbdImg = bridge.imgmsg_to_cv2(depth_message) depthImg = rgbdImg[:, :, 3] rgbImg = rgbdImg[:, :, :3] rgb_raw_crop = cv2.resize( rgbdImg[(304 - crop_size) // 2:(304 - crop_size) // 2 + crop_size, (304 - crop_size) // 2:(304 - crop_size) // 2 + crop_size], (Input_Res, Input_Res)) grey_crop = cv2.cvtColor(rgb_raw_crop, cv2.COLOR_RGB2GRAY) near = 0.01 far = 0.24 depth = far * near / (far - (far - near) * depthImg) depth_crop = cv2.resize( depth[(304 - crop_size) // 2:(304 - crop_size) // 2 + crop_size, (304 - crop_size) // 2:(304 - crop_size) // 2 + crop_size], (Input_Res, Input_Res)) depth_crop = cv2.resize(depth, (Input_Res, Input_Res)) # Replace nan with 0 for inpainting. depth_crop = depth_crop.copy() depth_nan = np.isnan(depth_crop).copy() # print(depth_nan) depth_crop[depth_nan] = 0 # np.save("/home/aarons/catkin_kinect/src/yumi_grasp/src/depth_raw_pub2.npy", depth_crop) # with TimeIt('Inpaint'): # open cv inpainting does weird things at the border. depth_crop = cv2.copyMakeBorder(depth_crop, 1, 1, 1, 1, cv2.BORDER_DEFAULT) mask = (depth_crop == 0).astype(np.uint8) # Scale to keep as float, but has to be in bounds -1:1 to keep opencv happy. depth_scale = np.abs(depth_crop).max() depth_crop = depth_crop.astype(np.float32) / ( depth_scale) # Has to be float32, 64 not supported. depth_crop = cv2.inpaint(depth_crop, mask, 1, cv2.INPAINT_NS) # Back to original size and value range. depth_crop = depth_crop[1:-1, 1:-1] depth_crop = depth_crop * depth_scale # kinect output unit is millemeter, but realsense output unit is meter # with TimeIt('Calculate Depth'): # Figure out roughly the depth in mm of the part between the grippers for collision avoidance. depth_crop_neighbor = depth_crop.copy() # cv2.imshow('fram22e',depth_crop_neighbor) # depth_center = depth_crop[100:141, 130:171].flatten() depth_center = depth_crop.flatten() depth_center.sort() # depth_center = depth_center[:10].mean() * 1000.0 depth_center = depth_center.mean() * 1000.0 depth_crop = (depth_crop - depth_crop.min() ) / np.float32(depth_crop.max() - depth_crop.min()) depth_raw_pub.publish(bridge.cv2_to_imgmsg(grey_crop)) rgb_crop = np.expand_dims( ((grey_crop - grey_crop.min()) / np.float32(grey_crop.max() - grey_crop.min())), -1) depth_crop = np.expand_dims(depth_crop, axis=2) rgbd_input = np.concatenate((rgb_crop, depth_crop), axis=2) rgbd_input = np.expand_dims(rgbd_input, axis=0) with TimeIt('Inference'): with graph.as_default(): # print("begin prediction") # pred_out = model.predict(depth_crop.reshape((1, Input_Res, Input_Res, 1))) pred_out = model.predict(rgbd_input) points_out = pred_out[0].squeeze() points_out[depth_nan] = 0 # with TimeIt('Trig'): # Calculate the angle map. cos_out = pred_out[1].squeeze() sin_out = pred_out[2].squeeze() ang_out = np.arctan2(sin_out, cos_out) / 2.0 width_out = pred_out[3].squeeze() * 150.0 # Scaled 0-150:0-1 # with TimeIt('Filter'): # Filter the outputs. points_out = ndimage.filters.gaussian_filter(points_out, 5.0) # 3.0 5.0 aaron ang_out = ndimage.filters.gaussian_filter(ang_out, 2.0) # with TimeIt('Control'): # Calculate the best pose from the camera intrinsics. maxes = None ALWAYS_MAX = False # Use ALWAYS_MAX = True for the open-loop solution. if ROBOT_Z > 0.34 or ALWAYS_MAX: # > 0.34 initialises the max tracking when the robot is reset. # Track the global max. max_pixel = np.array( np.unravel_index(np.argmax(points_out), points_out.shape)) prev_mp = max_pixel.astype(np.int) else: # Calculate a set of local maxes. Choose the one that is closes to the previous one. # maxes = peak_local_max(points_out, min_distance=20, threshold_abs=0.1, num_peaks=20) #min_distance=10, threshold_abs=0.1, num_peaks=3 15 0.1 20 maxes = peak_local_max( points_out, min_distance=5, threshold_abs=0.1, num_peaks=1 ) #min_distance=10, threshold_abs=0.1, num_peaks=3 15 0.1 20 if maxes.shape[0]: max_pixel = maxes[np.argmin( np.linalg.norm(maxes - prev_mp, axis=1))] # max_pixel = np.array(np.unravel_index(np.argmax(points_out), points_out.shape)) visual_max_pixel = max_pixel.copy() # Keep a global copy for next iteration. # prev_mp = (max_pixel * 0.25 + prev_mp * 0.75).astype(np.int) grasp_quality = points_out[max_pixel[0], max_pixel[1]] else: rospy.loginfo("no lacal maxes! ") grasp_quality = 0 cmd_msg = Float32MultiArray() cmd_msg.data = [ -63, -52, 699, 0.38, 56, 697, grasp_quality, 730, 109, 85 ] # rospy.loginfo(cmd_msg) cmd_pub.publish(cmd_msg) state_msg = Float32MultiArray() state_msg.data = [True] rospy.loginfo(state_msg) state_pub.publish(state_msg) return # max_pixel = maxes[np.argmin(np.linalg.norm(maxes - prev_mp, axis=1))] # visual_max_pixel = max_pixel.copy() # # Keep a global copy for next iteration. # prev_mp = (max_pixel * 0.25 + prev_mp * 0.75).astype(np.int) # # print(max_pixel) # grasp_quality = points_out[max_pixel[0],max_pixel[1]] if max_pixel[0] >= 10 and max_pixel[0] <= 394 and max_pixel[ 1] >= 10 and max_pixel[1] <= 394: # print('bound exists! ') depth_grasp_neighbor = depth_crop_neighbor[max_pixel[0] - 10:max_pixel[0] + 10, max_pixel[1] - 10:max_pixel[1] + 10].flatten() depth_grasp_neighbor.sort() depth_grasp_neighbor = depth_grasp_neighbor[:50].mean() * 1000.0 # print(depth_grasp_neighbor) else: depth_grasp_neighbor = depth_center ang = ang_out[max_pixel[0], max_pixel[1]] width = width_out[max_pixel[0], max_pixel[1]] if abs(depth_grasp_neighbor - depth_center) < 2 or abs(grey_crop.min() - grey_crop.mean()) < 35: rospy.loginfo('task space is empty!') print(depth_center - depth_grasp_neighbor) grasp_quality = 0 # Convert max_pixel back to uncropped/resized image coordinates in order to do the camera transform. max_pixel = ((np.array(max_pixel) / 304.0 * crop_size) + np.array([(304 - crop_size) // 2, (304 - crop_size) // 2])) #[2,1] max_pixel = np.round(max_pixel).astype(np.int) point_depth = depthImg[max_pixel[0], max_pixel[1]] # convert image space to world space OpenGL view_matrix = np.array( [[0.0, 1.0, -0.0, 0.0], [-1.0, 0.0, -0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [-0.0, -0.6499999761581421, -1.2400000095367432, 1.0]]) proj_matrix = np.array([[4.510708808898926, 0.0, 0.0, 0.0], [0.0, 4.510708808898926, 0.0, 0.0], [0.0, 0.0, -1.0020020008087158, -1.0], [0.0, 0.0, -0.0200200192630291, 0.0]]) inter_gl = np.dot(view_matrix, proj_matrix) px = 2.0 * (max_pixel[1] - 0) / 304.0 - 1.0 py = 1.0 - (2.0 * max_pixel[0]) / 304.0 pz = 2.0 * point_depth - 1.0 PP3D = np.array([px, py, pz, 1.0]) PP_world = np.dot(PP3D, np.linalg.inv(inter_gl)) # PP_world = np.dot( np.linalg.inv(inter_gl), PP3D) rospy.loginfo("PP_world") print(PP3D) # print(PP_world) print(PP_world / PP_world[3]) x = PP_world[0] / PP_world[3] y = PP_world[1] / PP_world[3] z = PP_world[2] / PP_world[3] # with TimeIt('Draw'): # Draw grasp markers on the points_out and publish it. (for visualisation) grasp_img = np.zeros((Input_Res, Input_Res, 3), dtype=np.uint8) # with open('/home/aarons/catkin_kinect/src/yumi_grasp/src/heatmap.pkl', 'w') as f: # pickle.dump(points_out, f) # print(points_out.shape) # np.save("/home/aarons/catkin_kinect/src/yumi_grasp/src/realsense_Umodel.npy", points_out) # np.savetxt("/home/aarons/catkin_kinect/src/yumi_grasp/src/light_txt.npy", points_out) # exit() # pd_pointout = pd.DataFrame(points_out) # pd_pointout.to_csv('/home/aarons/catkin_kinect/src/yumi_grasp/src/points_out.csv') # heatmap test code # fig, ax = plt.subplots() # ax = sns.heatmap(ang_out, cmap='jet', xticklabels=False, yticklabels=False, cbar=False) # fig.add_axes(ax) # fig.canvas.draw() # data_heatmap = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='') # data_heatmap = data_heatmap.reshape(fig.canvas.get_width_height()[::-1] + (3,)) # data_crop = cv2.resize(data_heatmap[(480-crop_size)//2:(480-crop_size)//2+crop_size, (640-crop_size)//2:(640-crop_size)//2+crop_size,:], (Input_Res, Input_Res)) # print(data_crop.shape) if VISUALISE: pointout_pub.publish( bridge.cv2_to_imgmsg(points_out)) # for visualization module grasp_img_plain = grasp_img.copy() grasp_img[:, :, 2] = (points_out * 255.0) # rr, cc = circle(prev_mp[0], prev_mp[1], 5) rr, cc = circle(visual_max_pixel[0], visual_max_pixel[1], 5) # depth_crop[rr, cc] = 200 grasp_img[rr, cc, 0] = 0 # R grasp_img[rr, cc, 1] = 255 # G grasp_img[rr, cc, 2] = 0 # B # with TimeIt('Publish'): grasp_img = bridge.cv2_to_imgmsg(grasp_img, 'bgr8') grasp_img.header = depth_message.header grasp_pub.publish(grasp_img) grasp_img_plain = bridge.cv2_to_imgmsg(grasp_img_plain, 'rgb8') grasp_img_plain.header = depth_message.header grasp_plain_pub.publish(grasp_img_plain) depth_pub.publish(bridge.cv2_to_imgmsg(depth_crop)) ang_pub.publish(bridge.cv2_to_imgmsg(ang_out)) # Output the best grasp pose relative to camera. cmd_msg = Float32MultiArray() cmd_msg.data = [ x, y, z, ang, width, depth_grasp_neighbor, grasp_quality, depth_center, visual_max_pixel[0], visual_max_pixel[1] ] rospy.loginfo(cmd_msg) cmd_pub.publish(cmd_msg) state_msg = Float32MultiArray() state_msg.data = [False] rospy.loginfo(state_msg) state_pub.publish(state_msg)
def main(): global source_image source_image = readnextimage(0) # start streaming if system == "linux": camera = pyfakewebcam.FakeWebcam(f'/dev/video{stream_id}', webcam_width, webcam_height) camera.print_capabilities() print( f"Fake webcam created on /dev/video{stream_id}. Use Firefox and join a Google Meeting to test." ) # capture webcam video_capture = cv2.VideoCapture(webcam_id) time.sleep(1) width = video_capture.get(3) # float height = video_capture.get(4) # float print("webcam dimensions = {} x {}".format(width, height)) # load models previous = None net = load_face_model() generator, kp_detector = demo.load_checkpoints( config_path=f'{first_order_path}config/vox-adv-256.yaml', checkpoint_path=f'{model_path}/vox-adv-cpk.pth.tar') # create windows cv2.namedWindow('Face', cv2.WINDOW_GUI_NORMAL) # extracted face cv2.moveWindow('Face', int(screen_width / 2) - 150, 100) cv2.resizeWindow('Face', 256, 256) cv2.namedWindow('DeepFake', cv2.WINDOW_GUI_NORMAL) # face transformation cv2.moveWindow('DeepFake', int(screen_width / 2) + 150, 100) cv2.resizeWindow('DeepFake', int(img_shape[1] / img_shape[0] * 256), 256) cv2.namedWindow('Stream', cv2.WINDOW_GUI_NORMAL) # rendered to fake webcam cv2.moveWindow('Stream', int(screen_width / 2) - int(webcam_width / 2), 400) cv2.resizeWindow('Stream', webcam_width, webcam_height) print( "Press C to center Webcam, Press B/N for previous/next image in media directory, T to alter between relative and absolute transformation, Q to quit" ) x1, y1, x2, y2 = [0, 0, 0, 0] relative = True while True: ret, frame = video_capture.read() frame = cv2.resize(frame, (640, 480)) frame = cv2.flip(frame, 1) if (previous is None or reset is True): x1, y1, x2, y2 = find_face_cut(net, frame) previous = cut_face_window(x1, y1, x2, y2, frame) reset = False #cv2.imshow('Previous',previous) curr_face = cut_face_window(x1, y1, x2, y2, frame) #cv2.imshow('Curr Face',curr_face) #cv2.imshow('Source Image',source_image) deep_fake = process_image(source_image, previous, curr_face, net, generator, kp_detector, relative) deep_fake = cv2.cvtColor(deep_fake, cv2.COLOR_RGB2BGR) #cv2.imshow('Webcam', frame) - get face cv2.imshow('Face', curr_face) cv2.imshow('DeepFake', deep_fake) rgb = cv2.resize(deep_fake, (int(img_shape[1] / img_shape[0] * 480), 480)) # pad image x_border = int((640 - (img_shape[1] / img_shape[0] * 480)) / 2) y_border = int((480 - (img_shape[0] / img_shape[1] * 640)) / 2) stream_v = cv2.copyMakeBorder(rgb, y_border if y_border >= 0 else 0, y_border if y_border >= 0 else 0, x_border if x_border >= 0 else 0, x_border if x_border >= 0 else 0, cv2.BORDER_CONSTANT) cv2.imshow('Stream', stream_v) #time.sleep(1/30.0) stream_v = cv2.flip(stream_v, 1) stream_v = cv2.cvtColor(stream_v, cv2.COLOR_BGR2RGB) stream_v = (stream_v * 255).astype(np.uint8) # stream to fakewebcam if system == "linux": #print("output to fakecam") camera.schedule_frame(stream_v) k = cv2.waitKey(1) # Hit 'q' on the keyboard to quit! if k & 0xFF == ord('q'): print("Quiting") video_capture.release() break elif k == ord('c'): # center print("Centering the image") reset = True elif k == ord('b'): # previous image print("Loading previous image") source_image = readpreviousimage() reset = True elif k == ord('n'): # next image print("Loading next image") source_image = readnextimage() reset = True elif k == ord('t'): # rotate relative = not relative print("Changing transform mode") cv2.destroyAllWindows() exit()
def step1(read_type): cv.destroyAllWindows() plt.close() os_path = os.getcwd() cell_area_hist_list = [] print("============Step 1 Start============") #-----read----- root = tk.Tk() root.withdraw() if read_type == 0: file_path = filedialog.askopenfilename() #img=cv.imread("G:\\2020summer\\Project\\Chromophobe_dataset1\\4.jpg") else: file_path = "G:\\2020summer\\Project\\Oncocytoma_dataset\\output\\" + str( read_type) + ".jpg" if not os.path.exists(os_path + '\\bin\\output'): os.makedirs(os_path + '\\bin\\output') if not os.path.exists(os_path + '\\bin\\output'): os.makedirs(os_path + '\\result') if not os.path.exists(os_path + '\\bin\\output'): os.mkdir(os_path + '\\output_single') if not os.path.exists(os_path + '\\bin\\output'): os.makedirs(os_path + '\\result_pdf') img = cv.imread(file_path) img_original = img print("Img size: [Width :", img.shape[0], "]", "[Height :", img.shape[1], "]") if img.shape[0] < 450 or img.shape[1] < 600: img = cv.copyMakeBorder(img, 80, 450, 360, 360, cv.BORDER_CONSTANT, value=[255, 255, 255]) else: img = cv.copyMakeBorder(img, 80, 450, 60, 60, cv.BORDER_CONSTANT, value=[255, 255, 255]) img_masked = img.copy() img_nucleus_white_img = img.copy() #-----preprocess----- gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) #cv.imshow("gray", gray) gauss = cv.GaussianBlur(gray, (5, 5), 5) #cv.imshow("gauss1",gauss) ret, thresh = cv.threshold(gauss, 190, 255, 0) cv.imwrite("bin\\figure3_left.jpg", thresh) #cv.imshow("thresh",thresh) erode = cv.erode(thresh, None, iterations=1) #cv.imshow("erode",erode) #-----remove outlines----- #cv.imshow("erode",erode) for i in range(0, img.shape[0]): for j in range(0, img.shape[1]): erode[0][j] = 255 #-----find contours----- cnts, hierarchy = cv.findContours(erode.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_NONE) def cnt_area(cnt): area = cv.contourArea(cnt) return area counter_number = 0 location_cells_center = {} area_of_cells_nucleus = [] Whole_pic_cell_area_ave_percent = [] Whole_pic_cell_color_ave = [] for i in range(0, len(cnts)): if 250 <= cnt_area(cnts[i]) <= 0.2 * (img.shape[0] * img.shape[1]): cell_area_hist_list.append(cnt_area(cnts[i])) #print(cnts[i]) #cell_area_hist_list.append(area_calculate_from_points(cnts[i])) counter_number += 1 #print(cnts[i]) #print("======") cv.drawContours(img_masked, cnts[i], -1, (0, 0, 255), 2) #draw contours cv.drawContours(img_nucleus_white_img, [cnts[i]], -1, (255, 255, 255), -1) #masked white M = cv.moments(cnts[i]) #检索每个细胞的内部颜色 x, y, w, h = cv.boundingRect(cnts[i]) #cv.imshow('single_cell', newimage) cell_area_percent = 0 for row in range(y, y + h): for col in range(x, x + w): result = cv.pointPolygonTest(cnts[i], (col, row), False) if result == -1: cv.circle(gray, (col, row), 1, 255, -1) cv.circle(img, (col, row), 1, (255, 255, 255), -1) cell_area_percent += 1 cv.rectangle(img, (x, y), (x + w, y + h), (153, 153, 0), 1) newimage_gray = gray[y:y + h, x:x + w] Single_Cell_Color_Distrution = [] for row in range(h): for col in range(w): if newimage_gray[row, col] != 255: Single_Cell_Color_Distrution.append(newimage_gray[row, col]) ''' plt.hist(Single_Cell_Color_Distrution,bins=50) plt.title(str(counter_number)) plt.show() ''' #print("this cell area percent= ",str(cell_area_percent/(w*h))) numpy.set_printoptions(precision=3) Whole_pic_cell_area_ave_percent.append(cell_area_percent / (w * h)) Whole_pic_cell_color_ave.append( numpy.mean(Single_Cell_Color_Distrution)) """#找出masked细胞内点的坐标 rect = cv.minAreaRect(cnts[i]) cx, cy = rect[0] box = cv.boxPoints(rect) box = np.int0(box) cv.drawContours(img_masked, [box], 0, (0, 0, 255), 2) #cv.circle(img_masked, (np.int32(cx), np.int32(cy)), 2, (255, 0, 0), 2, 8, 0) box_gray_color=[] for by in range(box[2][1],box[0][1]+1): for bx in range(box[1][0],box[3][0]+1): #print(bx,by) #cv.circle(img_masked,(bx, by), 1, (255, 0, 0), 2, 8, 0) box_gray_color.append(gray[bx,by]) plt.hist(box_gray_color) plt.hist(box_gray_color,bins=50) plt.title(str(counter_number)) plt.show() dist=cv.pointPolygonTest(cnts[i],(50,50),True) """ try: cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) cv.circle(img_masked, (cX, cY), 3, (255, 255, 255), -1) cv.putText(img_masked, str(counter_number), (cX - 20, cY - 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) area_of_cells_nucleus.append(cnt_area(cnts[i])) location_cells_center[counter_number] = [cX, cY] except: pass if counter_number == 1: x1 = cX y1 = cY if counter_number == 2: x2 = cX y2 = cY if counter_number == 3: x_sample = cX y_sample = cY #cv.drawContours(img_masked, [cnts[i]], -1, (255, 255, 255), -1)#mask contours print("Whole pic average cell nucleus area percent: ", Whole_pic_cell_area_ave_percent) print("Whole pic average cell nucleus area percent_ave: ", numpy.mean(Whole_pic_cell_area_ave_percent)) print("Whole pic average cell nucleus color deep percent: ", Whole_pic_cell_color_ave) print("Whole pic average cell nucleus color deep percent_ave: ", numpy.mean(Whole_pic_cell_color_ave)) cv.imwrite("result\\cell_clean.bmp", img) #-----put Text----- print("total cells number : ", counter_number) cv.line(img_masked, (x1, y1), (x2, y2), (0, 0, 255), 2) list_of_two_points = pixel_between_two_points(x1, x2, y1, y2) #-----output information on the line height_of_two_points = [] height_of_two_points_B = [] height_of_two_points_G = [] height_of_two_points_R = [] for m in range(0, len(list_of_two_points)): height = img[list_of_two_points[m][1], list_of_two_points[m][0]] try: height_B = img[list_of_two_points[m][1], list_of_two_points[m][0]][0] height_G = img[list_of_two_points[m][1], list_of_two_points[m][0]][1] height_R = img[list_of_two_points[m][1], list_of_two_points[m][0]][2] height_of_two_points_B.append(height_B) height_of_two_points_G.append(height_G) height_of_two_points_R.append(height_R) except: pass #print(height) height_of_two_points.append(height) img_sample = img.copy() cv.circle(img_sample, (x_sample, y_sample), 3, (0, 0, 255), -1) font = cv.FONT_HERSHEY_SIMPLEX cv.putText(img_sample, "Sample_Point", (x_sample - 20, y_sample - 20), font, 0.7, (255, 255, 255), 2) #cv.imshow("img_sample_location_RED_DOT", img_sample) # save to local f = open("bin\\dict.txt", 'w') f.write(str(location_cells_center)) f.close() # < list save file1 = open('bin\\area_of_nucleus.txt', 'w') for fp in area_of_cells_nucleus: file1.write(str(fp)) file1.write('\n') file1.close() # list save > cv.imwrite("bin\\temp.bmp", img_masked) cv.imwrite("bin\\temp_1.bmp", img_nucleus_white_img) cv.imwrite("bin\\figure3_right.jpg", img_masked) #================hist of cells area================== #plt.hist(cell_area_hist_list) #plt.show() #=================================excel write rb = xlrd.open_workbook( "G:\\2020summer\\Project\\nucleus_color.xls") # 打开weng.xls文件 wb = copy(rb) # 利用xlutils.copy下的copy函数复制 ws = wb.get_sheet(0) # 获取表单0 ws.write(read_type, 2, numpy.mean(Whole_pic_cell_color_ave)) # 改变(0,0)的值 #ws.write(read_type, 2, np.mean(not_circle_rate)) # 增加(8,0)的值 wb.save("G:\\2020summer\\Project\\nucleus_color.xls") print("Excel write success.") #=================================UI/ cv.putText(img_masked, "Overview", (80, 40), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2) image_size_text = "Image size: [Width :" + str( img_original.shape[0]) + "]" + "[Height :" + str( img_original.shape[1]) + "]" cv.putText(img_masked, image_size_text, (80, img.shape[0] - 400), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2) cv.putText(img_masked, "Total cells number: " + str(counter_number), (80, img.shape[0] - 350), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2) Cells_density = ('%.2f' % (10000 * counter_number / (img.shape[0] * img.shape[1]))) print("Cells density : ", Cells_density, " / 100*100 pixels") cv.putText(img_masked, "Cells density : " + str(Cells_density) + " / 100*100 pixels", (80, img.shape[0] - 300), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2) cv.putText(img_masked, "Close window to continue", (80, img.shape[0] - 250), cv.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 0), 1) #=================================/UI cv.imwrite("result\\overview_result1.bmp", img_masked) print("============Step 1 End============") #cv.waitKey() return counter_number, Cells_density
back_img2 = crop_list[4] multi_fr = crop_list[5] #process segmentation mask kernel_er = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) kernel_dil = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) rcnn = rcnn.astype(np.float32) / 255 rcnn[rcnn > 0.2] = 1 K = 25 zero_id = np.nonzero(np.sum(rcnn, axis=1) == 0) del_id = zero_id[0][zero_id[0] > 250] if len(del_id) > 0: del_id = [del_id[0] - 2, del_id[0] - 1, *del_id] rcnn = np.delete(rcnn, del_id, 0) rcnn = cv2.copyMakeBorder(rcnn, 0, K + len(del_id), 0, 0, cv2.BORDER_REPLICATE) rcnn = cv2.erode(rcnn, kernel_er, iterations=10) rcnn = cv2.dilate(rcnn, kernel_dil, iterations=5) rcnn = cv2.GaussianBlur(rcnn.astype(np.float32), (31, 31), 0) rcnn = (255 * rcnn).astype(np.uint8) rcnn = np.delete(rcnn, range(reso[0], reso[0] + K), 0) #convert to torch img = torch.from_numpy(bgr_img.transpose((2, 0, 1))).unsqueeze(0) img = 2 * img.float().div(255) - 1 bg = torch.from_numpy(bg_im.transpose((2, 0, 1))).unsqueeze(0) bg = 2 * bg.float().div(255) - 1 rcnn_al = torch.from_numpy(rcnn).unsqueeze(0).unsqueeze(0) rcnn_al = 2 * rcnn_al.float().div(255) - 1 multi_fr = torch.from_numpy(multi_fr.transpose((2, 0, 1))).unsqueeze(0)
def __init__(self): global prediction cam = cv2.VideoCapture(1) if cam.read()[0] == False: cam = cv2.VideoCapture(0) hist = get_hand_hist() x, y, w, h = 300, 100, 300, 300 while True: text = "" img = cam.read()[1] img = cv2.flip(img, 1) imgCrop = img[y:y + h, x:x + w] imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) dst = cv2.calcBackProject([imgHSV], [0, 1], hist, [0, 180, 0, 256], 1) disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10)) cv2.filter2D(dst, -1, disc, dst) blur = cv2.GaussianBlur(dst, (11, 11), 0) blur = cv2.medianBlur(blur, 15) thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] thresh = cv2.merge((thresh, thresh, thresh)) thresh = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY) thresh = thresh[y:y + h, x:x + w] contours = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1] if len(contours) > 0: contour = max(contours, key=cv2.contourArea) #print(cv2.contourArea(contour)) if cv2.contourArea(contour) > 10000: x1, y1, w1, h1 = cv2.boundingRect(contour) save_img = thresh[y1:y1 + h1, x1:x1 + w1] if w1 > h1: save_img = cv2.copyMakeBorder(save_img, int((w1 - h1) / 2), int((w1 - h1) / 2), 0, 0, cv2.BORDER_CONSTANT, (0, 0, 0)) elif h1 > w1: save_img = cv2.copyMakeBorder(save_img, 0, 0, int((h1 - w1) / 2), int((h1 - w1) / 2), cv2.BORDER_CONSTANT, (0, 0, 0)) pred_probab, pred_class = keras_predict(model, save_img) print(pred_class, pred_probab) if pred_probab * 100 > 80: text = get_pred_text_from_db(pred_class) print(text) blackboard = np.zeros((480, 640, 3), dtype=np.uint8) splitted_text = split_sentence(text, 2) put_splitted_text_in_blackboard(blackboard, splitted_text) #cv2.putText(blackboard, text, (30, 200), cv2.FONT_HERSHEY_TRIPLEX, 1.3, (255, 255, 255)) cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) res = np.hstack((img, blackboard)) cv2.imshow("Recognizing gesture", res) cv2.imshow("thresh", thresh) if cv2.waitKey(1) == ord('q'): break
# convert to grayscale gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Performance of DCT calculation, via the DFT/FFT, is better for array # sizes of power of two. Arrays whose size is a product of 2's, 3's, # and 5's are also processed quite efficiently. # Hence we modify the size of the array tothe optimal size (by padding # zeros) before finding DCT. pad_right = nwidth - width pad_bottom = nheight - height nframe = cv2.copyMakeBorder(gray_frame, 0, pad_bottom, 0, pad_right, cv2.BORDER_CONSTANT, value=0) # perform the DCT dct = cv2.dct(np.float32(nframe)) # perform low pass filtering radius = cv2.getTrackbarPos("radius", window_name2) lp_filter = create_low_pass_filter(nwidth, nheight, radius) dct_filtered = cv2.multiply(dct, lp_filter)
def img_only_color(self, filename, oldimg, img_contours): """ :param filename: 图像文件 :param oldimg: 原图像文件 :return: 已经定位好的车牌 """ pic_hight, pic_width = img_contours.shape[:2] lower_blue = np.array([100, 110, 110]) upper_blue = np.array([130, 255, 255]) lower_yellow = np.array([15, 55, 55]) upper_yellow = np.array([50, 255, 255]) lower_green = np.array([50, 50, 50]) upper_green = np.array([100, 255, 255]) hsv = cv2.cvtColor(filename, cv2.COLOR_BGR2HSV) mask_blue = cv2.inRange(hsv, lower_blue, upper_blue) mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow) mask_green = cv2.inRange(hsv, lower_yellow, upper_green) output = cv2.bitwise_and(hsv, hsv, mask=mask_blue + mask_yellow + mask_green) # 根据阈值找到对应颜色 output = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY) Matrix = np.ones((20, 20), np.uint8) img_edge1 = cv2.morphologyEx(output, cv2.MORPH_CLOSE, Matrix) img_edge2 = cv2.morphologyEx(img_edge1, cv2.MORPH_OPEN, Matrix) card_contours = img_math.img_findContours(img_edge2, oldimg) card_imgs = img_math.img_Transform(card_contours, oldimg, pic_width, pic_hight) colors, car_imgs = img_math.img_color(card_imgs) predict_result = [] roi = None card_color = None for i, color in enumerate(colors): if color in ("blue", "yello", "green"): card_img = card_imgs[i] gray_img = cv2.cvtColor(card_img, cv2.COLOR_BGR2GRAY) # 黄、绿车牌字符比背景暗、与蓝车牌刚好相反,所以黄、绿车牌需要反向 if color == "green" or color == "yello": gray_img = cv2.bitwise_not(gray_img) ret, gray_img = cv2.threshold( gray_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) x_histogram = np.sum(gray_img, axis=1) x_min = np.min(x_histogram) x_average = np.sum(x_histogram) / x_histogram.shape[0] x_threshold = (x_min + x_average) / 2 wave_peaks = img_math.find_waves(x_threshold, x_histogram) if len(wave_peaks) == 0: print("peak less 0:") continue # 认为水平方向,最大的波峰为车牌区域 wave = max(wave_peaks, key=lambda x: x[1] - x[0]) gray_img = gray_img[wave[0]:wave[1]] # 查找垂直直方图波峰 row_num, col_num = gray_img.shape[:2] # 去掉车牌上下边缘1个像素,避免白边影响阈值判断 gray_img = gray_img[1:row_num - 1] y_histogram = np.sum(gray_img, axis=0) y_min = np.min(y_histogram) y_average = np.sum(y_histogram) / y_histogram.shape[0] y_threshold = (y_min + y_average) / 5 # U和0要求阈值偏小,否则U和0会被分成两半 wave_peaks = img_math.find_waves(y_threshold, y_histogram) if len(wave_peaks) < 6: print("peak less 1:", len(wave_peaks)) continue wave = max(wave_peaks, key=lambda x: x[1] - x[0]) max_wave_dis = wave[1] - wave[0] # 判断是否是左侧车牌边缘 if wave_peaks[0][1] - wave_peaks[0][ 0] < max_wave_dis / 3 and wave_peaks[0][0] == 0: wave_peaks.pop(0) # 组合分离汉字 cur_dis = 0 for i, wave in enumerate(wave_peaks): if wave[1] - wave[0] + cur_dis > max_wave_dis * 0.6: break else: cur_dis += wave[1] - wave[0] if i > 0: wave = (wave_peaks[0][0], wave_peaks[i][1]) wave_peaks = wave_peaks[i + 1:] wave_peaks.insert(0, wave) point = wave_peaks[2] point_img = gray_img[:, point[0]:point[1]] if np.mean(point_img) < 255 / 5: wave_peaks.pop(2) if len(wave_peaks) <= 6: print("peak less 2:", len(wave_peaks)) continue part_cards = img_math.seperate_card(gray_img, wave_peaks) for i, part_card in enumerate(part_cards): # 可能是固定车牌的铆钉 if np.mean(part_card) < 255 / 5: print("a point") continue part_card_old = part_card w = abs(part_card.shape[1] - SZ) // 2 part_card = cv2.copyMakeBorder(part_card, 0, 0, w, w, cv2.BORDER_CONSTANT, value=[0, 0, 0]) part_card = cv2.resize(part_card, (SZ, SZ), interpolation=cv2.INTER_AREA) part_card = img_recognition.preprocess_hog([part_card]) if i == 0: resp = self.modelchinese.predict(part_card) charactor = img_recognition.provinces[int(resp[0]) - PROVINCE_START] else: resp = self.model.predict(part_card) charactor = chr(resp[0]) # 判断最后一个数是否是车牌边缘,假设车牌边缘被认为是1 if charactor == "1" and i == len(part_cards) - 1: if part_card_old.shape[0] / part_card_old.shape[ 1] >= 7: # 1太细,认为是边缘 continue predict_result.append(charactor) roi = card_img card_color = color break return predict_result, roi, card_color # 识别到的字符、定位的车牌图像、车牌颜色
canvas = np.ones((400, 600, 3), dtype="uint8") * 255 # loop over the canvas for y in xrange(0, 400, 20): for x in xrange(0, 600, 20): # generate a random (x, y) coordinate, radius, and color for # the circle (dX, dY) = np.random.randint(5, 10, size=(2,)) r = np.random.randint(5, 8) color = random.choice(colors)[::-1] # draw the circle on the canvas cv2.circle(canvas, (x + dX, y + dY), r, color, -1) # pad the border of the image canvas = cv2.copyMakeBorder(canvas, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=(255, 255, 255)) # convert the canvas to grayscale, threshold it, and detect contours # in the image gray = cv2.cvtColor(canvas, cv2.COLOR_BGR2GRAY) gray = cv2.bitwise_not(gray) thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)[1] (cnts, _) = cv2.findContours(gray.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # initialize the data matrix data = [] # loop over the contours for c in cnts: # construct a mask from the contour mask = np.zeros(canvas.shape[:2], dtype="uint8")
# 尋找臉部範圍資訊 image_bbox = face_model.get_bbox(frame) face_x1 = image_bbox[0] face_y1 = image_bbox[1] face_x2 = image_bbox[0] + image_bbox[2] face_y2 = image_bbox[1] + image_bbox[3] face_w = face_x2 - face_x1 face_h = face_y2 - face_y1 #尋找特徵點 crop_x1, crop_x2, crop_y1, crop_y2, dx, dy, edx, edy = crop_range( face_x1, face_x2, face_y1, face_y2, face_w, face_h) cropped = frame[int(crop_y1):int(crop_y2), int(crop_x1):int(crop_x2)] if (dx > 0 or dy > 0 or edx > 0 or edy > 0): cropped = cv2.copyMakeBorder(cropped, int(dy), int(edy), int(dx), int(edx), cv2.BORDER_CONSTANT, 0) ratio_w = face_w / 112 ratio_h = face_h / 112 cropped = cv2.resize(cropped, (112, 112)) face_input = cropped.copy() face_input = cv2.cvtColor(face_input, cv2.COLOR_BGR2RGB) face_input = headpose_transformer(face_input).unsqueeze(0).to(device) _, landmarks = plfd_backbone(face_input) pre_landmark = landmarks[0] pre_landmark = pre_landmark.cpu().detach().numpy().reshape( -1, 2) * [112, 112] #頭部姿態 h_start = time.time()
# Load image img = cv2.imread(IMG_PATH + "/" + ground_truth_img[0]) # load image with draws of multiple detections img_cumulative_path = results_files_path + "/images/" + ground_truth_img[ 0] if os.path.isfile(img_cumulative_path): img_cumulative = cv2.imread(img_cumulative_path) else: img_cumulative = img.copy() # Add bottom border to image bottom_border = 60 BLACK = [0, 0, 0] img = cv2.copyMakeBorder(img, 0, bottom_border, 0, 0, cv2.BORDER_CONSTANT, value=BLACK) # assign detection-results to ground truth object if any # open ground-truth with that file_id gt_file = TEMP_FILES_PATH + "/" + file_id + "_ground_truth.json" ground_truth_data = json.load(open(gt_file)) ovmax = -1 gt_match = -1 # load detected object bounding-box bb = [float(x) for x in detection["bbox"].split()] for obj in ground_truth_data: # look for a class_name match if obj["class_name"] == class_name: bbgt = [float(x) for x in obj["bbox"].split()]
def expand_layer(image, kernel=generatingKernel(0.4)): """Upsample the image to double the row and column dimensions, and then convolve it with a generating kernel. Upsampling the image means that every other row and every other column will have a value of zero (which is why we apply the convolution after). For grading purposes, it is important that you use a reflected border (i.e., padding equivalent to cv2.BORDER_REFLECT101) and only keep the valid region (i.e., the convolution operation should return an image of the same shape as the input) for the convolution. Finally, multiply your output image by a factor of 4 in order to scale it back up. If you do not do this (and you should try it out without that) you will see that your images darken as you apply the convolution. You must explain why this happens in your submission PDF. Example (assuming 3-tap filter and 1-pixel padding; 5-tap is analogous): 000000 Upsample A0B0 Pad 0A0B0B Convolve zyxw AB -------> 0000 -------> 000000 -------> vuts CD C0D0 BORDER 0C0D0D keep rqpo EF 0000 REFLECT 000000 valid nmlk E0F0 0E0F0F jihg 0000 000000 fedc 0E0F0F NOTE: Remember to multiply the output by 4. A "3-tap" filter means a 3-element kernel; a "5-tap" filter has 5 elements. Please consult the lectures for a more in-depth discussion of how to tackle the expand function. Parameters ---------- image : numpy.ndarray A grayscale image of shape (r, c). The array may have any data type (e.g., np.uint8, np.float64, etc.) kernel : numpy.ndarray (Optional) A kernel of shape (N, N). The array may have any data type (e.g., np.uint8, np.float64, etc.) Returns ------- numpy.ndarray(dtype=np.float64) An image of shape (2*r, 2*c). For instance, if the input is 3x4, then the output will be 6x8. """ # WRITE YOUR CODE HERE. # define ker = kernel height_pad = int((kernel.shape[0] - 1) / 2) #print("pad height ",height_pad) width_pad = int((kernel.shape[1] - 1) / 2) #print("pad width ",height_pad) #print("the image shape starts as \n", image.shape) nrow = image.shape[0] ncol = image.shape[1] elements = nrow * ncol #print("original image top left \n", image[0:8,0:8]) # Add zeros # add columns #print("mean before adding columns ", np.mean(image)) image = image.reshape(1, elements).ravel() zeros = np.zeros((1, elements)).ravel() new_image = [None] * elements * 2 new_image[::2] = image new_image[1::2] = zeros new_image = np.asarray(new_image) new_image = new_image.reshape(nrow, ncol * 2) #add rows newer_image = np.zeros((new_image.shape[0] * 2, new_image.shape[1])) newer_image[::2] = new_image #print("shape of new image ", new_image.shape) #print("shape of newer image ",newer_image.shape) image = newer_image #print("expanded matrix like \n",image[0:8,0:8]) # Add padding image = cv2.copyMakeBorder(image, height_pad, height_pad, width_pad, width_pad, borderType=cv2.BORDER_REFLECT101) #print("expanded matrix wtih padding like \n",image[0:8,0:8]) # convolve #print("mean before convole ", np.mean(image)) image = cv2.filter2D(image, ddepth=-1, kernel=kernel) #print("shape after filter2D ",image.shape) #print("mean AFTER convolve ", np.mean(image)) #print(image) #cv2.imshow("test expand",image) # multiply by 4 image = image * 4 #print("mean AFTER multiplying ", np.mean(image)) #print("shape after multiplying ", image.shape) # Remove padding # resize #print("about to remove padding (still in expand)") image = image[height_pad:image.shape[0] - height_pad, width_pad:image.shape[1] - width_pad] #print("shape after removing padding ",image.shape) # check size #print("shape after resize (still in expand) ",image.shape) #print("expanded matrix like ",image) return image
def __init__(self, color, mask): self.color = color self.mask = mask np.savetxt("Mask.csv", mask, delimiter=",") self.mMask = np.array([0, 1]) self.borderSize = 2 self.borderSizePosMap = 1 self.windowSize = 5 self.toLeft = np.array([0, -1]) self.toRight = np.array([0, 1]) self.toUp = np.array([-1, 0]) self.toDown = np.array([1, 0]) self.mPosMap = {} self.WO_BORDER, self.W_BORDER = 0, 1 self.vSptAdj = [ np.array([-1, -1]), np.array([-1, 0]), np.array([-1, 1]), np.array([0, -1]), np.array([0, 1]), np.array([1, -1]), np.array([1, 0]), np.array([1, 1]) ] self.mColor = np.zeros(self.color.shape) self.mColor = {0: None, 1: self.mColor} self.mColor[self.W_BORDER] = cv2.copyMakeBorder( self.color, self.borderSize, self.borderSize, self.borderSize, self.borderSize, cv2.BORDER_REFLECT) self.mMask = np.pad(np.zeros(self.mask.shape), ((self.borderSize, self.borderSize), (self.borderSize, self.borderSize)), 'edge') self.mMask = {0: self.mMask, 1: self.mMask} self.mMask[self.W_BORDER] = cv2.copyMakeBorder( self.mask, self.borderSize, self.borderSize, self.borderSize, self.borderSize, cv2.BORDER_REFLECT) self.mColor[self.WO_BORDER] = self.mColor[ self.W_BORDER][self.borderSize:self.borderSize + color.shape[0], self.borderSize:self.borderSize + color.shape[1]] self.mMask[self.WO_BORDER] = np.array( self.mMask[self.W_BORDER][self.borderSize:self.borderSize + mask.shape[0], self.borderSize:self.borderSize + mask.shape[1]]) self.mPosMap[self.WO_BORDER] = np.zeros( (*self.mColor[self.WO_BORDER].shape[0:2], 2)) for r in range(self.mPosMap[self.WO_BORDER].shape[0]): for c in range(self.mPosMap[self.WO_BORDER].shape[1]): if self.mMask[self.WO_BORDER][r, c] == 0: self.mPosMap[self.WO_BORDER][r, c] = self.getValidRandPos() else: self.mPosMap[self.WO_BORDER][r, c] = [r, c] # self.mPosMap[self.W_BORDER] = np.pad(np.zeros(self.mPosMap[self.WO_BORDER].shape), ((self.borderSizePosMap, self.borderSizePosMap), (self.borderSizePosMap, self.borderSizePosMap), (0, 0),(0,0)), 'edge') self.mPosMap[self.W_BORDER] = cv2.copyMakeBorder( self.mPosMap[self.WO_BORDER], self.borderSizePosMap, self.borderSizePosMap, self.borderSizePosMap, self.borderSizePosMap, cv2.BORDER_REFLECT) # self.mPosMap[self.W_BORDER] = np.pad(self.mPosMap[self.WO_BORDER], ((self.borderSizePosMap, self.borderSizePosMap), (self.borderSizePosMap, self.borderSizePosMap), (0, 0),(0,0)), 'edge') self.mPosMap[self.WO_BORDER] = np.array( self.mPosMap[self.W_BORDER][1:self.color.shape[0] + 1, 1:self.color.shape[1] + 1])
def pad(img, padding, value=0, borderType=cv2.BORDER_CONSTANT): ''' Based on `cv2.copyMakeBorder(src, top, bottom, left, right, borderType, value)` ''' return cv2.copyMakeBorder(img, padding[0], padding[1], padding[2], padding[3], borderType, value=value)
checkpoint = torch.load(path.join(models_folder, snapshot_name)) model.load_state_dict(checkpoint['state_dict']) print("loaded checkpoint '{}' (epoch {}, dice {})".format( snapshot_name, checkpoint['epoch'], checkpoint['best_score'])) model.eval() other_outputs = [] with torch.no_grad(): for f in tqdm(listdir(test_dir)): if '.png' in f: img = cv2.imread(path.join(test_dir, f), cv2.IMREAD_COLOR) img2 = cv2.imread(path.join(test_dir2, f), cv2.IMREAD_COLOR) img3 = cv2.imread(path.join(test_dir3, f), cv2.IMREAD_COLOR) img = np.concatenate([img, img2, img3], axis=2) img = cv2.copyMakeBorder(img, 14, 14, 14, 14, cv2.BORDER_REFLECT_101) inp = [] inp.append(img) inp = np.asarray(inp, dtype='float') inp = preprocess_inputs(inp) inp = torch.from_numpy(inp.transpose((0, 3, 1, 2))).float() inp = Variable(inp).cuda() nadir, cat_inp, coord_inp = parse_img_id(f) nadir = torch.from_numpy(np.asarray([nadir / 60.0 ]).copy()).float() cat_inp = torch.from_numpy(cat_inp.copy()[np.newaxis,
def get_cascade_from_array(InputMap, NumberLevels, BandpassFilter2D, CentreWaveLengths, zerothr = None, zeroPadding = 0, squeeze=True, verbose=0, doplot=0): if InputMap.ndim == 2: InputMap = InputMap[None,:,:] FFTSize = InputMap.shape[1] Nyquest = FFTSize/2 nrOfFields = InputMap.shape[0] Cascade = np.zeros((nrOfFields,FFTSize,FFTSize,NumberLevels)) CascadeMean = np.zeros((nrOfFields,NumberLevels)) CascadeStd = np.zeros((nrOfFields,NumberLevels)) for i in xrange(nrOfFields): thisInputMap = InputMap[i,:,:].copy() # Zero padding if zeroPadding > 0: thisInputMap = cv2.copyMakeBorder(thisInputMap,zeroPadding,zeroPadding,zeroPadding,zeroPadding,cv2.BORDER_CONSTANT,0) # Calculate the FFT fftNoShift = np.fft.fft2(thisInputMap) # For each level, filter the transform and place the data into the cascade if (verbose==1) and (n==0): print('Applying the filters',end="") stdout.flush() for Level in xrange(NumberLevels): if verbose==1: print('.',end="") stdout.flush() FFTPassedOut = np.zeros_like(fftNoShift) # Extract the filter for the given level Filter = BandpassFilter2D[Level,:,:].copy() # Apply the filter FFTPassedOut = fftNoShift * Filter # Calculate the inverse ff new_image = np.real(np.fft.ifft2(FFTPassedOut)) # Crop the zero edges if zeroPadding > 0: new_image = new_image[zeroPadding:-zeroPadding,zeroPadding:-zeroPadding] if zerothr is None: CascadeMean[i,Level] = new_image.mean() CascadeStd[i,Level] = new_image.std() Cascade[i,:,:,Level] = (new_image - CascadeMean[i,Level]) / CascadeStd[i,Level] else: wet_pixels = new_image > zerothr CascadeMean[i,Level] = new_image[wet_pixels].mean() CascadeStd[i,Level] = new_image[wet_pixels].std() new_image = (new_image - CascadeMean[i,Level]) / CascadeStd[i,Level] # new_image[~wet_pixels] = new_image[wet_pixels].min() Cascade[i,:,:,Level] = new_image # The k level is the residuals k = NumberLevels-1 CascadeSum = np.zeros(InputMap[i,:,:].shape) for LevelA in xrange(NumberLevels): if LevelA!=k: CascadeSum += Cascade[i,:,:,LevelA]*CascadeStd[i,LevelA] + CascadeMean[i,LevelA] Residual = InputMap[i,:,:] - CascadeSum CascadeMean[i,k] = Residual.mean() CascadeStd[i,k] = Residual.std() Cascade[i,:,:,k] = (Residual - CascadeMean[i,k]) / CascadeStd[i,k] if (doplot==1): vmaxorig = InputMap.max() nrows = np.ceil(NumberLevels/4) + 1 plt.subplot(nrows,4,1) plt.title('Original image') plt.imshow(InputMap[0,:,:],interpolation='none',vmin=0,vmax=vmaxorig) cbar = plt.colorbar() # cbar.set_label('dBZ') plt.axis('off') CascadeSum = np.zeros(InputMap[0,:,:].shape) for LevelA in xrange(NumberLevels): if LevelA!=k: CascadeSum += Cascade[0,:,:,LevelA]*CascadeStd[0,LevelA] + CascadeMean[0,LevelA] plt.subplot(nrows,4,2) plt.title('Sum of levels 0 to %i' % (NumberLevels-2)) plt.imshow(CascadeSum,interpolation='none',vmin=0,vmax=vmaxorig) cbar=plt.colorbar() # cbar.set_label('dBZ') plt.axis('off') recon_image = np.zeros(InputMap[0,:,:].shape) for nl in xrange(NumberLevels): plt.subplot(nrows,4,5+nl) plt.title('Level %i (%i km)' % (nl, CentreWaveLengths[nl])) if nl==NumberLevels-1: plt.title('Level %i (%i km + residuals)' % (nl, CentreWaveLengths[nl])) nlevel = Cascade[0,:,:,nl].copy()*CascadeStd[0,nl] + CascadeMean[0,nl] recon_image += nlevel vmax = np.percentile(nlevel,99.0) vmin = np.percentile(nlevel,1.0) plt.imshow(nlevel,vmin=vmin,vmax=vmax,interpolation='none') # plt.imshow(Cascade[:,:,nl],vmin=-5,vmax=5,interpolation='none') cbar = plt.colorbar() # cbar.set_label('dBZ') plt.axis('off') plt.subplot(nrows,4,3) plt.title('Reconstructed image + residuals') plt.imshow(recon_image,interpolation='nearest',vmin=0,vmax=vmaxorig) cbar = plt.colorbar() # cbar.set_label('dBZ') plt.axis('off') # plt.show() plt.savefig('cascade_decomposition.pdf') print('Saved: cascade_decomposition.pdf') if nrOfFields == 1 and squeeze: Cascade = Cascade[0,:,:,:] CascadeMean = CascadeMean[0,:] CascadeStd = CascadeStd[0,:] return Cascade,CascadeMean,CascadeStd
def reflect_border(self, image, b=12): return cv2.copyMakeBorder(image, b, b, b, b, cv2.BORDER_REFLECT)
def get_cascade_from_stack(dbzStack, NumberLevels, BandpassFilter2D, CentreWaveLengths, zerothr = None, zeroPadding = 0, verbose=0, doplot=0): nrOfFields = len(dbzStack) cascadeStack = [] cascadeMeanStack = [] cascadeStdStack = [] for n in xrange(nrOfFields): InputMap = dbzStack[n].copy() FFTSize = InputMap.shape[0] Nyquest = FFTSize/2 Cascade = np.zeros((FFTSize,FFTSize,NumberLevels)) CascadeMean = np.zeros(NumberLevels) CascadeStd = np.zeros(NumberLevels) # Zero padding if zeroPadding > 0: InputMap = cv2.copyMakeBorder(InputMap,zeroPadding,zeroPadding,zeroPadding,zeroPadding,cv2.BORDER_CONSTANT,0) # Calculate the FFT fftNoShift = np.fft.fft2(InputMap) # For each level, filter the transform and place the data into the cascade if (verbose==1) and (n==0): print('Applying the filters',end="") stdout.flush() for Level in xrange(NumberLevels): if verbose==1: print('.',end="") stdout.flush() FFTPassedOut = np.zeros_like(fftNoShift) # Extract the filter for the given level Filter = BandpassFilter2D[Level,:,:].copy() # Apply the filter FFTPassedOut = fftNoShift * Filter # Calculate the inverse ff new_image = np.real(np.fft.ifft2(FFTPassedOut)) # Crop the zero edges if zeroPadding > 0: new_image = new_image[zeroPadding:-zeroPadding,zeroPadding:-zeroPadding] if zerothr is None: CascadeMean[Level] = new_image.mean() CascadeStd[Level] = new_image.std() Cascade[:,:,Level] = (new_image - CascadeMean[Level]) / CascadeStd[Level] else: wet_pixels = new_image > zerothr CascadeMean[Level] = new_image[wet_pixels].mean() CascadeStd[Level] = new_image[wet_pixels].std() new_image = (new_image - CascadeMean[Level]) / CascadeStd[Level] # new_image[~wet_pixels] = new_image[wet_pixels].min() Cascade[:,:,Level] = new_image if zeroPadding > 0: InputMap = InputMap[zeroPadding:-zeroPadding,zeroPadding:-zeroPadding] # The k level is the residuals k = NumberLevels-1 CascadeSum = np.zeros((FFTSize,FFTSize)) for LevelA in xrange(NumberLevels): if LevelA!=k: CascadeSum += Cascade[:,:,LevelA]*CascadeStd[LevelA] + CascadeMean[LevelA] Residual = InputMap - CascadeSum CascadeMean[k] = Residual.mean() CascadeStd[k] = Residual.std() Cascade[:,:,k] = (Residual - CascadeMean[k]) / CascadeStd[k] if (doplot==1) and (n==(nrOfFields-1)): fsize = 14 if NumberLevels<3: ncols=2 elif NumberLevels<7: ncols=3 else: ncols=4 nrows = np.ceil(NumberLevels/ncols) #+ 1 plt.close() plt.figure(figsize=(5*ncols, 4.9*nrows)) # plt.subplot(nrows,4,1) # plt.title('Original image') # plt.imshow(InputMap,interpolation='none',vmin=0,vmax=45) # cbar = plt.colorbar() # cbar.set_label('dBZ') # plt.axis('off') # CascadeSum = np.zeros(InputMap.shape) # for LevelA in xrange(NumberLevels): # if LevelA!=k: # CascadeSum += Cascade[:,:,LevelA]*CascadeStd[LevelA] + CascadeMean[LevelA] # plt.subplot(nrows,4,2) # plt.title('Sum of levels 0 to %i' % (NumberLevels-2)) # plt.imshow(CascadeSum,interpolation='none',vmin=0,vmax=45) # cbar=plt.colorbar() # cbar.set_label('dBZ') # plt.axis('off') recon_image = np.zeros(InputMap.shape) for nl in xrange(NumberLevels): plt.subplot(nrows,ncols,1+nl) plt.title('(%s) Level %i (%i km)' % (chr(97+nl),nl, CentreWaveLengths[nl]),fontsize=fsize) if nl==NumberLevels-1: plt.title('(%s) Level %i (%i km + residuals)' % (chr(97+nl),nl, CentreWaveLengths[nl]),fontsize=fsize) nlevel = Cascade[:,:,nl].copy()*CascadeStd[nl] + CascadeMean[nl] recon_image += nlevel vmax = np.percentile(nlevel,99.0) vmin = np.percentile(nlevel,1.0) ax = plt.gca() im = ax.imshow(nlevel,vmin=vmin,vmax=vmax,interpolation='none') plt.axis('off') # plt.imshow(Cascade[:,:,nl],vmin=-5,vmax=5,interpolation='none') divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) cbar = plt.colorbar(im, cax=cax) cbar.set_label('dBZ',fontsize=fsize) # plt.subplot(nrows,4,3) # plt.title('Reconstructed image + residuals') # plt.imshow(recon_image,interpolation='nearest',vmin=0,vmax=45) # cbar = plt.colorbar() # cbar.set_label('dBZ') # plt.axis('off') # plt.show() plt.tight_layout() plt.savefig('fig_cascade_decomposition_%ilevels.pdf' % NumberLevels) print('Saved: fig_cascade_decomposition_%ilevels.pdf' % NumberLevels) # add to the stack cascadeStack.append(Cascade) cascadeMeanStack.append(CascadeMean) cascadeStdStack.append(CascadeStd) if verbose==1: print(' DONE!') return cascadeStack, cascadeMeanStack, cascadeStdStack
#Check frames and convert to grayscale: if np.size(np.shape(frame)) >= 2: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert frame to grayscale else: print('Corrupt frame with less than 2 dimensions!!') gray = np.zeros((width, height)) # Fill the corrupt frame with black # Pad frame to make it square adding "pad" black pixels to the bottom or to the right (frame,top,bottom,left,right) if height == width: gray2 = gray if height < width: gray2 = cv2.copyMakeBorder(gray, 0, pad, 0, 0, cv2.BORDER_CONSTANT, value=[0, 0, 0]) if height > width: gray2 = cv2.copyMakeBorder(gray, 0, 0, 0, pad, cv2.BORDER_CONSTANT, value=[0, 0, 0]) #Resize frame to newSize if any of the dimensions is different from newSize: if newSize[0] != height or newSize[0] != width: rs = cv2.resize(gray2, (newSize[0], newSize[1]))
cv2.destroyAllWindows() #%% Resimlerde cerceveleme import matplotlib.pyplot as plt color = cv2.imread("./images/Logo2.png") cv2.imshow("Original Image", color) green = [0,255,0] red = [0,0,255] border1 = cv2.copyMakeBorder(color,15,15,15,15,cv2.BORDER_WRAP) border2 = cv2.copyMakeBorder(color,15,15,15,15,cv2.BORDER_REFLECT) border3 = cv2.copyMakeBorder(color,15,15,15,15,cv2.BORDER_REFLECT101) border4 = cv2.copyMakeBorder(color,15,15,15,15,cv2.BORDER_REPLICATE) border5 = cv2.copyMakeBorder(color,15,15,15,15,cv2.BORDER_CONSTANT,value = green) border6 = cv2.copyMakeBorder(color,15,15,15,15,cv2.BORDER_ISOLATED,value =red) image_dict = {"wrap": border1,"reflect": border2, "reflect101":border3,"replicate":border4,"constant":border5, "isolated":border6} i=1 for key,value in image_dict.items(): plt.subplot(2,3,i) plt.imshow(value) plt.title(key) plt.axis("off")
img = cv2.imread(image) images.append(img) cv2.imshow("Image", img) cv2.waitKey(0) imageStitcher = cv2.Stitcher_create() error, stitched_img = imageStitcher.stitch(images) if not error: cv2.imwrite("stitchedOutput.png", stitched_img) cv2.imshow("Stitched Img", stitched_img) cv2.waitKey(0) stitched_img = cv2.copyMakeBorder(stitched_img, 10, 10, 10, 10, cv2.BORDER_CONSTANT, (0, 0, 0)) gray = cv2.cvtColor(stitched_img, cv2.COLOR_BGR2GRAY) thresh_img = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1] cv2.imshow("Threshold Image", thresh_img) cv2.waitKey(0) contours = cv2.findContours(thresh_img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours = imutils.grab_contours(contours) areaOI = max(contours, key=cv2.contourArea) mask = np.zeros(thresh_img.shape, dtype="uint8") x, y, w, h = cv2.boundingRect(areaOI)
str(i) + "a", "False" ])) fout.write("\n") if result['exists_in_rdw'] == "Exists" and result[ 'confidence'] > 80: confident_results_found = True # process image image = cv2.imread(img_file) screenCnt = get_edges(image) if screenCnt is None: print("No 4-cornered contour found") else: warp = process_rect(screenCnt) warp_padded = cv2.copyMakeBorder(warp, 50, 50, 50, 50, cv2.BORDER_CONSTANT) cv2.imwrite('warped.png', warp_padded) # alpr results = recognize_image('warped.png') for i, result in enumerate(results): if len(result['plate_nr']) == 6: results_found = True result = request_rdw(result) fout.write(",".join([ os.path.basename(video_file)[:-4], "True", os.path.basename(img_file), result['plate_nr'], str(result['confidence']), result['exists_in_rdw'], result['color'], result['brand'], str(i), "True"
def detect(): out, source, weights, view_img, save_txt, imgsz = \ opt.output, opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size webcam = source == '0' or source.startswith('rtsp') or source.startswith( 'http') or source.endswith('.txt') gap = opt.gap # Initialize set_logging() device = select_device(opt.device) # if os.path.exists(out): # shutil.rmtree(out) # delete output folder # os.makedirs(out) # make new output folder half = device.type != 'cpu' # half precision only supported on CUDA # Load model model = attempt_load(weights, map_location=device) # load FP32 model imgsz = check_img_size(imgsz, s=model.stride.max()) # check img_size if half: model.half() # to FP16 # Get names and colors names = model.module.names if hasattr(model, 'module') else model.names colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] # imglist = glob.glob(f'{source}/*.tif') imglist = [source] font = cv2.FONT_HERSHEY_SIMPLEX # 定义字体 font_size = 1 frame_size = imgsz - gap t0 = time.time() # get class_dict from txt f = open('./data/cls_dict.txt') data = f.readlines() cls_dict = [] for cls in data: cls = cls.replace('\n', '').strip() if cls == '': break cls_dict.append(cls) for j, imgPath in tqdm.tqdm(enumerate(imglist)): image_name = os.path.split(imgPath)[-1].split('.')[0] image = cv2.imread(imgPath, cv2.IMREAD_COLOR) dataset = gdal.Open(imgPath) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) raw_h, raw_w = image.shape[:2] row = raw_h // frame_size + 1 col = raw_w // frame_size + 1 radius_h = row * frame_size - raw_h radius_w = col * frame_size - raw_w image = cv2.copyMakeBorder(image, 0, radius_h, 0, radius_w, cv2.BORDER_REFLECT) image = cv2.copyMakeBorder(image, 0, gap, 0, gap, cv2.BORDER_REFLECT) boxes, scores = [], [] for i in tqdm.tqdm(range(row)): for j in range(col): image1 = image.copy() subImg = image1[i * frame_size:(i + 1) * frame_size + gap, j * frame_size:(j + 1) * frame_size + gap, :] subImg_ = subImg.copy() subImg = subImg.astype(np.float32) subImg /= 255.0 # 0 - 255 to 0.0 - 1.0 subImg = np.transpose(subImg, (2, 0, 1)) subImg = Variable(torch.from_numpy(np.array([subImg])).cuda()) subImg = subImg.half() if half else subImg.float() # Inference t1 = time_synchronized() pred = model(subImg, augment=opt.augment)[0] # Apply NMS preds = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms) try: for pred in preds[0]: pred = pred.cpu().numpy() pred[:4] = pred[:4].astype(np.int32).clip(min=0, max=imgsz - 1) pred[0] = pred[0] + j * frame_size pred[1] = pred[1] + i * frame_size pred[2] = pred[2] + j * frame_size pred[3] = pred[3] + i * frame_size boxes.append([ pred[0], pred[1], pred[2], pred[3], pred[5].astype(np.int32) ]) scores.append(pred[4]) # cv2.rectangle(subImg_, (int(pred[0]), int(pred[1])), (int(pred[2]), int(pred[3])), (0, 0, 255), 3) # text_location = (int(pred[0]) + 2, int(pred[1]) - 4) # subImg_ = cv2.putText(subImg_, f'garbage {pred[4] * 100:.2f}%', text_location, font, # fontScale=0.5, color=(0, 0, 255)) # plt.imshow(subImg_) # plt.show() except: continue # 丢弃原图像边界外的框 boxes, scores = np.array(boxes), np.array(scores) keep = (boxes[:, 0] < raw_w) & (boxes[:, 1] < raw_h) boxes = boxes[keep] scores = scores[keep] assert len(boxes) == len(scores), print( f'length of boxes :{len(boxes)}, length of scores :{len(scores)}') boxes, scores = nms(boxes, scores, opt.iou_thres) boxes_, scores_, clss_ = [], [], [] for box, score in zip(boxes, scores): # with open(f"{out}/{image_name}.txt", 'a+') as f: # f.write(f"{score} {int(box[0])} {int(box[1])} {int(box[2])} {int(box[3])} \n") xmin, ymin = imagexy2geo(dataset, int(box[0]), int(box[1])) xmax, ymax = imagexy2geo(dataset, int(box[2]), int(box[3])) boxes_.append([xmin, ymin, xmax, ymax]) scores_.append(int(score * 100)) clss_.append(box[4]) cv2.rectangle(image, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (0, 0, 255), 2) text_location = (int(box[0]) + 2, int(box[1]) - 4) image = cv2.putText(image, f'{score * 100:.2f}%', text_location, font, fontScale=0.5, color=(0, 0, 255)) # plt.imshow(image) # plt.show() # cv2.imwrite(os.path.join(out, f'{image_name}.tif'), image) # results2shp # imagexy2shp(imgPath, f"{out}/{image_name}.shp", boxes_, scores_) imagexy2shp(imgPath, out, boxes_, scores_, clss_, cls_dict) print('Done. (%.3fs)' % (time.time() - t0))
import cv2 #import math from matplotlib import pyplot as plt imageFileName = input("enter the image name with absolute path:\n ") image = cv2.imread(imageFileName, 0) height, width = image.shape print ('height:\n', height) print ('width:\n', width) # print 'channels:\n', channels m = 3 n = 3 print ('The size of the filter is: %d * %d\n' % (3, 3)) pad = int((n - 1) / 2) print ('pad:\n', pad) image_org = cv2.copyMakeBorder(image, pad, pad, pad, pad, cv2.BORDER_CONSTANT) m_new = m//2 n_new = n//2 ind = range(-m_new, m_new + 1) sobel_filter = np.asarray(np.meshgrid(ind, ind)) sobel_filter_hor = sobel_filter[1] sobel_filter_ver = sobel_filter[0] sobel_filter_hor[:, m_new] = 2*sobel_filter_hor[:, m_new] sobel_filter_ver[n_new, :] = 2*sobel_filter_ver[n_new, :] def sobel_filter_horizontal(): Blur_horizontal = np.zeros((height, width), int) for y in np.arange(pad, height - 1): for x in np.arange(pad, width - 1): mod = image_org[y - pad:y + pad + 1, x - pad:x + pad + 1] k = (mod * sobel_filter_hor).sum()
top = dh // 2 bottom = dh - top elif w < longest_edge: dw = longest_edge - w left = dw // 2 right = dw - left else: pass BLACK = [0] # 给图像增加边界,是图片长、宽等长,cv2.BORDER_CONSTANT指定边界颜色由value指定 constant = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=BLACK) # 调整图像大小并返回 image = cv2.resize(constant, (IMAGE_SIZE, IMAGE_SIZE)) img_test = np.reshape(image, (1, IMAGE_SIZE * IMAGE_SIZE)) testDataS = pca.transform(img_test) # 降维测试数据 result = svm(trainDataS, Label, testDataS) # 使用SVM进行分类 # result = knn(5, trainDataS, Label, testDataS) # 使用KNN进行分类,5为最近邻居数 faceID = result[0] #如果是“我” if faceID == 1: cv2.rectangle(frame, (x - 10, y - 10),