def search_roi(): roi = util.load_img('img/roi.png') roi_hsv = util.hsv(roi) tar = util.load_img('img/tar.png') tar_hsv = util.hsv(tar) # 计算目标直方图 颜色直方图优于灰度直方图 roihist = cv.calcHist([roi_hsv], [0, 1], None, [180, 256], [0, 180, 0, 256]) # 对象直方图进行normalize cv.calcBackProject返回概率图像 cv.normalize(roihist, roihist, 0, 255, cv.NORM_MINMAX) dst = cv.calcBackProject([tar_hsv], [0, 1], roihist, [0, 180, 0, 256], 1) # 与disc kernel卷积 disc = cv.getStructuringElement(cv.MORPH_ELLIPSE, (5, 5)) cv.filter2D(dst, -1, disc, dst) # threshold and binary AND ret, thresh = cv.threshold(dst, 50, 255, 0) # 使用merge变成通道图像 thresh = cv.merge((thresh, thresh, thresh)) # 蒙板 res = cv.bitwise_and(tar, thresh) res = np.hstack((tar, thresh, res)) util.show(res)
def hull_(): """ 类似于轮廓近似 凸壳 在多维空间中有一群散佈各处的点 凸包 是包覆这群点的所有外壳当中,表面积暨容积最小的一个外壳,而最小的外壳一定是凸的 凸: 图形内任意两点的连线不会经过图形外部 """ img = util.load_img('img/horse.png') convex = util.load_img('img/horse1.png') height = img.shape[0] width = img.shape[0] out = util.blank_img(width * 1.5, height, (255, 255, 255)) # 求轮廓 contours = get_cnt(img) convex_cnt = get_cnt(convex)[0] # 求凸包 cv.drawContours(out, contours, -1, (255, 0, 0), 5) cnt = contours[0] hull = cv.convexHull(cnt) # 检测一个曲线是不是凸的 print(cv.isContourConvex(cnt)) cv.drawContours(out, [hull], -1, (0, 0, 255), 5) util.show(out)
def __getitem__(self, index): # Load Image input = load_img(join(self.input_img_path, self.input_img_filenames[index])) input = self.transform(input) target = load_img(join(self.gt_img_path, self.gt_img_filenames[index])) target = self.transform(target) return input, target
def __getitem__(self, index): # Load Image input = load_img(join(self.photo_path, self.image_filenames[index])) input = self.transform(input) #.type(torch.float16) target = load_img(join(self.sketch_path, self.image_filenames[index])) target = self.transform(target) #.type(torch.float16) return input, target
def __getitem__(self, index): # Load Image input = load_img(join(self.photo_path, self.image_filenames[index]), 0) input = self.transform(input) target = load_img(join(self.sketch_path, self.image_filenames[index]), 1) target = self.transform(target) return input, target
def __getitem__(self, index): # Load Image input = load_img(join(self.inter_4_path, self.inter_4_filelist[index])) input = self.transform(input) target = load_img( join(self.high_resolution_images_path, self.inter_4_filelist[index])) target = self.transform(target) return input, target
def __getitem__(self, index): # Load Image input = load_img(join(self.photo_path, self.image_filenames[index])) input = self.transform(input) target = load_img(join(self.sketch_path, self.image_filenames[index])) target = self.transform(target) # target = io.imread(join(self.sketch_path, self.image_filenames[index])) # target = torch.Tensor(target) return input, target
def __getitem__(self, index): # Load Image content = load_img(join(self.content_path, self.image_filenames[index])) content = self.transform(content) style = load_img(join(self.style_path, self.image_filenames[index])) style = self.transform(style) target = load_img(join(self.target_path, self.image_filenames[index])) target = self.transform(target) return content, style, target
def __getitem__(self, index): base_name = self.files[index] img_file = self.root_path + '/JPEGImages/' + base_name gt_file = self.root_path + '/SegmentationClass/' + base_name realA = load_img(img_file) realA = self.transform(realA) if self.set == 'val': return base_name, realA else: realB = load_img(gt_file) realB = self.transform(realB) return realA, realB
def __getitem__(self, index): # Load Image input = load_img(join(self.input_path, self.image_filenames[index])) input = self.transform(input) output = load_img(join(self.output_path, self.image_filenames[index])) output = self.transform(output) # inputt = output.resize((16, 16), Image.BICUBIC) # input = inputt.resize((128, 128), Image.BICUBIC) return input, output
def feature(): src = util.load_img('img/s.png') # img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) ret, thresh = cv.threshold(src, 127, 255, cv.THRESH_BINARY) # cv.findContours()函数中有三个参数,第一个是源图像,第二个是轮廓检索模式,第三个是轮廓近似方法。它输出轮廓和层次结构 contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) # 第一个参数是源图像,第二个参数是轮廓,第三个参数是轮廓索引 -1是所有轮廓 其余参数是颜色,厚度 # res = cv.drawContours(src, contours, -1, (0, 255, 0), 3) # util.show(res) height = src.shape[0] width = src.shape[0] out = util.blank_img(width * 1.5, height, (255, 255, 255)) # util.show(img) for index, cnt in enumerate(contours): # 画轮廓 cv.drawContours(out, contours, index, (0, 0, 255), 1) # 质心坐标 M = cv.moments(cnt) cx = M['m10'] / M['m00'] cy = M['m01'] / M['m00'] cv.circle(out, (int(cx), int(cy)), 2, (255, 0, 0), -1) # 轮廓面积 M['m00'] area = cv.contourArea(cnt) # 轮廓周长 轮廓是否闭合True perimeter = cv.arcLength(cnt, True) print('({},{}) 面积={} 周长={}'.format(cx, cy, area, perimeter)) util.show(out)
def load_data(img_vals, width, height, channels): x = [] for img_val in img_vals: x.append(load_img(img_val, width, height, channels)) x = np.asarray(x) x /= 255 # normalized return x
def get_layer_outputs(layer_name, input_path): is_grayscale = (input_channels == 1) input_img = load_img(input_path, single_input_shape, grayscale=is_grayscale) output_generator = get_outputs_generator(model, layer_name) with get_evaluation_context(): layer_outputs = output_generator(input_img)[0] output_files = [] if keras.backend.backend() == 'theano': #correct for channel location difference betwen TF and Theano layer_outputs = np.rollaxis(layer_outputs, 0,3) for z in range(0, layer_outputs.shape[2]): img = layer_outputs[:, :, z] deprocessed = deprocess_image(img) filename = get_output_name(temp_folder, layer_name, input_path, z) output_files.append( relpath( filename, abspath(temp_folder) ) ) imsave(filename, deprocessed) return jsonify(output_files)
def handle(clientsocket): while 1: buf = clientsocket.recv(config.buffer_size) if buf == 'exit': return # client terminated connection if os.path.isfile(buf): try: out = model.predict(np.array([util.load_img(buf)])) prediction = np.argmax(out, axis=1) class_indices = dict( zip(config.classes, range(len(config.classes)))) keys = class_indices.keys() values = class_indices.values() answer = keys[values.index(prediction[0])] response = '{"probability":"%s","class":"%s"}' % ( out[0][prediction[0]], answer) print response clientsocket.sendall(response) except Exception as e: print e.message clientsocket.sendall(UNKNOWN_ERROR) else: clientsocket.sendall(str(FILE_DOES_NOT_EXIST))
def get_layer_outputs(layer_name, input_path): input_img = load_img(input_path, single_input_shape, grayscale) output_generator = get_outputs_generator(model, layer_name) with get_evaluation_context(): layer_outputs = output_generator(input_img)[0] output_files = [] print('layer_output SHAPE: ', layer_outputs.shape) for z in range(0, layer_outputs.shape[0]): img = layer_outputs[z, :, :] deprocessed = deprocess_image(img) filename = get_output_name(temp_folder, layer_name, input_path, z) output_files.append( relpath( filename, abspath(temp_folder) ) ) imsave(filename, deprocessed) return jsonify(output_files)
def __getitem__(self, index): # Load Image input = load_img(join(self.photo_path, self.image_filenames[index]), self.img_size, norm=self.norm, rgb=self.rgb, rotated=self.rotated) input = self.transform(input) if "cityscapes" in self.photo_path: #target = load_img(join(self.sketch_path, self.image_filenames[index][:-15] + 'gtFine_labelIds.png'), self.img_size, rgb=True, rotated=self.rotated, norm=self.norm) target = load_img(join(self.sketch_path, self.image_filenames[index][:-15] + 'gtFine_color.png'), self.img_size, rgb=True, rotated=self.rotated, norm=self.norm) else: target = load_img(join(self.sketch_path, self.image_filenames[index]), self.img_size, rgb=True, rotated=self.rotated, norm=self.norm) #(same name: image and label) #target = load_img(join(self.sketch_path, self.image_filenames[index][:-15] + 'gtFine_color.png'), self.img_size) #target = load_img(join(self.sketch_path, self.image_filenames[index][:-15] + 'gtFine_color.png')) if not self.val: target = self.transform(target) return input, target
def smooth(): """ 图像过滤 低通滤波LPF可以使图像去除噪声,高通滤波HPF可以找到图像的边缘。 图像平滑 内核卷积来实现图像模糊。它有助于消除噪音。实际上从图像中去除了高频内容(例如:噪声,边缘)。边缘会有点模糊。 均值过滤 调用blur()等效于调用将normalize=true的boxFilter(). 中位数 cv.medianBlur() 内核区域下所有像素的中值,并用该中值替换中心元素 双边过滤 cv.bilateralFilter() 降低噪音方面非常有效,同时保持边缘清晰。但与其他过滤器相比,操作速度较慢。 高斯滤波器采用像素周围的邻域并找到其高斯加权平均值 :return: """ img = util.load_img('img/opencv-logo-white.png') cv.blur(img, (5, 5)) blur = cv.GaussianBlur(img, (5, 5), 0) img = cv.medianBlur(img, 5) cv.blur(img, (5, 5))
def hough(): img = util.load_img('img/sudoku.png') img_gray = util.gray(img) edges = cv.Canny(img_gray, 100, 200) """cv.HoughLinesP""" lines = cv.HoughLinesP(edges, 1, np.pi / 180, 200, minLineLength=100, maxLineGap=10) for line in lines: x1, y1, x2, y2 = line[0] cv.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2) """cv.HoughLines""" # lines = cv.HoughLines(edges, 1, np.pi / 180, 200) # for line in lines: # rho, theta = line[0] # a = np.cos(theta) # b = np.sin(theta) # x0 = a * rho # y0 = b * rho # x1 = int(x0 + 1000 * (-b)) # y1 = int(y0 + 1000 * (a)) # x2 = int(x0 - 1000 * (-b)) # y2 = int(y0 - 1000 * (a)) # cv.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2) """""" util.show(img)
def main(args): if torch.cuda.is_available(): dtype = torch.cuda.FloatTensor else: dtype = torch.FloatTensor print('Loading the model...') trans_net = model.ImageTransformNet().type(dtype) trans_net = util.load_weight(model=trans_net, path=args.weight_path) print('Loading the model is done!') # content_img = (1, 3, 256, 256) content_img = util.load_img(path=args.content_path) content_img = Variable(content_img.type(dtype)) content_img = util.vgg_norm(content_img) # result_img = (1, 3, 256, 256) result_img = trans_net(content_img) # content_img = (1, 3, 256, 256) content_img = util.vgg_denorm(content_img) out_dir, _ = os.path.split(args.output_path) if os.path.exists(out_dir) is not True: os.makedirs(out_dir) torchvision.utils.save_image(result_img.data, args.output_path, nrow=1) print('Saved image : ' + args.output_path)
def onet_test(args, img_path): img = load_img(img_path) net = load_net(args, 'pnet') output = net((transforms.ToTensor()(img.resize( (12, 12), Image.BILINEAR))).unsqueeze(0)) print('prob:', output[0]) show_bboxes(img, [[(250 * t.item() + 250 * (i > 1)) for i, t in enumerate(output[1][0])]]).show()
def __getitem__(self, index): # Load Image input = load_img(join(self.fog_path, self.image_filenames[index])) input = input.convert("RGB") input = self.transform(input) target = load_img(join(self.photo_path, self.image_filenames[index])) target = target.convert("RGB") target = self.transform(target) seg = load_img(join(self.seg_path, self.image_filenames[index])) seg = seg.convert("RGB") seg = self.transform(seg) nom = self.image_filenames[index] return input, target, seg, nom
def classids(): img_dir = "dataset/spherical/" cl_dir = "dataset/spherical2/" #np_img =np.zeros((512,3,1024)) image_filenames = [x for x in os.listdir(img_dir) if is_image_file(x)] for image_name in image_filenames: np_img = load_img(img_dir + image_name, (1024, 512), rgb=True) image = encode_img_ids(np_img) #image = classify(np_img, img=False) save_img_np(decode_ids(image), cl_dir + image_name, (1024, 512))
def main(): args = handle_args() filename = args.filename rgb_img = util.load_img(filename) if np.max(rgb_img) > 1: rgb_img = rgb_img / 255 grey_img = util.rgb2gray(rgb_img) #dots_g = stippling.dot(grey_img, style='grid') #dots_v = stippling.dot(grey_img, style='voronoi', num_dots=10000) dots_cg = stippling.dot(grey_img, style='cgrid', num_dots=10000) #dots_dither = stippling.dot(grey_img, style='dithering') dots = dots_cg # fig = plt.figure("Input") # ax = fig.add_subplot(1,2,1) # stippling.show_dots(dots_cg, ax) # plt.show() print('Starting TSP') line = tsp.tsp(dots, style='rnn') print('Altering tour') line = tsp.alter_tour(line, max_len=100) if DISP: fig = plt.figure("Input") ax = fig.add_subplot(1,2,1) ax.imshow(rgb_img) ax = fig.add_subplot(1,2,2) ax.imshow(grey_img, cmap='Greys_r') fig = plt.figure("Art") ax = fig.add_subplot(1,3,1) ax.set_aspect('equal') #stippling.show_dots(dots_g, ax) ax = fig.add_subplot(1,3,2) ax.set_aspect('equal') stippling.show_dots(dots_cg, ax) ax = fig.add_subplot(1,3,3) ax.set_aspect('equal') #stippling.show_dots(dots_dither, ax) fig = plt.figure("TSP") ax = fig.add_subplot(1,1,1) ax.set_aspect('equal') postprocess(line, grey_img, 'thickness', ax) plt.show()
def temp(): """ 模板匹配 较大图像中搜索和查找模板图像位置的方法 与2D卷积一样 模板图像在输入图像上滑动(类似窗口),在每一个位置对模板图像和输入图像的窗口区域进行匹配。 与直方图的反向投影类似。 输入图像大小是W×H,模板大小是w×h,输出结果的大小(W-w+1,H-h+1)。 得到此结果后可以使用函数cv2.minMaxLoc()来找到其中的最小值和最大值的位置。第一个值为矩形左上角的位置,(w,h)是模板矩形的宽度和高度。矩形就是模板区域。 """ roi = util.load_img('img/roi.png', 0) w, h = roi.shape[::-1] tar = util.load_img('img/tar.png', 0) methods = [ 'cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', 'cv.TM_CCORR', 'cv.TM_CCORR_NORMED', 'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED' ] for meth in methods: img = roi.copy() method = eval(meth) res = cv.matchTemplate(img, tar, method) # 只匹配一个对象 min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res) if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]: # 最小值会给出最佳匹配 top_left = min_loc else: top_left = max_loc bottom_right = (top_left[0] + w, top_left[1] + h) cv.rectangle(img, top_left, bottom_right, 255, 2) plt.subplot(121), plt.imshow(res, cmap='gray') plt.title('Matching Result'), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(img, cmap='gray') plt.title('Detected Point'), plt.xticks([]), plt.yticks([]) plt.suptitle(meth) plt.show()
def cut(): img = util.load_img('img/messi5.jpg') mask = np.zeros(img.shape[:2], np.uint8) bgdModel = np.zeros((1, 65), np.float64) fgdModel = np.zeros((1, 65), np.float64) rect = (50, 50, 450, 290) cv.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv.GC_INIT_WITH_RECT) mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8') img = img * mask2[:, :, np.newaxis] plt.imshow(img), plt.colorbar(), plt.show()
def __get_embeddings(self, model): embeddings = {} for member in ALL_MEMBERS: print('Calculating embeddings for {} ...'.format(member)) embeddings[member] = [] for image_name in os.listdir(os.path.join(FACES_PATH, member)): embeddings[member].append( np.squeeze(model(util.load_img(os.path.join(FACES_PATH, member, image_name))))) return embeddings
def get_prediction(input_path): input_img = load_img(input_path, single_input_shape, grayscale) with get_evaluation_context(): return jsonify( json.loads( get_json( decode_predictions( model.predict(input_img) ) ) ) )
def his(): """ # cv.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) """ # hist = cv.calcHist([img], [0], None, [256], [0, 256]) # # hist, bins = np.histogram(img.ravel(), 256, [0, 256]) # mask = np.zeros(img.shape[:2], np.uint8) # mask[100:300, 100:400] = 255 # masked_img = cv.bitwise_and(img, img, mask=mask) # gray img = util.load_img('img/home.jpg', 0) # plt.hist(img.ravel(), 256, [0, 256]) # plt.show() mask = np.zeros(img.shape[:2], np.uint8) mask[100:300, 100:400] = 255 masked_img = cv.bitwise_and(img, img, mask=mask) hist_full = cv.calcHist([img], [0], None, [256], [0, 256]) hist_mask = cv.calcHist([img], [0], mask, [256], [0, 256]) plt.subplot(221), plt.imshow(img, 'gray') plt.subplot(222), plt.imshow(mask, 'gray') plt.subplot(223), plt.imshow(masked_img, 'gray') plt.subplot(224), plt.plot(hist_full), plt.plot(hist_mask) plt.xlim([0, 256]) plt.show() # color img = util.load_img('img/home.jpg') color = ('b', 'g', 'r') for i, col in enumerate(color): # histr = cv.calcHist([img], [i], None, [256], [0, 256]) plt.plot(histr, color=col) plt.xlim([0, 256]) plt.show()
def approx(): img = util.load_img('img/t.png') height = img.shape[0] width = img.shape[0] out = util.blank_img(width * 1.5, height, (255, 255, 255)) contours = get_cnt(img) # a = cv.drawContours(out, contours, -1, (255, 0, 0), 5) epsilon = 1 * cv.arcLength(contours[0], True) approx = cv.approxPolyDP(contours[0], epsilon, True) cv.drawContours(out, [approx], -1, (0, 0, 255), 5) util.show(out)
def water(): """ 使用距离变换和分水岭来分割相互接触的物体 靠近对象中心的区域是前景,离对象远的区域是背景,不确定的区域是边界。 物体没有相互接触/只求前景 可用侵蚀消除了边界像素 到距离变换并应用适当的阈值 膨胀操作会将对象边界延伸到背景,确保background区域只有background 边界 = 能否确认是否是背景的区域 - 确定是前景的区域 """ img = util.load_img('img/coins.png') gray = util.gray(img) ret, thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU) util.show(thresh) # noise removal kernel = np.ones((3, 3), np.uint8) opening = cv.morphologyEx(thresh, cv.MORPH_OPEN, kernel, iterations=2) # sure background area sure_bg = cv.dilate(opening, kernel, iterations=3) # Finding sure foreground area dist_transform = cv.distanceTransform(opening, cv.DIST_L2, 5) # 计算每个像素离最近0像素的距离 ret, sure_fg = cv.threshold(dist_transform, 0.7 * dist_transform.max(), 255, 0) # Finding unknown region sure_fg = np.uint8(sure_fg) unknown = cv.subtract(sure_bg, sure_fg) # Marker labelling 用0标记图像的背景 其他对象用从1开始的整数标记 ret, markers = cv.connectedComponents(sure_fg) """ 我们知道,如果背景标记为0,分水岭会将其视为未知区域。所以我们想用不同的整数来标记它。相反,我们将标记由未知定义的未知区域,为0。 """ # Add one to all labels so that sure background is not 0, but 1 markers = markers + 1 # Now, mark the region of unknown with zero markers[unknown == 255] = 0 markers = cv.watershed(img, markers) # 修改标记图像。边界区域将标记为-1 img[markers == -1] = [255, 0, 0] util.show(img, is_seq=True)
def __getitem__(self, index): # Load Image input = load_img(join(self.a_path, self.image_filenames[index])) target = load_img(join(self.b_path, self.image_filenames[index])) return input, target
parser.add_argument('--ngf', type=int, default=64, help='generator filters in first conv layer') parser.add_argument('--cuda', action='store_true', help='use cuda') opt = parser.parse_args() print(opt) # opt.input_nc, opt.output_nc, opt.ngf netG_state_dict = torch.load(opt.model) netG = G(opt.input_nc, opt.output_nc, opt.ngf) netG.load_state_dict(netG_state_dict) image_dir = "dataset/{}/val_2018/".format(opt.dataset) image_filenames = [x for x in os.listdir(image_dir) if is_image_file(x)] batchsize=2 for image_name in image_filenames: img, shape = load_img(image_dir + image_name) input_x_np = np.zeros((batchsize, 3, 128, 128)).astype(np.float32) input_x_np[0,:] = np.asarray(img[0]) input= Variable(torch.from_numpy(input_x_np)) if opt.cuda: netG = netG.cuda() input = input.cuda() out = netG(input) out = out.cpu() out_img = out.data[0]