📄 pixelmap.java
字号:
if (x<0 || y<0 || x>=this.width || y>=this.height) return false;
return matrix[x][y];
}
private boolean isBoundaryPoint(int x, int y) {
if (!getPointValue(x,y)) return false; // ak je bod biely, return false
// konturovy bod ma aspon jeden bod v okoli biely
if (!getPointValue(x-1,y-1) ||
!getPointValue(x-1,y+1) ||
!getPointValue(x+1,y-1) ||
!getPointValue(x+1,y+1) ||
!getPointValue(x,y+1) ||
!getPointValue(x,y-1) ||
!getPointValue(x+1,y) ||
!getPointValue(x-1,y) ) return true;
return false;
}
private int n(int x, int y) { // pocet ciernych bodov v okoli
int n=0;
if (getPointValue(x-1,y-1)) n++;
if (getPointValue(x-1,y+1)) n++;
if (getPointValue(x+1,y-1)) n++;
if (getPointValue(x+1,y+1)) n++;
if (getPointValue(x,y+1)) n++;
if (getPointValue(x,y-1)) n++;
if (getPointValue(x+1,y)) n++;
if (getPointValue(x-1,y)) n++;
return n;
}
// number of 0-1 transitions in ordered sequence 2,3,...,8,9,2
private int t(int x, int y) {
int n=0; // number of 0-1 transitions
// proceeding tranisions 2-3, 3-4, 4-5, 5-6, 6-7, 7-8, 8-9, 9-2
for (int i=2; i<=8; i++) {
if (!p(i,x,y) && p(i+1,x,y)) n++;
}
if (!p(9,x,y) && p(2,x,y)) n++;
return n;
}
/** okolie bodu p1
* p9 p2 p3
* p8 p1 p4
* p7 p6 p5
*/
private boolean p(int i, int x, int y) {
if (i==1) return getPointValue(x,y);
if (i==2) return getPointValue(x,y-1);
if (i==3) return getPointValue(x+1,y-1);
if (i==4) return getPointValue(x+1,y);
if (i==5) return getPointValue(x+1,y+1);
if (i==6) return getPointValue(x,y+1);
if (i==7) return getPointValue(x-1,y+1);
if (i==8) return getPointValue(x-1,y);
if (i==9) return getPointValue(x-1,y-1);
return false;
}
private boolean step1passed(int x, int y) {
int n = n(x,y);
return (
(2 <= n && n <= 6) &&
t(x,y) == 1 &&
( !p(2,x,y) || !p(4,x,y) || !p(6,x,y) ) &&
( !p(4,x,y) || !p(6,x,y) || !p(8,x,y) )
);
}
private boolean step2passed(int x, int y) {
int n = n(x,y);
return (
(2 <= n && n <= 6) &&
t(x,y) == 1 &&
( !p(2,x,y) || !p(4,x,y) || !p(8,x,y) ) &&
( !p(2,x,y) || !p(6,x,y) || !p(8,x,y) )
);
}
private void findBoundaryPoints(PointSet set) {
if (!set.isEmpty()) set.clear();
for (int x=0; x<this.width; x++) {
for (int y=0; y<this.height; y++) {
if (isBoundaryPoint(x,y)) set.add(new Point(x,y));
}
}
}
public PixelMap skeletonize() { // vykona skeletonizaciu
// thinning procedure
PointSet flaggedPoints = new PointSet();
PointSet boundaryPoints = new PointSet();
boolean cont;
do {
cont = false;
findBoundaryPoints(boundaryPoints);
// apply step 1 to flag boundary points for deletion
for (Point p : boundaryPoints) {
if (step1passed(p.x, p.y)) flaggedPoints.add(p);
}
// delete flagged points
if (!flaggedPoints.isEmpty()) cont = true;
for (Point p : flaggedPoints) {
this.matrix[p.x][p.y] = false;
boundaryPoints.remove(p);
}
flaggedPoints.clear();
// apply step 2 to flag remaining points
for (Point p : boundaryPoints) {
if (step2passed(p.x, p.y)) flaggedPoints.add(p);
}
// delete flagged points
if (!flaggedPoints.isEmpty()) cont = true;
for (Point p : flaggedPoints) {
this.matrix[p.x][p.y] = false;
}
boundaryPoints.clear();
flaggedPoints.clear();
} while (cont);
return (this);
}
// redukcia sumu /////////////////////////////
public PixelMap reduceNoise() {
PointSet pointsToReduce = new PointSet();
for (int x=0; x<this.width; x++) {
for (int y=0; y<this.height; y++) {
if (n(x,y) < 4) pointsToReduce.add(new Point(x,y)); // doporucene 4
}
}
// zmazemee oznacene body
for (Point p : pointsToReduce) this.matrix[p.x][p.y] = false;
return (this);
}
// reduce other pieces /////////////////////////////
private boolean isInPieces(PieceSet pieces, int x, int y) {
for (Piece piece : pieces) // pre vsetky kusky
for (Point point : piece) // pre vsetky body na kusku
if (point.equals(x,y)) return true;
return false;
}
private boolean seedShouldBeAdded(Piece piece, Point p) {
// ak sa nevymyka okrajom
if (p.x<0 || p.y<0 || p.x>=this.width || p.y>=this.height) return false;
// ak je cierny,
if (!this.matrix[p.x][p.y]) return false;
// ak este nie je sucastou ziadneho kuska
for (Point piecePoint : piece)
if (piecePoint.equals(p)) return false;
return true;
}
// vytvori novy piece, najde okolie (piece) napcha donho vsetky body a vrati
// vstupom je nejaka mnozina "ciernych" bodov, z ktorej algoritmus tie
// body vybera
private Piece createPiece(PointSet unsorted) {
Piece piece = new Piece();
PointSet stack = new PointSet();
stack.push(unsorted.lastElement());
while(!stack.isEmpty()) {
Point p = stack.pop();
if (seedShouldBeAdded(piece, p)) {
piece.add(p);
unsorted.removePoint(p);
stack.push(new Point(p.x+1, p.y));
stack.push(new Point(p.x-1, p.y));
stack.push(new Point(p.x, p.y+1));
stack.push(new Point(p.x, p.y-1));
}
}
piece.createStatistics();
return piece;
}
public PieceSet findPieces() {
//boolean continueFlag;
PieceSet pieces = new PieceSet();
// vsetky cierne body na kopu.
PointSet unsorted = new PointSet();
for (int x=0; x<this.width; x++)
for (int y=0; y<this.height; y++)
if (this.matrix[x][y]) unsorted.add(new Point(x,y));
// pre kazdy cierny bod z kopy,
while (!unsorted.isEmpty()) {
// createPiece vytvori novy piece s tym ze vsetky pouzite body vyhodi von z kopy
pieces.add(createPiece(unsorted));
}
/*
do {
continueFlag = false;
boolean loopBreak = false;
for (int x = 0; x<this.width; x++) {
for (int y = 0; y<this.height; y++) { // for each pixel
// ak je pixel cierny, a nie je sucastou ziadneho kuska
if (this.matrix[x][y] && !isInPieces(pieces,x,y)) {
continueFlag = true;
pieces.add(createPiece(x,y));
}
}// for y
} // for x
} while (continueFlag);
*/
return pieces;
}
// redukuje ostatne pieces a vracia ten najlepsi
public PixelMap reduceOtherPieces() {
if (this.bestPiece != null) return this; // bestPiece uz je , netreba dalej nic
PieceSet pieces = findPieces();
int maxCost = 0;
int maxIndex = 0;
// najdeme najlepsi cost
for (int i=0; i<pieces.size(); i++) {
if (pieces.elementAt(i).cost() > maxCost) {
maxCost = pieces.elementAt(i).cost();
maxIndex = i;
}
}
// a ostatne zmazeme
for (int i=0; i<pieces.size(); i++) {
if (i != maxIndex) pieces.elementAt(i).bleachPiece();
}
if (pieces.size()!=0) this.bestPiece = pieces.elementAt(maxIndex);
return this;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -