[Insight-users] binary image generating
Ming Chao
mingchao2005 at gmail.com
Fri Feb 13 12:00:53 EST 2009
Hi Luis,
Thanks very much for looking into the problem and sending me better codes,
especially using iterator to access pixel values.
You are right that the problem comes from the image viewer I used. Actually
I used Paraview to examine the generate image which was saved in vtk format.
I have been trusting Paraview and could not believe it doesn't interpret
16-bit image properly.
For the random number generator, the reason I used RNG function is random
generator on Windows XP (I don't have LINUX/UNIX) is a pseudo-random number
generator. Perhaps random() on LINUX is working better. From what I found,
RNG does give better results.
Thanks again,
Ming
On Tue, Feb 10, 2009 at 4:48 PM, Luis Ibanez <luis.ibanez at kitware.com>wrote:
>
> Hi Ming,
>
> Thanks for your clear description of the problem and for posting the source
> code.
>
> I just reformatted your code a bit, and ...
> it works for me.... :-)
>
> Well,
> I also replaced your RNG object with a call to the GNU/Linux function
> "random()".
>
> It produces an image of the correct size (33x33) and the pixel values are
> indeed
> 0 and 1000, and distributed close to 50% versus 50%.
>
> I'm wondering if the problem may be with the tool that you are using for
> looking
> at the output test.vtk file. It may be that the tool is not interpreting
> correctly the
> fact that you have 16-bits values.
>
> Independent of the problem that you are reporting, please not that your
> method
> for accessing pixel values is quite inefficient. I have converted your code
> to use
> ITK iterators (which is the fastest way of visiting pixel data in ITK).
>
> Please find attached to this email the following files:
>
>
> - Random.cxx: your source code reformatted
> - Random2.cxx: converted to use ITK iterators
> - CMakeLists.txt file for building both
>
>
> Please give it a try to building these two files and let us know if you
> still see any problem.
>
> (in which case, please let us know what tool you are using for looking
> at the output image).
>
>
> Thanks
>
>
> Luis
>
>
> ------------------------------------------
>
> On Mon, Feb 9, 2009 at 11:57 AM, Ming Chao <mingchao2005 at gmail.com> wrote:
>
>> I tried to generate a binary image based on a random number. I observed
>> something I haven't understood. What I did was quite simple. I generated a
>> uniform random number between 0 and 1. If the random number is smaller than
>> 0.5, then the image intensity value was set to zero, otherwise 1000. I
>> tested the idea by generating a 2D image. What I found was that when the
>> image sizes (both x and y) were set to 2^N (N= 1, 2, 3, 4....), the
>> generated image was fine. With other image sizes, there were two interesting
>> observations: (1) the generated image was not a binary image, ie, the image
>> intensities were not 0 and 1000 but there were some intermediate values; (2)
>> the sizes of the generated image were not the sizes I specified. For
>> instance, if I specified a 3X3 region, but the generated image would have
>> 4X4 sizes. I don't know what I did wrong here. Any help or suggestions would
>> be appreciated!
>>
>> Here is the code I used to generate the image.
>>
>> #include "itkImage.h"
>>
>> #include "itkImageFileReader.h"
>> #include "itkImageFileWriter.h"
>>
>> #include <fstream>
>> //#include <cstdlib>
>> //#include <ctime>
>>
>> #include "rng.h"
>>
>> //using namespace std;
>>
>>
>> //---------------------------------------------------------------------------------------
>> int main( )
>> {
>>
>> const unsigned int Dimension = 2;
>>
>> typedef unsigned short PixelType;
>> typedef itk::Image< PixelType, Dimension > ImageType;
>>
>> ImageType::Pointer image = ImageType::New();
>>
>> ImageType::IndexType start;
>> start[0] = 0; start[1] = 0; //start[2] = 0;
>>
>> ImageType::SizeType size;
>> size[0] = 33; size[1] = 33; //size[2] = 1;
>>
>> ImageType::RegionType region;
>> region.SetSize( size ); region.SetIndex( start );
>>
>> image->SetRegions( region );
>> image->Allocate();
>>
>> /*
>> ImageType::SpacingType spacing;
>> spacing[0] = 1.0; spacing[1] = 1.0; spacing[2] = 0.0;
>> image->SetSpacing( spacing );
>>
>> ImageType::PointType origin;
>> origin[0] = 0.0; origin[1] = 0.0; origin[2] = 0.0;
>> image->SetOrigin( origin );
>> */
>>
>> RNG rdm;
>> int counter=0;
>> for ( int i = 0; i < size[0]; i++) {
>> for ( int j = 0; j < size[1]; j++ ) {
>> //for ( int k = 0; k < size[2]; k++ ) {
>> ImageType::IndexType pixelIndex;
>> pixelIndex[0] = i;
>> pixelIndex[1] = j;
>> //pixelIndex[2] = k;
>>
>> double prob = rdm.uniform();
>> //std::cout << "probability = " << prob << std::endl;
>> if (prob <= 0.5) {
>> ImageType::PixelType pixelValue = itk::NumericTraits < PixelType >::Zero;
>> image->SetPixel( pixelIndex, pixelValue );
>> counter++;
>> } else {
>> ImageType::PixelType pixelValue = 1000;
>> image->SetPixel( pixelIndex, pixelValue );
>> }
>> //}
>> }
>> }
>>
>> for ( int i = 0; i < size[0]; i++) {
>> for ( int j = 0; j < size[1]; j++ ) {
>> //for ( int k = 0; k < size[2]; k++ ) {
>> ImageType::IndexType pixelIndex;
>> pixelIndex[0] = i;
>> pixelIndex[1] = j;
>> //pixelIndex[2] = k;
>>
>> ImageType::PixelType outPixel = image->GetPixel(pixelIndex);
>> //std::cout << "pixel value = " << outPixel << std::endl;
>>
>> //}
>> }
>> }
>>
>> std::cout << counter << " pixels of " << size[0]*size[1] << " have zero
>> values." << std::endl;
>>
>>
>> typedef itk::ImageFileWriter< ImageType > WriterType;
>> WriterType::Pointer imgWriter = WriterType::New();
>> imgWriter->SetInput( image );
>> imgWriter->SetFileName( "test.vtk" );
>> imgWriter->Update();
>>
>>
>>
>>
>>
>> return EXIT_SUCCESS;
>> }
>>
>> _____________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the ITK FAQ at:
>> http://www.itk.org/Wiki/ITK_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.itk.org/mailman/listinfo/insight-users
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090213/f01c8f5e/attachment.htm>
More information about the Insight-users
mailing list