Red Eye Removal

From Director Online Wiki
Jump to: navigation, search

This algorithm will remove the red eye effect that you get from cameras. It's important to note that this is not an automatic red eye removal algorithm. It will attempt to remove the red from any pixel that meets the conditions:

  • R > 50
  • R/(R+G+B) > 0.40
  • G/(R+G+B) < 0.31
  • B/(R+G+B) < 0.36

where R = red, G = green, and B = blue; components of a pixel.

As such, this algorithm should be used in conjunction with other methods whereby the user manually selects the red eyes in the image in order to isolate that part of the image for the algorithm.

-- Red Eye Removal Method
-- movie script
-- © 2009 by Josh Chunick
-- resources: 
-- http://stackoverflow.com/questions/133675/red-eye-reduction-algorithm
-- http://research.microsoft.com/en-us/um/people/leizhang/paper/icip04-lei.pdf
 
on redEyeRemoval _img
  --  intStart = the milliseconds
  if ilk(_img) <> #image then return VOID
  imgProc = _img.duplicate()
  intWidth = imgProc.width - 1
  intHeight = imgProc.height - 1
  repeat with y = 0 to intHeight
    repeat with x = 0 to intWidth
      colPixel = imgProc.getPixel(x,y)
      intR = colPixel.red
      intG = colPixel.green
      intB = colPixel.blue
      if (intR > 50) then
        if float(intR)/(intR+intG+intB) > 0.40 then
          if float(intG)/(intR+intG+intB) < 0.31 then
            if float(intB)/(intR+intG+intB) < 0.36 then
              intVal = (integer((intG + intB)/2.0) * 65536) + (intG * 256) + intB
              imgProc.setPixel(x, y, intVal)
            end if
          end if
        end if
      end if
    end repeat
  end repeat
  --  put "Elapsed Time: " & the milliseconds - intStart
  return imgProc
end