public static boolean checkAngleL(Mat binary) { int leftPointX = -1; double pixel[]; for (int i = 0; i < binary.cols(); i++) { pixel = binary.get(0, i); if (pixel[0] != 0.0) { leftPointX = i; } } int rightPointY = -1; for (int i = 0; i < binary.rows(); i++) { pixel = binary.get(i, binary.cols() - 1); if (pixel[0] != 0.0) { rightPointY = i; break; } } double opposite = rightPointY; double adjacent = binary.cols() - 1 - leftPointX; double angle = Math.atan2(opposite, adjacent) * (180 / Math.PI); if (angle >= 50.0 & angle < 70.0) { return true; // angle indicates L } else { return false; // angle indicates not L } }
private static double perceptualHash(String sourceImagePath, String targetImagePath) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat matSrc = Imgcodecs.imread(sourceImagePath, Imgcodecs.CV_LOAD_IMAGE_COLOR); Mat matTrgt = Imgcodecs.imread(targetImagePath, Imgcodecs.CV_LOAD_IMAGE_COLOR); Mat matSrcResize = new Mat(); Mat matTrgtResize = new Mat(); Imgproc.resize( matSrc, matSrcResize, new Size(matSrc.width(), matSrc.height()), 0, 0, Imgproc.INTER_NEAREST); Imgproc.resize( matTrgt, matTrgtResize, new Size(matTrgt.width(), matTrgt.height()), 0, 0, Imgproc.INTER_LANCZOS4); Mat matSrcDst = new Mat(); Mat matTrgtDst = new Mat(); Imgproc.resize(matSrcResize, matSrcDst, new Size(8, 8), 0, 0, Imgproc.INTER_CUBIC); Imgproc.resize(matTrgtResize, matTrgtDst, new Size(8, 8), 0, 0, Imgproc.INTER_CUBIC); Imgproc.cvtColor(matSrcDst, matSrcDst, Imgproc.COLOR_BGR2GRAY); Imgproc.cvtColor(matTrgtDst, matTrgtDst, Imgproc.COLOR_BGR2GRAY); int iAvgSrc = 0, iAvgTrgt = 0; int[] arrSrc = new int[64]; int[] arrTrgt = new int[64]; for (int i = 0; i < 8; i++) { byte[] dataSrc = new byte[8]; matSrcDst.get(i, 0, dataSrc); byte[] dataTrgt = new byte[8]; matTrgtDst.get(i, 0, dataTrgt); int tmp = i * 8; for (int j = 0; j < 8; j++) { int tmpSrc = tmp + j; arrSrc[tmpSrc] = dataSrc[j] / 4 * 4; arrTrgt[tmpSrc] = dataTrgt[j] / 4 * 4; iAvgSrc += arrSrc[tmpSrc]; iAvgTrgt += arrTrgt[tmpSrc]; } } iAvgSrc /= 64; iAvgTrgt /= 64; for (int i = 0; i < 64; i++) { arrSrc[i] = (arrSrc[i] >= iAvgSrc) ? 1 : 0; arrTrgt[i] = (arrTrgt[i] >= iAvgTrgt) ? 1 : 0; } int iDiffNum = 0; for (int i = 0; i < 64; i++) if (arrSrc[i] != arrTrgt[i]) ++iDiffNum; return 1.0 - (double) iDiffNum / 64; }
Mat setupMatrix(double hSmallToGlass[], double hGlassToSmall[]) { Mat hSmallToGlassMat = HMatFromArray(hSmallToGlass); Mat hGlassToSmallMat = hSmallToGlassMat.inv(); double denominator = hGlassToSmallMat.get(2, 2)[0]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) hGlassToSmall[i * 3 + j] = hGlassToSmallMat.get(i, j)[0] / denominator; return hSmallToGlassMat; }
public SaliencyResult saliencyalgorithmInterface(ImageObj imgobj, String method) { // TODO Auto-generated method stub float min = Float.MAX_VALUE; float max = Float.MIN_VALUE; String imgpath = imgobj.getSourcePath(); int k_num = imgobj.getK_num(); SaliencyResult result = new SaliencyResult(); Mat img = Highgui.imread(imgpath, Highgui.CV_LOAD_IMAGE_GRAYSCALE); Mat saliencyMap = new Mat(); saliencyMap.create(img.rows(), img.cols(), CvType.CV_16U); int HistGram[] = new int[256]; int Gray[] = new int[img.cols() * img.rows()]; int Dist[] = new int[256]; float DistMap[] = new float[img.rows() * img.cols()]; for (int row = 0; row < img.rows(); row++) { int CurIndex = row * img.cols(); for (int col = 0; col < img.cols(); col++) { HistGram[(int) (img.get(row, col)[0])]++; Gray[CurIndex] = (int) (img.get(row, col)[0]); CurIndex++; } } for (int Y = 0; Y < 256; Y++) { int Value = 0; for (int X = 0; X < 256; X++) Value += Math.abs(Y - X) * HistGram[X]; Dist[Y] = Value; } for (int row = 0; row < img.rows(); row++) { int CurIndex = row * img.cols(); for (int col = 0; col < img.cols(); col++) { DistMap[CurIndex] = Dist[Gray[CurIndex]]; if (DistMap[CurIndex] < min) min = DistMap[CurIndex]; if (DistMap[CurIndex] > max) max = DistMap[CurIndex]; CurIndex++; } } for (int row = 0; row < img.rows(); row++) { int CurIndex = row * img.cols(); for (int col = 0; col < img.cols(); col++) { saliencyMap.put(row, col, partTwo((DistMap[CurIndex] - min) / (max - min) * 255)); CurIndex++; } } new findMarkUtil(); int nums[] = null; if (method == "kmeans") { nums = findMarkUtil.findMarkUtil_kmeans(saliencyMap, k_num, 255, 0, 5); } else if (method == "random") { nums = findMarkUtil.findMarkUtil_random(saliencyMap, k_num, 255); } result.setK_num(k_num); result.setSource(imgpath); result.setResult(nums); result.setSaliency(saliencyMap); return result; }
private float derivImodelAlpha(int i, int idx) { float Gx, Gy; float res; int x = model.get(idx).getX(); int y = model.get(idx).getY(); Gx = (float) dxModel.get(x, y)[0]; Gy = (float) dyModel.get(x, y)[0]; res = Gx * s[idx * 3][i] + Gy * s[idx * 3 + 2][i]; return res; }
/** * Helper function to convert a Mat into a BufferedImage. Taken from * http://answers.opencv.org/question/10344/opencv-java-load-image-to-gui Author: 'Lucky Luke' * * @param matrix Mat of type CV_8UC3 or CV_8UC1 * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY */ private static BufferedImage matToBufferedImage(Mat matrix) { int cols = matrix.cols(); int rows = matrix.rows(); int elemSize = (int) matrix.elemSize(); byte[] data = new byte[cols * rows * elemSize]; int type; matrix.get(0, 0, data); switch (matrix.channels()) { case 1: type = BufferedImage.TYPE_BYTE_GRAY; break; case 3: type = BufferedImage.TYPE_3BYTE_BGR; // bgr to rgb byte b; for (int i = 0; i < data.length; i = i + 3) { b = data[i]; data[i] = data[i + 2]; data[i + 2] = b; } break; default: return null; } BufferedImage image = new BufferedImage(cols, rows, type); image.getRaster().setDataElements(0, 0, cols, rows, data); return image; }
private Mat rotateImage(Mat src, double angle) { Point center = new Point(src.cols() / 2, src.rows() / 2); double scale = 1.0; Mat rotateMat = Imgproc.getRotationMatrix2D(center, angle, scale); Mat dst = new Mat(); Size frameSize = new Size(src.rows(), src.cols()); // adjust dst center point double m[] = new double[6]; rotateMat.get(0, 0, m); m[2] += (frameSize.width - src.cols()) / 2; m[5] += (frameSize.height - src.rows()) / 2; rotateMat.put(0, 0, m); Imgproc.warpAffine( src, dst, rotateMat, frameSize, Imgproc.INTER_LINEAR, Imgproc.BORDER_CONSTANT, Scalar.all(0)); return dst; }
private Scalar converScalarHsv2Rgba(Scalar hsvColor) { Mat pointMatRgba = new Mat(); Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor); Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL, 4); return new Scalar(pointMatRgba.get(0, 0)); }
protected static void rotateXAxis(Mat rotation) { // get the matrix corresponding to the rotation vector Mat R = new Mat(3, 3, CvType.CV_64FC1); Calib3d.Rodrigues(rotation, R); // create the matrix to rotate 90º around the X axis // 1, 0, 0 // 0 cos -sin // 0 sin cos double[] rot = { 1, 0, 0, 0, 0, -1, 0, 1, 0 }; // multiply both matrix Mat res = new Mat(3, 3, CvType.CV_64FC1); double[] prod = new double[9]; double[] a = new double[9]; R.get(0, 0, a); for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { prod[3 * i + j] = 0; for (int k = 0; k < 3; k++) { prod[3 * i + j] += a[3 * i + k] * rot[3 * k + j]; } } // convert the matrix to a vector with rodrigues back res.put(0, 0, prod); Calib3d.Rodrigues(res, rotation); }
public static void main(String[] args) { try { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); File input = new File("traffic_signal.jpg"); BufferedImage image = ImageIO.read(input); byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3); mat.put(0, 0, data); Mat mat1 = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC1); Imgproc.cvtColor(mat, mat1, Imgproc.COLOR_RGB2GRAY); byte[] data1 = new byte[mat1.rows() * mat1.cols() * (int) (mat1.elemSize())]; mat1.get(0, 0, data1); BufferedImage image1 = new BufferedImage(mat1.cols(), mat1.rows(), BufferedImage.TYPE_BYTE_GRAY); image1.getRaster().setDataElements(0, 0, mat1.cols(), mat1.rows(), data1); File ouptut = new File("output\\grayscale_" + new Date().getTime() + ".jpg"); ImageIO.write(image1, "jpg", ouptut); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } }
private static void printMatI(Mat mat) { int[] data = new int[mat.channels()]; for (int r = 0; r < mat.rows(); r++) { for (int c = 0; c < mat.cols(); c++) { mat.get(r, c, data); log(lvl, "(%d, %d) %s", r, c, Arrays.toString(data)); } } }
public static BufferedImage bufferedImage(Mat m) { int type = BufferedImage.TYPE_BYTE_GRAY; if (m.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; } BufferedImage image = new BufferedImage(m.cols(), m.rows(), type); m.get( 0, 0, ((DataBufferByte) image.getRaster().getDataBuffer()).getData()); // get all the pixels return image; }
public void predictOutput() { network.load(Settings.networkFile); for (int ntestSample = 0; ntestSample < networkInput.rows(); ntestSample++) { Mat testSample = networkInput.row(ntestSample); Mat classificationResult = new Mat(1, Settings.CLASSES, CvType.CV_32F); // System.out.println(testSample.dump()); network.predict(testSample, classificationResult); // System.out.println(classificationResult.dump()); int maxIndex = 0; double value = 0; double maxValue = classificationResult.get(0, 0)[0]; for (int index = 1; index < Settings.CLASSES; index++) { value = classificationResult.get(0, index)[0]; if (value > maxValue) { maxValue = value; maxIndex = index; } } if (networkOutput.get(ntestSample, maxIndex)[0] != 1) { for (int classIndex = 0; classIndex < Settings.CLASSES; classIndex++) { if (networkOutput.get(ntestSample, classIndex)[0] == 1) { results[classIndex][1]++; } } } else { results[maxIndex][0]++; results[maxIndex][1]++; } } }
public static Image convertMatToImage(Mat m) { int type = BufferedImage.TYPE_BYTE_GRAY; if (m.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; } int bufferSize = m.channels() * m.cols() * m.rows(); byte[] b = new byte[bufferSize]; m.get(0, 0, b); BufferedImage image = new BufferedImage(m.cols(), m.rows(), type); final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(b, 0, targetPixels, 0, b.length); return image; }
/** Capture images and run color processing through here */ public void capture() { VideoCapture camera = new VideoCapture(); camera.set(12, -20); // change contrast, might not be necessary // CaptureImage image = new CaptureImage(); camera.open(0); // Useless if (!camera.isOpened()) { System.out.println("Camera Error"); // Determine whether to use System.exit(0) or return } else { System.out.println("Camera OK"); } boolean success = camera.read(capturedFrame); if (success) { try { processWithContours(capturedFrame, processedFrame); } catch (Exception e) { System.out.println(e); } // image.processFrame(capturedFrame, processedFrame); // processedFrame should be CV_8UC3 // image.findCaptured(processedFrame); // image.determineKings(capturedFrame); int bufferSize = processedFrame.channels() * processedFrame.cols() * processedFrame.rows(); byte[] b = new byte[bufferSize]; processedFrame.get(0, 0, b); // get all the pixels // This might need to be BufferedImage.TYPE_INT_ARGB img = new BufferedImage( processedFrame.cols(), processedFrame.rows(), BufferedImage.TYPE_INT_RGB); int width = (int) camera.get(Highgui.CV_CAP_PROP_FRAME_WIDTH); int height = (int) camera.get(Highgui.CV_CAP_PROP_FRAME_HEIGHT); // img.getRaster().setDataElements(0, 0, width, height, b); byte[] a = new byte[bufferSize]; System.arraycopy(b, 0, a, 0, bufferSize); Highgui.imwrite("camera.jpg", processedFrame); System.out.println("Success"); } else System.out.println("Unable to capture image"); camera.release(); }
private void seamlessEdges(Mat image) { int imageHeight = image.height(); int imageWidth = image.width(); double smoothRange = smoothValue * (imageWidth / 2000f); double smoothRangeHalf = smoothRange / 2; double pixel1[]; double pixel2[]; double tempPixel[] = new double[4]; tempPixel[3] = 255; int i, j; for (i = 0; i < smoothRangeHalf; i++) { for (j = 0; j < imageHeight; j++) { pixel1 = image.get(j, i); pixel2 = image.get(j, imageWidth - i - 1); tempPixel[0] = pixel1[0] * ((smoothRangeHalf + i) / smoothRange) + pixel2[0] * ((smoothRangeHalf - i) / smoothRange); tempPixel[1] = pixel1[1] * ((smoothRangeHalf + i) / smoothRange) + pixel2[1] * ((smoothRangeHalf - i) / smoothRange); tempPixel[2] = pixel1[2] * ((smoothRangeHalf + i) / smoothRange) + pixel2[2] * ((smoothRangeHalf - i) / smoothRange); pixel2[0] = pixel2[0] * ((smoothRangeHalf + i) / smoothRange) + pixel1[0] * ((smoothRangeHalf - i) / smoothRange); pixel2[1] = pixel2[1] * ((smoothRangeHalf + i) / smoothRange) + pixel1[1] * ((smoothRangeHalf - i) / smoothRange); pixel2[2] = pixel2[2] * ((smoothRangeHalf + i) / smoothRange) + pixel1[2] * ((smoothRangeHalf - i) / smoothRange); image.put(j, i, tempPixel); image.put(j, imageWidth - i - 1, pixel2); } } }
private static Mat quat2rpy(Mat quat) { double[] q = new double[4]; quat.get(0, 0, q); double[] rpy = { Math.atan2(2 * (q[0] * q[1] + q[2] * q[3]), 1 - 2 * (q[1] * q[1] + q[2] * q[2])), Math.asin(2 * (q[0] * q[2] - q[3] * q[1])), Math.atan2(2 * (q[0] * q[3] + q[1] * q[2]), 1 - 2 * (q[2] * q[2] + q[3] * q[3])) }; Mat rpym = new Mat(3, 1, CvType.CV_64F); rpym.put(0, 0, rpy); return rpym; }
public void copyMat(Mat src, Mat dest) { int srcRows = src.rows(); int srcCols = src.cols(); int destRows = dest.rows(); int destCols = dest.cols(); for (int i = 0; i < srcRows; i++) { for (int j = 0; j < srcCols; j++) { double bit = src.get(i, j)[0]; dest.put(i, j, bit); System.out.println(bit); } } }
private static Mat rodr2quat(Mat rodr) { double t = Core.norm(rodr); double[] r = new double[3]; rodr.get(0, 0, r); double[] quat = { Math.cos(t / 2), Math.sin(t / 2) * r[0] / t, Math.sin(t / 2) * r[1] / t, Math.sin(t / 2) * r[2] / t }; Mat quatm = new Mat(4, 1, CvType.CV_64F); quatm.put(0, 0, quat); return quatm; }
public static Mat getCCH(Mat image) { ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); Imgproc.findContours( image, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE); Mat chainHistogram = Mat.zeros(1, 8, CvType.CV_32F); int n = 0; MatOfPoint2f approxCurve = new MatOfPoint2f(); for (MatOfPoint contour : contours) { // get the freeman chain code from the contours int rows = contour.rows(); // System.out.println("\nrows"+rows+"\n"+contour.dump()); int direction = 7; Mat prevPoint = contours.get(0).row(0); n += rows - 1; for (int i = 1; i < rows; i++) { // get the current point double x1 = contour.get(i - 1, 0)[1]; double y1 = contour.get(i - 1, 0)[0]; // get the second point double x2 = contour.get(i, 0)[1]; double y2 = contour.get(i, 0)[0]; if (x2 == x1 && y2 == y1 + 1) direction = 0; else if (x2 == x1 - 1 && y2 == y1 + 1) direction = 1; else if (x2 == x1 - 1 && y2 == y1) direction = 2; else if (x2 == x1 - 1 && y2 == y1 - 1) direction = 3; else if (x2 == x1 && y2 == y1 - 1) direction = 4; else if (x2 == x1 + 1 && y2 == y1 - 1) direction = 5; else if (x2 == x1 + 1 && y2 == y1) direction = 6; else if (x2 == x1 + 1 && y2 == y1 + 1) direction = 7; else System.out.print("err"); double counter = chainHistogram.get(0, direction)[0]; chainHistogram.put(0, direction, ++counter); System.out.print(direction); } } System.out.println("\n" + chainHistogram.dump()); Scalar alpha = new Scalar(n); // the factor Core.divide(chainHistogram, alpha, chainHistogram); System.out.println("\nrows=" + n + " " + chainHistogram.dump()); return chainHistogram; }
/** * fills 'cTable' with the values in confMatrix * * @param confMatrix Mat - a result from TestingResult 'res' */ public void fillConfTable(Mat confMatrix) { // initialize table header JTableHeader header = cTable.getTableHeader(); for (int i = 0; i < cTable.getColumnCount() - 1; i++) { TableColumn column1 = cTable.getTableHeader().getColumnModel().getColumn(i + 1); column1.setHeaderValue(wordClasses[i]); } System.out.println(cTable.getColumnCount()); // fill the first column with classes for (int i = 0; i < wordClasses.length; i++) { for (int j = 0; j < wordClasses.length; j++) { cTable.setValueAt(wordClasses[i], i, 0); cTable.setValueAt(confMatrix.get(i, j)[0], i, j + 1); } } }
private boolean setCircleParameters(Mat circles) { /** Determine which circle in on the left and right */ if (circles.get(0, 0)[0] < circles.get(0, 1)[0]) { circle1 = circles.get(0, 0)[0]; circle2 = circles.get(0, 1)[0]; } else { circle1 = circles.get(0, 1)[0]; circle2 = circles.get(0, 0)[0]; } minRadius = (int) ((circles.get(0, 1)[2] + circles.get(0, 0)[2]) / 2) - 10; if (minRadius < 5) minRadius = 5; maxRadius = minRadius + 20; minDistance = (int) (circle2 - circle1 - 10); return true; }
protected static void glGetModelViewMatrix(double[] modelview_matrix, Mat Rvec, Mat Tvec) throws ExtParamException { // check if parameters are valid boolean invalid = false; double[] tvec = new double[3]; double[] rvec = new double[3]; Rvec.get(0, 0, rvec); Tvec.get(0, 0, tvec); for (int i = 0; i < 3 && !invalid; i++) { if (tvec[i] != -999999) invalid |= false; if (rvec[i] != -999999) invalid |= false; } if (invalid) throw new ExtParamException("extrinsic parameters are not set Marker.getModelViewMatrix"); Mat Rot = new Mat(3, 3, CvType.CV_32FC1); Mat Jacob = new Mat(); Calib3d.Rodrigues(Rvec, Rot, Jacob); // TODO jacob no se vuelve a usar double[][] para = new double[3][4]; double[] rotvec = new double[9]; Rot.get(0, 0, rotvec); for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) para[i][j] = rotvec[3 * i + j]; // now, add the translation para[0][3] = tvec[0]; para[1][3] = tvec[1]; para[2][3] = tvec[2]; double scale = 1; // R1C2 modelview_matrix[0 + 0 * 4] = para[0][0]; modelview_matrix[0 + 1 * 4] = para[0][1]; modelview_matrix[0 + 2 * 4] = para[0][2]; modelview_matrix[0 + 3 * 4] = para[0][3]; // R2 modelview_matrix[1 + 0 * 4] = para[1][0]; modelview_matrix[1 + 1 * 4] = para[1][1]; modelview_matrix[1 + 2 * 4] = para[1][2]; modelview_matrix[1 + 3 * 4] = para[1][3]; // R3 modelview_matrix[2 + 0 * 4] = -para[2][0]; modelview_matrix[2 + 1 * 4] = -para[2][1]; modelview_matrix[2 + 2 * 4] = -para[2][2]; modelview_matrix[2 + 3 * 4] = -para[2][3]; modelview_matrix[3 + 0 * 4] = 0.0f; modelview_matrix[3 + 1 * 4] = 0.0f; modelview_matrix[3 + 2 * 4] = 0.0f; modelview_matrix[3 + 3 * 4] = 1.0f; if (scale != 0.0) { modelview_matrix[12] *= scale; modelview_matrix[13] *= scale; modelview_matrix[14] *= scale; } // rotate 90º around the x axis // rotating around x axis in OpenGL is equivalent to // multiply the model matrix by the matrix: // 1, 0, 0, 0, 0, cos(a), sin(a), 0, 0, -sin(a), cos(a), 0, 0, 0, 0, 1 // double[] auxRotMat = new double[]{ // 1, 0, 0, 0, // 0, 0, 1, 0, // 0, -1, 0, 0, // 0, 0, 0, 1 // }; // Utils.matrixProduct(modelview_matrix1, auxRotMat, modelview_matrix); }
public Mat onCameraFrame(Mat inputFrame) { inputFrame.copyTo(mRgba); switch (ImageManipulationsActivity.viewMode) { case ImageManipulationsActivity.VIEW_MODE_RGBA: break; case ImageManipulationsActivity.VIEW_MODE_HIST: if ((mSizeRgba == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height)) CreateAuxiliaryMats(); int thikness = (int) (mSizeRgba.width / (mHistSizeNum + 10) / 5); if (thikness > 5) thikness = 5; int offset = (int) ((mSizeRgba.width - (5 * mHistSizeNum + 4 * 10) * thikness) / 2); // RGB for (int c = 0; c < 3; c++) { Imgproc.calcHist(Arrays.asList(mRgba), mChannels[c], mMat0, mHist, mHistSize, mRanges); Core.normalize(mHist, mHist, mSizeRgba.height / 2, 0, Core.NORM_INF); mHist.get(0, 0, mBuff); for (int h = 0; h < mHistSizeNum; h++) { mP1.x = mP2.x = offset + (c * (mHistSizeNum + 10) + h) * thikness; mP1.y = mSizeRgba.height - 1; mP2.y = mP1.y - 2 - (int) mBuff[h]; Core.line(mRgba, mP1, mP2, mColorsRGB[c], thikness); } } // Value and Hue Imgproc.cvtColor(mRgba, mIntermediateMat, Imgproc.COLOR_RGB2HSV_FULL); // Value Imgproc.calcHist( Arrays.asList(mIntermediateMat), mChannels[2], mMat0, mHist, mHistSize, mRanges); Core.normalize(mHist, mHist, mSizeRgba.height / 2, 0, Core.NORM_INF); mHist.get(0, 0, mBuff); for (int h = 0; h < mHistSizeNum; h++) { mP1.x = mP2.x = offset + (3 * (mHistSizeNum + 10) + h) * thikness; mP1.y = mSizeRgba.height - 1; mP2.y = mP1.y - 2 - (int) mBuff[h]; Core.line(mRgba, mP1, mP2, mWhilte, thikness); } // Hue Imgproc.calcHist( Arrays.asList(mIntermediateMat), mChannels[0], mMat0, mHist, mHistSize, mRanges); Core.normalize(mHist, mHist, mSizeRgba.height / 2, 0, Core.NORM_INF); mHist.get(0, 0, mBuff); for (int h = 0; h < mHistSizeNum; h++) { mP1.x = mP2.x = offset + (4 * (mHistSizeNum + 10) + h) * thikness; mP1.y = mSizeRgba.height - 1; mP2.y = mP1.y - 2 - (int) mBuff[h]; Core.line(mRgba, mP1, mP2, mColorsHue[h], thikness); } break; case ImageManipulationsActivity.VIEW_MODE_CANNY: if ((mRgbaInnerWindow == null) || (mGrayInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height)) CreateAuxiliaryMats(); Imgproc.Canny(mRgbaInnerWindow, mIntermediateMat, 80, 90); Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4); break; case ImageManipulationsActivity.VIEW_MODE_SOBEL: Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY); if ((mRgbaInnerWindow == null) || (mGrayInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height)) CreateAuxiliaryMats(); Imgproc.Sobel(mGrayInnerWindow, mIntermediateMat, CvType.CV_8U, 1, 1); Core.convertScaleAbs(mIntermediateMat, mIntermediateMat, 10, 0); Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4); break; case ImageManipulationsActivity.VIEW_MODE_SEPIA: Core.transform(mRgba, mRgba, mSepiaKernel); break; case ImageManipulationsActivity.VIEW_MODE_ZOOM: if ((mZoomCorner == null) || (mZoomWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height)) CreateAuxiliaryMats(); Imgproc.resize(mZoomWindow, mZoomCorner, mZoomCorner.size()); Size wsize = mZoomWindow.size(); Core.rectangle( mZoomWindow, new Point(1, 1), new Point(wsize.width - 2, wsize.height - 2), new Scalar(255, 0, 0, 255), 2); break; case ImageManipulationsActivity.VIEW_MODE_PIXELIZE: if ((mRgbaInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height)) CreateAuxiliaryMats(); Imgproc.resize(mRgbaInnerWindow, mIntermediateMat, mSize0, 0.1, 0.1, Imgproc.INTER_NEAREST); Imgproc.resize( mIntermediateMat, mRgbaInnerWindow, mSizeRgbaInner, 0., 0., Imgproc.INTER_NEAREST); break; case ImageManipulationsActivity.VIEW_MODE_POSTERIZE: if ((mRgbaInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height)) CreateAuxiliaryMats(); /* Imgproc.cvtColor(mRgbaInnerWindow, mIntermediateMat, Imgproc.COLOR_RGBA2RGB); Imgproc.pyrMeanShiftFiltering(mIntermediateMat, mIntermediateMat, 5, 50); Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_RGB2RGBA); */ Imgproc.Canny(mRgbaInnerWindow, mIntermediateMat, 80, 90); mRgbaInnerWindow.setTo(new Scalar(0, 0, 0, 255), mIntermediateMat); Core.convertScaleAbs(mRgbaInnerWindow, mIntermediateMat, 1. / 16, 0); Core.convertScaleAbs(mIntermediateMat, mRgbaInnerWindow, 16, 0); break; } return mRgba; }
boolean decodeJPEGAndAnalyze(Context context, CalibrationEntries calibrationEntries) { boolean result = false; BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferQualityOverSpeed = true; BitmapRegionDecoder brd; byte[] data = mData; try { brd = BitmapRegionDecoder.newInstance(data, 0, data.length, false); Rect rect; rect = new Rect(0, 0, brd.getWidth(), brd.getHeight()); Bitmap bitmap = brd.decodeRegion(rect, opt); MatBitmapHolder mRgbaMatHolder = new MatBitmapHolder(bitmap); Mat rgbaMat = mRgbaMatHolder.pin(); Mat grayData = new Mat(); Imgproc.cvtColor(rgbaMat, grayData, Imgproc.COLOR_RGB2GRAY, 1); Mat centersCalibCircles = new Mat(); Size patternSize = CalibrationEntries.Pattern_ASYMMETRIC_CIRCLES_GRID_SIZE; Size imageSize = new Size(grayData.cols(), grayData.rows()); boolean patternWasFound = Calib3d.findCirclesGridDefault( grayData, patternSize, centersCalibCircles, Calib3d.CALIB_CB_ASYMMETRIC_GRID); if (patternWasFound && this.orientation != null) { int addedIdx = calibrationEntries.addEntry(this.orientation, centersCalibCircles); if (addedIdx >= 0) { Log.d( "CALIB", String.format( "PictureCapture: Added calibration entry at %d tot: %d", addedIdx, calibrationEntries.getNumEntries())); if (calibrationEntries.getNewlyAdded() > 5) { List<Mat> imagePoints = calibrationEntries.getPoints(); List<Mat> objectPoints = calibrationEntries.getObjectPointsAsymmentricList(imagePoints.size()); if (CalibrationEntries.isEnoughCalibrationPoints(imagePoints.size())) { calibrationEntries.resetNewlyAdded(); CameraCalibrationData cameraCalibrationData = new CameraCalibrationData(); List<Mat> rvecs = new Vector<Mat>(imagePoints.size()); List<Mat> tvecs = new Vector<Mat>(imagePoints.size()); int flags = 0; flags |= Calib3d.CALIB_FIX_K4 | Calib3d.CALIB_FIX_K5; Log.d("CALIB", String.format("PictureCapture: Calling Calib3d.calibrateCamera")); Mat K = new Mat(); Mat kdist = new Mat(); double rms = Calib3d.calibrateCamera( objectPoints, imagePoints, imageSize, K, kdist, rvecs, tvecs, flags); double[] Karray = new double[9]; double[] distcoeffs_array = new double[5]; K.get(0, 0, Karray); kdist.get(0, 0, distcoeffs_array); cameraCalibrationData.setData( grayData.cols(), grayData.rows(), Karray, distcoeffs_array, rms); Log.d( "CALIB", String.format( "PictureCapture: Calibration data: %s", cameraCalibrationData.formatCalibrationDataString())); calibrationEntries.setCalibrationData(cameraCalibrationData); result = true; } } } } // mRightBitmap = brd.decodeRegion(rect, opt); rect = new Rect(brd.getWidth() - brd.getWidth() / 3, 0, brd.getWidth(), brd.getHeight()); // mLeftBitmap = brd.decodeRegion(rect, opt); mRgbaMatHolder.unpin(rgbaMat); } catch (IOException e) { XLog.e("Region decoder doesn't want to cooperate", e); } return result; }
public static void Circle(List<MatOfPoint> contours, int index) { int i = index; Mat mRGBA = new Mat(); Utils.bitmapToMat(image, mRGBA); // cyklus s podmienkou na konci do { int buff[] = new int[4]; hierarchy.get(0, i, buff); // Get contour form list Mat contour = contours.get(i); // id kont�ry int id = i; // dostaneme �a��ie id kont�ry i = buff[0]; // zis�ujeme �i m�me dostato�ne ve�k� kont�ru aby sme sa �ou v�bec zaoberali if (Imgproc.contourArea(contour) > 500) { List<Point> points = new ArrayList<Point>(); // dostaneme celkov� po�et kont�r int num = (int) contour.total(); // vytvor�me si pole o dvojn�sobnej ve�kosti samotnej kontury int temp[] = new int[num * 2]; // na��tame si kont�ru do do�asnej premennej contour.get(0, 0, temp); // konvertujeme List<Point> do MatOfPoint2f pre pou�itie fitEllipse for (int j = 0; j < num * 2; j = j + 2) { points.add(new Point(temp[j], temp[j + 1])); } MatOfPoint2f specialPointMtx = new MatOfPoint2f(points.toArray(new Point[0])); // do premennej bound uklad�me dokonal� elipsu RotatedRect bound = Imgproc.fitEllipse(specialPointMtx); // Vypo��ta sa hodnota pi double pi = Imgproc.contourArea(contour) / ((bound.size.height / 2) * (bound.size.width / 2)); // zis�ujeme toleranciu pi - zaoplenie if (Math.abs(pi - 3.14) > 0.03) { int k = buff[2]; // zis�ujeme �i existuje nejak� rodi� kont�ry if (k != -1) { Circle(contours, k); } continue; } // konvertujeme MatOfPoint2f do MatOfPoint pre funckiu fitEllipse - rozdie� je len v 32-bit // float a 32-bit int MatOfPoint NewMtx = new MatOfPoint(specialPointMtx.toArray()); // dostaneme s�radnice najmen�ieho mo�n�ho �tvorca Rect box = Imgproc.boundingRect(NewMtx); // nacita obrazok znova Mat mat_for_count = new Mat(); Utils.bitmapToMat(image, mat_for_count); // vytvori sa klon stvorca - dobry kandidat pre vyhladanie Mat candidate = ((mat_for_count).submat(box)).clone(); // napln maticu binarnou ciernou Mat mask = new Mat(box.size(), candidate.type(), new Scalar(0, 0, 0)); // naplni ciernu plochu bielimi konturami Imgproc.drawContours( mask, contours, id, new Scalar(255, 255, 255), -1, 8, hierarchy, 0, new Point(-box.x, -box.y)); // ulozi sa kandidat Mat roi = new Mat(candidate.size(), candidate.type(), new Scalar(255, 255, 255)); // ulozia sa len informacie o kandidatovi candidate.copyTo(roi, mask); double longAxis; double shortAxis; // ziska dve osy elipsy if (bound.size.height < bound.size.width) { shortAxis = bound.size.height / 2; longAxis = bound.size.width / 2; } else { shortAxis = bound.size.width / 2; longAxis = bound.size.height / 2; } // zastavi sa vyhladavanie pokial je elipsa prilis ovalna if ((longAxis / shortAxis) < 2.0) { signList.add(roi); boxList.add(box); } } // zis�uje sa �i je tam e�te �al�� kandid�t } while (i != -1); }
/** * Extracts and classifies colour bands for each Resistor. Each ColourBand object is instantiated * and linked to their parent Resistor object. * * @param resistorList A list of Resistor objects from which to extract the colour bands * @param paintDebugInfo If ture, the extracted colour band ROIs are displayed on the GUI */ private void extractColourBandsAndClassify(List<Resistor> resistorList, boolean paintDebugInfo) { if (resistorList.size() > 0) { for (int r = 0; r < resistorList.size(); r++) { Mat resImg = resistorList.get(r).resistorMat; Mat imgHSV = new Mat(); Mat satImg = new Mat(); Mat hueImg = new Mat(); // convert to HSV Imgproc.cvtColor(resImg, imgHSV, Imgproc.COLOR_BGR2HSV); ArrayList<Mat> channels = new ArrayList<Mat>(); Core.split(imgHSV, channels); // extract channels satImg = channels.get(1); // saturation hueImg = channels.get(0); // hue // threshold saturation channel Mat threshedROISatBands = new Mat(); // ~130 sat thresh val Imgproc.threshold(satImg, threshedROISatBands, SAT_BAND_THRESH, 255, Imgproc.THRESH_BINARY); // threshold hue channel Mat threshedROIHueBands = new Mat(); // ~50 hue thresh val Imgproc.threshold(hueImg, threshedROIHueBands, HUE_BAND_THRESH, 255, Imgproc.THRESH_BINARY); // combine the thresholded binary images Mat bandROI = new Mat(); Core.bitwise_or(threshedROIHueBands, threshedROISatBands, bandROI); // find contours in binary ROI image ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); Imgproc.findContours( bandROI, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0)); // remove any remaining noise by only keeping contours which area > threshold for (int i = 0; i < contours.size(); i++) { double area = Imgproc.contourArea(contours.get(i)); if (area < MIN_BAND_AREA) { contours.remove(i); i--; } } // create a ColourBand object for each detected band // storing its center, the contour and the bandROI for (int i = 0; i < contours.size(); i++) { MatOfPoint contour = contours.get(i); // extract this colour band and store in a Mat Rect boundingRect = Imgproc.boundingRect(contour); Mat mask = Mat.zeros(bandROI.size(), CvType.CV_8U); Imgproc.drawContours(mask, contours, i, new Scalar(255), Core.FILLED); Mat imageROI = new Mat(); resImg.copyTo(imageROI, mask); Mat colourBandROI = new Mat(imageROI, boundingRect); // instantiate new ColourBand object ColourBand cb = new ColourBand(findCenter(contour), contour, colourBandROI); // cluster the band colour cb.clusterBandColour(BAND_COLOUR_K_MEANS); // classify using the Lab colourspace as feature vector Mat sampleMat = new Mat(1, 3, CvType.CV_32FC1); // create a Mat contacting the clustered band colour sampleMat.put(0, 0, cb.clusteredColourLAB[0]); sampleMat.put(0, 1, cb.clusteredColourLAB[1]); sampleMat.put(0, 2, cb.clusteredColourLAB[2]); Mat classifiedValue = new Mat(1, 1, CvType.CV_32FC1); Mat neighborResponses = new Mat(); // dont actually use this Mat dists = new Mat(); // dont actually use this // classify knn.find_nearest(sampleMat, 3, classifiedValue, neighborResponses, dists); // cast classified value into Colour enum and store cb.classifiedColour = ColourEnumVals[(int) classifiedValue.get(0, 0)[0]]; // add the band to the parent resistor resistorList.get(r).bands.add(cb); } // paint the extracted band ROIs if (paintDebugInfo) { Mat finalBandROIMask = Mat.zeros(bandROI.size(), CvType.CV_8U); for (int i = 0; i < contours.size(); i++) { Scalar color = new Scalar(255, 255, 255); Imgproc.drawContours( finalBandROIMask, contours, i, color, -1, 4, hierarchy, 0, new Point()); } Mat colourROI = new Mat(); resImg.copyTo(colourROI, finalBandROIMask); paintResistorSubRegion(colourROI, r); } } } }
public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); /** *** Configuration Variables **** */ int imgWidth = 200; int imgHeight = 200; int numPatch = 2000; int patchWidth = 40; int patchHeight = 40; int k = 200; // kmeans number of center int numBins = 8; String filePathRed = "base/Red/"; String filePathBlack = "base/Black"; String procPathRed = "base/ProcRed"; String procPathBlack = "base/ProcBlack"; /** ******************************** */ ArrayList<String> fileNames = new ArrayList<String>(); sources = new ArrayList<Mat>(); /* Image IO */ try { /* Read Red Staplers */ File folder = new File(filePathRed); BufferedImage currentImage; for (final File fileEntry : folder.listFiles()) { if (!fileEntry.isDirectory()) { // Resize Image currentImage = ImageProc.resize(ImageIO.read(fileEntry), imgWidth, imgHeight); File outFile = new File(procPathRed + "/" + fileEntry.getName()); ImageIO.write(currentImage, "JPG", outFile); sources.add(Highgui.imread(outFile.getPath())); fileNames.add(outFile.getName()); } } /* Read Black Staplers */ folder = new File(filePathBlack); for (final File fileEntry : folder.listFiles()) { if (!fileEntry.isDirectory()) { // Resize Image currentImage = ImageProc.resize(ImageIO.read(fileEntry), imgWidth, imgHeight); File outFile = new File(procPathBlack + "/" + fileEntry.getName()); ImageIO.write(currentImage, "JPG", outFile); sources.add(Highgui.imread(outFile.getPath())); fileNames.add(outFile.getName()); } } } catch (IOException e) { e.printStackTrace(); } /** ************************************* */ float[] p1 = new float[30]; float[] p2 = new float[30]; /* Create Image Patches and calculate color feature vector for each patch */ Iterator<Mat> imgIter = sources.iterator(); Mat thisImage; Mat featureMat = new Mat(); List<Mat> imagePatches = null; Iterator<Mat> patchIter = null; while (imgIter.hasNext()) { thisImage = imgIter.next(); // Randomly Sample Patches imagePatches = ImageProc.sampleImage(thisImage, patchWidth, patchHeight, numPatch); patchIter = imagePatches.iterator(); // Create color feature vector for each patch while (patchIter.hasNext()) { featureMat.push_back(ImageProc.calBGRFeature(patchIter.next(), numBins)); } } Mat centers = new Mat(); Mat bestLabels = new Mat(); Core.kmeans( featureMat, k, bestLabels, new TermCriteria(TermCriteria.EPS, 0, Math.pow(10, -5)), 0, Core.KMEANS_RANDOM_CENTERS, centers); MatOfFloat bestLabelRange = new MatOfFloat(0, k); ArrayList<Mat> centerHist = new ArrayList<Mat>(); Mat centerHistMat = new Mat(0, k, CvType.CV_32FC1); imgIter = sources.listIterator(); Iterator<String> nameIter = fileNames.iterator(); int ptr = 0; int cnt = 0; // Output CSV try { File outCSV = new File("output/res.csv"); FileWriter fstream = new FileWriter(outCSV); BufferedWriter out = new BufferedWriter(fstream); StringBuilder sb; out.write("@relation staplers\n"); for (int n = 0; n < 200; n++) { out.write("@attribute " + "a" + n + " real\n"); } out.write("@attribute class {RedStapler, BlackStapler}\n\n"); out.write("@data\n\n"); while (imgIter.hasNext()) { Mat thisMat = new Mat(bestLabels, new Range(ptr, ptr + numPatch), new Range(0, 1)); Mat mat = new Mat(); thisMat.convertTo(mat, CvType.CV_32F); ArrayList<Mat> bestLabelList = new ArrayList<Mat>(); bestLabelList.add(mat); Mat thisHist = new Mat(); Imgproc.calcHist( bestLabelList, new MatOfInt(0), new Mat(), thisHist, new MatOfInt(k), bestLabelRange); centerHist.add(thisHist); // Create file sb = new StringBuilder(); float[] histArr = new float[(int) thisHist.total()]; thisHist.get(0, 0, histArr); for (int m = 0; m < histArr.length; m++) { sb.append(histArr[m] + ","); } if (cnt++ < 10) sb.append("RedStapler"); else sb.append("BlackStapler"); sb.append("\n"); out.write(sb.toString()); // Close the output stream centerHistMat.push_back(thisHist.t()); ptr += numPatch; imgIter.next(); } out.close(); } catch (IOException e) { // Catch exception if any System.err.println("Error: " + e.getMessage()); System.exit(-1); } /* Support Vector Machine Validation */ Mat labelMat = new Mat(sources.size(), 1, CvType.CV_32FC1); double[] labels = new double[20]; for (int i = 0; i < 10; i++) { labels[i] = 1; labels[i + 10] = -1; } labelMat.put(0, 0, labels); CvSVMParams params = new CvSVMParams(); params.set_kernel_type(CvSVM.LINEAR); CvSVM svm = new CvSVM(); svm.train(centerHistMat, labelMat, new Mat(), new Mat(), params); svm.save("base/haha.txt"); String basePath = "base/predict/"; try { File testCSV = new File("output/test.arff"); FileWriter testStream = new FileWriter(testCSV); BufferedWriter testOut = new BufferedWriter(testStream); testOut.write("@relation staplers\n"); for (int n = 0; n < 200; n++) { testOut.write("@attribute " + "a" + n + " real\n"); } testOut.write("@attribute class {RedStapler, BlackStapler}\n\n"); testOut.write("@data\n\n"); for (int m = 0; m < 21; m++) { // System.out.println(basePath + m + ".jpg"); Mat testImg = Highgui.imread(basePath + m + ".jpg"); List<Mat> patches = ImageProc.sampleImage(testImg, patchWidth, patchHeight, numPatch); List<Mat> features = new ArrayList<Mat>(); for (int i = 0; i < patches.size(); i++) { Mat testVector = ImageProc.calBGRFeature(patches.get(i), numBins); features.add(testVector); } Mat testData = ImageProc.calFeatureVector(features, centers); StringBuilder testsb = new StringBuilder(); // String name = nameIter.next(); // sb.append(name + ","); float[] data = new float[testData.cols()]; testData.get(0, 0, data); for (int o = 0; o < data.length; o++) { testsb.append(data[o] + ","); } if (m < 6) testsb.append("RedStapler"); else testsb.append("BlackStapler"); testsb.append("\n"); testOut.write(testsb.toString()); System.out.println("Img" + m + " " + svm.predict(testData)); } } catch (IOException e) { e.printStackTrace(); System.exit(-1); } }
/** new algo */ public static String findTips(Mat image) { Mat binary = new Mat(); // Convert to B&W image Imgproc.threshold(image, binary, 0, 255, Imgproc.THRESH_BINARY); // Convert to binary image, 1 channel Imgproc.cvtColor(binary, binary, Imgproc.COLOR_RGB2GRAY); boolean flag_bg = false; boolean handInQuarterOfImg = false; int count_finger = 0; int start = -1; int end = -1; int tester = 0; // test variable CAN BE REMOVED double startPercent = 0, endPercent = 0; int leftHeight = -1; int firstHandPixel = -1; int lastHandPixel = -1; int KV_midFinger = -1; int KV_indxFinger = -1; double pixel[]; for (int i = 0; i < binary.cols(); i++) { pixel = binary.get((int) (binary.rows() * 0.23), i); if (pixel[0] != 0.0) { if (firstHandPixel == -1) firstHandPixel = i; if (i < image.cols() / 4) handInQuarterOfImg = true; lastHandPixel = i; leftHeight = i; } if (pixel[0] != 0.0) { if (flag_bg == false) { // flag_bg is false count_finger++; flag_bg = true; start = i; } end = i; } else { if (count_finger == 1) KV_midFinger = end; else if (count_finger == 2) KV_indxFinger = start; flag_bg = false; if (tester != count_finger) { // remove this line -- just for // testing startPercent = ((double) start / (double) binary.cols()) * 100; endPercent = ((double) end / (double) binary.cols()) * 100; } tester = count_finger; // remove this line -- just for testing } } if (count_finger == 3) { if (endPercent > 80.0) { return "W"; } else { return "F"; } } else if (count_finger == 4) { return "B"; } else if (count_finger == 1) { if (endPercent > 70.0) if (handInQuarterOfImg) return "B"; else { int result = identify_R_U(binary); switch (result) { case 1: return "R"; case 2: return "U"; default: return " "; // return "RU"; } } else { if ((float) (lastHandPixel - firstHandPixel) / image.cols() > 0.35) return "F"; else { image = Filter.furtherClean(image); Mat binary2 = new Mat(); // Convert to B&W image Imgproc.threshold(image, binary2, 0, 255, Imgproc.THRESH_BINARY); // Convert to binary image, 1 channel Imgproc.cvtColor(binary2, binary2, Imgproc.COLOR_RGB2GRAY); flag_bg = false; int countHandArea = 0; for (int i = 0; i < binary2.rows(); i++) { pixel = binary2.get(i, (int) (binary2.cols() * 0.70)); if (pixel[0] != 0.0) { if (!flag_bg) { flag_bg = true; countHandArea++; } } else flag_bg = false; } if (countHandArea == 1) { if (checkAngleL(binary)) return "L"; else { int result = identify_R_U(binary); switch (result) { case 1: return "R"; case 2: return "U"; default: return " "; } } } else if (countHandArea == 2) return "D"; else return " "; } } } else if (count_finger == 2) { // int result = identify_K_V(rgb, binary, new Point(KV_midFinger, // (int) (binary.rows() * 0.23)), new Point(KV_indxFinger, // (int) (binary.rows() * 0.23))); int startX = (int) KV_midFinger; int startY = (int) (binary.rows() * 0.23); int lastDir = 1; // 0 up; 1 down int currDir = 1; int ctr = 1; Point KV_PtsArray[] = new Point[3]; KV_PtsArray[0] = new Point(startX, startY); // Core.circle(rgb, // new Point(UL.x + startX, UL.y + startY), 5, // new Scalar(0, 0, 255)); // double pixel[]; Log.i("START", "" + startX); Log.i("END", "" + startY); for (int i = startX; i < KV_indxFinger; i++) { for (int k = (int) (binary.rows() * 0.23); k < binary.rows(); k++) { pixel = binary.get(k, i + 1); Log.i("CHECK1", "" + binary.rows()); Log.i("CHECK2", "" + binary.rows() * 0.23); Log.i("CHECK3", "" + (int) (binary.rows() * 0.23)); Log.i("CHECK4", "" + pixel[0]); if (pixel[0] != 0.0) { // Plots the edges from hullPoint[0] to hullPoint[1] // Core.circle(rgb, new Point(UL.x + startX + 1, // UL.y // + k), 5, new Scalar(0, 255, 0)); if (k > startY) currDir = 1; else if (k < startY) currDir = 0; if (currDir != lastDir && ctr < 3) { // Plots 3 significant pts in K and V // Core.circle(rgb, // new Point(UL.x + i + 1, UL.y + k), 5, // new Scalar(0, 0, 255)); KV_PtsArray[ctr] = new Point(i + 1, k); ctr++; } lastDir = currDir; // startX = startX + 1; startY = k; break; } } if (ctr < 3 && i + 1 == (int) KV_indxFinger) { // Core.circle(rgb, new Point(UL.x + startX, UL.y + startY), // 5, // new Scalar(0, 0, 255)); KV_PtsArray[ctr] = new Point(i, startY); ctr++; } if (ctr == 3) break; } if (ctr == 3) { if (KV_PtsArray[0].y == KV_PtsArray[2].y) return "V"; // V else return "K"; // K } else return " "; } return Integer.toString(count_finger); }
// Bhattacharyya coefficient for two distributions // a, b are original data samples // better if a, b are dimension-reduced Principal Components public static double Bhattacharyya(Mat a, Mat b) { // if not dimension-reduced // int k = a.cols()/2; // Mat temp1 = new Mat(); // Mat temp2 = new Mat(); // Mat pa = new Mat(); // Mat pb = new Mat(); // Core.PCACompute(a, temp1, temp2, k); // Core.PCAProject(a, temp1, temp2, pa); // Core.PCACompute(b, temp1, temp2, k); // Core.PCAProject(b, temp1, temp2, pb); // Mat cova = new Mat(); Mat covb = new Mat(); Mat meana = new Mat(); Mat meanb = new Mat(); Core.calcCovarMatrix(a, cova, meana, Core.COVAR_NORMAL | Core.COVAR_ROWS); Core.calcCovarMatrix(b, covb, meanb, Core.COVAR_NORMAL | Core.COVAR_ROWS); // if not dimension-reduced // Core.calcCovarMatrix(pa, cova, meana, Core.COVAR_NORMAL|Core.COVAR_ROWS); // Core.calcCovarMatrix(pb, covb, meanb, Core.COVAR_NORMAL|Core.COVAR_ROWS); // System.out.println("cov"); // System.out.println(cova.dump()); // System.out.println(covb.dump()); double detcova = Core.determinant(cova); double detcovb = Core.determinant(covb); Mat cov = new Mat(); Core.add(cova, covb, cov); // cov = cov.mul(cov, 0.5); // Core.scaleAdd(cov, 0.5, new Mat(), cov); for (int i = 0; i < cov.rows(); i++) for (int j = 0; j < cov.cols(); j++) { cov.put(i, j, 0.5 * cov.get(i, j)[0]); } double detcov = Core.determinant(cov); Mat inv = new Mat(); // inverse of cov Core.invert(cov, inv); Mat mabsub = new Mat(); Core.subtract(meana, meanb, mabsub); Mat mabsub_tp = new Mat(); Core.transpose(mabsub, mabsub_tp); Mat temp = new Mat(); Core.gemm(mabsub, inv, 1, new Mat(), 0, temp); // System.out.println(temp.dump()); // System.out.println(mabsub_tp.dump()); Mat res = new Mat(); Core.gemm(temp, mabsub_tp, 1, new Mat(), 0, res); // System.out.println(res.dump()); // Mat temp = mabsub_tp.mul(inv); // System.out.println(temp.dump()); // // Mat res = temp.mul(mabsub); // System.out.println(res.dump()); double s2 = Math.sqrt(detcov / (Math.sqrt(detcova * detcovb))); // s1 = 0.125 * (mabsub_tp*inv*mabsub) double s1 = 0.125 * res.get(0, 0)[0]; double bdis = 1 / (Math.exp(s1) * s2); return bdis; }