Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Converting Matlab code to OpenCV (C,C++) code

I would appreciate it if someone can help to covert the Matlab code below to OpenCV (c, c++) code, thanks in advance!!!

Basically, the code is used to remove the text region of a document image, results can be found:,dd172fr#1

% Set these values depending on your input image

img = imread('');

MinArea = 2000; % Minimum area to consider, in pixels
% End User inputs

gsImg = 255 - rgb2gray(img); % convert to grayscale (and invert 'cause that's how I think)
threshImg = gsImg > graythresh(gsImg)*max(gsImg(:)); % Threshold automatically

% Detect regions, using the saturation in place of 'intensity'
regs = regionprops(threshImg, 'BoundingBox', 'Area');

% Process regions to conform to area and saturation thresholds
regKeep = false(length(regs), 1);
for k = 1:length(regs)

    regKeep(k) = (regs(k).Area > MinArea);


regs(~regKeep) = []; % Delete those regions that don't pass qualifications for image

% Make a new blank image to hold the passed regions
newImg = 255*ones(size(img), 'uint8');

for k = 1:length(regs)

    boxHere = regs(k).BoundingBox; % Pull out bounding box for current region
    boxHere([1 2]) = floor(boxHere([1 2])); % Round starting points down to next integer
    boxHere([3 4]) = ceil(boxHere([3 4])); % Round ranges up to next integer
    % Insert pixels within bounding box from original image into the new
    % image
    newImg(boxHere(2):(boxHere(2)+boxHere(4)), ...
        boxHere(1):(boxHere(1)+boxHere(3)), :) = img(boxHere(2):(boxHere(2)+boxHere(4)), ...
        boxHere(1):(boxHere(1)+boxHere(3)), :);


% Display