Java Code Examples for org.opencv.core.CvType#CV_32FC2

The following examples show how to use org.opencv.core.CvType#CV_32FC2 . 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: CVProcessor.java    From CVScanner with GNU General Public License v3.0 6 votes vote down vote up
/**
 *
 * @param src - actual image
 * @param pts - points scaled up with respect to actual image
 * @return
 */
public static Mat fourPointTransform( Mat src , Point[] pts ) {
    Point tl = pts[0];
    Point tr = pts[1];
    Point br = pts[2];
    Point bl = pts[3];

    double widthA = Math.sqrt(Math.pow(br.x - bl.x, 2) + Math.pow(br.y - bl.y, 2));
    double widthB = Math.sqrt(Math.pow(tr.x - tl.x, 2) + Math.pow(tr.y - tl.y, 2));

    double dw = Math.max(widthA, widthB);
    int maxWidth = Double.valueOf(dw).intValue();


    double heightA = Math.sqrt(Math.pow(tr.x - br.x, 2) + Math.pow(tr.y - br.y, 2));
    double heightB = Math.sqrt(Math.pow(tl.x - bl.x, 2) + Math.pow(tl.y - bl.y, 2));

    double dh = Math.max(heightA, heightB);
    int maxHeight = Double.valueOf(dh).intValue();

    Mat doc = new Mat(maxHeight, maxWidth, CvType.CV_8UC4);

    Mat src_mat = new Mat(4, 1, CvType.CV_32FC2);
    Mat dst_mat = new Mat(4, 1, CvType.CV_32FC2);

    src_mat.put(0, 0, tl.x, tl.y, tr.x, tr.y, br.x, br.y, bl.x, bl.y);
    dst_mat.put(0, 0, 0.0, 0.0, dw, 0.0, dw, dh, 0.0, dh);

    Mat m = Imgproc.getPerspectiveTransform(src_mat, dst_mat);

    Imgproc.warpPerspective(src, doc, m, doc.size());

    return doc;
}
 
Example 2
Source File: MainActivity.java    From SimpleDocumentScanner-Android with MIT License 5 votes vote down vote up
/**
 * NOTE:
 * Based off of http://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/
 *
 * @param src
 * @param pts
 * @return
 */
private Mat fourPointTransform(Mat src, Point[] pts) {
    double ratio = src.size().height / (double) MAX_HEIGHT;

    Point ul = pts[0];
    Point ur = pts[1];
    Point lr = pts[2];
    Point ll = pts[3];

    double widthA = Math.sqrt(Math.pow(lr.x - ll.x, 2) + Math.pow(lr.y - ll.y, 2));
    double widthB = Math.sqrt(Math.pow(ur.x - ul.x, 2) + Math.pow(ur.y - ul.y, 2));
    double maxWidth = Math.max(widthA, widthB) * ratio;

    double heightA = Math.sqrt(Math.pow(ur.x - lr.x, 2) + Math.pow(ur.y - lr.y, 2));
    double heightB = Math.sqrt(Math.pow(ul.x - ll.x, 2) + Math.pow(ul.y - ll.y, 2));
    double maxHeight = Math.max(heightA, heightB) * ratio;

    Mat resultMat = new Mat(Double.valueOf(maxHeight).intValue(), Double.valueOf(maxWidth).intValue(), CvType.CV_8UC4);

    Mat srcMat = new Mat(4, 1, CvType.CV_32FC2);
    Mat dstMat = new Mat(4, 1, CvType.CV_32FC2);
    srcMat.put(0, 0, ul.x * ratio, ul.y * ratio, ur.x * ratio, ur.y * ratio, lr.x * ratio, lr.y * ratio, ll.x * ratio, ll.y * ratio);
    dstMat.put(0, 0, 0.0, 0.0, maxWidth, 0.0, maxWidth, maxHeight, 0.0, maxHeight);

    Mat M = Imgproc.getPerspectiveTransform(srcMat, dstMat);
    Imgproc.warpPerspective(src, resultMat, M, resultMat.size());

    srcMat.release();
    dstMat.release();
    M.release();

    return resultMat;
}
 
Example 3
Source File: OpticalFlowOp.java    From StormCV with Apache License 2.0 4 votes vote down vote up
@Override
public List<CVParticle> execute(List<CVParticle> input) throws Exception {
	List<CVParticle> result = new ArrayList<CVParticle>();
	if(input.size() != 2 || !(input.get(0) instanceof Frame) || !(input.get(1) instanceof Frame))
		return result;
	
	Frame frame1 = (Frame)input.get(0);
	Frame frame2 = (Frame)input.get(1);
	
	MatOfByte mob1 = new MatOfByte(frame1.getImageBytes());
	Mat image1 = Highgui.imdecode(mob1, Highgui.CV_LOAD_IMAGE_ANYCOLOR);
	Mat image1Gray = new Mat( image1.size(), CvType.CV_8UC1 );
	Imgproc.cvtColor( image1, image1Gray, Imgproc.COLOR_RGB2GRAY );
	
	MatOfByte mob2 = new MatOfByte(frame2.getImageBytes());
	Mat image2 = Highgui.imdecode(mob2, Highgui.CV_LOAD_IMAGE_ANYCOLOR);
	Mat image2Gray = new Mat( image2.size(), CvType.CV_8UC1 );
	Imgproc.cvtColor( image2, image2Gray, Imgproc.COLOR_RGB2GRAY );
	
	Mat opticalFlow = new Mat( image1Gray.size(), CvType.CV_32FC2 );
	Video.calcOpticalFlowFarneback( image1Gray, image2Gray, opticalFlow, 0.5, 1, 1, 1, 7, 1.5, 1 );
	
	int cols = opticalFlow.cols();
	int rows = opticalFlow.rows();
	int maxz = opticalFlow.get(0,0).length;
	float[] tmp = new float[maxz];
	float[][][] dense = new float[cols][rows][maxz];
	for(int y=0; y<opticalFlow.rows(); y++){
		for(int x=0; x<opticalFlow.cols(); x++){
			opticalFlow.get(y,x, tmp);
			dense[x][y][0] = tmp[0];
			dense[x][y][1] = tmp[1];
		}
	}
	
	Feature feature = new Feature(frame1.getStreamId(), frame1.getSequenceNr(), name, frame2.getSequenceNr()-frame1.getSequenceNr(), null, dense);
	if(outputFrame){
		frame1.getFeatures().add(feature);
		result.add(frame1);
	}else{
		result.add(feature);
	}

	return result;
}