MATLAB Answers


How can I reconstruct part of an image using imreconstruct

Asked by Amani
on 15 Nov 2012

I want to morhpologically reconstruct part of an image based on a line so after identify the position of the line I'm not able to reconstruct the lower part because the size of the marker should be the same as the size of the mask

the code\\\\\\\\\\\

for i=46:65

    k= imreconstruct(fe,f1(i:i));




Error using imreconstructmex Function imreconstruct expected MARKER and MASK to be the same size.

Error in imreconstruct (line 77) im = imreconstructmex(marker,mask); \\\\\\\\\\\\\\\\\\\\

I tried to crop both the marker and the mask, it worked but I want to keep the whole image for further analysis


on 15 Nov 2012

thank you very much for ur reply but can u please clarify ? what do you mean by mask out?

Just looked at your code, I am now not sure if I understand what you are trying to do. Can you post example code using your crop approach and possibly a demo image which comes with the toolbox?

on 16 Nov 2012



mask= ones(size(marker));


rconstructedImage= imreconstruct(marker,mask);

1 Answer

Answer by Image Analyst
on 16 Nov 2012

Your code doesn't make any sense. You cannot use a single pixel to reconstruct. You have to use a whole binary image the same size as the mask image. Here's a demo:

clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
imtool close all;  % Close all imtool figures.
clear;  % Erase all existing variables.
workspace;  % Make sure the workspace panel is showing.
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
	% User does not have the toolbox installed.
	message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
	reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
	if strcmpi(reply, 'No')
		% User said No, so exit.
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'eight.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
	% Didn't find it there.  Check the search path for it.
	fullFileName = baseFileName; % No path this time.
	if ~exist(fullFileName, 'file')
		% Still didn't find it.  Alert user.
		errorMessage = sprintf('Error: %s does not exist.', fullFileName);
grayImage = imread(fullFileName);
% Get the dimensions of the image.  numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Image Analysis Demo','numbertitle','off') 
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2); 
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
maskImage = imfill(grayImage < 220, 'holes');
% Display the image.
subplot(2, 2, 2);
imshow(maskImage, []);
title('Mask Image (Original Image Thresholded)', 'FontSize', fontSize);
markerImage = false(size(maskImage));
markerImage(20:70, 225:230) = true;
% Display the image.
subplot(2, 2, 3);
imshow(markerImage, []);
title('Marker Image', 'FontSize', fontSize);
finalImage = imreconstruct(markerImage, maskImage);
% Display the image.
subplot(2, 2, 4);
imshow(finalImage, []);
title('Final Reconstructed Image', 'FontSize', fontSize);


on 18 Nov 2012

I just used tree image becuase it is in the demo file, but I'm working on binary images, so even with the binary image I have the same error (Error using imreconstructmex Function imreconstruct expected MARKER and MASK to have the same class.) let me explain what I want to do, I'm working on text recognition and I need to get a baseline of the text and reconstruct the part that is below the line and after some analysis (based on CC)I want to reconstruct just one region above the baseline(region of intereset) based on the analysis (width of some CC below the baseline) I don't want to crop so will lose some parts of the image so what I'm trying to do is similar to example 10.8 in this link

do you have any suggessions ?


Well did you try the code I posted for you? Because this is exactly what it does - the same thing as the book. And did you try the code in Steve's book - the example 10.8 you referred to? That works also. If you can't adapt it then post your code and I'll try to fix it when i get time.

on 19 Nov 2012

yes I tried both of them but I'm still not able to reconstruct half of the coin without cropping ... is there any way to do it? thanks

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!