我对OpenCV以及寻找特定形状有一个简短的问题。在我的PC上,我得到了一些形状的图片,但是我只想要矩形的轮廓:
我做了什么:
这就是我坚持的地方。我不知道如何消除错误的轮廓。我尝试了遍历我的轮廓,并删除了不正确的轮廓。但是我不知道如何找到错误的轮廓。有没有我需要使用的配方师?像这样?我找到了带有“ arcLength”的东西,但我不明白这一点。
这是我的代码:
package main;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import helper.ImageProcHelper;
public class Main {
public static void main(String[] args) throws Exception {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
File file = new File("C:\\Users\\Enrico Gründig\\Desktop\\Samples\\pic4.png");
Mat mat = new Mat(CvType.CV_8UC4);
Mat procMat = new Mat();
Mat hierarchy = new Mat();
Scalar color = new Scalar(0,0,255);
List<MatOfPoint> contours = new ArrayList<>();
try {
BufferedImage picture = ImageIO.read(file);
BufferedImage image = new BufferedImage(picture.getWidth(), picture.getHeight(), 5);
image.getGraphics().drawImage(picture, 0, 0, null);
System.out.println(image.getType());
mat = ImageProcHelper.ImageToMat(image);
Imgproc.cvtColor(mat, procMat, Imgproc.COLOR_RGBA2GRAY);
Imgproc.blur(procMat, procMat, new Size(3,3));
Imgproc.Canny(procMat, procMat, 127, 255);
//Konturen finden
Imgproc.findContours(procMat, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
MatOfPoint2f[] contoursPoly = new MatOfPoint2f[contours.size()];
Rect[] boundRect = new Rect[contours.size()];
for(int i = 0; i < contours.size(); i++) {
contoursPoly[i] = new MatOfPoint2f();
Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(i).toArray()), contoursPoly[i], 0.1, true);
boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contours.get(i).toArray()));
}
for (int i = 0; i < contours.size(); i++) {
Imgproc.rectangle(mat, boundRect[i].tl(), boundRect[i].br(), color, 1);
}
image = ImageProcHelper.MatToImage(mat);
ImageIO.write((RenderedImage)image, "png", new File ("C:\\Users\\Enrico Gründig\\Desktop\\Samples\\output.png"));
} catch (IOException e) {
System.out.println("Error");
}
}
}
这个项目有什么意义:
我有一个IP摄像机正在播放视频。在这个项目中,我想找到流中的所有QR码,将它们裁剪并传递给解码器(例如ZXing)。我只用ZXing尝试过,但是角度,大小等方面都有问题。这就是为什么我要使用OpenCV查找代码并操纵它们以减少从IP摄像机到解码器的流量,并(也许)增加点击率的原因。
非常感谢您的帮助。
答案 0 :(得分:0)
我没有足够的声誉来发表评论,但是您似乎缺少的是检查每个轮廓的边。在您的代码中,您需要使用ContoursPoly [i] .size()来区分不同的形状。您的代码最终需要看起来像这样:
package main;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import helper.ImageProcHelper;
public class Main {
public static void main(String[] args) throws Exception {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
File file = new File("C:\\Users\\Enrico Gründig\\Desktop\\Samples\\pic4.png");
Mat mat = new Mat(CvType.CV_8UC4);
Mat procMat = new Mat();
Mat hierarchy = new Mat();
Scalar color = new Scalar(0,0,255);
List<MatOfPoint> contours = new ArrayList<>();
try {
BufferedImage picture = ImageIO.read(file);
BufferedImage image = new BufferedImage(picture.getWidth(), picture.getHeight(), 5);
image.getGraphics().drawImage(picture, 0, 0, null);
System.out.println(image.getType());
mat = ImageProcHelper.ImageToMat(image);
Imgproc.cvtColor(mat, procMat, Imgproc.COLOR_RGBA2GRAY);
Imgproc.blur(procMat, procMat, new Size(3,3));
Imgproc.Canny(procMat, procMat, 127, 255);
//Konturen finden
Imgproc.findContours(procMat, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
MatOfPoint2f[] contoursPoly = new MatOfPoint2f[contours.size()];
Rect[] boundRect = new Rect[contours.size()];
for(int i = 0; i < contours.size(); i++) {
contoursPoly[i] = new MatOfPoint2f();
Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(i).toArray()), contoursPoly[i], 0.1, true);
boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contours.get(i).toArray()));
}
for (int i = 0; i < contours.size(); i++) {
if (contoursPoly[i].size()>15){
Imgproc.rectangle(mat, boundRect[i].tl(), boundRect[i].br(), color, 1);
}
}
image = ImageProcHelper.MatToImage(mat);
ImageIO.write((RenderedImage)image, "png", new File ("C:\\Users\\Enrico Gründig\\Desktop\\Samples\\output.png"));
} catch (IOException e) {
System.out.println("Error");
}
}
}
我没有使用OpenCV进行Java设置,因此我无法测试此代码,但是这个想法来自这个link。您可能需要弄乱“ 15”才能区分矩形和圆形。