public Letter[] getOpenCirclePositionAsLetters() { openCircle = getOpenCircle(); if (openCircle == null) { return null; } int x = openCircle.getLocation()[0] + (openCircle.getWidth() / 2); int y = openCircle.getLocation()[1] + (openCircle.getHeight() / 2); return getPostionLetters(x, y); }
private double scanPreIntersection( int xx, int yy, int left, int top, int tmpIntersectionWidth, int tmpIntersectionHeight) { double tmpError; int yStep = Math.max(1, tmpIntersectionHeight / (preScanFaktor + 1)); tmpPixelBoth = 0; tmpPixelBButNotA = 0; tmpPixelAButNotB = 0; pixelAll = 0; for (int y = yStep; y <= tmpIntersectionHeight - yStep; y += yStep) { for (int x = 0; x < tmpIntersectionWidth; x++) { pixelAll++; int pixelType = getPixelType(x, y, xx, yy, left, top); switch (pixelType) { case 0: tmpPixelBoth++; break; case 1: tmpPixelBButNotA++; break; case 2: tmpPixelAButNotB++; break; } } } // logger.info((Utilities.getTimer() - startTime)+ "intersection // scantime: "+(Utilities.getTimer()-starter2)); if (pixelAll > 0) { tmpErrorA = (double) tmpPixelAButNotB / (double) (tmpPixelBoth + tmpPixelAButNotB); tmpErrorB = (double) tmpPixelBButNotA / (double) (tmpPixelBButNotA + tmpPixelBoth); tmpErrorTotal = tmpErrorA * errorAWeight + tmpErrorB * errorbWeight; setLocalHeightPercent((double) tmpIntersectionHeight / (double) b.getHeight()); localWidthPercent = (double) tmpIntersectionWidth / (double) b.getWidth(); double lhp = 1.0 - getLocalHeightPercent(); double lwp = 1.0 - localWidthPercent; tmpHeightFaktor = lhp * lhp; tmpWidthFaktor = lwp * lwp; // tmpHeightAFaktor = Math.pow(1.0 - (double) tmpIntersectionHeight // / (double) a.getHeight(), 2); // tmpWidthAFaktor = Math.pow(1.0 - (double) tmpIntersectionWidth / // (double) a.getWidth(), 2); tmpError = tmpErrorTotal; tmpError += Math.min(1.0, tmpHeightFaktor) * intersectionDimensionWeight; tmpError += Math.min(1.0, tmpWidthFaktor) * intersectionDimensionWeight; tmpError += Math.min(1.0, tmpHeightAFaktor) * intersectionAHeightWeight; tmpError += Math.min(1.0, tmpWidthAFaktor) * intersectionAWidthWeight; tmpError /= prescanDivider; tmpError *= 1.2; return (int) (100 * tmpError); } else { return 100; } }
/** * returns the open circle * * @return */ public Letter getOpenCircle() { if (openCircle != null) { return openCircle; } // Graphics g = image.getGraphics(); // g.setColor(Color.black); // g.drawOval(55, 55, 18, 18); getCircles(); // if(true)return null; Letter best = null; int bestwda = Integer.MIN_VALUE; for (PixelObject pixelObject : objectArray) { Letter let = pixelObject.toColoredLetter(); int w = 0; for (int x = 0; x < let.getWidth(); x++) { for (int y = 0; y < let.getHeight(); y++) { if (isBackground(let.getPixelValue(x, y))) { w++; } else { break; } } } for (int y = 0; y < let.getHeight(); y++) { for (int x = 0; x < let.getWidth(); x++) { if (isBackground(let.getPixelValue(x, y))) { w++; } else { break; } } } for (int x = 0; x < let.getWidth(); x++) { for (int y = let.getHeight() - 1; y > 0; y--) { if (isBackground(let.getPixelValue(x, y))) { w++; } else { break; } } } for (int y = 0; y < let.getHeight(); y++) { for (int x = let.getWidth() - 1; x > 0; x--) { if (isBackground(let.getPixelValue(x, y))) { w++; } else { break; } } } int wda = w * 100 / let.getArea(); BasicWindow.showImage(pixelObject.toColoredLetter().getImage(), "" + wda); // TODO if (wda > bestwda && let.getArea() > minArea) { best = let; bestwda = wda; } } openCircle = best; return best; }
/** * returns the Circles Bounds on the Captcha TODO geht nur bei x entlang sollte noch bei y gemacht * werden um bessere ergebnisse zu bekommen * * @param pixelObject * @param captcha * @return */ private int[] getBounds(PixelObject pixelObject) { if (pixelObject.getSize() < 5 || pixelObject.getArea() < minArea) { return null; } Letter let = pixelObject.toColoredLetter(); int r = let.getWidth() / 2; try { int ratio = pixelObject.getHeight() * 100 / pixelObject.getWidth(); if ((ratio > 95 && ratio < 105) || equalElements(let.getGrid()[r][0], let.getGrid()[0][r]) || equalElements(let.getGrid()[r][let.getWidth() - 1], let.getGrid()[0][r]) || equalElements(let.getGrid()[r][0], let.getGrid()[let.getWidth() - 1][r]) || equalElements( let.getGrid()[r][let.getWidth() - 1], let.getGrid()[let.getWidth() - 1][r])) { return new int[] {let.getLocation()[0] + r, let.getLocation()[1] + let.getWidth()}; } } catch (Exception e) { } java.util.List<int[]> best = new ArrayList<int[]>(); int h = let.getLocation()[1] + let.getHeight(); for (int x = let.getLocation()[0]; x < let.getLocation()[0] + let.getWidth(); x++) { int y = let.getLocation()[1]; int c = captcha.grid[x][y]; if (!isBackground(c)) { y++; for (; y < h; y++) { if (isBackground(captcha.grid[x][y])) { break; } } // if (oldy == y || h < y) continue; int oldy = y; for (; y < h; y++) { if (!isBackground(captcha.grid[x][y]) && equalElements(c, captcha.grid[x][y])) { break; } } if (oldy == y || h < y) { continue; } oldy = y; for (; y < h; y++) { if (isBackground(captcha.grid[x][y])) { break; } } if (oldy == y) { continue; } if (y == let.getHeight() && Math.abs(let.getHeight() - let.getWidth()) > 15) { continue; } if (best.size() > 0) { if (y > best.get(0)[0]) { best = new ArrayList<int[]>(); best.add(new int[] {x, y}); } else if (y == best.get(0)[1]) { best.add(new int[] {x, y}); } } else { best.add(new int[] {x, y}); } } } if (best.size() == 0) { return null; } else { int x = 0; for (int[] is : best) { x += is[0]; } return new int[] {x / best.size(), best.get(0)[1]}; } }
private double scanIntersection( int xx, int yy, int left, int top, int tmpIntersectionWidth, int tmpIntersectionHeight) { offset = new int[] {left, top}; imgOffset = new int[] {xx, yy}; intersectionDimension = new int[] {tmpIntersectionWidth, tmpIntersectionHeight}; double tmpError; pixelAll = 0; tmpPixelBButNotA = 0; tmpPixelAButNotB = 0; tmpPixelBoth = 0; tmpCoverageFaktorA = 0; tmpCoverageFaktorB = 0; // long starter=Utilities.getTimer(); bothElements.removeAllElements(); elementGrid = new int[tmpIntersectionWidth][tmpIntersectionHeight]; for (int x = 0; x < tmpIntersectionWidth; x += scanStepX) { for (int y = 0; y < tmpIntersectionHeight; y += scanStepY) { int pixelType = getPixelType(x, y, xx, yy, left, top); pixelAll++; switch (pixelType) { case 0: if (isCreateIntersectionLetter()) { intersectionGrid[x][y] = BOTHCOLOR; } if (cleftFaktor > 0) { getElement( x, y, xx, yy, left, top, pixelType, elementGrid, element = new Vector<Integer>()); if (element.size() > minCleftSize) { bothElements.add(element); } } tmpPixelBoth++; break; case 1: if (overlayNoiseSize <= 0 || hasNeighbour(x, y, xx, yy, left, top, pixelType) > overlayNoiseSize) { tmpPixelBButNotA++; if (isCreateIntersectionLetter()) { intersectionGrid[x][y] = BNACOLOR; } } else { if (isCreateIntersectionLetter()) { intersectionGrid[x][y] = BNAFILTEREDCOLOR; } } break; case 2: if (overlayNoiseSize <= 0 || hasNeighbour(x, y, xx, yy, left, top, pixelType) > overlayNoiseSize) { tmpPixelAButNotB++; if (isCreateIntersectionLetter()) { intersectionGrid[x][y] = ANBCOLOR; } } else { if (isCreateIntersectionLetter()) { intersectionGrid[x][y] = ANBFILTEREDCOLOR; } } break; default: if (isCreateIntersectionLetter()) { intersectionGrid[x][y] = 0xffffff; } } } } // logger.info("Scanner: "+Utilities.getTimer(starter)); // if(getDecodedValue().equalsIgnoreCase("v")&&getBothElementsNum()==3){ // logger.info("JJJ"); // } if (pixelAll > 0 && (bothElements.size() > 0 || cleftFaktor == 0)) { tmpErrorA = (double) tmpPixelAButNotB / (double) (tmpPixelBoth + tmpPixelAButNotB); tmpErrorB = (double) tmpPixelBButNotA / (double) (tmpPixelBButNotA + tmpPixelBoth); tmpErrorTotal = tmpErrorA * errorAWeight + tmpErrorB * errorbWeight; tmpCoverageFaktorA = 1.0 - tmpPixelBoth / ((double) a.getElementPixel() / (scanStepX * scanStepY)); tmpCoverageFaktorB = 1.0 - tmpPixelBoth / ((double) b.getElementPixel() / (scanStepX * scanStepY)); setLocalHeightPercent((double) tmpIntersectionHeight / (double) b.getHeight()); localWidthPercent = (double) tmpIntersectionWidth / (double) b.getWidth(); double lhp = 1.0 - getLocalHeightPercent(); double lwp = 1.0 - localWidthPercent; tmpHeightFaktor = lhp * lhp; tmpWidthFaktor = lwp * lwp; tmpHeightAFaktor = 1.0 - (double) tmpIntersectionHeight / (double) a.getHeight(); tmpWidthAFaktor = 1.0 - (double) tmpIntersectionWidth / (double) a.getWidth(); // logger.info(tmpIntersectionWidth+ "/"+a.getWidth()+" = // "+localWidthPercent+" --> "+tmpWidthFaktor); tmpError = tmpErrorTotal; tmpError += Math.min(1.0, tmpCoverageFaktorA) * coverageFaktorAWeight; tmpError += Math.min(1.0, tmpCoverageFaktorB) * coverageFaktorBWeight; tmpError += Math.min(1.0, tmpHeightFaktor) * intersectionDimensionWeight; tmpError += Math.min(1.0, tmpWidthFaktor) * intersectionDimensionWeight; tmpError += Math.min(1.0, tmpHeightAFaktor) * intersectionAHeightWeight; tmpError += Math.min(1.0, tmpWidthAFaktor) * intersectionAWidthWeight; if (bothElements.size() > 0) { tmpError += (bothElements.size() - 1) * cleftFaktor; } tmpExtensionError = 0.0; if (extensionCodeMethod != null) { try { extensionCodeArguments[1] = tmpError / divider; extensionCodeMethod.invoke(null, extensionCodeArguments); } catch (Exception e) { JDLogger.exception(e); } } tmpError += tmpExtensionError; tmpError /= divider; // tmpError = Math.min(1.0, tmpError); // logger.info(pixelBoth+"_"+(tmpIntersectionHeight * // tmpIntersectionWidth)); if (tmpPixelBoth * owner.getJas().getDouble("inverseFontWeight") < tmpIntersectionHeight * tmpIntersectionWidth) { tmpError = tmpErrorA = tmpErrorB = tmpErrorTotal = 10000.0 / 100.0; } return 100.0 * tmpError; } else { return 10000.0; } }
/** Scan ist die eigentliche vergleichsfunktion. a und b werden dabei gegeneinander verschoben. */ private void scan() { long startTime = System.currentTimeMillis(); double bestValue = 20000.0; preValityPercent = 20000.0; tmpPreScanValue = 20000.0; // logger.info(b.getDecodedValue()+"----"); // scanvarianzen geben an wieviel beim verschieben über die grenzen // geschoben wird. große werte brauchen CPU int vx = getScanVarianceX(); int vy = getScanVarianceY(); vx = Math.min(vx, b.getWidth()); vy = Math.min(vy, b.getHeight()); int scanXFrom = -vx; int scanXTo = a.getWidth() - b.getWidth() + vx; int scanYFrom = -vy; int scanYTo = a.getHeight() - b.getHeight() + vy; int tmp; if (scanXTo < scanXFrom) { tmp = scanXTo; scanXTo = scanXFrom; scanXFrom = tmp; } if (scanYTo < scanYFrom) { tmp = scanYTo; scanYTo = scanYFrom; scanYFrom = tmp; } double value; Letter tmpIntersection = null; if (isCreateIntersectionLetter()) { tmpIntersection = new Letter(); } int left; int right; int top; int bottom; int tmpIntersectionWidth; int tmpIntersectionHeight; // if(b.id==1371) logger.info(" Scan from " + scanXFrom + "/" + scanXTo // + " - " + scanYFrom + "/" + scanYTo + " Var: " + vx + "/" + vy); // schleife verschieb a und b gegeneinander. Dabei wird um den // jeweiligen Mittelpunkt herumgesprungen. Die Warscheinlichsten Fälle // in der Nullage werden zuerst geprüft // if (this.getDecodedValue().equals("1")) // logger.info(this.getDecodedValue() + " :start"); for (int xx = Utilities.getJumperStart(scanXFrom, scanXTo); Utilities.checkJumper(xx, scanXFrom, scanXTo); xx = Utilities.nextJump(xx, scanXFrom, scanXTo, 1)) { for (int yy = Utilities.getJumperStart(scanYFrom, scanYTo); Utilities.checkJumper(yy, scanYFrom, scanYTo); yy = Utilities.nextJump(yy, scanYFrom, scanYTo, 1)) { // Offsets left = Math.max(0, xx); right = Math.min(xx + b.getWidth(), a.getWidth()); top = Math.max(0, yy); bottom = Math.min(yy + b.getHeight(), a.getHeight()); // intersection ^=ausschnitt tmpIntersectionWidth = right - left; tmpIntersectionHeight = bottom - top; if (tmpIntersectionWidth <= 0 || tmpIntersectionHeight <= 0) { // logger.warning("Scannvarianzen zu groß: " + // tmpIntersectionWidth + "/" + tmpIntersectionHeight); continue; } if (isCreateIntersectionLetter()) { tmpIntersection = new Letter(); tmpIntersection.setOwner(owner); intersectionGrid = new int[tmpIntersectionWidth][tmpIntersectionHeight]; tmpIntersection.setGrid(intersectionGrid); } // if (preScanFilter > 0) { tmpPreScanValue = scanPreIntersection(xx, yy, left, top, tmpIntersectionWidth, tmpIntersectionHeight); // logger.info("_"+preScan); if ((int) tmpPreScanValue > preScanFilter) { if (preValityPercent > tmpPreScanValue) { setPreValityPercent(tmpPreScanValue); } continue; } } // logger.info("Scan // "+tmpIntersectionWidth+"/"+tmpIntersectionHeight+" - // "+a.getElementPixel()); value = scanIntersection(xx, yy, left, top, tmpIntersectionWidth, tmpIntersectionHeight); // logger.info(tmpIntersectionWidth+"/"+tmpIntersectionHeight+" // : "+" scanIntersection: "); // // if(getDecodedValue().equalsIgnoreCase("v")&&getBothElementsNum()==3){ // logger.info("JJJ"); // } if (value < bestValue) { bestValue = value; setExtensionError(tmpExtensionError); setValityPercent(value); setHeightFaktor(tmpHeightFaktor); setWidthFaktor(tmpWidthFaktor); setIntersectionHeight(tmpIntersectionHeight); setIntersectionWidth(tmpIntersectionWidth); setIntersectionStartX(xx); setIntersectionStartY(yy); setPosition(left, top); setBothElementNum(bothElements.size()); setCoverageFaktorA(tmpCoverageFaktorA); setCoverageFaktorB(tmpCoverageFaktorB); setPixelErrorA(tmpErrorA); setPreValityPercent(tmpPreScanValue); setPixelErrorB(tmpErrorB); setPixelANotB(tmpPixelAButNotB); setPixelBNotA(tmpPixelBButNotA); setPixelBoth(tmpPixelBoth); intersectionAHeightFaktor = 0.0; intersectionAWidthFaktor = 0.0; setIntersectionAHeightFaktor(tmpHeightAFaktor); setIntersectionAWidthFaktor(tmpWidthAFaktor); setTotalPixelError(tmpErrorTotal); if (isCreateIntersectionLetter()) { setIntersectionLetter(tmpIntersection); } } } } // } scanTime = (int) (System.currentTimeMillis() - startTime); }