gab.opencv
Class OpenCV

java.lang.Object
  extended by gab.opencv.OpenCV

public class OpenCV
extends Object

This is a template class and can be used to start a new processing library or tool. Make sure you rename this class as well as the name of the example package 'template' to your own library or tool naming convention.


Field Summary
static String CASCADE_CLOCK
           
static String CASCADE_EYE
           
static String CASCADE_FRONTALFACE
           
static String CASCADE_FULLBODY
           
static String CASCADE_LOWERBODY
           
static String CASCADE_MOUTH
           
static String CASCADE_NOSE
           
static String CASCADE_PEDESTRIAN
           
static String CASCADE_PEDESTRIANS
           
static String CASCADE_PROFILEFACE
           
static String CASCADE_RIGHT_EAR
           
static String CASCADE_UPPERBODY
           
 CascadeClassifier classifier
           
 int colorSpace
           
 int height
           
static int HORIZONTAL
           
 Mat matA
           
 Mat matB
           
 Mat matBGRA
           
 Mat matG
           
 Mat matGray
           
 Mat matH
           
 Mat matHSV
           
 Mat matR
           
 Mat matROI
           
 Mat matS
           
 Mat matV
           
 Mat nonROImat
           
static String VERSION
           
static int VERTICAL
           
 int width
           
 
Constructor Summary
OpenCV(PApplet theParent, int width, int height)
          Initialize OpenCV with a width and height.
OpenCV(PApplet theParent, PImage img)
          Initialize OpenCV with an image.
OpenCV(PApplet theParent, PImage img, boolean useColor)
          Initialize OpenCV with an image.
OpenCV(PApplet theParent, String pathToImg)
          Initialize OpenCV with the path to an image.
OpenCV(PApplet theParent, String pathToImg, boolean useColor)
          Initialize OpenCV with the path to an image.
 
Method Summary
 void adaptiveThreshold(int blockSize, int c)
           
static void ARGBtoBGRA(Mat rgba, Mat bgra)
           
 void blur(int blurSize)
           
 void blur(int blurW, int blurH)
           
 void brightness(int amt)
          Adjust the brightness of the image.
 void contrast(float amt)
          Adjust the contrast of the image.
 Rectangle[] detect()
           
static void diff(Mat mat1, Mat mat2)
           
 void diff(PImage img)
           
 void dilate()
           
 void equalizeHistogram()
           
 void erode()
           
 void findCannyEdges(int lowThreshold, int highThreshold)
           
 ArrayList<PVector> findChessboardCorners(int patternWidth, int patternHeight)
           
 ArrayList<Contour> findContours()
           
 ArrayList<Contour> findContours(boolean findHoles, boolean sort)
           
 Histogram findHistogram(Mat mat, int numBins)
           
 Histogram findHistogram(Mat mat, int numBins, boolean normalize)
           
 ArrayList<Line> findLines(int threshold, double minLineLength, double maxLineGap)
           
 void findScharrEdges(int direction)
           
 void findSobelEdges(int dx, int dy)
           
 Mat getA()
           
 Mat getB()
           
 Mat getColor()
           
 int getColorSpace()
           
 Mat getG()
           
 Mat getGray()
           
 Mat getH()
           
 PImage getInput()
           
 PImage getOutput()
           
 Mat getR()
           
 Mat getROI()
           
 Mat getS()
           
 int getSize()
           
 PImage getSnapshot()
           
 PImage getSnapshot(Mat m)
           
 boolean getUseColor()
           
 Mat getV()
           
 void gray()
           
static Mat gray(Mat src)
           
static Mat imitate(Mat m)
           
 void inRange(int lowerBound, int upperBound)
          Filter the image for values between a lower and upper bound.
 void invert()
           
 void loadCascade(String cascadeFileName)
          load a cascade xml file from the data folder NB: ant build scripts copy the data folder outside of the jar so that this will work.
 void loadImage(PImage img)
           
 void loadImage(String imgPath)
          Load an image from a path.
static ArrayList<PVector> matToPVectors(MatOfPoint mat)
           
static ArrayList<PVector> matToPVectors(MatOfPoint2f mat)
           
 String matToS(Mat mat)
           
 PVector max()
          Get the x-y location of the maximum value in the current image.
 PVector min()
          Get the x-y location of the minimum value in the current image.
static PVector pointToPVector(Point p)
           
 void releaseROI()
           
 void setColor(Mat m)
           
 void setGray(Mat m)
           
 boolean setROI(int x, int y, int w, int h)
           
 void startBackgroundSubtraction(int history, int nMixtures, double backgroundRatio)
           
 void threshold(int threshold)
           
