當前位置: 首頁>>代碼示例>>Java>>正文


Java Imgproc.drawContours方法代碼示例

本文整理匯總了Java中org.opencv.imgproc.Imgproc.drawContours方法的典型用法代碼示例。如果您正苦於以下問題:Java Imgproc.drawContours方法的具體用法?Java Imgproc.drawContours怎麽用?Java Imgproc.drawContours使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.opencv.imgproc.Imgproc的用法示例。


在下文中一共展示了Imgproc.drawContours方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: Contours

import org.opencv.imgproc.Imgproc; //導入方法依賴的package包/類
void Contours() {
    Mat grayMat = new Mat();
    Mat cannyEdges = new Mat();
    Mat hierarchy = new Mat();

    List<MatOfPoint> contourList = new ArrayList<MatOfPoint>(); //A list to store all the contours

    //Converting the image to grayscale
    Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);

    Imgproc.Canny(originalMat, cannyEdges, 10, 100);

    //finding contours
    Imgproc.findContours(cannyEdges, contourList, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    //Drawing contours on a new image
    Mat contours = new Mat();
    contours.create(cannyEdges.rows(), cannyEdges.cols(), CvType.CV_8UC3);
    Random r = new Random();
    for (int i = 0; i < contourList.size(); i++) {
        Imgproc.drawContours(contours, contourList, i, new Scalar(r.nextInt(255), r.nextInt(255), r.nextInt(255)), -1);
    }
    //Converting Mat back to Bitmap
    Utils.matToBitmap(contours, currentBitmap);
    imageView.setImageBitmap(currentBitmap);
}
 
開發者ID:johnhany,項目名稱:MOAAP,代碼行數:27,代碼來源:MainActivity.java

示例2: irregularQuadrangle_Simplified

import org.opencv.imgproc.Imgproc; //導入方法依賴的package包/類
public static Mat irregularQuadrangle_Simplified(Mat irrInput, Point p1, Point p2, Point p3, Point p4,
		double shift_x, double shift_y, boolean noULandLRcorner, double ulCornerRatio, double lrCornerRatio) {
	final Mat maskCopyTo = Mat.zeros(irrInput.size(), CvType.CV_8UC1); // ����copyTo������mask����С��ԭͼ����һ��

	// �����ĵ���Ϊ��ʼ����
	final double centerX = (p1.x + p2.x + p3.x + p4.x) / 4;
	final double centerY = (p1.y + p2.y + p3.y + p4.y) / 4;
	final Point cc = new Point(centerX, centerY);

	final List<MatOfPoint> counter = new ArrayList<>();
	final Point aP1 = new Point(p1.x + shift_x, p1.y + shift_y);
	final Point aP2 = new Point(p2.x - shift_x, p2.y + shift_y);
	final Point aP3 = new Point(p3.x + shift_x, p3.y - shift_y);
	final Point aP4 = new Point(p4.x - shift_x, p4.y - shift_y);

	if (noULandLRcorner) {
		final Point aP1L = new Point(aP1.x, aP1.y + (aP3.y - aP1.y) * ulCornerRatio);
		final Point aP1R = new Point(aP1.x + (aP2.x - aP1.x) * ulCornerRatio, aP1.y);
		final Point aP4L = new Point(aP4.x - (aP4.x - aP3.x) * lrCornerRatio, aP4.y);
		final Point aP4R = new Point(aP4.x, aP4.y - (aP4.y - aP2.y) * lrCornerRatio);

		counter.add(new MatOfPoint(aP1L, aP1R, aP2, aP4R, aP4L, aP3)); // ����һ��������Ķ���Σ�û�����ϽǺ����½�
	} else
		counter.add(new MatOfPoint(aP1, aP2, aP4, aP3)); // ����һ��������Ķ����

	// floodFill��mask��width��height�����������ͼ��������������أ��������ᱨ��
	Imgproc.drawContours(maskCopyTo, counter, -1, Scalar.all(255)); // ��������
	// MatView.imshow(maskCopyTo, "Irregular shape edge");
	final Mat maskFloodFill = new Mat(irrInput.rows() + 2, irrInput.cols() + 2, CvType.CV_8UC1); // ����floodFill������mask���ߴ��ԭͼ��һЩ
	Imgproc.floodFill(maskCopyTo, maskFloodFill, new Point(centerX, centerY), Scalar.all(255), null, Scalar.all(20),
			Scalar.all(20), 4); // ��ˮ��䷨����ڲ�
	// MatView.imshow(maskFloodFill, "Irregular shape��Mask of floodFill"); //
	// ����copyTo������mask
	// MatView.imshow(maskCopyTo, "Irregular shape��Mask of copyTo"); //
	// ����floodFill������mask
	final Mat imgIrregularROI = new Mat();
	irrInput.copyTo(imgIrregularROI, maskCopyTo); // ��ȡ��������״��ROI
	// MatView.imshow(imgIrregularROI, "Irregular shape ROI");
	return imgIrregularROI;
}
 
