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

Thread Subject:
problem with imreconstruct

Subject: problem with imreconstruct

From: Paul

Date: 19 Feb, 2011 20:48:22

Message: 1 of 14

I have 3 different binary images (16bit) and I am doing reconstruction of these images using the marker obtained as;

Img=bwareaopen(myBinaryImage,10);
myMarker=logical(littleWhiteSquare.*Img);
Img2=imreconstruct(myMarker,myBinaryImage);

The Img2 is good only for one image, the reconstruction does not give good results for other two images (these two result images are very very similar to the markers not to the myBinaryImage ) Do you know any alternative to imreconstruct command ?
Thanks

Subject: problem with imreconstruct

From: ImageAnalyst

Date: 19 Feb, 2011 21:01:14

Message: 2 of 14

Paul:
You shouldn't need an alternative. It should work. What is does is
to reconstruct any binary image that includes pixels that are also
included in your marker image. First of all, a binary image is
usually a logical image, not a 16 bit integer image.

So first you throw out blobs smaller than 10 pixels from your image.
Then you mask that with an image called littleWhite Square, which
presumably has one or more white squares that reside inside the large
blobs in your image. This is your marker image. So your final
reconstruction should give all blobs that both contain the square(s)
and are larger than 10 pixels. Any blobs smaller than 10 or not
containing your square(s) will be gone. And the shape of the
remaining blobs should be exactly what it was in the original image,
pixel for pixel. If you think something is wrong with that then
upload your images and code and I'll be happy to test/verify it.
ImageAnalyst

Subject: problem with imreconstruct

From: Paul

Date: 19 Feb, 2011 22:05:22

Message: 3 of 14

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <9dd6a81c-bf21-40dc-a7b8-9844bc58fa4c@z3g2000prz.googlegroups.com>...
> Paul:
> You shouldn't need an alternative. It should work. What is does is
> to reconstruct any binary image that includes pixels that are also
> included in your marker image. First of all, a binary image is
> usually a logical image, not a 16 bit integer image.
>
> So first you throw out blobs smaller than 10 pixels from your image.
> Then you mask that with an image called littleWhite Square, which
> presumably has one or more white squares that reside inside the large
> blobs in your image. This is your marker image. So your final
> reconstruction should give all blobs that both contain the square(s)
> and are larger than 10 pixels. Any blobs smaller than 10 or not
> containing your square(s) will be gone. And the shape of the
> remaining blobs should be exactly what it was in the original image,
> pixel for pixel. If you think something is wrong with that then
> upload your images and code and I'll be happy to test/verify it.
> ImageAnalyst

Thanks a lot ImageAnalyst,

The codes are;

img=imread('im1.png');
myBinaryImg=img>14000;

littleSquare=zeros(256,256);
littleSquare(145:205,120:140)=1;

myBinaryImg11=bwareaopen(myBinaryImg,10);
mymarker=logical(littleSquare.*myBinaryImg11);

resultimg=imreconstruct(mymarker,myBinaryImg);
figure, imshow(resultimg);

% -------------------------------------------------------------------------
% The littleSquare is obtained according to the length and the width
% of the foreground image, so for im2 the littleSquare have to be this:
% littleSquare(154:214,120:140)=1;

And the images are at;
http://blogger-imageanalysis.blogspot.com/2011/02/blog-post_897.html

Could you please test the codes for these two images. (The same codes give good result for my third image, when I calculate the littleSquare parameters for it) I think the problem is due to the square paramaters. The square is tried to locate on the rib, since this square is also used to remove the rib
Many thanks again

Subject: problem with imreconstruct

From: ImageAnalyst

Date: 19 Feb, 2011 22:46:09

Message: 4 of 14

Paul:
For the first (top) one it worked fine. It extracted all blobs that
your white rectangle touched. Separate blobs that were not touched by
your white rectangle are not extracted, just as expected. No problems
at all with this image and everything is working fine, just as
expected.

For the second (bottom) one, the max value is 12850 so setting a
threshold of 14000 will not give anything in the binary image. You
would need to adjust your threshold.
Hope that helps.
ImageAnalyst

