【发布时间】:2017-10-15 08:14:08
【问题描述】:
我正在尝试检测任何颜色的激光的激光点。我已经从这里OpenCV Android Track laser dot做了一些参考代码
该代码运行完美,仅用于红色检测,我想要任何颜色的激光点检测。
我是 OpenCV 的新手。
这是我到目前为止所做的:
Mat originalFrame= new Mat();
Mat frame = new Mat();
cvf.rgba().copyTo(originalFrame);
cvf.rgba().copyTo(frame);
Mat frameH;
Mat frameV;
Mat frameS;
mRgba = cvf.rgba();
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
// Mat frameS;
// Convert it to HSV
Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGB2HSV);
// Split the frame into individual components (separate images for H, S,
// and V)
mChannels.clear();
Core.split(frame, mChannels); // Split channels: 0-H, 1-S, 2-V
frameH = mChannels.get(0);
frameS = mChannels.get(1);
frameV = mChannels.get(2);
// Apply a threshold to each component
Imgproc.threshold(frameH, frameH, 155, 160, Imgproc.THRESH_BINARY);
// Imgproc.threshold(frameS, frameS, 0, 100, Imgproc.THRESH_BINARY);
Imgproc.threshold(frameV, frameV, 250, 256, Imgproc.THRESH_BINARY);
// Perform an AND operation
Core.bitwise_and(frameH, frameV, frame);
//
// Core.bitwise_and(frame,frameS,frame);
Imgproc.findContours(frame, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));
hierarchy.release();
for ( int contourIdx=0; contourIdx < contours.size(); contourIdx++ )
{
// Minimum size allowed for consideration
MatOfPoint2f approxCurve = new MatOfPoint2f();
MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(contourIdx).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 rect = Imgproc.boundingRect(points);
Imgproc.rectangle(originalFrame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255), 3);
}
【问题讨论】:
-
如果你开始你的代码,你会知道你正在寻找的激光的颜色吗?例如,您是否希望能够将某些配置设置为“现在我们要检测蓝色激光而不是红色激光”,或者您想检测任何图像中的任何激光?第一种情况应该很容易,第二种情况会很棘手并且可能容易出错。是否有任何其他假设(例如场景/任务?)
-
嘿@Micka,激光的颜色可以是红色、绿色、蓝色、紫色等任何颜色......我想在实时相机帧中检测它,而不是从图像中检测。你能帮我吗?谢谢。
-
实况视频也是图像。我想知道用户是否可以/可以在运行时限制可能的激光颜色,或者您是否想同时检测所有激光颜色(这将更加困难)。例如,用户可以将设置更改为“现在我想检测紫色激光,没有别的,因为我知道演示者使用的激光是紫色的,不会有任何其他激光。”由于我们不了解您的应用程序,我们无法判断这些假设是否有效。
-
是的@Micka 激光颜色一次只能检测一种,我想一次只检测一种激光颜色点,但光的颜色不能固定,可以是任何颜色。那我们该怎么办?
-
难题,我会尝试在 HSV 值通道中对高值区域进行阈值处理并在色调通道中查找 blob。您可能需要一些反复试验的方法和一些计算机视觉方面的经验。
标签: android c++ opencv feature-detection