%  Copyright 2018 The MathWorks, Inc.

function [imgPacked] = myNDNet_Postprocess(Iori, num, bbox, scores, wi, he, ch)

%% Parameters of input image
% wi = 640;
% he = 480;
% ch = 3;

labeltbl = {'NG';'OK'};
colortbl = [255 255 102; 73 6 248];

%%
Iori = reshape(Iori, [he wi 3]);
bbox = reshape(bbox, [4 4]);
scores = reshape(scores, [2 4]);

%%
img2 = Iori;
for i = 1:num
    idx = (scores(1, i) < scores(2, i)) + 1;
    img2 = insertObjectAnnotation(img2, 'rectangle', bbox(i,:),...
        labeltbl{idx}, 'FontSize', 18, 'Color', colortbl(idx,:));         
end

%% Convert image data format to OpenCV compatible image format
imgPacked = coder.nullcopy(zeros([1,he*wi*ch], 'uint8'));
for i = 1:he
    for j = 1:wi
        imgPacked((i-1)*wi*ch + (j-1)*ch + 3) = img2(i,j,1);
        imgPacked((i-1)*wi*ch + (j-1)*ch + 2) = img2(i,j,2);
        imgPacked((i-1)*wi*ch + (j-1)*ch + 1) = img2(i,j,3);
    end
end

end