static void toCv(PImage img, Mat m)
          Convert a Processing PImage to an OpenCV Mat.
 void toPImage(Mat m, PImage img)
          Convert an OpenCV Mat object into a PImage to be used in other Processing code.
 void updateBackground()
           
 void useColor()
          Apply subsequent image processing to the color version of the loaded image.
 void useColor(int colorSpace)
           
 void useGray()
           
static String version()
          return the version of the library.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

width

public int width

height

public int height

matBGRA

public Mat matBGRA

matR

public Mat matR

matG

public Mat matG

matB

public Mat matB

matA

public Mat matA

matHSV

public Mat matHSV

matH

public Mat matH

matS

public Mat matS

matV

public Mat matV

matGray

public Mat matGray

matROI

public Mat matROI

nonROImat

public Mat nonROImat

colorSpace

public int colorSpace

classifier

public CascadeClassifier classifier

VERSION

public static final String VERSION
See Also:
Constant Field Values

CASCADE_FRONTALFACE

public static final String CASCADE_FRONTALFACE
See Also:
Constant Field Values

CASCADE_PEDESTRIANS

public static final String CASCADE_PEDESTRIANS
See Also:
Constant Field Values

CASCADE_EYE

public static final String CASCADE_EYE
See Also:
Constant Field Values

CASCADE_CLOCK

public static final String CASCADE_CLOCK
See Also:
Constant Field Values

CASCADE_NOSE

public static final String CASCADE_NOSE
See Also:
Constant Field Values

CASCADE_MOUTH

public static final String CASCADE_MOUTH
See Also:
Constant Field Values

CASCADE_UPPERBODY

public static final String CASCADE_UPPERBODY
See Also:
Constant Field Values

CASCADE_LOWERBODY

public static final String CASCADE_LOWERBODY
See Also:
Constant Field Values

CASCADE_FULLBODY

public static final String CASCADE_FULLBODY
See Also:
Constant Field Values

CASCADE_PEDESTRIAN

public static final String CASCADE_PEDESTRIAN
See Also:
Constant Field Values

CASCADE_RIGHT_EAR

public static final String CASCADE_RIGHT_EAR
See Also:
Constant Field Values

CASCADE_PROFILEFACE

public static final String CASCADE_PROFILEFACE
See Also:
Constant Field Values

HORIZONTAL

public static final int HORIZONTAL
See Also:
Constant Field Values

VERTICAL

public static final int VERTICAL
See Also:
Constant Field Values
Constructor Detail

OpenCV

public OpenCV(PApplet theParent,
              String pathToImg)
Initialize OpenCV with the path to an image. The image will be loaded and prepared for processing.

Parameters:
theParent - - A PApplet representing the user sketch, i.e "this"
pathToImg - - A String with a path to the image to be loaded

OpenCV

public OpenCV(PApplet theParent,
              String pathToImg,
              boolean useColor)
Initialize OpenCV with the path to an image. The image will be loaded and prepared for processing.

Parameters:
theParent - - A PApplet representing the user sketch, i.e "this"
pathToImg - - A String with a path to the image to be loaded
useColor - - (Optional) Set to true if you want to use the color version of the image for processing.

OpenCV

public OpenCV(PApplet theParent,
              PImage img)
Initialize OpenCV with an image. The image's pixels will be copied and prepared for processing.

Parameters:
theParent - A PApplet representing the user sketch, i.e "this"
img - A PImage to be loaded

OpenCV

public OpenCV(PApplet theParent,
              PImage img,
              boolean useColor)
Initialize OpenCV with an image. The image's pixels will be copiedd and prepared for processing.

Parameters:
theParent - A PApplet representing the user sketch, i.e "this"
img - A PImage to be loaded
useColor - (Optional) Set to true if you want to use the color version of the image for processing.

OpenCV

public OpenCV(PApplet theParent,
              int width,
              int height)
Initialize OpenCV with a width and height. You will need to load an image in before processing. See copy(PImage img).

Parameters:
theParent - A PApplet representing the user sketch, i.e "this"
width - int
height - int
Method Detail

useColor

public void useColor()
Apply subsequent image processing to the color version of the loaded image. Note: Many OpenCV functions require a grayscale image. Those functions will raise an exception if attempted on a color image.


getColorSpace

public int getColorSpace()

useColor

public void useColor(int colorSpace)

useGray

public void useGray()

getUseColor

public boolean getUseColor()

loadCascade

public void loadCascade(String cascadeFileName)
load a cascade xml file from the data folder NB: ant build scripts copy the data folder outside of the jar so that this will work.

Parameters:
cascadeFileName -

detect

public Rectangle[] detect()

startBackgroundSubtraction

public void startBackgroundSubtraction(int history,
                                       int nMixtures,
                                       double backgroundRatio)

updateBackground

public void updateBackground()

contrast

public void contrast(float amt)
Adjust the contrast of the image. Works on color or black and white images.

