756. 金字塔转换矩阵

772 篇文章 2 订阅

现在,我们用一些方块来堆砌一个金字塔。 每个方块用仅包含一个字母的字符串表示,例如 “Z”。

使用三元组表示金字塔的堆砌规则如下:

(A, B, C) 表示,“C”为顶层方块,方块“A”、“B”分别作为方块“C”下一层的的左、右子块。当且仅当(A, B, C)是被允许的三元组,我们才可以将其堆砌上。

初始时,给定金字塔的基层 bottom,用一个字符串表示。一个允许的三元组列表 allowed,每个三元组用一个长度为 3 的字符串表示。

如果可以由基层一直堆到塔尖返回true,否则返回false。

示例 1:

输入: bottom = "XYZ", allowed = ["XYD", "YZE", "DEA", "FFF"]
输出: true
解析:
可以堆砌成这样的金字塔:
    A
   / \
  D   E
 / \ / \
X   Y   Z

因为符合('X', 'Y', 'D'), ('Y', 'Z', 'E') 和 ('D', 'E', 'A') 三种规则。

示例 2:

输入: bottom = "XXYX", allowed = ["XXX", "XXY", "XYX", "XYY", "YXZ"]
输出: false
解析:
无法一直堆到塔尖。
注意, 允许存在三元组(A, B, C)和 (A, B, D) ,其中 C != D.

