如何根据图像调整大小更改边界框位置?

时间:2021-04-13 12:24:26

标签: matlab image-processing computer-vision ocr bounding-box

当我发现 here 时,我使用 [239, 397] 函数将二值图像从 [128, 128] 更改并标准化为 processLetter。该图像包含由真实值中的框包围的阿拉伯字母。当我改变大小时,我必须相应地改变边界框。我写了这段代码,边界框没有根据你可以在这里找到的原始图像进行相应调整:

Original image

我正在努力解决这个问题,但我卡住了,失败了。我需要你的帮助。

简单的代码在这里:

%% clean workspace
clear;
clc;
%% Read the image
im_origin = imread('C:\Users\asa\Desktop\im_origin.bmp');
% Get the original image size
[w1,h1,c1] = size(im_origin)
% function to resize the image size into [128 128]
im_resized = processLetter(im_origin);
[w2,h2,c2]= size(im_resized);
%% Read Bound box values in form [ax ay w h]
GTBoxes = [263 74   68 78;
            161 74  101 78;
            61  74  99  78];
[rowGT colGT] = size(GTBoxes);
%% Resize bounding box
% Get scale from division of height of the resized image(h2) over the 
% original image(h1)
scale = h2/h1;
BBresized = bboxresize(GTBoxes,scale);
imshow(im_resized);
hold on;
%% Draw bounding boxes around the letters
 for i=1:rowGT
  rectangle('Position',BBresized(i,:),'EdgeColor','y');
  pause(2);
 end

这是调整大小后的图像:

3

而且,这是带有相应(错误)边界框的调整大小的图像。

enter image description here

感谢任何帮助或建议。

1 个答案:

答案 0 :(得分:1)

这里至少有两个问题:

  1. [w1, h1, c1] = size(im_origin) 错了,是 [h, w, c]
  2. 您有不同的宽度和高度缩放系数。

我使用简单的 imresize 和手动缩放重新编写了您的示例,因为我现在只有 Octave rght 用于测试(下面的代码也适用于 MATLAB Online)。该 processLetter 函数似乎也从图像中裁剪文本!?因此,您还需要确保对此进行更正(超出我的回答范围)!

% Read original image, get size
im_origin = imread('Dmkoh.png');
[h1, w1, c1] = size(im_origin)

% Set up original bounding boxes, get size
GTBoxes = [263 74  68 78;
           161 74 101 78;
            61 74  99 78]
[rowGT, colGT] = size(GTBoxes);

% Plot original image with bounding boxes
figure(1);
imshow(im_origin);
hold on;
for i = 1:rowGT
  rectangle('Position', GTBoxes(i, :), 'EdgeColor', 'y');
end
hold off;

% Resize image (using imresize here), get size
im_resized = imresize(im_origin, [128, 128]);
[h2, w2, c2] = size(im_resized)

% Resize bounding boxes (using simple scaling)
BBresized = GTBoxes;
BBresized(:, [1, 3]) = BBresized(:, [1, 3]) * w2/w1;
BBresized(:, [2, 4]) = BBresized(:, [2, 4]) * h2/h1;

% Plot resized image with bounding boxes
figure(2);
imshow(im_resized);
hold on;
for i = 1:rowGT
  rectangle('Position', BBresized(i, :), 'EdgeColor', 'y');
end
hold off;

原文:

Original

调整大小:

Resized