CImage:Setn


The Setn method sets the pixel value of a numeric data type at the specified image coordinates. The coordinates are unit-based, so that coordinates {1,1,...} correspond to the first pixel in the image array. This method is a faster, but less general version of the Set method. A specific method for setting pixels of RGB type images is Setr.

Syntax

CImage:Setn( nVal )

CImage:Setn( x[], nVal )

CImage:Setn( x, nVal )

CImage:Setn( x, y, nVal )

CImage:Setn( x, y, z, nVal )

where

    nVal is a number. If only the nVal argument is passed, the entire image is set ot the value.

    x[] is a coordinate array.

    x,y, and z are column, row, and plane coordinates of the pixel.

  

To assign a single value to all pixels of the image, use the first form without coordinates. To set all pixels inside a rectangular region, use SetRegionVal.

Example

The first script below shows the creation of a mask image in which all pixels with values above 1000 are set to 0. The script uses Getn to test the value, then changes it if necessary. In this case, every pixel is processed, so a single array index is used for the entire image:

I = CImage:new()

-- Create a CImage

I:Open( sPath )

-- Open the image from file path sPath.

n = I:Pixels()

-- count the number of image pixels

for i = 1, n do

-- loop over all image pixels

  if I:Getn( i ) > 1000 then

-- if pixel i has value > 1,000

    I:Setn( i, 0.0 )

-- then set pixel i to 0

  end

 

end

 

I:Save()

-- save the image

  

The next script creates a mask like the previous script but processes only a rectangular region of the image. This application therefore requires using 2 array indices to access only the rectangular region. The rectangular region is presumed to already be determined and have limits [nMinCol,nMaxCol:nMinRow,nMaxRow].

I = CImage:new()

-- Create a CImage

I:Open( sPath )

-- Open the image from file path sPath.

n = I:Pixels()

-- count the number of image pixels

for row = nMinRow, nMaxRow do

-- loop over the row range

  for col = nMinCol, nMaxCol do

-- loop over the column range

    if I:Getn( col, row ) > 1000 then

-- if pixel i has value > 1,000

      I:Setn( col, row, 0.0 )

-- then set pixel i to 0

    end

 

  end

 

end

 

I:Save()

-- save the image

Related Topics

Getn

Gets

Set

Setr

SetRegionVal

CImage class