我正在使用Android OpenCV构建实时对象检测应用程序.我正在使用带有TextureView的Android Camera2 API来捕获图像.我想添加OpenCV代码来进行一些实时图像处理并预览结果.
这是我的拍照代码
protected void takePicture() {
if(null == cameraDevice) {
Log.e(TAG, "cameraDevice is null");
return;
}
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
Size[] jpegSizes = null;
if (characteristics != null) {
jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.JPEG);
}
int width = 640;
int height = 480;
if (jpegSizes != null && 0 < jpegSizes.length) {
width = jpegSizes[0].getWidth();
height = jpegSizes[0].getHeight();
}
ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
List<Surface> outputSurfaces = new ArrayList<Surface>(2);
outputSurfaces.add(reader.getSurface()); …Run Code Online (Sandbox Code Playgroud) 我正在开发一个程序来检测矩形并在检测到的区域绘制边界框。
对于边缘检测,我使用了Canny边缘检测。然后,我使用霍夫变换提取线。
这是原始图像 在这里输入图像描述
这是结果图像,请 在此处输入图像描述
我的问题是我无法在检测到的区域绘制边界框。看来我的程序只能检测到一条水平线。如何检测矩形并将矩形线绘制到检测到的形状?
我已经读过类似的问题,需要找到矩形的4个角点,检查该点是否为90度,并找到相交点。我真的很困惑如何在Java opencv中进行编码。其他检测形状并将边界框绘制到检测到的框的方法也可以。
这是代码
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.*;
import org.opencv.imgproc.Imgproc;
public class HoughTransformCV2 {
public static void main(String[] args) {
try {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat source = Imgcodecs.imread("rectangle.jpg", Imgcodecs.CV_LOAD_IMAGE_ANYCOLOR);
Mat destination = new Mat(source.rows(), source.cols(), source.type());
Imgproc.cvtColor(source, destination, Imgproc.COLOR_RGB2GRAY);
Imgproc.equalizeHist(destination, destination);
Imgproc.GaussianBlur(destination, destination, new Size(5, 5), 0, 0, Core.BORDER_DEFAULT);
Imgproc.Canny(destination, destination, 50, 100);
//Imgproc.adaptiveThreshold(destination, destination, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 40);
Imgproc.threshold(destination, destination, 0, 255, Imgproc.THRESH_BINARY); …Run Code Online (Sandbox Code Playgroud)