Java Code Examples for org.opencv.imgproc.Imgproc#putText()

The following examples show how to use org.opencv.imgproc.Imgproc#putText() . 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: VideoFaceTests.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
/**
 * OpenCV-4.0.0 人脸识别
 * 
 * @date: 2019年5月7日12:16:55
 * @param image
 *            待处理Mat图片(视频中的某一帧)
 * @return 处理后的图片
 * @throws IOException
 */
public static Mat getFace(Mat image) throws IOException {
	// 1 读取OpenCV自带的人脸识别特征XML文件
	File faceFile = new ClassPathResourcePatternResolver().getResource("opencv/data/haarcascade_frontalface_alt.xml")
			.getFile();
	CascadeClassifier facebook = new CascadeClassifier(faceFile.getAbsolutePath());
	// 2 特征匹配类
	MatOfRect face = new MatOfRect();
	// 3 特征匹配
	facebook.detectMultiScale(image, face);
	Rect[] rects = face.toArray();
	System.out.println("匹配到 " + rects.length + " 个人脸");
	// 4 为每张识别到的人脸画一个圈
	for (int i = 0; i < rects.length; i++) {
		Imgproc.rectangle(image, new Point(rects[i].x, rects[i].y),
				new Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height), new Scalar(0, 255, 0));
		Imgproc.putText(image, "Human", new Point(rects[i].x, rects[i].y), Imgproc.FONT_HERSHEY_SCRIPT_SIMPLEX, 1.0,
				new Scalar(0, 255, 0), 1, Imgproc.LINE_AA, false);
		// Mat dst=image.clone();
		// Imgproc.resize(image, image, new Size(300,300));
	}
	return image;
}
 
Example 2
Source File: DogeCVDetector.java    From DogeCV with GNU General Public License v3.0 6 votes vote down vote up
@Override
public final Mat processFrame(Mat input) {
    size = input.size();

    Log.d("DogeCVDetector", "Input mat size:" + input.size());
    input.copyTo(workingMat);

    if(workingMat.empty()){
        return input;
    }

    workingMat = process(workingMat);

    //Print Info
    Imgproc.putText(workingMat,"DogeCV 2020.1 " + detectorName + ": " + stageToRenderToViewport.toString(), new Point(5,30),0,0.5,new Scalar(0,255,255),2);

    return workingMat;
}
 
Example 3
Source File: MainActivity.java    From MOAAP with MIT License 6 votes vote down vote up
static Mat drawMatches(Mat img1, MatOfKeyPoint key1, Mat img2, MatOfKeyPoint key2, MatOfDMatch matches, boolean imageOnly){
    //https://github.com/mustafaakin/image-matcher/tree/master/src/in/mustafaak/imagematcher
    Mat out = new Mat();
    Mat im1 = new Mat();
    Mat im2 = new Mat();
    Imgproc.cvtColor(img1, im1, Imgproc.COLOR_GRAY2RGB);
    Imgproc.cvtColor(img2, im2, Imgproc.COLOR_GRAY2RGB);
    if ( imageOnly){
        MatOfDMatch emptyMatch = new MatOfDMatch();
        MatOfKeyPoint emptyKey1 = new MatOfKeyPoint();
        MatOfKeyPoint emptyKey2 = new MatOfKeyPoint();
        Features2d.drawMatches(im1, emptyKey1, im2, emptyKey2, emptyMatch, out);
    } else {
        Features2d.drawMatches(im1, key1, im2, key2, matches, out);
    }
    //Bitmap bmp = Bitmap.createBitmap(out.cols(), out.rows(), Bitmap.Config.ARGB_8888);
    Imgproc.cvtColor(out, out, Imgproc.COLOR_BGR2RGB);
    Imgproc.putText(out, "Frame", new Point(img1.width() / 2,30), Core.FONT_HERSHEY_PLAIN, 2, new Scalar(0,255,255),3);
    Imgproc.putText(out, "Match", new Point(img1.width() + img2.width() / 2,30), Core.FONT_HERSHEY_PLAIN, 2, new Scalar(255,0,0),3);
    return out;
}
 
