android opencv camera实例,java - How to increase Android Opencv camera FPS? - Stack Overflow

I am developing an Android application and I'm processing frames using Opencv in real time.

The image processing is heavy as I'm detecting the largest contour, rotating and cropping it and apply some sort of segmentation on the cropped Mat object.

The problem is that FPS start with a value of 3 and drops to 1.

I'm not doing some heavy operations like matToBitmap and I'm not making native calls yet.

It's my first big project and I don't have much experience in Opencv. What can I do to increase my FPS?

Here is my code:

long e1 = Core.getTickCount();

Mat mGray = new Mat();

MatOfDouble mu = new MatOfDouble();

MatOfDouble stddev = new MatOfDouble();

Imgproc.cvtColor(origMat, origMat, Imgproc.COLOR_BGRA2BGR);

Imgproc.cvtColor(origMat, mGray, Imgproc.COLOR_BGR2GRAY);

Core.meanStdDev(mGray, mu, stddev);

Imgproc.GaussianBlur(mGray, mGray, new Size(5, 5), 5);

Imgproc.Canny(mGray, mGray, (mu.get(0, 0)[0]) * 0.66, (mu.get(0, 0)[0]) * 1.33, 3, false);

Mat kernell = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(9, 9));

Imgproc.morphologyEx(mGray, mGray, Imgproc.MORPH_CLOSE, kernell);

Imgproc.dilate(mGray, mGray, Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(3, 3)));

List contours = new ArrayList();

Mat hierarchy = new Mat();

Imgproc.findContours(mGray, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

double largest_area = 0;

Rect rect = new Rect();

int largest_idx = 0;

for (int idx = 0; idx < contours.size(); idx++) {

double a = Imgproc.contourArea(contours.get(idx)); //Find the area of contour

if (a > largest_area) {

largest_area = a;

largest_idx = idx;

}

}

if (contours.size() > 0) {

MatOfPoint2f new_mat = new MatOfPoint2f(contours.get(largest_idx).toArray());

RotatedRect rbox = Imgproc.minAreaRect(new_mat);

Point vertices[] = new Point[4];

rbox.points(vertices);

List boxContours = new ArrayList<>();

boxContours.add(new MatOfPoint(vertices));

for (int i = 0; i < 4; ++i) {

Imgproc.line(origMat, vertices[i], vertices[(i + 1) % 4], new Scalar(255, 0, 0));

}

double rect_angle = rbox.angle - 90.0f;

Size rect_size = rbox.size;

double d = rect_size.width;

rect_size.width = rect_size.height;

rect_size.height = d;

M = Imgproc.getRotationMatrix2D(rbox.center, rect_angle, 1.0);

Imgproc.warpAffine(origMat, rotated, M, origMat.size());

if (rect_size.width > 70 && rect_size.height > 70)

Imgproc.getRectSubPix(rotated, new Size(rect_size.width - 70, rect_size.height - 70), rbox.center, rotated);

Imgproc.resize(rotated, rotated, origMat.size());

Mat orr = rotated.clone();

Imgproc.cvtColor(orr, orr, Imgproc.COLOR_RGB2HSV);

Core.split(orr, channels);

orr = channels.get(2);

CLAHE clahe = Imgproc.createCLAHE();

clahe.setClipLimit(1);

clahe.apply(orr, orr);

Imgproc.GaussianBlur(orr, orr, new Size(5, 5), 5);

Core.meanStdDev(mGray, mu, stddev);

Imgproc.Canny(orr, orr, (mu.get(0, 0)[0]) * 0.66, (mu.get(0, 0)[0]) * 1.33, 3, false);

Imgproc.morphologyEx(orr, orr, Imgproc.MORPH_CLOSE, kernell);

Imgproc.dilate(orr, orr, Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(3, 3)));

Imgproc.findContours(orr, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

for (int i = contours.size() - 1; i >= 0; i--) {

double area = Imgproc.contourArea(contours.get(i), false); // Find the area of contour

if (area < min_area)

contours.remove(i);

}

if (contours.size() > 0) {

MatOfPoint2f approxCurve = new MatOfPoint2f();

rectList.clear();

for (int idx = 0; idx < contours.size(); idx++) {

//Convert contours(i) from MatOfPoint to MatOfPoint2f

MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(idx).toArray());

//Processing on mMOP2f1 which is in type MatOfPoint2f

double approxDistance = Imgproc.arcLength(contour2f, true) * 0.02;

Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

//Convert back to MatOfPoint

MatOfPoint points = new MatOfPoint(approxCurve.toArray());

// Get bounding rect of contour

Rect rectt = Imgproc.boundingRect(points);

rectList.add(rect);

Mat miniature = new Mat(orr, new Rect(rectt.tl(), rectt.br()));

mats.add(miniature);

// draw enclosing rectangle (all same color, but you could use variable i to make them unique)

Imgproc.rectangle(rotated, rectt.tl(), rectt.br(), new Scalar(255, 0, 0));

}

}

long e2 = Core.getTickCount();

long e = e2 - e1;

double time = e / Core.getTickFrequency();

Log.d("timeTAG", "" + time);

return orr;

}

return rotated;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值