Parameters:
amt - Amount of contrast to apply. 0-1.0 reduces contrast. Above 1.0 increases contrast.

max

public PVector max()
Get the x-y location of the maximum value in the current image.

Returns:
A PVector with the location of the maximum value.

min

public PVector min()
Get the x-y location of the minimum value in the current image.

Returns:
A PVector with the location of the minimum value.

pointToPVector

public static PVector pointToPVector(Point p)

brightness

public void brightness(int amt)
Adjust the brightness of the image. Works on color or black and white images.

Parameters:
amt - The amount to brighten the image. Ranges -255 to 255.

imitate

public static Mat imitate(Mat m)

diff

public void diff(PImage img)

diff

public static void diff(Mat mat1,
                        Mat mat2)

threshold

public void threshold(int threshold)

adaptiveThreshold

public void adaptiveThreshold(int blockSize,
                              int c)

equalizeHistogram

public void equalizeHistogram()

invert

public void invert()

dilate

public void dilate()

erode

public void erode()

blur

public void blur(int blurSize)

blur

public void blur(int blurW,
                 int blurH)

findCannyEdges

public void findCannyEdges(int lowThreshold,
                           int highThreshold)

findSobelEdges

public void findSobelEdges(int dx,
                           int dy)

findScharrEdges

public void findScharrEdges(int direction)

findContours

public ArrayList<Contour> findContours()

findContours

public ArrayList<Contour> findContours(boolean findHoles,
                                       boolean sort)

findLines

public ArrayList<Line> findLines(int threshold,
                                 double minLineLength,
                                 double maxLineGap)

findChessboardCorners

public ArrayList<PVector> findChessboardCorners(int patternWidth,
                                                int patternHeight)

findHistogram

public Histogram findHistogram(Mat mat,
                               int numBins)
Parameters:
mat - The mat from which to calculate the histogram. Get this from getGray(), getR(), getG(), getB(), etc.. By default this will normalize the histogram (scale the values to 0.0-1.0). Pass false as the third argument to keep values unormalized.
numBins - The number of bins into which divide the histogram should be divided.
normalize - (optional) Whether or not to normalize the histogram (scale the values to 0.0-1.0). Defaults to true.
Returns:
A Histogram object that you can call draw() on.

findHistogram

public Histogram findHistogram(Mat mat,
                               int numBins,
                               boolean normalize)

inRange

public void inRange(int lowerBound,
                    int upperBound)
Filter the image for values between a lower and upper bound. Converts the current image into a binary image with white where pixel values were within bounds and black elsewhere.

Parameters:
lowerBound -
upperBound -

gray

public static Mat gray(Mat src)
Parameters:
src - A Mat of type 8UC4 with channels arranged as BGRA.
Returns:
A Mat of type 8UC1 in grayscale.

gray

public void gray()

setROI

public boolean setROI(int x,
                      int y,
                      int w,
                      int h)

releaseROI

public void releaseROI()

loadImage

public void loadImage(String imgPath)
Load an image from a path.

Parameters:
imgPath - String with the path to the image

loadImage

public void loadImage(PImage img)

ARGBtoBGRA

public static void ARGBtoBGRA(Mat rgba,
                              Mat bgra)

getSize

public int getSize()

toPImage

public void toPImage(Mat m,
                     PImage img)
Convert an OpenCV Mat object into a PImage to be used in other Processing code. Copies the Mat's pixel data into the PImage's pixel array. Iterates over each pixel in the Mat, i.e. expensive. (Mainly used internally by OpenCV. Inspired by toCv() from KyleMcDonald's ofxCv.)

Parameters:
m - A Mat you want converted
img - The PImage you want the Mat converted into.

toCv

public static void toCv(PImage img,
                        Mat m)
Convert a Processing PImage to an OpenCV Mat. (Inspired by Kyle McDonald's ofxCv's toOf())

Parameters:
img - The PImage to convert.
m - The Mat to receive the image data.

matToPVectors

public static ArrayList<PVector> matToPVectors(MatOfPoint mat)

matToPVectors

public static ArrayList<PVector> matToPVectors(MatOfPoint2f mat)

matToS

public String matToS(Mat mat)

getInput

public PImage getInput()

getOutput

public PImage getOutput()

getSnapshot

public PImage getSnapshot()

getSnapshot

public PImage getSnapshot(Mat m)

getR

public Mat getR()

getG

public Mat getG()

getB

public Mat getB()

getA

public Mat getA()

getH

public Mat getH()

getS

public Mat getS()

getV

public Mat getV()

getGray

public Mat getGray()

setGray

public void setGray(Mat m)

setColor

public void setColor(Mat m)

getColor

public Mat getColor()

getROI

public Mat getROI()

version

public static String version()
return the version of the library.

Returns:
String


Processing library opencv_processing by Greg Borenstein. (c) 2013