def match_2(parentdir, targetimg, templateimg, _type='demo', denoise=True): """ :param parentdir: the directory to save the image :param targetimg: target -> bg image :param templateimg: template -> block :param _type: demo or login or register :param denoise: True or False :return: distance to the left of the north """ target = cv2.imread(targetimg, 0) template = cv2.imread(templateimg, 0) # image_arr_rgb.shape # (H, W, C) w, h = template.shape[::-1] temp = parentdir + 'temp_gray_' + _type + '.png' targ = parentdir + 'targ_gray_' + _type + '.jpg' cv2.imwrite(temp, template) cv2.imwrite(targ, target) template = cv2.imread(temp) target = cv2.imread(targ) if denoise: import utils noisename = parentdir + 'temp_denoise_' + _type + '.png' utils.denoise(temp, 15, noisename) # noisename = cv1.mean_blur(noisename) template = cv2.imread(noisename) result = cv2.matchTemplate(target, template, cv2.TM_CCOEFF_NORMED) # result.shape形状的矩阵,第result.argmax()个元素的展开的坐标,第一个参数是一种整数数组,其元素是维度数组dims的扁平版本的索引。 # 例子: # Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element ? # >>> print np.unravel_index(100,(6,7,8)) # (1, 5, 4) # 解释: # 给定一个矩阵,shape=(6,7,8),即3维的矩阵,求第n个元素的下标是什么?矩阵各维的下标从0开始 # 如果indices参数是一个标量,那么返回的是一个向量,维数=矩阵的维数,向量的值其实就是在矩阵中对应的下标。如6*7*8*9的矩阵, # 1621/(7*8*9)=3,(1621-3*7*8*9)/(8*9)=1,(1621-3*7*8*9-1*8*9)/9=4,(1621-3*7*8*9-1*8*9-4*9)=1。 # 所以返回的向量为array(3,1,4,1) # 如果indices参数是一个向量的,那么通过该向量中值求出对应的下标。下标的个数就是矩阵的维数,每一维下标组成一个向量, # 所以返回的向量的个数=矩阵维数。如7*6的矩阵,第22个元素是 3*6+4,所以对应的下标是(3,4),那么返回的值是 array([3]),array([4] # --------------------- # 原文:https://blog.csdn.net/dn_mug/article/details/70256109 x, y = np.unravel_index(result.argmax(), result.shape) # shape:(H,W) # 展示圈出来的区域 cv2.rectangle(target, (y, x), (y + w, x + h), (7, 249, 151), 2) cv2.imshow('Show', target) cv2.waitKey(0) cv2.destroyAllWindows() return y
def parse_document(struct_table, img): denoise_img = denoise(img) invoice_units = parse_invoice_units(struct_table, denoise_img) # total_n = len(y_coords)-1 # total_amount_without_tax = get_cell_digits(5,total_n, table_coords, denoise_img) # total_amount_tax = get_cell_digits(8,total_n, table_coords, denoise_img) # total_amount_with_tax = get_cell_digits(9,total_n, table_coords, denoise_img) # print(total_amount_without_tax,total_amount_tax,total_amount_with_tax) x_table = min([x['cell'][0] for x in struct_table]) y_table = min([x['cell'][1] for x in struct_table]) invoice_data = None if y_table > 100: invoice_header_cell = denoise_img[0:y_table, x_table:denoise_img.shape[1] // 2] x, y, w, h = invoice_image_rect(invoice_header_cell) invoice_header_cell = denoise_img[y:y + h, x:x + w] if DEBUG: cv2_imshow(invoice_header_cell) try: invoice_data = get_invoice_data(invoice_header_cell) except Exception as E: logger.info('Не определен заголовок фактуры') logger.exception(E) else: logger.info('Не определен заголовок фактуры') return invoice_data, invoice_units
def match_4_for_crawler(parentdir, targetimg, templateimg, _type, i, denoise=True): template = cv2.imread(templateimg, 0) target = cv2.imread(targetimg, 0) # 获取图像的长和宽 w, h = template.shape[::-1] temp = parentdir + 'temp_gray_' + _type + '.png' targ = parentdir + 'targ_gray_' + _type + '.jpg' cv2.imwrite(temp, template) cv2.imwrite(targ, target) # template = abs(255 - template) # cv2.imwrite(temp, template) template = cv2.imread(temp) target = cv2.imread(targ) if denoise: import utils noisename = parentdir + 'temp_denoise_' + _type + '.png' # 15不错 utils.denoise(temp, 13, noisename) # noisename = cv1.mean_blur(noisename) template = cv2.imread(noisename) # 模板匹配 result = cv2.matchTemplate(target, template, cv2.TM_CCOEFF_NORMED) x, y = np.unravel_index(result.argmax(), result.shape) t = cv2.imread(targetimg, 1) # 展示圈出来的区域 cv2.rectangle(t, (y, x), (y + w, x + h), (0, 0, 255), 2) font = cv2.FONT_HERSHEY_SIMPLEX # 定义字体 cv2.putText(t, 'notch 1.000', (y, x - 2), font, 0.4, (255, 0, 0), 1) # 图像,文字内容, 坐标 ,字体,大小,颜色,字体厚度 # cv2.imshow('Show', t) cv2.imwrite(parentdir + 'mark/target' + i + _type + '.jpg', t) cv2.waitKey(0) cv2.destroyAllWindows() return y
def wrapper(func, frame_no, partition_number, coords, img_frame, ret_list): img_frame = utils.denoise(img_frame) map_ret = func(coords, img_frame) obj = { 'fno': frame_no, 'pno': partition_number, 'coords': coords, 'return': map_ret, # 'd_frame': img_frame } print(map_ret) ret_list.append(obj)
if ret_value == False: break # frame = imutils.resize(frame, width=700) clone = frame[:, :] # get the height and width of the frame (height, width) = frame.shape[:2] # convert the roi to grayscale and blur it gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (7, 7), 0) thresh = cv2.erode(gray, None, iterations=2) thresh = cv2.dilate(thresh, None, iterations=4) # calibrating our running average until a threshold is reached denoised_clone = utils.denoise(clone) start_time = time() if num_frames < 6: running_avg(thresh, avg_wt) else: seg_img = segment(thresh) if seg_img is not None: # i.e. if segmented (thresholded, segmented) = seg_img # print("Center", center) if num_frames < 50: selected_partitions = utils.segment_divider(thresholded)
from PIL import Image from pylab import * import utils im = array(Image.open('wisma-nusantara.jpg').convert('L')) U, T = utils.denoise(im, im) figure() gray() imshow(U) axis('equal') axis('off') show()
sr_gen = generator() sr_gen.load_weights('weight/srgan/gan_generator.h5') # Load model 2 -- Illumination improvement mbllen_gen = Network.build_mbllen((32, 32, 3)) mbllen_gen.load_weights('weight/mbllen/LOL_img_lowlight.h5') # Load test image img = readImage(r'C:\Users\ywqqq\Documents\PRS_prj\maskdetection\test.jpg') # If noise in image, denoise. gaussian_noise = False salt_and_pepper_noise = False if gaussian_noise: img = denoise(img, 'gaussian') if salt_and_pepper_noise: img = denoise(img, 'salt-and-pepper') # Get the luminance of the image. # If luminance < 70, then apply illumination improvemtn imgHSV = cv2.cvtColor(img, cv2.COLOR_RGB2HSV) H, S, V = cv2.split(imgHSV) if V < 70: img = resolve_single(mbllen_gen, 'mbllen', img) # Get the resolution of the image. # If the resolution < 10000, then apply super resolution r = img.shape[0] * img.shape[1]
data = data.reshape(-1, 784) return data def load_label(file_name): file_path = dataset_dir + '/' + file_name with gzip.open(file_path, 'rb') as f: labels = np.frombuffer(f.read(), np.uint8, offset=8) return labels dataset = {} dataset['train_img'] = load_img(key_file['train_img']) dataset['train_label'] = load_label(key_file['train_label']) dataset['test_img'] = load_img(key_file['test_img']) dataset['test_label'] = load_label(key_file['test_label']) import scipy.misc import scipy as sp for i in range(len(dataset['test_img'])): sp.misc.imsave('./datasets/mnist_test_image/test' + str(i) + '.jpg', dataset['test_img'][i].reshape(28, 28)) testdatasets = glob.glob("./datasets/mnist_test_image/*") for i in testdatasets: denoise(i)
def match_3(parentdir, targetimg, templateimg, _type="demo", denoise=True): """ :param parentdir: :param _type: demo or login or regist :param denoise: True or False :param targetimg: path of the bg image :param templateimg: path of the slide block :return: distance """ # 参考https://blog.csdn.net/weixin_42081389/article/details/87935735 # a=np.array([[2,3,4,5],[5,6,78,9],[1,3,4,5]]) # print(a) # # 函数功能:假设有一个矩阵a,现在需要求这个矩阵的最小值,最大值,并得到最大值,最小值的索引,注意是先x后y,先列之间距离,再横向行距。 # min_val,max_val,min_indx,max_indx=cv2.minMaxLoc(a) # print(min_val,max_val,min_indx,max_indx) img = cv2.imread(targetimg, 0) # img2 = img.copy() template = cv2.imread(templateimg, 0) temp = parentdir + 'temp_gray_' + _type + '.png' targ = parentdir + 'targ_gray_' + _type + '.jpg' cv2.imwrite(targ, img) # cv2.imshow('messi', img) # cv2.imshow('face', template) # cv2.waitKey(0) # cv2.destroyAllWindows() # 也可以写作: h, w = template.shape[:2],因为shape是(H,W,C) w, h = template.shape[::-1] # All the 6 methods for comparison in a list # methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', # 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED'] # 这个要不要翻转颜色很关键,试试就知道 # template = abs(255 - template) meth = 'cv2.TM_CCOEFF_NORMED' # img = img2.copy() ''' exec可以用来执行储存在字符串文件中的python语句 例如可以在运行时生成一个包含python代码的字符串 然后使用exec语句执行这些语句 eval语句用来计算存储在字符串中的有效python表达式 ''' cv2.imwrite(temp, template) target = cv2.imread(targ) template = cv2.imread(temp) method = eval(meth) if denoise: import utils noisename = parentdir + 'temp_denoise_' + _type + '.png' utils.denoise(temp, 15, noisename) # noisename = cv1.mean_blur(noisename) template = cv2.imread(noisename) # 匹配应用 res = cv2.matchTemplate(target, template, method) # print(res) # print(np.array(res)) # print("res.shape: ", res.shape) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # 使用不同的方法,对结果的解释不同 # 方法判断 # if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: # top_left = min_loc # else: # top_left = max_loc # 最大值的坐标就是距离左边界和上边界的距离 top_left = max_loc # print('偏移像素', top_left[0]) bottom_right = (top_left[0] + w, top_left[1] + h) cv2.rectangle(img, top_left, bottom_right, 255, 2) cv2.imshow('Show', img) 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('method: ' + meth) plt.savefig(parentdir + 'mark3/target_res.jpg') plt.show() # cv2.imwrite(parentdir + 'mark3/target' + i + _type + '.jpg', img) cv2.imwrite(parentdir + 'mark3/target' + _type + '.jpg', img) cv2.waitKey(0) cv2.destroyAllWindows() return top_left[0]