開發者ID:zylo117,項目名稱:SpotSpotter,代碼行數:41,代碼來源:ROI_Irregular.java

示例3: findRectangle

import org.opencv.imgproc.Imgproc; //導入方法依賴的package包/類
public void findRectangle() {
        Imgproc.cvtColor(originalImage, image, Imgproc.COLOR_BGR2GRAY);
        setFilter();
        this.rects.clear();

        //Find Contours
        Imgproc.findContours(image, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));

        //For conversion later on
        MatOfPoint2f approxCurve = new MatOfPoint2f();

        //For each contour found
        for (int i = 0; i < contours.size(); i++) {

            //Convert contours from MatOfPoint to MatOfPoint2f
            MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(i).toArray());
            //Processing on mMOP2f1 which is in type MatOfPoint2f
            double approxDistance = Imgproc.arcLength(contour2f, true) * 0.02;

            if (approxDistance > 1) {
                //Find Polygons
                Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

                //Convert back to MatOfPoint
                MatOfPoint points = new MatOfPoint(approxCurve.toArray());

                //Rectangle Checks - Points, area, convexity
                if (points.total() == 4 && Math.abs(Imgproc.contourArea(points)) > 1000 && Imgproc.isContourConvex(points)) {
                    double cos = 0;
                    double mcos = 0;
                    for (int sc = 2; sc < 5; sc++) {
                        // TO-DO Figure a way to check angle
                        if (cos > mcos) {
                            mcos = cos;
                        }
                    }
                    if (mcos < 0.3) {
                        // Get bounding rect of contour
                        Rect rect = Imgproc.boundingRect(points);

//                        if (Math.abs(rect.height - rect.width) < 1000) {
                            System.out.println(i + "| x: " + rect.x + " + width("+rect.width+"), y: " + rect.y + "+ width("+rect.height+")");
                            rects.add(rect);
                            Core.rectangle(originalImage, rect.tl(), rect.br(), new Scalar(20, 20, 20), -1, 4, 0);
                            Imgproc.drawContours(originalImage, contours, i, new Scalar(0, 255, 0, .8), 2);
                            
                            // Highgui.imwrite("detected_layers"+i+".png", originalImage);
//                        }
                    }
                }
            }
        }
        // Pass raw parameters
        ImageDetection id = new ImageDetection();
        HyperTextBuilder.rects = this.rects;
        HyperTextBuilder.rect_height = this.HEIGHT;
        HyperTextBuilder.rect_width = this.WIDTH;
        id.setData(Utility.matToBufferedImage(originalImage));
    }
 
開發者ID:lupino22,項目名稱:kronometer,代碼行數:60,代碼來源:SketchRecognition.java

示例4: fillContours

import org.opencv.imgproc.Imgproc; //導入方法依賴的package包/類
public static Mat fillContours(Size size, List<MatOfPoint> contours, Point[] seeds) {
    Mat im = Mat.zeros(size, CvType.CV_8U);
    Mat mask = Mat.zeros(new Size(size.width + 2, size.height + 2), CvType.CV_8U);

    for (int ind = 0; ind < contours.size(); ++ind) {
        Imgproc.drawContours(mask, contours, ind, Util.SCALAR_WHITE);
    }

    for (Point p : seeds) {
        Imgproc.floodFill(im, mask, p, Util.SCALAR_WHITE);
    }

    return im;
}
 
開發者ID:hgs1217,項目名稱:Paper-Melody,代碼行數:15,代碼來源:Util.java

示例5: drawContour

import org.opencv.imgproc.Imgproc; //導入方法依賴的package包/類
private static void drawContour(Mat img, Contour contour, Color color, int thickness) {
    List<MatOfPoint> contoursOut = new ArrayList<>();
    contoursOut.add(contour.getData());
    Imgproc.drawContours(img, contoursOut, -1, color.getScalarRGBA(), thickness);
}
 
開發者ID:ykarim,項目名稱:FTC2016,代碼行數:6,代碼來源:Drawing.java

示例6: drawContours

import org.opencv.imgproc.Imgproc; //導入方法依賴的package包/類
public static void drawContours(Mat img, List<Contour> contours, Color color, int thickness) {
    List<MatOfPoint> contoursOut = new ArrayList<>();
    for (Contour contour : contours)
        contoursOut.add(contour.getData());
    Imgproc.drawContours(img, contoursOut, -1, color.getScalarRGBA(), thickness);
}
 
開發者ID:ykarim,項目名稱:FTC2016,代碼行數:7,代碼來源:Drawing.java

示例7: irregularQuadrangle

