Java Code Examples for org.opencv.imgcodecs.Imgcodecs#imread()

The following examples show how to use org.opencv.imgcodecs.Imgcodecs#imread() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageFinder.java    From opentest with MIT License 6 votes vote down vote up
/**
 * Finds a template image on the screen. Throws an exception when the image
 * wasn't found or the desired accuracy couldn't be met.
 *
 * @param sourceScreenRect The rectangle on the screen to look into.
 * @param templateImage The template image to find.
 * @param desiredAccuracy The desired accuracy of the find operation as a
 * number between 0 and 1.
 * @return An ImageFinderResult object that stores the rectangle of the
 * found image and desired accuracy.
 */
@Override
public ImageFinderResult findImage(Rectangle sourceScreenRect, File templateImage, double desiredAccuracy) {
    try {
        BufferedImage capture = new Robot().createScreenCapture(sourceScreenRect);
        Mat sourceMat = CvHelper.convertToMat(capture);
        Mat templateMat = Imgcodecs.imread(templateImage.getAbsolutePath());
        return this.findImage(sourceMat, templateMat, desiredAccuracy);
    } catch (Exception ex) {
        throw new RuntimeException(String.format(
                "An error ocurred while trying to find an image on screen at (%s, %s, %s, %s)",
                sourceScreenRect.x,
                sourceScreenRect.y,
                sourceScreenRect.width,
                sourceScreenRect.height), ex);
    }
}
 
Example 2
Source File: OpenCVoperation.java    From Human-hair-detection with Apache License 2.0 6 votes vote down vote up
public void skinSegmentation_WithThreshold()
{ 
    //-----code for skin detection--using hsv color method
    
    orgMask= new Mat(matrix2_grabcut.size(),CvType.CV_8UC3);
    orgMask.setTo(new Scalar(0,0,0));
    
    Mat hsvImage = new Mat();
    Imgproc.cvtColor(matrix2_grabcut, hsvImage, Imgproc.COLOR_BGR2HSV);
    Core.inRange(hsvImage, new Scalar(3,30,50),new Scalar(33,255,255),orgMask);
    
    Imgcodecs.imwrite(resultDirectory + maskOutput, orgMask);
    
    newMask = Imgcodecs.imread(resultDirectory + maskOutput);
    //code for getting rgb skin mask from hsv skin mask
    mask_rgb = new Mat(newMask.size(),CvType.CV_8SC3);
    Imgproc.cvtColor(newMask, mask_rgb, Imgproc.COLOR_HSV2RGB);    
    Imgcodecs.imwrite(resultDirectory+maskRgbOutput, mask_rgb);
    
    //getting only skin image with red background
    matrix3_skindetection= new Mat(sourceImage.size(), sourceImage.type());
    matrix3_skindetection.setTo(new Scalar(0,0,255));
    sourceImage.copyTo(matrix3_skindetection,orgMask);
    
    Imgcodecs.imwrite(resultDirectory+skinDetectionOutput,matrix3_skindetection);
}
 