Example 4
Source File: ComparisonFrameRender.java    From OpenCV-AndroidSamples with MIT License 6 votes vote down vote up
@Override
public Mat render(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    Mat undistortedFrame = new Mat(inputFrame.rgba().size(), inputFrame.rgba().type());
    Imgproc.undistort(inputFrame.rgba(), undistortedFrame,
            mCalibrator.getCameraMatrix(), mCalibrator.getDistortionCoefficients());

    Mat comparisonFrame = inputFrame.rgba();
    undistortedFrame.colRange(new Range(0, mWidth / 2)).copyTo(comparisonFrame.colRange(new Range(mWidth / 2, mWidth)));
    List<MatOfPoint> border = new ArrayList<MatOfPoint>();
    final int shift = (int)(mWidth * 0.005);
    border.add(new MatOfPoint(new Point(mWidth / 2 - shift, 0), new Point(mWidth / 2 + shift, 0),
            new Point(mWidth / 2 + shift, mHeight), new Point(mWidth / 2 - shift, mHeight)));
    Imgproc.fillPoly(comparisonFrame, border, new Scalar(255, 255, 255));

    Imgproc.putText(comparisonFrame, mResources.getString(R.string.original), new Point(mWidth * 0.1, mHeight * 0.1),
            Core.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar(255, 255, 0));
    Imgproc.putText(comparisonFrame, mResources.getString(R.string.undistorted), new Point(mWidth * 0.6, mHeight * 0.1),
            Core.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar(255, 255, 0));

    return comparisonFrame;
}
 
Example 5
Source File: ImageProcessor.java    From pasm-yolov3-Android with GNU General Public License v3.0 5 votes vote down vote up
public Mat drawBoxes(List<Classifier.Recognition> boxes, double confidenceThreshold){
        rgbImage.copyTo(boxesImage);
        Scalar color;


        for (Classifier.Recognition box : boxes) {
            Log.i(TAG, String.valueOf(box));
            if (box.getTitle().equals("person")) {
                if (box.getConfidence() > confidenceThreshold) {
//                color.val[0] = (color.val[0] + 25) % 255;
//                color.val[1] = (color.val[1] + 35) % 255;
//                color.val[2] = (color.val[2] + 45) % 255;
                    color = colors.get(box.getTitle());

                    Point pt1 = new Point(box.getLocation().left * widthRatio, box.getLocation().top * heightRatio);
                    Point pt2 = new Point(box.getLocation().right * widthRatio, box.getLocation().bottom * heightRatio);
                    Imgproc.rectangle(boxesImage, pt1, pt2, color, 3, 8);
                    Point pt3 = new Point(box.getLocation().left * widthRatio, box.getLocation().top * heightRatio);
                    Point pt4 = new Point(Math.min(box.getLocation().right, box.getLocation().left + (box.getTitle().length() * 13)) * widthRatio, (box.getLocation().top + 11) * heightRatio);
                    Imgproc.rectangle(boxesImage, pt3, pt4, color, FILLED, 8);

                    pt1.set(new double[]{pt1.x + 2 * heightRatio, (pt1.y + 10 * heightRatio)});
                    Imgproc.putText(boxesImage, box.getTitle(), pt1, Core.FONT_HERSHEY_SIMPLEX, 0.4 * heightRatio, (isLight(color) ? BLACK : WHITE), (int) (1 * heightRatio), LINE_AA);
                }
            }
        }

        return boxesImage;
    }
 
Example 6
Source File: MainActivity.java    From MOAAP with MIT License 5 votes vote down vote up
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

        //Get image size and draw a rectangle on the image for reference
        Mat temp = inputFrame.rgba();
        Imgproc.rectangle(temp, new Point(temp.cols()/2 - 200, temp.rows() / 2 - 200), new Point(temp.cols() / 2 + 200, temp.rows() / 2 + 200), new Scalar(255,255,255),1);
        Mat digit = temp.submat(temp.rows()/2 - 180, temp.rows() / 2 + 180, temp.cols() / 2 - 180, temp.cols() / 2 + 180).clone();
        Core.transpose(digit,digit);
        int predict_result = mnist.FindMatch(digit);
        Imgproc.putText(temp, Integer.toString(predict_result), new Point(50, 150), FONT_HERSHEY_SIMPLEX, 3.0, new Scalar(0, 0, 255), 5);

        return temp;
    }
 
Example 7
Source File: Drawing.java    From FTCVision with MIT License 5 votes vote down vote up
public static void drawText(Mat img, String text, Point origin, float scale, Color color, Anchor locationOnImage) {
    if (locationOnImage == Anchor.BOTTOMLEFT)
        Transform.flip(img, Transform.FlipType.FLIP_ACROSS_Y);
    Imgproc.putText(img, text, origin, Core.FONT_HERSHEY_SIMPLEX, scale, color.getScalarRGBA(), 2, Core.LINE_8,
            (locationOnImage == Anchor.BOTTOMLEFT || locationOnImage == Anchor.BOTTOMLEFT_UNFLIPPED_Y));
    if (locationOnImage == Anchor.BOTTOMLEFT)
        Transform.flip(img, Transform.FlipType.FLIP_ACROSS_Y);
}
 