Subject: problem with imreconstruct

From: Paul

Date: 19 Feb, 2011 23:42:07

Message: 5 of 14

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <f9a31ee7-d883-48e0-b8b8-683943da4f96@q2g2000pre.googlegroups.com>...
> Paul:
> For the first (top) one it worked fine. It extracted all blobs that
> your white rectangle touched. Separate blobs that were not touched by
> your white rectangle are not extracted, just as expected. No problems
> at all with this image and everything is working fine, just as
> expected.
>
> For the second (bottom) one, the max value is 12850 so setting a
> threshold of 14000 will not give anything in the binary image. You
> would need to adjust your threshold.
> Hope that helps.
> ImageAnalyst

Thank you ImageAnalyst :)
What is your advice about the algorithm to find the optimal threshold for these two images in order to obtain only the organs (without ribs, spine) ?

Subject: problem with imreconstruct

From: ImageAnalyst

Date: 19 Feb, 2011 23:48:33

Message: 6 of 14

On Feb 19, 6:42 pm, "Paul " <tkitt...@gmail.com> wrote:
> Thank you ImageAnalyst :)
> What is your advice about the algorithm to find the optimal threshold for these two images in order to obtain only the organs (without ribs, spine) ?
----------------------------------------------------------------------------
Paul:
I'd ask a medical imaging researcher - maybe there is some known
algorithm - like bone always has a 50% higher signal than organs or
something like that.

Or search the literature:
http://iris.usc.edu/Vision-Notes/bibliography/contentsmedical.html#Medical%20Applications,%20CAT,%20MRI,%20Ultrasound,%20Heart%20Models,%20Brain%20Models

Or you can use my thresholding app to do it visually/manually/
interactively:
http://www.mathworks.com/matlabcentral/fileexchange/authors/31862

Or develop your own based on some ad hoc algorithm that works for your
particular set of images.
Good luck,
ImageAnalyst

Subject: problem with imreconstruct

From: Paul

Date: 20 Feb, 2011 00:01:18

Message: 7 of 14

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <03a4f786-d4ae-4418-a93f-799216c42120@y4g2000prh.googlegroups.com>...
> On Feb 19, 6:42 pm, "Paul " <tkitt...@gmail.com> wrote:
> > Thank you ImageAnalyst :)
> > What is your advice about the algorithm to find the optimal threshold for these two images in order to obtain only the organs (without ribs, spine) ?
> ----------------------------------------------------------------------------
> Paul:
> I'd ask a medical imaging researcher - maybe there is some known
> algorithm - like bone always has a 50% higher signal than organs or
> something like that.
>
> Or search the literature:
> http://iris.usc.edu/Vision-Notes/bibliography/contentsmedical.html#Medical%20Applications,%20CAT,%20MRI,%20Ultrasound,%20Heart%20Models,%20Brain%20Models
>
> Or you can use my thresholding app to do it visually/manually/
> interactively:
> http://www.mathworks.com/matlabcentral/fileexchange/authors/31862
>
> Or develop your own based on some ad hoc algorithm that works for your
> particular set of images.
> Good luck,
> ImageAnalyst

Thanks for your interest and help,
cheers

Subject: problem with imreconstruct

From: ImageAnalyst

Date: 20 Feb, 2011 00:15:19

Message: 8 of 14

On Feb 19, 7:01 pm, "Paul " <tkitt...@gmail.com> wrote:
> Thanks for your interest and help,
> cheers
-----------------------------------------
Hopefully I've convinced you that there's nothing wrong with
imreconstruct.

Subject: problem with imreconstruct

From: Paul

Date: 20 Feb, 2011 01:09:23

Message: 9 of 14

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <7962c764-6cf2-4fbc-99f9-4d39c198c0f9@y4g2000prh.googlegroups.com>...
> On Feb 19, 7:01 pm, "Paul " <tkitt...@gmail.com> wrote:
> > Thanks for your interest and help,
> > cheers
> -----------------------------------------
> Hopefully I've convinced you that there's nothing wrong with
> imreconstruct.