Example 3
Source File: CSVFileUtils.java    From classchecks with Apache License 2.0 6 votes vote down vote up
public static void loadImage(String saveRoute, List<Mat> matLists, List<Integer> labels) {
	File saveRoot = new File(saveRoute);
	if(!saveRoot.exists()) {
		LOG.info("图片保存路径--" + saveRoute + "--不存在");
		return;
	}
	if(!saveRoot.isDirectory()) {
		LOG.info("图片路径--" + saveRoute+ "--不是一个文件夹");
		return;
	}
	
	File[] procImages = saveRoot.listFiles(new ImageFileFilter());
	for(int i = 0; i < procImages.length; i ++) {
		LOG.info("加载图片:" + procImages[i].getAbsolutePath());
		Mat m = Imgcodecs.imread(procImages[i].getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
		matLists.add(m);
		labels.add(Integer.parseInt(procImages[i].getName().split("_")[0]));
	}
}
 
Example 4
Source File: OpenCVNonMavenExamples.java    From Java-for-Data-Science with MIT License 6 votes vote down vote up
public static void denoise() {
        String imgInPath = "captchaExample.jpg";
        imgInPath = "MyCaptcha.PNG";
        imgInPath = "blurredtext.jpg";
        String imgOutPath = "captchaNoiseRemovedExample.png";
        imgOutPath = "MyNoiseRemovedCaptcha.PNG";

        Mat image = Imgcodecs.imread(imgInPath);
        Mat out = new Mat();
        Mat tmp = new Mat();
        Mat kernel = new Mat(new Size(3, 3), CvType.CV_8UC1, new Scalar(255));
//        Mat kernel = new Mat(image.size(), CvType.CV_8UC1, new Scalar(255));
        Imgproc.morphologyEx(image, tmp, Imgproc.MORPH_OPEN, kernel);
        Imgproc.morphologyEx(tmp, out, Imgproc.MORPH_CLOSE, kernel);
        Imgcodecs.imwrite(imgOutPath, out);
    }
 
Example 5
Source File: CSVFileUtils.java    From classchecks with Apache License 2.0 6 votes vote down vote up
public static void loadImage(String saveRoute, List<Mat> matLists) {
	File saveRoot = new File(saveRoute);
	if(!saveRoot.exists()) {
		LOG.info("图片保存路径--" + saveRoute + "--不存在");
		return;
	}
	if(!saveRoot.isDirectory()) {
		LOG.info("图片路径--" + saveRoute+ "--不是一个文件夹");
		return;
	}
	File[] procImages = saveRoot.listFiles(new ImageFileFilter());
	for(int i = 0; i < procImages.length; i ++) {
		//LOG.info("加载图片:" + procImages[i].getAbsolutePath());
		Mat m = Imgcodecs.imread(procImages[i].getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
		matLists.add(m);
	}
}
 
Example 6
Source File: ALTMRetinexExample.java    From OptimizedImageEnhance with MIT License 5 votes vote down vote up
public static void main (String[] args) {
    String imgPath = "src/main/resources/dark_images/liahthouse.png";
    Mat image = Imgcodecs.imread(imgPath, Imgcodecs.CV_LOAD_IMAGE_COLOR);
    new ImShow("Original").showImage(image);
    Mat result = ALTMRetinex.enhance(image, r, eps, eta, lambda, krnlRatio);
    new ImShow("result").showImage(result);
}
 
Example 7
Source File: OptimizedContrastEnhanceExample.java    From OptimizedImageEnhance with MIT License 5 votes vote down vote up
public static void main (String[] args) {
    String imgPath = "src/main/resources/haze_images/canon_2.jpg";
    Mat image = Imgcodecs.imread(imgPath, Imgcodecs.CV_LOAD_IMAGE_COLOR);
    new ImShow("org-image").showImage(image);
    Mat result = OptimizedContrastEnhance.enhance(image, blkSize, patchSize, lambda, eps, krnlSize);
    result.convertTo(result, CvType.CV_8UC1);
    new ImShow("dehaze-image").showImage(result);
}
 
Example 8
Source File: ImageTest.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Test
public void testSift1() {
	System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
	
	Mat src = Imgcodecs.imread("g:/test/find-src.jpg");
	Mat dst = Imgcodecs.imread("g:/test/find-dest.jpg");
	
	MatOfRect mr = getFace(dst);
	Mat sub = dst.submat(mr.toArray()[0]);
	
	Imgcodecs.imwrite("g:/test/sift-result.jpg", FeatureSiftLannbased(src.t(), sub));
}
 
Example 9
Source File: TrainingThread.java    From ml-authentication with Apache License 2.0 5 votes vote down vote up
/**
 * Load image into OpenCV Mat object
 * Extract features from TensorFlow model
 * @param tensorFlow
 * @param studentImage
 * @return
 */
private synchronized String getFeatureVectorString(TensorFlow tensorFlow, StudentImage studentImage){
    // Load image into OpenCV Mat object
    Mat img = Imgcodecs.imread(studentImage.getImageFileUrl());
    Log.i(getClass().getName(), "StudentImage has been loaded from file " + studentImage.getImageFileUrl());
    // Extract features from TensorFlow model
    Mat featureVector = tensorFlow.getFeatureVector(img);
    List<Float> featureVectorList = new ArrayList<>();
    Converters.Mat_to_vector_float(featureVector, featureVectorList);
    String serializedFeatureVector = gson.toJson(featureVectorList);
    Log.i(getClass().getName(), "Feature vector has been extracted for StudentImage: " + studentImage.getId());
    return serializedFeatureVector;
}
 
Example 10
Source File: FusionEnhanceExample.java    From OptimizedImageEnhance with MIT License 5 votes vote down vote up
public static void main (String[] args) {
    String imgPath = "src/main/resources/underwater_images/underwater_scene.jpg";
    Mat image = Imgcodecs.imread(imgPath, Imgcodecs.CV_LOAD_IMAGE_COLOR);
    new ImShow("original").showImage(image);
    Mat fusion = FusionEnhance.enhance(image, level);
    fusion.convertTo(fusion, CvType.CV_8UC1);
    new ImShow("fusion").showImage(fusion);
}
 
Example 11
Source File: DetectObjectTest.java    From classchecks with Apache License 2.0 5 votes vote down vote up
@Test
public void testDetectManyObject() {
	String opencvDLL = "G:/java/JavaProjectRelease/classchecks/src/main/webapp/WEB-INF/dll/x64/opencv_java320.dll";
	System.load(opencvDLL);
	
	String haarcascade = "haarcascade_frontalface_alt.xml";
	
	
	CascadeClassifier cascade = new CascadeClassifier(XMLFilePath + haarcascade);
	
	Mat src = Imgcodecs.imread(imageDir + "/split/14.jpg");
	
	MatOfRect objects = new MatOfRect();
	int scaledWidth = src.width();
	
	DetectObject.detectManyObject(src, cascade, objects, scaledWidth);
	
	Rect [] rects = objects.toArray();
	int i = 0;
	for(Rect r : rects) {
		/*Imgproc.rectangle(src, new Point(r.x-100 , r.y-100 ), 
				new Point(r.x + r.width + 80, 
						r.y + r.height + 80), new Scalar(0, 0, 255), 3);*/
		Imgproc.rectangle(src, r.tl(), 
				r.br(), new Scalar(0, 0, 255), 3);
		/*r.width += 120;
		r.height += 120;
		r.x -= 100;
		r.y -= 100;
		System.out.println(r);
		Mat roi = new Mat(src, r);
		Imgcodecs.imwrite("e:/classchecks/2017417/split/"+i+".jpg", roi);
		i ++;*/
	}
	Imgcodecs.imwrite("e:/classchecks/2017417/dectctManyObject.jpg", src);
	//Imgcodecs.imwrite("e:/classchecks/dectctManyObject.jpg", src);
}
 
Example 12
Source File: OpenCVNonMavenExamples.java    From Java-for-Data-Science with MIT License 5 votes vote down vote up
public void enhanceImageBrightness() {
    double alpha = 1;   // Change to 2 for more brightness
    double beta = 50;
    String fileName = "cat.jpg";

    Mat source = Imgcodecs.imread("cat.jpg");
    Mat destination = new Mat(source.rows(), source.cols(),
            source.type());
    source.convertTo(destination, -1, 1, 50);
    Imgcodecs.imwrite("brighterCat.jpg", destination);
}
 
Example 13
Source File: OpenCVNonMavenExamples.java    From Java-for-Data-Science with MIT License 5 votes vote down vote up
public void enhanceImageContrast() {
    Mat source = Imgcodecs.imread("GrayScaleParrot.png",
            Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
    Mat destination = new Mat(source.rows(), source.cols(), source.type());
    Imgproc.equalizeHist(source, destination);
    Imgcodecs.imwrite("enhancedParrot.jpg", destination);
}
 
Example 14
Source File: HighGuiUtil.java    From javautils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取人脸范围
 * @param fileName
 * @return
 */
public static MatOfRect takeFace(String fileName) {
    CascadeClassifier faceDetector = new CascadeClassifier("libs/lbpcascade_frontalface.xml");
    Mat image = Imgcodecs.imread(fileName);
    MatOfRect faceDetections = new MatOfRect();
    // 指定人脸识别的最大和最小像素范围
    Size minSize = new Size(120, 120);
    Size maxSize = new Size(250, 250);
    // 参数设置为scaleFactor=1.1f, minNeighbors=4, flags=0 以此来增加识别人脸的正确率
    faceDetector.detectMultiScale(image, faceDetections, 1.1f, 4, 0,
            minSize, maxSize);
    return faceDetections;
}
 
Example 15
Source File: Test.java    From classchecks with Apache License 2.0 5 votes vote down vote up
public static void recognitionByLBPH() {
	String modelFilePath = "E:\\classchecks\\2017417\\train\\trainLBPHModel-201704171530.xml";
	LBPHFaceRecognizer model = TrainFaces.loadLBPHModel(modelFilePath);
	Mat waitRecoMat = Imgcodecs.imread("E:\\classchecks\\2017417\\split\\14.jpg");
	//Imgcodecs.imr
	Mat preProc = PreProcessFace.rawProcessedFace(waitRecoMat);
	ImageGui.imshow(preProc, "preProc");
	Imgproc.resize(preProc, preProc, new Size(92, 112));
	//Mat reconstructMat = Recognition.reconstructFace(model, preProc);
	
	//double similarity = Recognition.getSimilarity(preProc, reconstructMat);
       //System.out.println("similarity=" + similarity);
       int pridictLabel = model.predict_label(preProc);
       System.out.println("pridictLabel=" + pridictLabel);
}
 
Example 16
Source File: DetectionTestActivity.java    From Android-Face-Recognition-with-Deep-Learning-Test-Framework with Apache License 2.0 4 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();
    final Handler handler = new Handler(Looper.getMainLooper());
    thread = new Thread(new Runnable() {
        public void run() {
            if(!Thread.currentThread().isInterrupted()){
                PreProcessorFactory ppF = new PreProcessorFactory(getApplicationContext());

                FileHelper fileHelper = new FileHelper();
                File[] detectionFolders = fileHelper.getDetectionTestList();
                if (detectionFolders.length > 0) {
                    // total and matches are used to calculate the accuracy afterwards
                    int total = 0;
                    int matches = 0;
                    List<String> results = new ArrayList<>();
                    results.add("Expected Name;Expected File;Result");
                    Date time_start = new Date();
                    for (File folder : detectionFolders) {
                        File[] files = folder.listFiles();
                        int counter = 1;
                        for (File file : files) {
                            if (FileHelper.isFileAnImage(file)) {
                                Mat imgRgba = Imgcodecs.imread(file.getAbsolutePath());
                                Imgproc.cvtColor(imgRgba, imgRgba, Imgproc.COLOR_BGRA2RGBA);

                                List<Mat> images = ppF.getProcessedImage(imgRgba, PreProcessorFactory.PreprocessingMode.DETECTION);
                                Rect[] faces = ppF.getFacesForRecognition();

                                String result = "";

                                if (faces == null || faces.length == 0) {
                                    result = RESULT_NEGATIVE;
                                } else {
                                    result = RESULT_POSITIVE;
                                    faces = MatOperation.rotateFaces(imgRgba, faces, ppF.getAngleForRecognition());
                                    for(int i = 0; i<faces.length; i++){
                                        MatOperation.drawRectangleAndLabelOnPreview(images.get(0), faces[i], "", false);
                                    }
                                }

                                // Save images
                                String[] tokens = file.getName().split("\\.");
                                String filename = tokens[0];
                                for (int i=0; i<images.size();i++){
                                    MatName m = new MatName(filename + "_" + (i + 1), images.get(i));
                                    fileHelper.saveMatToImage(m, FileHelper.RESULTS_PATH + "/" + time_start.toString() + "/");
                                }

                                tokens = file.getParent().split("/");
                                final String name = tokens[tokens.length - 1];

                                results.add(name + ";" + file.getName() + ";" + result);

                                total++;

                                if (name.equals(result)) {
                                    matches++;
                                }
                                // Update screen to show the progress
                                final int counterPost = counter;
                                final int filesLength = files.length;
                                progress.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        progress.append("Image " + counterPost + " of " + filesLength + " from " + name + "\n");
                                    }
                                });
                                counter++;
                            }
                        }
                    }
                    Date time_end = new Date();
                    long duration = time_end.getTime() - time_start.getTime();
                    int durationPerImage = (int) duration / total;
                    double accuracy = (double) matches / (double) total;
                    Map<String, ?> printMap = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getAll();
                    fileHelper.saveResultsToFile(printMap, accuracy, durationPerImage, results);

                    final Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.putExtra("accuracy", accuracy);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            startActivity(intent);
                        }
                    });
                }
            } else {
                Thread.currentThread().interrupt();
            }
        }
    });
    thread.start();
}
 
