opencv/javacv:如何迭代轮廓进行形状识别?
我正在使用JavaCV开发一个形状识别项目,我发现了一些OpenCV代码来识别特定图像中的U形。我尝试将其转换为JavaCV,但它没有给出相同的输出。你能帮我把这个OpenCV代码转换成JavaCV吗 这是OpenCV代码:opencv/javacv:如何迭代轮廓进行形状识别?,java,image-processing,opencv,javacv,Java,Image Processing,Opencv,Javacv,我正在使用JavaCV开发一个形状识别项目,我发现了一些OpenCV代码来识别特定图像中的U形。我尝试将其转换为JavaCV,但它没有给出相同的输出。你能帮我把这个OpenCV代码转换成JavaCV吗 这是OpenCV代码: import cv2 import numpy as np img = cv2.imread('sofud.jpg') gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(gray,
import cv2
import numpy as np
img = cv2.imread('sofud.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,1)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
if 10 < w/float(h) or w/float(h) < 0.1:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
cv2.imshow('res',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
导入cv2
将numpy作为np导入
img=cv2.imread('sofud.jpg'))
灰色=cv2.CVT颜色(img,cv2.COLOR\U BGR2GRAY)
ret,thresh=cv2。阈值(灰色,127255,1)
等高线,层次=cv2.findContours(阈值,cv2.RETR\u列表,cv2.CHAIN\u近似值\u简单)
对于轮廓中的cnt:
x、 y,w,h=cv2.boundingRect(cnt)
如果10
这是预期的输出
这是转换后的代码:
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import static com.googlecode.javacpp.Loader.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import java.io.File;
import javax.swing.JFileChooser;
public class TestBeam {
public static void main(String[] args) {
CvMemStorage storage=CvMemStorage.create();
CvSeq squares = new CvContour();
squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage);
JFileChooser f=new JFileChooser();
int result=f.showOpenDialog(f);//show dialog box to choose files
File myfile=null;
String path="";
if(result==0){
myfile=f.getSelectedFile();//selected file taken to myfile
path=myfile.getAbsolutePath();//get the path of the file
}
IplImage src = cvLoadImage(path);//hear path is actual path to image
IplImage grayImage = IplImage.create(src.width(), src.height(), IPL_DEPTH_8U, 1);
cvCvtColor(src, grayImage, CV_RGB2GRAY);
cvThreshold(grayImage, grayImage, 127, 255, CV_THRESH_BINARY);
CvSeq cvSeq=new CvSeq();
CvMemStorage memory=CvMemStorage.create();
cvFindContours(grayImage, memory, cvSeq, Loader.sizeof(CvContour.class), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
System.out.println(cvSeq.total());
for (int i = 0; i < cvSeq.total(); i++) {
CvRect rect=cvBoundingRect(cvSeq, i);
int x=rect.x(),y=rect.y(),h=rect.height(),w=rect.width();
if (10 < (w/h) || (w/h) < 0.1){
cvRectangle(src, cvPoint(x, y), cvPoint(x+w, y+h), CvScalar.RED, 1, CV_AA, 0);
//cvSeqPush(squares, rect);
}
}
CanvasFrame cnvs=new CanvasFrame("Beam");
cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
cnvs.showImage(src);
//cvShowImage("Final ", src);
}
}
import com.googlecode.javacpp.Loader;
导入com.googlecode.javacv.CanvasFrame;
导入静态com.googlecode.javacpp.Loader.*;
导入静态com.googlecode.javacv.cpp.opencv_core.*;
导入静态com.googlecode.javacv.cpp.opencv_imgproc.*;
导入静态com.googlecode.javacv.cpp.opencv_highgui.*;
导入java.io.File;
导入javax.swing.JFileChooser;
公共类测试波束{
公共静态void main(字符串[]args){
CvMemStorage=CvMemStorage.create();
CvSeq squares=新的CvContour();
正方形=cvCreateSeq(0,sizeof(CvContour.class),sizeof(CvSeq.class),存储);
JFileChooser f=新的JFileChooser();
int result=f.showOpenDialog(f);//显示选择文件的对话框
File myfile=null;
字符串路径=”;
如果(结果==0){
myfile=f.getSelectedFile();//所选文件被带到myfile
path=myfile.getAbsolutePath();//获取文件的路径
}
IplImage src=cvLoadImage(path);//hear path是映像的实际路径
IplImage grayImage=IplImage.create(src.width(),src.height(),IPL_DEPTH_8U,1);
CVT颜色(src、灰度图像、CV_RGB2GRAY);
cvThreshold(灰度图像,灰度图像,127,255,CV_阈值_二进制);
CvSeq CvSeq=新的CvSeq();
CvMemStorage memory=CvMemStorage.create();
cvFindContours(灰度图像、内存、cvSeq、Loader.sizeof(CvContour.class)、CV_RETR_CCOMP、CV_CHAIN_近似简单);
System.out.println(cvSeq.total());
对于(int i=0;i
这是我得到的输出。有人能帮我解决这个问题吗
检查您的类型促销,例如:
if (10 < (w/h) || (w/h) < 0.1){
(尽管我不确定这是否是问题所在。)
快乐编码 编辑:下面是最有趣的发现- 我认为您没有正确地遍历轮廓-您应该执行以下操作:
CvRect rect = cvBoundingRect(cvGetSeqElem(cvSeq, i),0); //python default?
或:
首先,关于比率的计算,我认为pst是正确的——您必须将宽度转换为浮动
其次,我看到在python中制作灰色图像时使用的是COLOR\u bgr2 gray
,而在java中使用的是CV\u RGB2GRAY
,这可能会产生完全不同的灰色图像。当(10<(w/h)|(w/h)<0.1)
为真时,我会在两个程序上添加一些调试步骤来保存临时灰度图像,并将它们作为x,y,w
和h
值的打印输出进行比较
另一件事是,在java解决方案中,您使用CV_RETR\u CCOMP
获得轮廓,在python解决方案中,您使用CV_RETR\u LIST
,根据文档:
CV_RETR_LIST检索所有轮廓,而不建立任何
层次关系CV_RETR_CCOMP检索所有轮廓
并将其组织为两级层次结构:顶层为
第二层上组件的外部边界是
洞的边界。如果在连接部件的孔内
还有另一个轮廓,它仍然会被放在顶层
因此,首先我会仔细检查两个程序中所有cv的参数是否相同,然后我会添加调试步骤,以查看中间变量是否包含相同的数据。这段代码对我有效,我只需输入cvSeq=cvSeq.h_next()将行插入程序并删除for循环add while for thet
package Beam;
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import static com.googlecode.javacpp.Loader.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import java.io.File;
import javax.swing.JFileChooser;
public class TestBeam2 {
public static void main(String[] args) {
JFileChooser f=new JFileChooser();
int result=f.showOpenDialog(f);//show dialog box to choose files
File myfile=null;
String path="";
if(result==0){
myfile=f.getSelectedFile();//selected file taken to myfile
path=myfile.getAbsolutePath();//get the path of the file
}
IplImage src = cvLoadImage(path);//hear path is actual path to image
IplImage grayImage = IplImage.create(src.width(), src.height(), IPL_DEPTH_8U, 1);
cvCvtColor(src, grayImage, CV_RGB2GRAY);
cvThreshold(grayImage, grayImage, 127, 255, CV_THRESH_BINARY);
CvSeq cvSeq=new CvSeq();
CvMemStorage memory=CvMemStorage.create();
cvFindContours(grayImage, memory, cvSeq, Loader.sizeof(CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
while (cvSeq != null && !cvSeq.isNull()) {
CvRect rect=cvBoundingRect(cvSeq, 0);
int x=rect.x(),y=rect.y(),h=rect.height(),w=rect.width();
if (10 < w/h || w/h < 0.1){
cvRectangle(src, cvPoint(x, y), cvPoint(x+w, y+h), CvScalar.RED, 1, CV_AA, 0);
}
cvSeq=cvSeq.h_next();
}
CanvasFrame cnvs=new CanvasFrame("Beam");
cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
cnvs.showImage(src);
//cvShowImage("Final ", src);
}
}
封装梁;
导入com.googlecode.javacpp.Loader;
导入com.googlecode.javacv.CanvasFrame;
导入静态com.googlecode.javacpp.Loader.*;
导入静态com.googlecode.javacv.cpp.opencv_core.*;
导入静态com.googlecode.javacv.cpp.opencv_imgproc.*;
导入静态com.googlecode.javacv.cpp.opencv_highgui.*;
导入java.io.File;
导入javax.swing.JFileChooser;
公共类TestBeam2{
公共静态void main(字符串[]args){
JFileChooser f=新的JFileChooser();
int result=f.showOpenDialog(f);//显示选择文件的对话框
File myfile=null;
字符串路径=”;
如果(结果==0){
myfile=f.getSelectedFile();//所选文件被带到myfile
path=myfile.getAbsolutePath();//获取文件的路径
}
IplImage src=cvLoadImage(path);//hear path是映像的实际路径
IplImage grayImage=IplImage.create(src.width(),src.height(),IPL_DEPTH_8U,1);
CVT颜色(src、灰度图像、CV_RGB2GRAY);
cvTh
// ...
CvSeq contours = new CvSeq();
CvSeq ptr = new CvSeq();
CvRect rect = null;
// ...
cvFindContours(..., contours, ...);
for (ptr = contours; ptr != null; ptr = ptr.h_next()) {
rect = cvBoundingRect(ptr, 0);
// ... Draw the box if meets criteria
}
package Beam;
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import static com.googlecode.javacpp.Loader.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import java.io.File;
import javax.swing.JFileChooser;
public class TestBeam2 {
public static void main(String[] args) {
JFileChooser f=new JFileChooser();
int result=f.showOpenDialog(f);//show dialog box to choose files
File myfile=null;
String path="";
if(result==0){
myfile=f.getSelectedFile();//selected file taken to myfile
path=myfile.getAbsolutePath();//get the path of the file
}
IplImage src = cvLoadImage(path);//hear path is actual path to image
IplImage grayImage = IplImage.create(src.width(), src.height(), IPL_DEPTH_8U, 1);
cvCvtColor(src, grayImage, CV_RGB2GRAY);
cvThreshold(grayImage, grayImage, 127, 255, CV_THRESH_BINARY);
CvSeq cvSeq=new CvSeq();
CvMemStorage memory=CvMemStorage.create();
cvFindContours(grayImage, memory, cvSeq, Loader.sizeof(CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
while (cvSeq != null && !cvSeq.isNull()) {
CvRect rect=cvBoundingRect(cvSeq, 0);
int x=rect.x(),y=rect.y(),h=rect.height(),w=rect.width();
if (10 < w/h || w/h < 0.1){
cvRectangle(src, cvPoint(x, y), cvPoint(x+w, y+h), CvScalar.RED, 1, CV_AA, 0);
}
cvSeq=cvSeq.h_next();
}
CanvasFrame cnvs=new CanvasFrame("Beam");
cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
cnvs.showImage(src);
//cvShowImage("Final ", src);
}
}