我在OpenCV的Java版本中有一个Mat。现在我需要在窗口中查看。如何才能做到这一点?我觉得它应该很简单。我找到了大量的Python和C ++示例,但没有简单的Java示例。这里有什么帮助吗?
答案 0 :(得分:0)
我尝试将python示例转换为另一个答案。我对java的经验有限,所以也许我错过了一些东西。话虽如此,我对opencv的java绑定的事态感到非常失望。我建议你使用javacv(https://github.com/bytedeco)而不是opencv官方绑定。由于缺少imshow和鼠标事件挂钩的绑定,它不像javacv那样可用。
我在下面提供了使用javacv为我工作的代码,并且其行为或多或少与python答案类似。从您提供的Wiki链接下载notre.jpg
。我建议如果可能的话,你可能想要将语言改为c ++。我觉得java绑定可能存在真正不可接受的错误或严重的可用性问题。
package javaapplication2;
import java.util.ArrayList;
import java.util.List;
import org.bytedeco.javacpp.Pointer;
import static org.bytedeco.javacpp.opencv_calib3d.*;
import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_highgui.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;
public class JavaApplication2 {
public static void main(String[] args) throws Exception {
System.loadLibrary("opencv_core249");//without these two lines, I get unsatisfied link exception on my pc. you should unzip the contents of opencv-windows-x86_64.jar etc to a folder and run with -Djava.library.path="C:\opencv\javacv-bin\bin"
System.loadLibrary("jniopencv_core");
Mat im =new Mat(480,640,CV_8UC3);//dummy
Mat dst = null;//new Mat(480,640,CV_8UC1);//dummy
final List<Point> pSrc=new ArrayList<Point>();
final List<Point> pDst=new ArrayList<Point>();
/*640x480 camera
pDst.add(new Point(120,84)); //cam
pDst.add(new Point(525,84));//cam
pDst.add(new Point(525,427));//cam
pDst.add(new Point(120,427));//cam
*/
pDst.add(new Point(94,72));//notre
pDst.add(new Point(343,72));//notre
pDst.add(new Point(343,501));//notre
pDst.add(new Point(94,501));//notre
namedWindow("src",0);
namedWindow("dst",0);
MouseCallback mouseSrc= new MouseCallback() {
@Override
public void call(int event, int x, int y, int flags,Pointer param) {
if(event==CV_EVENT_LBUTTONDOWN)
{
if(pSrc.size()>=4) pSrc.clear();
pSrc.add(new Point(x,y));
}
}
};
setMouseCallback("src", mouseSrc, null);
/* this should work but no..bug?
MouseCallback moustDst= new MouseCallback() {
@Override
public void call(int event, int x, int y, int flags,Pointer param) {
if(event==CV_EVENT_LBUTTONDOWN)
{
if(pDst.size()>=4) pDst.clear();
pDst.add(new Point(x,y));
}
}
};
setMouseCallback("dst", moustDst, null);*/
/* uncomment this one to read from webcam
VideoCapture cap = new VideoCapture(1);
while (cap.read(im)) {*/
im = imread("c:/data/notre.jpg");
while(true){
if(dst==null)
{
dst=im.clone();
}
Mat imD = im.clone();
Mat dstD = dst.clone();
for(int i=0;i<pSrc.size();++i)
{
circle(imD,pSrc.get(i),4,new Scalar(255,0,0,255),-1,1,1);
}
if(pSrc.size()==4 && pDst.size()==4)
{
Mat msrc = new Mat(4,2,CV_64FC1);
Mat mdst = new Mat(4,2,CV_64FC1);
for(int i=0;i<pSrc.size();++i)
{
//this is probably not the right way for element access, but I cant find a more straightforward way
msrc.asCvMat().put(i,0,(double)pSrc.get(i).x());
msrc.asCvMat().put(i,1,(double)pSrc.get(i).y());
}
for(int i=0;i<pDst.size();++i)
{
mdst.asCvMat().put(i,0,(double)pDst.get(i).x());
mdst.asCvMat().put(i,1,(double)pDst.get(i).y());
}
Mat H =findHomography(msrc, mdst );
warpPerspective(imD, dstD, H, new Size(dstD.cols(),dstD.rows()));
}
for(int i=0;i<pDst.size();++i)
{
circle(dstD,pDst.get(i),4,new Scalar(255,255,0,255),-1,1,1);
}
imshow("src",imD);
imshow("dst",dstD);
if(cvWaitKey(1)==27)
break;
}
//cap.release();
}
}