Yes, I see what the imreconstruct method does, thank you.
I decided to use the thresholded image for the next step of the application instead of the reconstructed image. Since sometimes the marker might not include any foreground pixel thefore the reconstructed image might be very different from the necessary image.
When you have any suggestion about the automatic thresholding for this kind of images and write to my blog website then I will be very grateful

have a nice day

Subject: problem with imreconstruct

From: Ferrari P.

Date: 26 Feb, 2011 11:39:28

Message: 10 of 14

Dear ImageAnalyst,

I am using Matlab209b version and getting this error:

??? Error using ==> imreconstructmex
MARKER pixels must be <= MASK pixels.

Error in ==>imreconstruct at 71
  im = imreconstructmex(marker,mask);

Even, I checked the marker and mask sizes. They are both logical and 256x256. Have you any suggestions to get rid of this error ?
thanks


ImageAnalyst <imageanalyst@mailinator.com> wrote in message <7962c764-6cf2-4fbc-99f9-4d39c198c0f9@y4g2000prh.googlegroups.com>...
> On Feb 19, 7:01 pm, "Paul " <tkitt...@gmail.com> wrote:
> > Thanks for your interest and help,
> > cheers
> -----------------------------------------
> Hopefully I've convinced you that there's nothing wrong with
> imreconstruct.

Subject: problem with imreconstruct

From: ImageAnalyst

Date: 26 Feb, 2011 15:37:04

Message: 11 of 14

On Feb 26, 6:39 am, "Ferrari P. " <tkitt...@gmail.com> wrote:
> Dear ImageAnalyst,
>
> I am using Matlab209b version and getting this error:
>
> ??? Error using ==> imreconstructmex
> MARKER pixels must be <= MASK pixels.
>
> Error in ==>imreconstruct at 71
>   im =  imreconstructmex(marker,mask);
>
> Even, I checked the marker and mask sizes. They are both logical and 256x256. Have you any suggestions to get rid of this error ?
> thanks
------------------------------------------------------------------------------
Give a small demo, for example using the cameraman demo image. Read
it in, threshold it, erode it with imerode, then reconstruct it. Post
your code here and let me know how it goes.

Subject: problem with imreconstruct

From: ImageAnalyst

Date: 26 Feb, 2011 16:13:18

Message: 12 of 14

Ferrari P. :
OK, I spent a couple of minutes and did the demo for you. I don't see
any problem with it. How does this differ from your code? I can't
help as much if you don't provide your code, your image, or any more
information:

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.
% function test1
clc;
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% 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

grayImage = peaks(256);
% Display the original image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Image for deriving a binary image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));

% Get a binary image.
binaryImage = grayImage > 3;
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('The Starting Binary Image', 'FontSize', fontSize);

% Erode it to form a marker image.
% Erode it wnough to have only 2 blobs instead of 3
markerImage = imerode(binaryImage, ones(15));
% Display the image.
subplot(2, 2, 3);
imshow(markerImage, []);
title('Marker Image = Eroded Binary Image', 'FontSize', fontSize);

reconst = imreconstruct(markerImage, binaryImage);
% Display the reconstructed image.
subplot(2, 2, 4);
imshow(reconst, []);
title('Final Reconstructed Image', 'FontSize', fontSize);

Subject: problem with imreconstruct

From: Ferrari P.

Date: 26 Feb, 2011 18:20:22

Message: 13 of 14

Thank you very much ImageAnalyst for your time. I wrote my codes using the cameraman image and again I am getting the same error. Could you please test the following codes:

 im=imread('cameraman.tif');
  m3=zeros(256,256);
  
  m3(im>=1.6753e+004)=1;
  logicalm3=logical(m3);

M=zeros(256, 256);
M(141:151,88:108)=1;

 Ieroded=imerode(M,strel('disk',1));
    Iskeleton=bwmorph(Ieroded,'skel',Inf);
    mymarker=M & Iskeleton;
    
 I3= imreconstruct(mymarker, logicalm3);

The error is again ;
??? Error using ==> imreconstructmex
MARKER pixels must be <= MASK pixels.

Error in ==> imreconstruct at 71
  im = imreconstructmex(marker,mask);

