我是面向对象编程的新手,它正在显示!
这就是我希望我的程序要做的事情:
显示像这样的黑白图像,除了白色而不是粉红色: []
不幸的是,我有两个问题。
chain[2]
)保存到我的链表中时,它会保存所有其他链。所以我只想保存chain[2]
中的点数,但这些完全相同的点会被放入chain[1]
和chain[0]
。我很遗憾我的代码可能有多乱。我从未接受过正规训练,只是想学习。
// Computes "chain code" to trace edge of a blob.
// This code only traces the first blob found,
// Where "first" is defined as "having a pixel with a lower index" (i.e. higher and to the left).
// For multi-blobs, additional code would be needed to label connected blobs first.
// (See "connected component labelling" or "clustering").
// Note that the code does not find interior (nested) contours.
Point2d firstPixel;
PImage img;
int N=1;
//a class for single coordinate pairs that we will feed into a chain
class Point2d {
int x, y;
Point2d (int inx, int iny) {
x = inx;
y = iny;
}
}
ArrayList<ArrayList<Point2d>> chainlist = new ArrayList<ArrayList<Point2d>>();
ArrayList<Point2d>[] chain = (ArrayList<Point2d>[])new ArrayList[5];
//===============================================
void setup() {
img = loadImage("ex1.png");
size(img.width, img.height);
firstPixel = new Point2d(0, 0);
chain[0] = new ArrayList<Point2d>();
}
//===============================================
void draw() {
background(255);
image(img,0,0);
loadPixels();
findFirstBlobPixel();
compute8NeighborChainCode (firstPixel.x, firstPixel.y);
drawChainCode();
drawAnnotations();
}
//===============================================
void drawSomeWhiteBlobs() {
noStroke();
fill(0);
ellipse(mouseX, mouseY, 140, 150);
ellipse(230, 200, 100, 70);
ellipse(280, 190, 70, 140);
rect(300, frameCount%height, 50, 50);
}
//===============================================
void findFirstBlobPixel() {
boolean foundFirst = false;
for (int y=mouseY; y<height; y++) {
for (int x=mouseX; x<width; x++) {
//all pixels in the display window are kept in an array called pixels[]
//we check the color of the current pixel
color val = pixels[y*width + x];
//we check for a black pixel
if (!foundFirst && brightness(val) < 255) {
firstPixel.x = x;
firstPixel.y = y;
foundFirst = true;
}
}
}
}
//===============================================
/* Compute the chain code of the object beginning at pixel (i,j).
Return the code as NN integers in the array C. */
void compute8NeighborChainCode (int i, int j) {
int val, n, m, q, r, ii, d, dii;
int lastdir, jj;
chain[0].clear();
// Table given index offset for each of the 8 directions.
int di[] = {
0, -1, -1, -1, 0, 1, 1, 1
};
int dj[] = {
1, 1, 0, -1, -1, -1, 0, 1
};
val = pixels[j*width+i];
n = 0; /* Initialize for starting pixel */
q = i;
r = j;
lastdir = 4;
do {
m = 0;
dii = -1;
d = 100;
for (ii=lastdir+1; ii<lastdir+8; ii++) { /* Look for next */
jj = ii%8;
if (isPixelLocationLegal (di[jj]+q, dj[jj]+r)) {
if ( pixels[(dj[jj]+r)*width + (di[jj]+q)] == val) {
dii = jj;
m = 1;
break;
}
}
}
if (m != 0) { /* Found the next pixel ... */
Point2d P = new Point2d(q, r);
chain[0].add(P);
q += di[dii];
r += dj[dii];
lastdir = (dii+5)%8;
}
else {
break; /* NO next pixel */
}
}
while ( (q!=i) || (r!=j) ); /* Stop when next to start pixel */
}
//===============================================
void drawChainCode() {
noFill();
strokeWeight(3);
stroke(255, 0, 0);
/* beginShape();
for (int i=0; i<chain.size(); i++) {
Point2d P = (Point2d)chain.get(i);
vertex(P.x, P.y);
}
endShape(); */
for (int C=0; C<chainlist.size(); C++) {
ArrayList<Point2d> currchain = chainlist.get(C);
beginShape();
//print all point pairs in that chain
for (int i=0; i<currchain.size(); i++) {
Point2d P = (Point2d)currchain.get(i);
vertex(P.x, P.y);
}
endShape();
}
}
//===============================================
void drawAnnotations() {
fill(#8BC0FF);
String heading = "BITMAP-TO-VECTOR BLOB TRACER\n";
heading += "This app extracts the contour (red) of the topleft-most blob,\n";
heading += "using an 8-neighbor connected chain code.\n";
heading += "# Contour points: " + chain[0].size();
heading += "# Chains saved: " + chainlist.size();
text(heading, 15, 20);
}
//===============================================
boolean isPixelLocationLegal (int x, int y) {
if (x < 0 || x >= width) return false;
if (y < 0 || y >= height) return false;
return true;
}
void keyPressed() {
if (key == 's'){
println("Saving pointchain in slot "+N);
chain[N] = new ArrayList<Point2d>();
println("the chain we're saving is this big: "+chain[0].size());
chain[N] = chain[0];
chainlist.add(chain[N]);
println("A new pointchain "+chain[N].size() + " points long has been added.");
for (int B=0; B<N; B++){
println("Pointchain "+B+" is "+chain[B].size() + " points long.");
}
N ++;
println("You now have "+ chainlist.size() +" chains saved.");
}
if (key == 'p'){
//select each chain and print it
println("Printing chains. You have "+chainlist.size()+".");
for (int C=0; C<chainlist.size(); C++) {
println("Chain " +C+ ", which is "+chain[C].size()+" points long, printing.");
for (int i=0; i<chain[C].size(); i++) {
Point2d D = (Point2d)chain[C].get(i);
println(D.x, D.y);
}
}
}
}
答案 0 :(得分:0)