Skip to content

Commit

Permalink
combine mat-to-pimage pixel functions into one; cleanup all examples …
Browse files Browse the repository at this point in the history
…to not call for P2D explicitly
  • Loading branch information
atduskgreg committed Oct 17, 2014
1 parent 054a989 commit f25fe8d
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 77 deletions.
2 changes: 1 addition & 1 deletion examples/BrightnessContrast/BrightnessContrast.pde
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ OpenCV opencv;

void setup(){
img = loadImage("test.jpg");
size(img.width, img.height, P2D);
size(img.width, img.height);
opencv = new OpenCV(this, img);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/CalibrationDemo/CalibrationDemo.pde
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ OpenCV opencv;
void setup() {
src = loadImage("checkerboard.jpg");
src.resize(500, 0);
size(src.width, src.height, P2D);
size(src.width, src.height);

opencv = new OpenCV(this, src);
opencv.gray();
Expand Down
3 changes: 2 additions & 1 deletion examples/ColorChannels/ColorChannels.pde
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ int imgH, imgW;

void setup() {
src = loadImage("green_object.png");
src.resize(800,0);
opencv = new OpenCV(this, src);
size(int(opencv.width*1.5), int(opencv.height * 1.5), P2D);
size(int(opencv.width*1.5), int(opencv.height * 1.5));

imgH = src.height/2;
imgW = src.width/2;
Expand Down
2 changes: 1 addition & 1 deletion examples/DilationAndErosion/DilationAndErosion.pde
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ OpenCV opencv;
void setup() {
src = loadImage("pen_sketch.jpg");
src.resize(src.width/2, 0);
size(src.width*2, src.height*2, P2D);
size(src.width*2, src.height*2);

opencv = new OpenCV(this, src);

Expand Down
2 changes: 1 addition & 1 deletion examples/FindContours/FindContours.pde
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ArrayList<Contour> polygons;

void setup() {
src = loadImage("test.jpg");
size(src.width, src.height/2, P2D);
size(src.width, src.height/2);
opencv = new OpenCV(this, src);

opencv.gray();
Expand Down
2 changes: 1 addition & 1 deletion examples/FindEdges/FindEdges.pde
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PImage src, canny, scharr, sobel;

void setup() {
src = loadImage("test.jpg");
size(src.width, src.height, P2D);
size(src.width, src.height);

opencv = new OpenCV(this, src);
opencv.findCannyEdges(20,75);
Expand Down
2 changes: 1 addition & 1 deletion examples/FindHistogram/FindHistogram.pde
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Histogram grayHist, rHist, gHist, bHist;
PImage img;

void setup() {
size(640, 400, P2D);
size(640, 400);
img = loadImage("test.jpg");
opencv = new OpenCV(this, img);

Expand Down
2 changes: 1 addition & 1 deletion examples/HistogramSkinDetection/HistogramSkinDetection.pde
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Mat skinHistogram;
void setup(){
src = loadImage("test.jpg");
src.resize(src.width/2, 0);
size(src.width*2 + 256, src.height, P2D);
size(src.width*2 + 256, src.height);
// third argument is: useColor
opencv = new OpenCV(this, src, true);

Expand Down
2 changes: 1 addition & 1 deletion examples/HoughLineDetection/HoughLineDetection.pde
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ArrayList<Line> lines;
void setup() {
PImage src = loadImage("film_scan.jpg");
src.resize(0, 800);
size(src.width, src.height, P2D);
size(src.width, src.height);

opencv = new OpenCV(this, src);
opencv.findCannyEdges(20, 75);
Expand Down
2 changes: 1 addition & 1 deletion examples/HueRangeSelection/HueRangeSelection.pde
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int upperb = 100;
void setup() {
img = loadImage("colored_balls.jpg");
opencv = new OpenCV(this, img);
size(opencv.width, opencv.height, P2D);
size(opencv.width, opencv.height);
opencv.useColor(HSB);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/LoadAndDisplayImage/LoadAndDisplayImage.pde
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ OpenCV opencv;

void setup() {
opencv = new OpenCV(this, "test.jpg");
size(opencv.width, opencv.height, P2D);
size(opencv.width, opencv.height);
}

void draw() {
Expand Down
2 changes: 1 addition & 1 deletion examples/LumaVsGray/LumaVsGray.pde
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ PImage colorImage, grayImage;
void setup() {
colorImage = loadImage("flashlight.jpg");
opencv = new OpenCV(this, colorImage);
size(opencv.width, opencv.height, P2D);
size(opencv.width, opencv.height);
// Save the gray image so we can compare it to Luma
grayImage = opencv.getSnapshot();
// Use built-in OpenCV function to conver the color image from BGR to LAB color space.
Expand Down
2 changes: 1 addition & 1 deletion examples/RegionOfInterest/RegionOfInterest.pde
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ boolean useROI = true;
void setup() {
src = loadImage("test.jpg");
opencv = new OpenCV(this, src);
size(opencv.width, opencv.height, P2D);
size(opencv.width, opencv.height);
}

void draw() {
Expand Down
88 changes: 24 additions & 64 deletions src/gab/opencv/OpenCV.java
Original file line number Diff line number Diff line change
Expand Up @@ -1068,74 +1068,30 @@ public static void ARGBtoBGRA(Mat rgba, Mat bgra){
public int getSize(){
return width * height;
}


/**
* Convert a 3 channel OpenCV Mat object into
* pixels to be shoved into a 4 channel ARGB PImage's
* pixel array.
*
* @param m
* A Mat you want converted
*/
public int[] threeChanMatToARGBPixels(Mat m){
int pImageChannels = 4;
int numPixels = m.width()*m.height();
int[] intPixels = new int[numPixels];
Mat m2 = new Mat();

// Assumes output PImage is ARGB.
Imgproc.cvtColor(m, m2, Imgproc.COLOR_RGB2RGBA);
byte[] matPixels = new byte[numPixels*pImageChannels];

m2.get(0,0, matPixels);
ByteBuffer.wrap(matPixels).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(intPixels);
return intPixels;
}

/**
* Convert a single channel, gray OpenCV Mat object into
* pixels to be shoved into a 4 channel ARGB PImage's
* pixel array.
*
* @param m
* A Mat you want converted
*/
public int[] grayMatToARGBPixels(Mat m){
int pImageChannels = 4;
int numPixels = m.width()*m.height();
int[] intPixels = new int[numPixels];
Mat m2 = new Mat();

// Assumes output PImage is ARGB.
Imgproc.cvtColor(m, m2, Imgproc.COLOR_GRAY2RGBA);
byte[] matPixels = new byte[numPixels*pImageChannels];

m2.get(0,0, matPixels);
ByteBuffer.wrap(matPixels).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(intPixels);
return intPixels;
}


/**
*
* Convert a 4 channel OpenCV Mat object into
* pixels to be shoved into a 4 channel ARGB PImage's
* pixel array.
*
*
* @param m
* A Mat you want converted
* An RGBA Mat we want converted
* @return
* An int[] formatted to be the pixels of a PImage
*/
public int[] fourChanMatToARGBPixels(Mat m){
int pImageChannels = 4;
int numPixels = m.width()*m.height();
int[] intPixels = new int[numPixels];
byte[] matPixels = new byte[numPixels*pImageChannels];

m.get(0,0, matPixels);
ByteBuffer.wrap(matPixels).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(intPixels);
return intPixels;
public int[] matToARGBPixels(Mat m){
int pImageChannels = 4;
int numPixels = m.width()*m.height();
int[] intPixels = new int[numPixels];
byte[] matPixels = new byte[numPixels*pImageChannels];
m.get(0,0, matPixels);
ByteBuffer.wrap(matPixels).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(intPixels);
return intPixels;
}

/**
* Convert an OpenCV Mat object into a PImage
* to be used in other Processing code.
Expand All @@ -1154,11 +1110,15 @@ public void toPImage(Mat m, PImage img){
img.loadPixels();

if(m.channels() == 3){
img.pixels = threeChanMatToARGBPixels(m);
Mat m2 = new Mat();
Imgproc.cvtColor(m, m2, Imgproc.COLOR_RGB2RGBA);
img.pixels = matToARGBPixels(m2);
} else if(m.channels() == 1){
img.pixels = grayMatToARGBPixels(m);
Mat m2 = new Mat();
Imgproc.cvtColor(m, m2, Imgproc.COLOR_GRAY2RGBA);
img.pixels = matToARGBPixels(m2);
} else if(m.channels() == 4){
img.pixels = fourChanMatToARGBPixels(m);
img.pixels = matToARGBPixels(m);
}

img.updatePixels();
Expand Down

0 comments on commit f25fe8d

Please sign in to comment.