How can I reconstruct part of an image using imreconstruct

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));
end
//////////////
error\\\\\\\\\\\\
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

4 Comments

Create a copy and mask out areas outside your ROI, reconstruct and then merge back?
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?
clc;
marker=imread('trees.tif');
mask= ones(size(marker));
mask(6:20,4:50)=0;
rconstructedImage= imreconstruct(marker,mask);

Sign in to comment.

Answers (1)

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.
return;
end
end
% 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);
uiwait(warndlg(errorMessage));
return;
end
end
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);
bar(pixelCount);
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);

5 Comments

but what I'm trying to do is to reconstruct part of an image like bounding box(extract specific region) regardless of the connectivity outside that box
\\\\\\\\\\\\code\\\\\\\\clc; marker=imread('trees.tif'); mask= ones(size(marker)); mask(6:20,4:50)=0; rconstructedImage= imreconstruct(marker,mask); \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
when I tried this code I got this error \\\\\\\\\\\\error\\\\\\\\\\\ Error using imreconstructmex Function imreconstruct expected MARKER and MASK to have the same class.
Error in imreconstruct (line 77) im = imreconstructmex(marker,mask); \\\\\\\\\\\\\\\\\\\\\\\\\
How should I define the mask?
I don't know what this means: "reconstruct part of an image like bounding box(extract specific region)". Why don't you just use imcrop()? I don't think you know what morphological reconstruction does with a gray scale image, and I don't blame you because it's not obvious or intuitive. I'm surprised you want to try it. It makes a lot more sense for binary images. But you're passing in a uint8 marker image and a double mask image. You can't do that - they have to both be uint8.
mask = ones(size(marker), 'uint8');
but like I said, I'm pretty sure you will be surprised at the unexpected result that it will produce.
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) ....so 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 http://www.mathworks.com/tagteam/64199_91822v00_eddins_final.pdf
do you have any suggessions ?
thanks
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.
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

Sign in to comment.

Categories

Asked:

on 15 Nov 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!