Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F112208929
Canny4109982850453785798.autosave
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
Thu, May 8, 22:31
Size
6 KB
Mime Type
text/x-c++
Expires
Sat, May 10, 22:31 (2 d)
Engine
blob
Format
Raw Data
Handle
26049567
Attached To
rBAFOURPROJECT InfoVisuGit
Canny4109982850453785798.autosave
View Options
import gab.opencv.*;
static enum kernelType {
scale, blur, gaussian
};
class Canny extends PApplet {
static final int imageWidth=800;
static final int imageHeight=600;
int setupPartSize;
int resizeX;
int resizeY;
OpenCV opencv;
TwoDThreeD twoD3D;
PImage img0;
PImage blobDetection;
PImage afterScharr;
PImage camImage;
List<PVector> corners;
int minHThreshold = 100, maxHThreshold =140;
int minSThreshold = 80, maxSThreshold = 255;
int minBThreshold = 10, maxBThreshold = 170;
Canny(int setupPartSize) {
this.setupPartSize = setupPartSize;
resizeX =setupPartSize-20;
resizeY= resizeX * imageHeight/imageWidth ;
opencv = new OpenCV(this, 100, 100);
twoD3D=new TwoDThreeD(imageWidth, imageHeight, 0);
}
PVector update(int minHThreshold , int maxHThreshold,
int minSThreshold, int maxSThreshold,
int minBThreshold , int maxBThreshold) {
this.minHThreshold = minHThreshold;
this.maxHThreshold = maxHThreshold;
this.minSThreshold = minSThreshold;
this.maxSThreshold = maxSThreshold;
this.minBThreshold = minBThreshold;
this.maxBThreshold = maxBThreshold;
if (cam.available() == true) {
cam.read();
}
camImage = cam.get();
img0 = camImage.copy();
camImage.resize(resizeX, resizeY);
int I = 100;
img0 = thresholdHSB(img0, minHThreshold, maxHThreshold, minSThreshold, maxSThreshold, minBThreshold, maxBThreshold );
img0 = new BlobDetection().findConnectedComponents(img0, true);
blobDetection =img0.copy();
blobDetection.resize(resizeX, resizeY);
img0 = convolute(img0, kernelType.gaussian);
img0 = scharr(img0);
img0 = threshold(img0, I, false);
afterScharr=img0.copy();
afterScharr.resize(resizeX, resizeY);
ArrayList<PVector> lines=hough(img0, 4);
corners=new QuadGraph().findBestQuad(lines, imageWidth, imageHeight, imageWidth*imageHeight, 0, false);
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);
return rotation;
}
//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;
}
PImage getBlobDetection(){
return blobDetection;
}
PImage getCamImage(){
return camImage;
}
PImage getAfterScharr(){
return afterScharr;
}
List<PVector> getCorners(){
List<PVector> out = new ArrayList();
float coef = 1.25;
for (PVector c : corners) {
out.add(new PVector(c.x/((float)(imageWidth))*resizeX*coef, c.y/((float)(imageHeight))*resizeY*coef, 1));
}
return out;
}
}
Event Timeline
Log In to Comment