Example 8
Source File: Puzzle15Processor.java    From OpenCV-AndroidSamples with MIT License 5 votes vote down vote up
public synchronized Mat puzzleFrame(Mat inputPicture) {
    Mat[] cells = new Mat[GRID_AREA];
    int rows = inputPicture.rows();
    int cols = inputPicture.cols();

    rows = rows - rows%4;
    cols = cols - cols%4;

    for (int i = 0; i < GRID_SIZE; i++) {
        for (int j = 0; j < GRID_SIZE; j++) {
            int k = i * GRID_SIZE + j;
            cells[k] = inputPicture.submat(i * inputPicture.rows() / GRID_SIZE, (i + 1) * inputPicture.rows() / GRID_SIZE, j * inputPicture.cols()/ GRID_SIZE, (j + 1) * inputPicture.cols() / GRID_SIZE);
        }
    }

    rows = rows - rows%4;
    cols = cols - cols%4;

    // copy shuffled tiles
    for (int i = 0; i < GRID_AREA; i++) {
        int idx = mIndexes[i];
        if (idx == GRID_EMPTY_INDEX)
            mCells15[i].setTo(GRID_EMPTY_COLOR);
        else {
            cells[idx].copyTo(mCells15[i]);
            if (mShowTileNumbers) {
                Imgproc.putText(mCells15[i], Integer.toString(1 + idx), new Point((cols / GRID_SIZE - mTextWidths[idx]) / 2,
                        (rows / GRID_SIZE + mTextHeights[idx]) / 2), 3/* CV_FONT_HERSHEY_COMPLEX */, 1, new Scalar(255, 0, 0, 255), 2);
            }
        }
    }

    for (int i = 0; i < GRID_AREA; i++)
        cells[i].release();

    drawGrid(cols, rows, mRgba15);

    return mRgba15;
}
 
Example 9
Source File: CameraActivity.java    From AndroidObjectDetection-OpenCV with MIT License 4 votes vote down vote up
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    Mat frame = inputFrame.rgba();
    Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2RGB);
    Size frame_size = new Size(416, 416);
    Scalar mean = new Scalar(127.5);

    Mat blob = Dnn.blobFromImage(frame, 1.0 / 255.0, frame_size, mean, true, false);
    //save_mat(blob);
    net.setInput(blob);

    List<Mat> result = new ArrayList<>();
    List<String> outBlobNames = net.getUnconnectedOutLayersNames();

    net.forward(result, outBlobNames);
    float confThreshold = 0.5f;

    for (int i = 0; i < result.size(); ++i) {
        // each row is a candidate detection, the 1st 4 numbers are
        // [center_x, center_y, width, height], followed by (N-4) class probabilities
        Mat level = result.get(i);
        for (int j = 0; j < level.rows(); ++j) {
            Mat row = level.row(j);
            Mat scores = row.colRange(5, level.cols());
            Core.MinMaxLocResult mm = Core.minMaxLoc(scores);
            float confidence = (float) mm.maxVal;
            Point classIdPoint = mm.maxLoc;
            if (confidence > confThreshold) {

                int centerX = (int) (row.get(0, 0)[0] * frame.cols());
                int centerY = (int) (row.get(0, 1)[0] * frame.rows());
                int width = (int) (row.get(0, 2)[0] * frame.cols());
                int height = (int) (row.get(0, 3)[0] * frame.rows());

                int left = (int) (centerX - width * 0.5);
                int top =(int)(centerY - height * 0.5);
                int right =(int)(centerX + width * 0.5);
                int bottom =(int)(centerY + height * 0.5);

                Point left_top = new Point(left, top);
                Point right_bottom=new Point(right, bottom);
                Point label_left_top = new Point(left, top-5);
                DecimalFormat df = new DecimalFormat("#.##");

                int class_id = (int) classIdPoint.x;
                String label= classNames.get(class_id) + ": " + df.format(confidence);
                Scalar color= colors.get(class_id);

                Imgproc.rectangle(frame, left_top,right_bottom , color, 3, 2);
                Imgproc.putText(frame, label, label_left_top, Imgproc.FONT_HERSHEY_SIMPLEX, 1, new Scalar(0, 0, 0), 4);
                Imgproc.putText(frame, label, label_left_top, Imgproc.FONT_HERSHEY_SIMPLEX, 1, new Scalar(255, 255, 255), 2);
            }
        }
    }
    return frame;
}
 
