List of usage examples for org.opencv.core Mat cols
public int cols()
From source file:view.TelaComCaptura.java
public String salvarPgm2(String path, Mat mat) { FileOutputStream fout;/* w ww .j a v a 2 s. com*/ int pixels[][] = new int[mat.rows()][mat.cols()]; try { fout = new FileOutputStream(path); //write image header //write PGM magic value 'P5' String tstr; tstr = "P5" + "\n"; fout.write(tstr.getBytes()); //write comment // comment = comment + "\n"; //fout.write(comment.getBytes()); //write cols tstr = Integer.toString(mat.cols()); fout.write(tstr.getBytes()); fout.write(32); //write blank space //write rows tstr = Integer.toString(mat.rows()); fout.write(tstr.getBytes()); fout.write(32); //write blank space //write maxgray tstr = Integer.toString(255); tstr = tstr + "\n"; fout.write(tstr.getBytes()); for (int r = 0; r < mat.rows(); r++) { for (int c = 0; c < mat.cols(); c++) { double[] pixelsRgb = mat.get(r, c); int red = (int) (pixelsRgb[0] * 0.299); int green = (int) (pixelsRgb[1] * 0.587); int blue = (int) (pixelsRgb[1] * 0.114); Color newColor = new Color(red + green + blue, red + green + blue, red + green + blue); //int pixel = (int) (pixelsRgb[0] + pixelsRgb[1] + pixelsRgb[1]); //pixel = pixel / 3; fout.write(newColor.getRGB()); // fout.write((int) mat.get(r, c)[0]); } } fout.close(); return path; } catch (Exception e) { System.out.println("Erro ao salvar PGM: " + e); } return ""; }
From source file:vinylsleevedetection.Analyze.java
public void Check() { count = 1;// w w w . j a v a2 s . com //load openCV library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //for loop to compare source images to user image for (int j = 1; j < 4; j++) { //source image location (record sleeve) String Object = "E:\\Users\\Jamie\\Documents\\NetBeansProjects\\VinylSleeveDetection\\Source\\" + j + ".jpg"; //user image location String Scene = "E:\\Users\\Jamie\\Documents\\NetBeansProjects\\VinylSleeveDetection\\Output\\camera.jpg"; //load images Mat objectImage = Imgcodecs.imread(Object, Imgcodecs.CV_LOAD_IMAGE_COLOR); Mat sceneImage = Imgcodecs.imread(Scene, Imgcodecs.CV_LOAD_IMAGE_COLOR); //use BRISK feature detection MatOfKeyPoint objectKeyPoints = new MatOfKeyPoint(); FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.BRISK); //perform feature detection on source image featureDetector.detect(objectImage, objectKeyPoints); KeyPoint[] keypoints = objectKeyPoints.toArray(); //use descriptor extractor MatOfKeyPoint objectDescriptors = new MatOfKeyPoint(); DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.BRISK); descriptorExtractor.compute(objectImage, objectKeyPoints, objectDescriptors); Mat outputImage = new Mat(objectImage.rows(), objectImage.cols(), Imgcodecs.CV_LOAD_IMAGE_COLOR); Scalar newKeypointColor = new Scalar(255, 0, 0); Features2d.drawKeypoints(objectImage, objectKeyPoints, outputImage, newKeypointColor, 0); MatOfKeyPoint sceneKeyPoints = new MatOfKeyPoint(); MatOfKeyPoint sceneDescriptors = new MatOfKeyPoint(); featureDetector.detect(sceneImage, sceneKeyPoints); descriptorExtractor.compute(sceneImage, sceneKeyPoints, sceneDescriptors); Mat matchoutput = new Mat(sceneImage.rows() * 2, sceneImage.cols() * 2, Imgcodecs.CV_LOAD_IMAGE_COLOR); Scalar matchestColor = new Scalar(0, 255, 0); List<MatOfDMatch> matches = new LinkedList<>(); DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE); descriptorMatcher.knnMatch(objectDescriptors, sceneDescriptors, matches, 2); LinkedList<DMatch> goodMatchesList = new LinkedList<DMatch>(); float nndrRatio = 0.7f; for (int i = 0; i < matches.size(); i++) { MatOfDMatch matofDMatch = matches.get(i); DMatch[] dmatcharray = matofDMatch.toArray(); DMatch m1 = dmatcharray[0]; DMatch m2 = dmatcharray[1]; if (m1.distance <= m2.distance * nndrRatio) { goodMatchesList.addLast(m1); } } //if the number of good mathces is more than 150 a match is found if (goodMatchesList.size() > 150) { System.out.println("Object Found"); List<KeyPoint> objKeypointlist = objectKeyPoints.toList(); List<KeyPoint> scnKeypointlist = sceneKeyPoints.toList(); LinkedList<Point> objectPoints = new LinkedList<>(); LinkedList<Point> scenePoints = new LinkedList<>(); for (int i = 0; i < goodMatchesList.size(); i++) { objectPoints.addLast(objKeypointlist.get(goodMatchesList.get(i).queryIdx).pt); scenePoints.addLast(scnKeypointlist.get(goodMatchesList.get(i).trainIdx).pt); } MatOfPoint2f objMatOfPoint2f = new MatOfPoint2f(); objMatOfPoint2f.fromList(objectPoints); MatOfPoint2f scnMatOfPoint2f = new MatOfPoint2f(); scnMatOfPoint2f.fromList(scenePoints); Mat homography = Calib3d.findHomography(objMatOfPoint2f, scnMatOfPoint2f, Calib3d.RANSAC, 3); Mat obj_corners = new Mat(4, 1, CvType.CV_32FC2); Mat scene_corners = new Mat(4, 1, CvType.CV_32FC2); obj_corners.put(0, 0, new double[] { 0, 0 }); obj_corners.put(1, 0, new double[] { objectImage.cols(), 0 }); obj_corners.put(2, 0, new double[] { objectImage.cols(), objectImage.rows() }); obj_corners.put(3, 0, new double[] { 0, objectImage.rows() }); Core.perspectiveTransform(obj_corners, scene_corners, homography); Mat img = Imgcodecs.imread(Scene, Imgcodecs.CV_LOAD_IMAGE_COLOR); //draw a green square around the matched object Imgproc.line(img, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)), new Scalar(0, 255, 0), 10); Imgproc.line(img, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)), new Scalar(0, 255, 0), 10); Imgproc.line(img, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)), new Scalar(0, 255, 0), 10); Imgproc.line(img, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)), new Scalar(0, 255, 0), 10); MatOfDMatch goodMatches = new MatOfDMatch(); goodMatches.fromList(goodMatchesList); Features2d.drawMatches(objectImage, objectKeyPoints, sceneImage, sceneKeyPoints, goodMatches, matchoutput, matchestColor, newKeypointColor, new MatOfByte(), 2); //output image with match, image of the match locations and keypoints image String folder = "E:\\Users\\Jamie\\Documents\\NetBeansProjects\\VinylSleeveDetection\\Output\\"; Imgcodecs.imwrite(folder + "outputImage.jpg", outputImage); Imgcodecs.imwrite(folder + "matchoutput.jpg", matchoutput); Imgcodecs.imwrite(folder + "found.jpg", img); count = j; break; } else { System.out.println("Object Not Found"); count = 0; } } }
From source file:xored.vpn.fixer.TokenDetector.java
public boolean run() throws IOException, InterruptedException { saveImage();/*from w w w . j a v a2 s . c om*/ Mat source = Highgui.imread(workspace + "original.jpg", Highgui.CV_LOAD_IMAGE_COLOR); Mat destination = new Mat(source.rows(), source.cols(), source.type()); List<Mat> mats = new ArrayList<>(); mats.add(new Mat()); mats.add(new Mat()); mats.add(new Mat()); Core.split(source, mats); mats.get(1).convertTo(destination, -1, 2); imwrite(workspace + "green-ajusted.jpg", destination); Mat image = Highgui.imread(workspace + "green-ajusted.jpg", Imgproc.COLOR_RGB2GRAY); Rect rect = new Rect(176, 265, 178, 52); Mat imageB = threshold(image.submat(rect), 15, 2); int iWidth = 28; int iHeight = 45; List<Mat> matList = Arrays.asList(imageB, imageB.submat(new Rect(0, 0, iWidth, iHeight)), imageB.submat(new Rect(28, 2, iWidth, iHeight)), imageB.submat(new Rect(54, 2, iWidth, iHeight)), imageB.submat(new Rect(95, 5, iWidth, iHeight)), imageB.submat(new Rect(122, 6, iWidth, iHeight)), imageB.submat(new Rect(150, 7, iWidth, iHeight))); imwrite(temp + "1.jpg", matList.get(1)); imwrite(temp + "2.jpg", matList.get(2)); imwrite(temp + "3.jpg", matList.get(3)); imwrite(temp + "4.jpg", matList.get(4)); imwrite(temp + "5.jpg", matList.get(5)); imwrite(temp + "6.jpg", matList.get(6)); Mat m = addTo(source, matList); Core.rectangle(m, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255)); String code = recognize(); imwrite(workspace + "ResultsDebug/" + code + ".jpg", m); Main.log("Using code: " + code); Main.log("Using username: " + Main.username); Main.log("Using pin: " + Main.pin); String command = Main.nclauncher + " -url https://vpn.spirent.com/xored -r \"Contractor - Xored\" -u " + Main.username + " -p " + Main.pin + code; String response = Main.execCmd(command); Main.log(response); return response.indexOf("is already running") >= 0; }
From source file:xored.vpn.fixer.TokenDetector.java
private Mat addTo(Mat matA, List<Mat> mats) { Mat m = new Mat(matA.rows(), matA.cols(), matA.type()); for (int i = 0; i < matA.rows(); i++) { for (int j = 0; j < matA.cols(); j++) { m.put(i, j, matA.get(i, j)); }//from w w w . j a v a 2 s . c om } int xOffset = 0; for (Mat mat : mats) { for (int i = 0; i < mat.rows(); i++) { for (int j = 0; j < mat.cols(); j++) { double[] v = mat.get(i, j); if (v.length == 1) { double[] v1 = { v[0], v[0], v[0] }; m.put(i, j + xOffset, v1); } else { m.put(i, j + xOffset, v); } } } xOffset += mat.cols() + 10; } return m; }