注意:

  1. bottom 的长度范围在 [2, 8]
  2. allowed 的长度范围在[0, 200]
  3. 方块的标记字母范围为{'A', 'B', 'C', 'D', 'E', 'F', 'G'}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是利用高斯金字塔实现ORB特征点提取的 Matlab 代码: ```matlab function [keypoints, descriptors] = ORB(image) % 将图像灰度化 gray_image = rgb2gray(image); % 构建高斯金字塔 num_octaves = 4; num_levels = 5; k = sqrt(2); sigma = 1.6; [gauss_pyramid, dog_pyramid] = create_pyramid(gray_image, num_octaves, num_levels, k, sigma); % 计算特征点 threshold = 0.01; radius = 3; [keypoints, responses] = detect_features(dog_pyramid, threshold, radius); % 计算描述子 patch_size = 31; descriptors = compute_descriptors(gauss_pyramid, keypoints, patch_size); end % 构建高斯金字塔和差分金字塔 function [gauss_pyramid, dog_pyramid] = create_pyramid(image, num_octaves, num_levels, k, sigma) % 将图像转换成双精度类型 image = im2double(image); % 构建高斯金字塔 gauss_pyramid = cell(num_octaves, num_levels); % 不同octave之间的sigma值是一样的,而在同一个octave中,sigma的值是不断增加的 for i = 1:num_octaves for j = 1:num_levels if i == 1 && j == 1 % 第一个金字塔的第一层就是原始图像 gauss_pyramid{i, j} = image; elseif j == 1 % 每个octave的第一层使用前一octave的最后一层下采样得到 gauss_pyramid{i, j} = imresize(gauss_pyramid{i-1, end}, 0.5, 'bilinear'); else % 计算当前层的sigma值 sigma_curr = sigma * k^((j-2)+(i-1)*num_levels); % 使用当前层的sigma值构建高斯滤波器 h = fspecial('gaussian', ceil(sigma_curr*3)*2+1, sigma_curr); % 对上一层进行高斯模糊操作 gauss_pyramid{i, j} = imfilter(gauss_pyramid{i, j-1}, h, 'replicate', 'same'); end end end % 构建差分金字塔 dog_pyramid = cell(num_octaves, num_levels-1); for i = 1:num_octaves for j = 1:num_levels-1 % 相邻两层相减得到差分金字塔 dog_pyramid{i, j} = gauss_pyramid{i, j+1} - gauss_pyramid{i, j}; end end end % 计算特征点 function [keypoints, responses] = detect_features(dog_pyramid, threshold, radius) [num_octaves, num_levels] = size(dog_pyramid); keypoints = cell(num_octaves, num_levels-3); responses = cell(num_octaves, num_levels-3); for i = 1:num_octaves for j = 2:num_levels-2 % 在当前层的3x3邻域内找到极值点 current_layer = dog_pyramid{i, j}; above_layer = dog_pyramid{i, j-1}; below_layer = dog_pyramid{i, j+1}; extrema = detect_extrema(current_layer, above_layer, below_layer, threshold, radius); keypoints{i, j-1} = extrema; % 计算响应值 [r, c, num_extrema] = size(extrema); current_responses = zeros(num_extrema, 1); for k = 1:num_extrema current_responses(k) = compute_response(current_layer, extrema(k,1), extrema(k,2)); end responses{i, j-1} = current_responses; end end % 将所有octave中的特征点合并到一起 keypoints = cell2mat(keypoints); responses = cell2mat(responses); end % 在当前层的3x3邻域内找到极值点 function extrema = detect_extrema(current_layer, above_layer, below_layer, threshold, radius) [h, w] = size(current_layer); extrema = []; for i = radius+1:h-radius for j = radius+1:w-radius current_pixel = current_layer(i, j); % 判断当前像素是否是3x3邻域内的极值点 if abs(current_pixel) > threshold && ... ((current_pixel > 0 && current_pixel == max(max(current_layer(i-radius:i+radius, j-radius:j+radius)))) || ... (current_pixel < 0 && current_pixel == min(min(current_layer(i-radius:i+radius, j-radius:j+radius))))) && ... ((current_pixel > 0 && current_pixel == max(max(above_layer(i-radius:i+radius, j-radius:j+radius)))) || ... (current_pixel < 0 && current_pixel == min(min(above_layer(i-radius:i+radius, j-radius:j+radius))))) && ... ((current_pixel > 0 && current_pixel == max(max(below_layer(i-radius:i+radius, j-radius:j+radius)))) || ... (current_pixel < 0 && current_pixel == min(min(below_layer(i-radius:i+radius, j-radius:j+radius))))) extrema = [extrema; i, j]; end end end end % 计算响应值 function response = compute_response(image, row, col) % 计算像素点周围2x2邻域内的梯度值 dx = (image(row, col+1) - image(row, col-1)) / 2; dy = (image(row+1, col) - image(row-1, col)) / 2; % 计算Hessian矩阵 dxx = image(row, col+1) - 2*image(row, col) + image(row, col-1); dyy = image(row+1, col) - 2*image(row, col) + image(row-1, col); dxy = (image(row+1, col+1) - image(row+1, col-1) - image(row-1, col+1) + image(row-1, col-1)) / 4; % 计算响应值 trace = dxx + dyy; det = dxx * dyy - dxy * dxy; response = det - 0.04 * trace^2; end % 计算描述子 function descriptors = compute_descriptors(gauss_pyramid, keypoints, patch_size) [num_keypoints, ~] = size(keypoints); descriptors = zeros(num_keypoints, 256); for i = 1:num_keypoints octave = keypoints(i, 3); level = keypoints(i, 4); scale = keypoints(i, 5); row = keypoints(i, 1); col = keypoints(i, 2); % 计算旋转角度 angle = compute_orientation(gauss_pyramid{octave, level}, row, col, scale); % 构建描述子 patch = get_patch(gauss_pyramid{octave, level}, row, col, scale, angle, patch_size); descriptors(i, :) = reshape(patch, 1, []); end end % 计算旋转角度 function angle = compute_orientation(image, row, col, scale) % 计算像素点周围16x16邻域内的梯度值和方向 dx = zeros(16, 16); dy = zeros(16, 16); orientation = zeros(16, 16); for i = -8:7 for j = -8:7 dx(i+9, j+9) = (image(row+i, col+j+1) - image(row+i, col+j-1)) / 2; dy(i+9, j+9) = (image(row+i+1, col+j) - image(row+i-1, col+j)) / 2; orientation(i+9, j+9) = atan2(dy(i+9, j+9), dx(i+9, j+9)); end end % 将方向分成36个bin,计算每个bin中的梯度值之和 hist = zeros(1, 36); for i = 1:4 for j = 1:4 hist((i-1)*9+j) = sum(sum((orientation((i-1)*4+1:i*4, (j-1)*4+1:j*4) >= (j-1)*pi/18) & ... (orientation((i-1)*4+1:i*4, (j-1)*4+1:j*4) < j*pi/18))) * ... sum(sum((orientation((i-1)*4+1:i*4, (j-1)*4+1:j*4) >= (j-1)*pi/18) & ... (orientation((i-1)*4+1:i*4, (j-1)*4+1:j*4) < j*pi/18)) .* ... sqrt(dx((i-1)*4+1:i*4, (j-1)*4+1:j*4).^2 + dy((i-1)*4+1:i*4, (j-1)*4+1:j*4).^2)); end end % 找到最大值对应的bin,作为旋转角度 [~, max_bin] = max(hist); angle = (max_bin-1) * pi/18; end % 获取patch function patch = get_patch(image, row, col, scale, angle, patch_size) patch = zeros(patch_size, patch_size); for i = -patch_size/2+1:patch_size/2 for j = -patch_size/2+1:patch_size/2 x = row + i * scale * cos(angle) + j * scale * sin(angle); y = col - i * scale * sin(angle) + j * scale * cos(angle); patch(i+patch_size/2+1, j+patch_size/2+1) = interpolate_pixel(image, x, y); end end % 归一化 patch = (patch - mean(patch(:))) / std(patch(:)); end % 插值获取像素值 function pixel = interpolate_pixel(image, row, col) if row < 1 || row > size(image, 1) || col < 1 || col > size(image, 2) pixel = 0; return end row1 = floor(row); row2 = ceil(row); col1 = floor(col); col2 = ceil(col); if row1 == row2 && col1 == col2 pixel = image(row1, col1); return end if row1 == row2 pixel = (col2-col)*image(row1, col1) + (col-col1)*image(row1, col2); elseif col1 == col2 pixel = (row2-row)*image(row1, col1) + (row-row1)*image(row2, col1); else pixel = (row2-row)*(col2-col)*image(row1, col1) + (row-row1)*(col2-col)*image(row2, col1) + ... (row2-row)*(col-col1)*image(row1, col2) + (row-row1)*(col-col1)*image(row2, col2); end end ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值