Example 10
Source File: StoneDetector.java    From DogeCV with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Mat process(Mat input) {
    screenPositions.clear();
    foundRects.clear();
    
    input.copyTo(rawImage);
    input.copyTo(workingMat);
    input.copyTo(displayMat);
    input.copyTo(yellowMask);

    // Imgproc.GaussianBlur(workingMat,workingMat,new Size(5,5),0);
    filter.process(workingMat.clone(), yellowMask);

    List<MatOfPoint> contoursYellow = new ArrayList<>();
    Imgproc.findContours(yellowMask, contoursYellow, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
    Imgproc.drawContours(displayMat,contoursYellow,-1,new Scalar(230,70,70),2);

    // Current result
    ArrayList<Rect> bestRects = new ArrayList<>();
    double bestDifference = Double.MAX_VALUE; // MAX_VALUE since less difference = better

    Collections.sort(contoursYellow, new Comparator<MatOfPoint>() {
        @Override
        public int compare(MatOfPoint matOfPoint, MatOfPoint t1) {
            return calculateScore(matOfPoint) > calculateScore(t1) ? 1 : 0;
        }
    });

    List<MatOfPoint> subList = contoursYellow;

    if (contoursYellow.size() > stonesToFind) {
        subList = contoursYellow.subList(0, stonesToFind);
    }

    for (MatOfPoint contour : subList) {
        Rect rect = Imgproc.boundingRect(contour);

        // Show chosen result
        Imgproc.rectangle(displayMat, rect.tl(), rect.br(), new Scalar(255, 0, 0), 4);
        Imgproc.putText(displayMat, "Chosen", rect.tl(), 0, 1, new Scalar(255, 255, 255));

        screenPositions.add(new Point(rect.x, rect.y));
        foundRects.add(rect);
    }

    if (foundRects.size() > 0) {
        found = true;
    }
    else {
        found = false;
    }

    switch (stageToRenderToViewport) {
        case THRESHOLD: {
            Imgproc.cvtColor(yellowMask, yellowMask, Imgproc.COLOR_GRAY2BGR);

            return yellowMask;
        }
        case RAW_IMAGE: {
            return rawImage;
        }
        default: {
            return displayMat;
        }
    }
}
 
Example 11
Source File: MatOperation.java    From Android-Face-Recognition-with-Deep-Learning-Library with Apache License 2.0 4 votes vote down vote up
public static void drawRectangleAndLabelOnPreview(Mat img, Rect face, String label, boolean front_camera){
    Point tl = drawRectangleOnPreview(img, face, front_camera);
    Imgproc.putText(img, label, tl, Core.FONT_HERSHEY_PLAIN, FONT_SIZE, FACE_RECT_COLOR, THICKNESS);
}
 
Example 12
Source File: CVRenderer.java    From faceswap with Apache License 2.0 4 votes vote down vote up
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        Imgproc.cvtColor(inputFrame.rgba(), displayFrame, Imgproc.COLOR_BGRA2BGR);
        if (this.delegate!=null){
            //TODO: needed so that bg encoding won't interfere with foreground getting a new image
            displayFrame.copyTo(transmitFrame);
            delegate.onCVPreviewAvailable(transmitFrame);
        }
        synchronized (this.faces){
            for (Face face:faces){
                int[] roi = face.realRoi;
                boundryCheck(roi, displayFrame.width(), displayFrame.height());
                Point leftTop=new Point(roi[0], roi[1]);
                Point rightBottom=new Point(roi[2], roi[3]);
                if (face.isRenderring && face.argbMat!=null){
                    rightBottom=new Point(roi[0]+face.argbMat.width(),
                            roi[1]+face.argbMat.height());
                    Rect pRoi = new Rect(roi[0],roi[1],
                            face.argbMat.width(), face.argbMat.height());
                    Log.d("debug", "pRoi : " + pRoi.toString());
                    Log.d("debug", "display frame width : " + displayFrame.width()
                            + " display frame height: " + displayFrame.height());
                    Mat pRoiMat=displayFrame.submat(pRoi);
                    Log.d("debug", "display frame width : " + displayFrame.width()
                            + " display frame height: " + displayFrame.height());

                    face.argbMat.copyTo(pRoiMat);
                }
                Imgproc.rectangle(displayFrame,
                        leftTop,
                        rightBottom,
                        new Scalar(255, 0, 0));
                Imgproc.putText(displayFrame,
                        face.getName(),
                        new Point(roi[0], roi[1]),
                        0,
                        0.8,
                        new Scalar(255,255,0));

            }
        }
        Log.d(LOG_TAG, "rendered");
        return displayFrame;
//                Mat pRoi=mRgba.submat(10,10+replaceImg.width(),10,10+replaceImg.height());
//                Log.d("debug", "mat width: " + pRoi.width() + " height: " + pRoi.height());
//                Log.d("debug", "img width: " + replaceImg.width() + " height: " + replaceImg.height());
//                replaceImg.copyTo(pRoi);
    }
 
