Search code examples
matlabimage-processingcomputer-visionocrbounding-box

How to change bounding boxes position according to image resize?


I changed and normalize a binary image from [239, 397] to [128, 128] using processLetter function as I found here. The image contained Arabic letters bounded by boxes in ground truth values. When I change the size, I must change the bounding boxes consequently. I wrote this code and the bounding boxes are not adjusted accordingly to the original image which you can find here:

Original image

I'm digging hard to fix this problem, but I'm stuck and fail. I need your help.

Simple code is going here:

%% 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

This is the image after resizing:

3

And, here is the resized image with the corresponding (wrong) bounding boxes.

enter image description here

I appreciate any help or suggestions.


Solution

  • There are at least two issues here:

    1. [w1, h1, c1] = size(im_origin) is wrong, it's [h, w, c].
    2. You have different scaling factors for width and height.

    I reworked your example using simple imresize, and manually scaling, since I only have Octave rght now for testing (below code also works in MATLAB Online). That processLetter function also seems to crop the text from the image!? So, you'd need to make sure to correct for that also (outside the scope of my answer)!

    % 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:

    Original

    Resized:

    Resized