What must I do to get rid of this problem ?

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <021cd4f9-eea5-4dec-ba2f-73fdb50eb490@y36g2000pra.googlegroups.com>...
> Ferrari P. :
> OK, I spent a couple of minutes and did the demo for you. I don't see
> any problem with it. How does this differ from your code? I can't
> help as much if you don't provide your code, your image, or any more
> information:
>
> % IMPORTANT: The newsreader may break long lines into multiple lines.
> % Be sure to join any long lines that got split into multiple single
> lines.
> % These can be found by the red lines on the left side of your
> % text editor, which indicate syntax errors, or else just run the
> % code and it will stop at the split lines with an error.
> % function test1
> clc;
> clear; % Erase all existing variables.
> workspace; % Make sure the workspace panel is showing.
> fontSize = 20;
>
> % Change the current folder to the folder of this m-file.
> if(~isdeployed)
> cd(fileparts(which(mfilename)));
> end
>
> % 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
>
> grayImage = peaks(256);
> % Display the original image.
> subplot(2, 2, 1);
> imshow(grayImage, []);
> title('Image for deriving a binary image', 'FontSize', fontSize);
> % Enlarge figure to full screen.
> set(gcf, 'Position', get(0,'Screensize'));
>
> % Get a binary image.
> binaryImage = grayImage > 3;
> % Display the image.
> subplot(2, 2, 2);
> imshow(binaryImage, []);
> title('The Starting Binary Image', 'FontSize', fontSize);
>
> % Erode it to form a marker image.
> % Erode it wnough to have only 2 blobs instead of 3
> markerImage = imerode(binaryImage, ones(15));
> % Display the image.
> subplot(2, 2, 3);
> imshow(markerImage, []);
> title('Marker Image = Eroded Binary Image', 'FontSize', fontSize);
>
> reconst = imreconstruct(markerImage, binaryImage);
> % Display the reconstructed image.
> subplot(2, 2, 4);
> imshow(reconst, []);
> title('Final Reconstructed Image', 'FontSize', fontSize);

Subject: problem with imreconstruct

From: ImageAnalyst

Date: 26 Feb, 2011 20:54:41

Message: 14 of 14

Ferrari P.:
You really need to think about what you're doing first before coding
up a useless algorithm like this. First of all, your binary image m3
is all zeros because there is no pixel in the cameraman image that has
a gray level greater than 16653. Then you make up an image with a
white block in it and you erode it and then skeletonize the erosion.
But then you make a marker image which is the AND of the white block
and the skeleton. Don't you realize that this is the same as the
skeleton alone? Think about it.

So then you use this all skeleton image as the marker image, trying to
find white blobs in logicalm3. But remember, logicalm3 had nothing at
all in it? So you get absolutely nothing except all black images. By
the way, the code ran with no errors, as it should. But you need to
think about how to design an algorithm. For a good example on how to
use imreconstruct, you should look to the code I already gave you. Or
else look at this additional demo where I use imreconstruct to segment
out only the nickels from the coins.png demo image.

ImageAnalyst

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

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 = 14;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% 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 = 'coins.png';
% 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, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

% Get a binary image.
binaryImage = grayImage > 94;
% Display the image.
subplot(2, 3, 2);
imshow(binaryImage, []);
title('The Starting Binary Image', 'FontSize', fontSize);

% Erode it to form a marker image.
% Erode it wnough to have only 2 blobs instead of 3
markerImage = imerode(binaryImage, ones(40));
% Display the image.
subplot(2, 3, 3);
imshow(markerImage, []);
title('Marker Image = Eroded Binary Image', 'FontSize', fontSize);

reconst = imreconstruct(markerImage, binaryImage);
% Display the reconstructed image.
subplot(2, 3, 4);
imshow(reconst, []);
title('Final Reconstructed Image', 'FontSize', fontSize);

% Mask the image
nickelOnlyImage = zeros(size(grayImage));
nickelOnlyImage(reconst) = grayImage(reconst);
% Display the masked image.
subplot(2, 3, 5);
imshow(nickelOnlyImage, []);
title('Masked, Nickels-Only Image', 'FontSize', fontSize);

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us