Example 17
Source File: ImageRecognition.java    From onetwo with Apache License 2.0 4 votes vote down vote up
private Mat readImage(String path) {
	return Imgcodecs.imread(path, Imgcodecs.CV_LOAD_IMAGE_COLOR);
}
 
Example 18
Source File: FaceDetection.java    From tutorials with MIT License 4 votes vote down vote up
public static Mat loadImage(String imagePath) {
    Imgcodecs imageCodecs = new Imgcodecs();
    return imageCodecs.imread(imagePath);
}
 
Example 19
Source File: MergeThread.java    From ml-authentication with Apache License 2.0 4 votes vote down vote up
/**
 * Find similar students
 * Case 1: Student was added during fallback but in the meantime the same person has an existing StudentImageCollectionEvent and a new Student entry
 * ---> Use the avatar image as input for the recognition
 * @param ppF
 * @param tensorFlow
 */
private synchronized void findSimilarStudentsUsingAvatarImages(PreProcessorFactory ppF, TensorFlow tensorFlow){
    Log.i(getClass().getName(), "findSimilarStudentsUsingAvatarImages");
    // Iterate through all Students
    List<Student> students = studentDao.loadAll();
    for (Student student : students){
        // Take the avatar image of the Student
        Mat avatarImage = Imgcodecs.imread(student.getAvatar());
        // Search for faces in the avatar image
        List<Mat> faceImages = ppF.getCroppedImage(avatarImage);
        if (faceImages != null && faceImages.size() == 1) {
            // Proceed if exactly one face has been detected
            Mat faceImage = faceImages.get(0);
            if (faceImage != null) {
                // Get detected face rectangles
                Rect[] faces = ppF.getFacesForRecognition();
                if (faces != null && faces.length == 1) {
                    // Proceed if exactly one face rectangle exists
                    RecognitionThread recognitionThread = new RecognitionThread(tensorFlow, studentImageCollectionEventDao);
                    recognitionThread.setImg(faceImage);
                    recognitionThread.setStudent(student);
                    Log.i(getClass().getName(), "findSimilarStudentsUsingAvatarImages: recognitionThread will be started to recognize student: " + student.getUniqueId());
                    recognitionThread.start();
                    try {
                        recognitionThread.join();
                        List<Student> recognizedStudents = recognitionThread.getRecognizedStudent();
                        if (recognizedStudents.size() > 0){
                            for (Student recognizedStudent : recognizedStudents){
                                if (recognizedStudent != null){
                                    Log.i(getClass().getName(), "findSimilarStudentsUsingAvatarImages: The student " + student.getUniqueId() + " has been recognized as " + recognizedStudent.getUniqueId());
                                    mergeSimilarStudents(student, recognizedStudent);
                                }
                            }
                        } else {
                            Log.i(getClass().getName(), "findSimilarStudentsUsingAvatarImages: The student " + student.getUniqueId() + " was not recognized");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
 
Example 20
Source File: TestUtils.java    From go-bees with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Load image from test directory into OpenCV mat.
 * Ex: TestUtils.loadImage("res/img/frame.jpg", Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
 *
 * @param imgPath path to the image (relative from test directory).
 * @param flags   CV_LOAD_IMAGE_ANYDEPTH, CV_LOAD_IMAGE_COLOR, CV_LOAD_IMAGE_GRAYSCALE.
 * @return mat of the image.
 */
public static Mat loadImage(String imgPath, int flags) {
    return Imgcodecs.imread(DIRECTORY_TEST + imgPath, flags);
}