Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F92346831
Canny.pde
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Nov 19, 14:13
Size
6 KB
Mime Type
text/x-java
Expires
Thu, Nov 21, 14:13 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
22422869
Attached To
rBAFOURPROJECT InfoVisuGit
Canny.pde
View Options
import gab.opencv.*;
OpenCV opencv;
TwoDThreeD twoD3D;
PImage board1Thresholded, board1Blurred, board1Scharr;
int minHThreshold = 100, maxHThreshold =140;
int minSThreshold = 80, maxSThreshold = 255;
int minBThreshold = 10, maxBThreshold = 170;
PImage img;
final int imageWidth=(800*2/3);
final int imageHeight=(600*2/3);
//--------------------- CHANGE HERE TO MAKE APPEAR ONE OF THE IMAGE OR ALL OF THEM IN LOOP-------------------------
int IMAGE_NUMBER=2; //{1,2,3,4}
boolean DOES_LOOP=false;
//-----------------------------------------------------------------------------------------------------------------
enum kernelType {
scale, blur, gaussian
};
int variable=0;
int variable2=0;
void settings() {
size(imageWidth*3, imageHeight);//width=(800*2/3)*3 and height=(600*2/3)
}
void setup() {
opencv = new OpenCV(this,100,100);
twoD3D=new TwoDThreeD(imageWidth, imageHeight,0);
colorMode(HSB);
frameRate(1);
if (!DOES_LOOP) {
noLoop();
}
}
void draw() {
/*if (cam.available() == true) {
cam.read();
}
img = cam.get();
int I = 100;
img = thresholdHSB(img, minHThreshold, maxHThreshold, minSThreshold, maxSThreshold, minBThreshold, maxBThreshold );
img = new BlobDetection().findConnectedComponents(img, true);
img = convolute(img, kernelType.gaussian);
img = scharr(img);
img = threshold(img, I, false);
image(img, 0, 0);
drawLines(hough(img), img);*/
img=loadImage("src/board"+IMAGE_NUMBER+".jpg");
int resizeX=img.width*2/3;
int resizeY=img.height*2/3;
//img=loadImage("src/board"+4+".jpg");
PImage dispImg=img.copy();
dispImg.resize(resizeX, resizeY);//,
image(dispImg, 0, 0);//show image
int I = 100;
PImage img1=img.copy();
img1 = thresholdHSB(img1, minHThreshold, maxHThreshold, minSThreshold, maxSThreshold, minBThreshold, maxBThreshold );
img1 = new BlobDetection().findConnectedComponents(img1, true);
PImage img3=img1.copy();
img3.resize(resizeX, resizeY);
img1 = convolute(img1, kernelType.gaussian);
img1 = scharr(img1);
img1 = threshold(img1, I, false);
PImage img2=img1.copy();
img2.resize(resizeX, resizeY);
ArrayList<PVector> lines=hough(img2, 4);
List<PVector> corners=new QuadGraph().findBestQuad(lines, resizeX, resizeY, resizeX*resizeY, 0, false);
stroke(0);
fill(0x808080FF);
for (PVector pv : corners) {
ellipse(pv.x, pv.y, 30, 30);
}
noFill();
stroke(255, 0, 0);
drawLines(lines, dispImg);
for(PVector c:corners){
c.set(c.x,c.y,1);
}
PVector rotation=twoD3D.get3DRotations(corners);
rotation.set((rotation.x)/PI*180,(rotation.y)/PI*180,(rotation.z)/PI*180);
print(rotation);
image(img2, resizeX, 0);//show image
image(img3, resizeX*2, 0);//show image
IMAGE_NUMBER=(IMAGE_NUMBER)%4+1;
}
//if inverted = false take values for 0 to threshol
PImage threshold(PImage img, int threshold, boolean inverted) {
PImage result = createImage(img.width, img.height, RGB);
for (int i = 0; i < img.width * img.height; i++) {
result.pixels[i]=brightness(img.pixels[i])>threshold^inverted?color(255):color(0);
}
return result;
}
PImage thresholdHSB(PImage img, int minH, int maxH, int minS, int maxS, int minB, int maxB) {
PImage result = createImage(img.width, img.height, RGB);
for (int i = 0; i < img.width * img.height; i++) {
if (hue(img.pixels[i])<minH||maxH<hue(img.pixels[i])||
saturation(img.pixels[i])<minS||maxS<saturation(img.pixels[i])||
brightness(img.pixels[i])<minB||maxB<brightness(img.pixels[i])) {
result.pixels[i]=0;
} else {
result.pixels[i]=Integer.MAX_VALUE;
}
}
return result;
}
boolean imagesEqual(PImage img1, PImage img2) {
if (img1.width != img2.width || img1.height != img2.height)
return false;
for (int i = 0; i < img1.width*img1.height; i++)
//assuming that all the three channels have the same value
if (red(img1.pixels[i]) != red(img2.pixels[i])) {
//if((i%img1.width)!=0 ||(i%img1.width)!=img1.width-1 ||(i/img1.width)!=0 ||(i/img1.width)!=img1.width-1 )
//println(i%img1.width+" "+i/img1.width + (red(img1.pixels[i]) - red(img2.pixels[i])) );
return false;
}
return true;
}
PImage scharr(PImage img) {
float[][] vKernel = {
{ 3, 0, -3 },
{ 10, 0, -10 },
{ 3, 0, -3 } };
float[][] hKernel = {
{ 3, 10, 3 },
{ 0, 0, 0 },
{ -3, -10, -3 } };
PImage result = createImage(img.width, img.height, ALPHA);
// clear the image
for (int i = 0; i < img.width * img.height; i++) {
result.pixels[i] = color(0);
}
float max=0;
float[] buffer = new float[img.width * img.height];
// *************************************
// Implement here the double convolution
int N = 3; //kernel size
for (int x = 1; x<img.width-1; ++x) {
for (int y = 1; y<img.height-1; y++) {
float sum_h = 0, sum_v = 0;
for (int i = 0; i< N; ++i) {
for (int j = 0; j<N; ++j) {
float brightness = brightness(img.pixels[(x-N/2+i)+ img.width *(y-N/2+j)]);
sum_h += hKernel[i][j] * brightness;
sum_v += vKernel[i][j] * brightness;
}
}
float value = sqrt(pow(sum_h, 2) + pow(sum_v, 2));
max = (value > max )? value : max;
buffer[x+y*img.width] = value;
}
}
// *************************************
for (int y = 1; y < img.height - 1; y++) { // Skip top and bottom edges
for (int x = 1; x < img.width - 1; x++) { // Skip left and right
int val=(int) ((buffer[y * img.width + x] / max)*255);
result.pixels[y * img.width + x]=color(val);
}
}
return result;
}
PImage convolute(PImage img, kernelType kt) {
int N = 3; //kernel size
float[][] kernel;
float normFactor ;
if (kt == kernelType.scale ) {
float[][] kernel1 = {{ 0, 0, 0 },
{ 0, 2, 0 },
{ 0, 0, 0 }};
kernel = kernel1;
normFactor = 1f;
} else if (kt == kernelType.blur) {
float[][] kernel2 = {{ 0, 1, 0 },
{ 1, 0, 1 },
{ 0, 1, 0 }};
kernel = kernel2;
normFactor = 1f;
} else {
float[][] gaussianKernel =
{{ 9, 12, 9 },
{ 12, 15, 12 },
{ 9, 12, 9 }};
kernel = gaussianKernel;
normFactor = 0f;
for (int i = 0; i< N; ++i) {
for (int j = 0; j<N; ++j) {
normFactor += kernel[i][j];
}
}
}
// create a greyscale image (type: ALPHA) for output
PImage result = createImage(img.width, img.height, ALPHA);
//
// for each (x,y) pixel in the image:
for (int x = 1; x< img.width-1; x++) {
for (int y = 1; y<img.height-1; y++) {
int sum = 0;
for (int i = 0; i< N; ++i) {
for (int j = 0; j<N; ++j) {
sum += kernel[i][j] * brightness(img.pixels[(x-N/2+i)+ img.width *(y-N/2+j)]);
}
}
result.pixels[x+y*img.width] = color((int)(sum / normFactor));
}
}
return result;
}
Event Timeline
Log In to Comment