Example 13
Source File: FXController.java    From Face-Recognition with Apache License 2.0 4 votes vote down vote up
/**
	 * Method for face detection and tracking
	 * 
	 * @param frame
	 *            it looks for faces in this frame
	 */
	private void detectAndDisplay(Mat frame)
	{
		MatOfRect faces = new MatOfRect();
		Mat grayFrame = new Mat();
		
		// convert the frame in gray scale
		Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY);
		// equalize the frame histogram to improve the result
		Imgproc.equalizeHist(grayFrame, grayFrame);
		
		// compute minimum face size (20% of the frame height, in our case)
		if (this.absoluteFaceSize == 0)
		{
			int height = grayFrame.rows();
			if (Math.round(height * 0.2f) > 0)
			{
				this.absoluteFaceSize = Math.round(height * 0.2f);
			}
		}
		
		// detect faces
		this.faceCascade.detectMultiScale(grayFrame, faces, 1.1, 2, 0 | Objdetect.CASCADE_SCALE_IMAGE,
				new Size(this.absoluteFaceSize, this.absoluteFaceSize), new Size());
				
		// each rectangle in faces is a face: draw them!
		Rect[] facesArray = faces.toArray(); 
		for (int i = 0; i < facesArray.length; i++) {
			Imgproc.rectangle(frame, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0), 3);

			// Crop the detected faces
			Rect rectCrop = new Rect(facesArray[i].tl(), facesArray[i].br());
			Mat croppedImage = new Mat(frame, rectCrop);
			// Change to gray scale
			Imgproc.cvtColor(croppedImage, croppedImage, Imgproc.COLOR_BGR2GRAY);
			// Equalize histogram
			Imgproc.equalizeHist(croppedImage, croppedImage);
			// Resize the image to a default size
			Mat resizeImage = new Mat();
			Size size = new Size(250,250);
			Imgproc.resize(croppedImage, resizeImage, size);
			
			// check if 'New user' checkbox is selected
			// if yes start collecting training data (50 images is enough)
			if ((newUser.isSelected() && !newname.isEmpty())) {
				if (index<20) {
					Imgcodecs.imwrite("resources/trainingset/combined/" +
					random + "-" + newname + "_" + (index++) + ".png", resizeImage);
				}
			}
//			int prediction = faceRecognition(resizeImage);
			double[] returnedResults = faceRecognition(resizeImage);
			double prediction = returnedResults[0];
			double confidence = returnedResults[1];
			
//			System.out.println("PREDICTED LABEL IS: " + prediction);
			int label = (int) prediction;
			String name = "";
			if (names.containsKey(label)) {
				name = names.get(label);
			} else {
				name = "Unknown";
			}
			
			// Create the text we will annotate the box with:
//            String box_text = "Prediction = " + prediction + " Confidence = " + confidence;
            String box_text = "Prediction = " + name + " Confidence = " + confidence;
            // Calculate the position for annotated text (make sure we don't
            // put illegal values in there):
            double pos_x = Math.max(facesArray[i].tl().x - 10, 0);
            double pos_y = Math.max(facesArray[i].tl().y - 10, 0);
            // And now put it into the image:
            Imgproc.putText(frame, box_text, new Point(pos_x, pos_y), 
            		Core.FONT_HERSHEY_PLAIN, 1.0, new Scalar(0, 255, 0, 2.0));
		}
	}
 
Example 14
Source File: CameraCalibrator.java    From OpenCV-AndroidSamples with MIT License 4 votes vote down vote up
private void renderFrame(Mat rgbaFrame) {
    drawPoints(rgbaFrame);

    Imgproc.putText(rgbaFrame, "Captured: " + mCornersBuffer.size(), new Point(rgbaFrame.cols() / 3 * 2, rgbaFrame.rows() * 0.1),
            Core.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar(255, 255, 0));
}