import org.opencv.imgproc.Imgproc; //導入方法依賴的package包/類
public static Mat irregularQuadrangle(Mat irrInput, Point p1, Point p2, Point p3, Point p4, double shift,
		boolean noULandLRcorner, double ulCornerRatio, double lrCornerRatio) {
	final Mat maskCopyTo = Mat.zeros(irrInput.size(), CvType.CV_8UC1); // ����copyTo������mask����С��ԭͼ����һ��

	final double angle = MathBox.slopeAngle(p1, p2);
	// System.out.println(angle);
	final double sin = Math.sin(angle);
	final double cos = Math.cos(angle);

	// �����ĵ���Ϊ��ʼ����
	final double centerX = (p1.x + p2.x + p3.x + p4.x) / 4;
	final double centerY = (p1.y + p2.y + p3.y + p4.y) / 4;
	final Point cc = new Point(centerX, centerY);

	final List<MatOfPoint> counter = new ArrayList<>();
	final Point aP1raw = new Point(p1.x + shift, p1.y + shift);
	final Point aP2raw = new Point(p2.x - shift, p2.y + shift);
	final Point aP3raw = new Point(p3.x + shift, p3.y - shift);
	final Point aP4raw = new Point(p4.x - shift, p4.y - shift);

	final Point aP1 = MathBox.rotateAroundAPoint(cc, aP1raw, angle);
	final Point aP2 = MathBox.rotateAroundAPoint(cc, aP2raw, angle);
	final Point aP3 = MathBox.rotateAroundAPoint(cc, aP3raw, angle);
	final Point aP4 = MathBox.rotateAroundAPoint(cc, aP4raw, angle);

	if (noULandLRcorner) {
		final Point aP1Lraw = new Point(aP1.x, aP1.y + (aP3.y - aP1.y) * ulCornerRatio);
		final Point aP1Rraw = new Point(aP1.x + (aP2.x - aP1.x) * ulCornerRatio, aP1.y);
		final Point aP4Lraw = new Point(aP4.x - (aP4.x - aP3.x) * lrCornerRatio, aP4.y);
		final Point aP4Rraw = new Point(aP4.x, aP4.y - (aP4.y - aP2.y) * lrCornerRatio);

		final Point aP1L = MathBox.rotateAroundAPoint(cc, aP1Lraw, angle);
		final Point aP1R = MathBox.rotateAroundAPoint(cc, aP1Rraw, angle);
		final Point aP4L = MathBox.rotateAroundAPoint(cc, aP4Lraw, angle);
		final Point aP4R = MathBox.rotateAroundAPoint(cc, aP4Rraw, angle);

		counter.add(new MatOfPoint(aP1L, aP1R, aP2, aP4R, aP4L, aP3)); // ����һ��������Ķ���Σ�û�����ϽǺ����½�
	} else
		counter.add(new MatOfPoint(aP1, aP2, aP4, aP3)); // ����һ��������Ķ����

	// floodFill��mask��width��height�����������ͼ��������������أ��������ᱨ��
	Imgproc.drawContours(maskCopyTo, counter, -1, Scalar.all(255)); // ��������
	MatView.imshow(maskCopyTo, "Irregular shape edge");
	final Mat maskFloodFill = new Mat(irrInput.rows() + 2, irrInput.cols() + 2, CvType.CV_8UC1); // ����floodFill������mask���ߴ��ԭͼ��һЩ
	Imgproc.floodFill(maskCopyTo, maskFloodFill, new Point(centerX, centerY), Scalar.all(255), null, Scalar.all(20),
			Scalar.all(20), 4); // ��ˮ��䷨����ڲ�
	// MatView.imshow(maskFloodFill, "Irregular shape��Mask of floodFill"); //
	// ����copyTo������mask
	// MatView.imshow(maskCopyTo, "Irregular shape��Mask of copyTo"); //
	// ����floodFill������mask
	final Mat imgIrregularROI = new Mat();
	irrInput.copyTo(imgIrregularROI, maskCopyTo); // ��ȡ��������״��ROI
	// MatView.imshow(imgIrregularROI, "Irregular shape ROI");
	return imgIrregularROI;
}
 
開發者ID:zylo117,項目名稱:SpotSpotter,代碼行數:56,代碼來源:ROI_Irregular.java

示例8: drawContours

import org.opencv.imgproc.Imgproc; //導入方法依賴的package包/類
public static void drawContours(Mat im, List<MatOfPoint> contours, Scalar color) {
    for (int ind = 0; ind < contours.size(); ++ind) {
        Imgproc.drawContours(im, contours, ind, color, 2);
    }
}
 
開發者ID:hgs1217,項目名稱:Paper-Melody,代碼行數:6,代碼來源:Util.java

示例9: drawContours

import org.opencv.imgproc.Imgproc; //導入方法依賴的package包/類
/**
 * Draws a list of contours unto a mat.
 * 
 * @param feed the mat to draw on
 * @param contours list of contours to draw
 * @param color color to draw with
 * @see Imgproc#drawContours(Mat, List, int, Scalar)
 */
public static void drawContours(Mat feed, List<MatOfPoint> contours, Scalar color) {
	for(int i = 0; i < contours.size(); i++)
		Imgproc.drawContours(feed, contours, i, color);
}
 
開發者ID:Flash3388,項目名稱:FlashLib,代碼行數:13,代碼來源:CvProcessing.java


注:本文中的org.opencv.imgproc.Imgproc.drawContours方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。