CImage:Setr


The Setr method sets the pixel value of an RGB 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 numeric type images is Setn.

Syntax

CImage:Setr( sRgbVal )

CImage:Setr( x[], sRgbVal )

CImage:Setr( x, sRgbVal )

CImage:Setr( x, y, sRgbVal )

CImage:Setr( x, y, z, sRgbVal )

where

    sRgbVal is a string containing RGB components, such as "25,45.35, 100". If only the sRgbVal 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.

Examples

The first script below shows the creation of a mask image in which all pixels with values above "100,100,100" are set to 0. The script uses Getr 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

  sRgb = I:Getr( i )

-- get RGB pixel value as a triplet string

  r,g,b = StrToRgb( sRgb )

-- split the returned pixel value into r,g,b

  if r > 100 and g > 100 and b > 100 then

-- if pixel i has value > 1,000

    I:Setr( i, "0,0,0" )

-- then set pixel i to 0,0,0 passed as a string

  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

    sRgb = I:Getr( col, row )

-- get RGB pixel value as a triplet string

    r,g,b = StrToRgb( sRgb )

-- split the returned pixel value into r,g,b

    if r > 100 and g > 100 and b > 100 then

-- if pixel i has value > 1,000

      I:Setr( col, row, "0,0,0" )

-- then set pixel i to "0,0,0"

    end

 

  end

 

end

 

I:Save()

-- save the image

Related Topics

Getr

Gets

Set

Setn

SetRegionVal

CImage class