使用opencv为android

时间:2016-06-24 04:21:48

标签: android opencv sift

我正在使用opencv sdk进行筛选 我在使用

绘制两个图像之间的匹配时遇到了困难
  

Feature2d.drawMatches()

每当我运行应用程序时,它都会执行所有步骤,但在到达绘图功能时会停止。

这里是完整的代码:

public class MainActivity extends AppCompatActivity {
static {
    System.loadLibrary("opencv_java");
    System.loadLibrary("nonfree");
}

private ImageView imageView;

private FeatureDetector detector = FeatureDetector.create(FeatureDetector.SIFT);
DescriptorExtractor descriptorExtractor=DescriptorExtractor.create(DescriptorExtractor.SIFT);
DescriptorMatcher descriptorMatcher= DescriptorMatcher.create(3);

Bitmap inputImage;
Bitmap inputImage2;
Bitmap out;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    if(OpenCVLoader.initDebug()){
        Toast.makeText(this,"Hiii",Toast.LENGTH_SHORT).show();}

    inputImage = BitmapFactory.decodeResource(getResources(), R.drawable.object);
    inputImage2 = BitmapFactory.decodeResource(getResources(), R.drawable.objecttest);
    out = BitmapFactory.decodeResource(getResources(), R.drawable.test);
    imageView = (ImageView) this.findViewById(R.id.imageView);
     sift();
}
public void onResume() {
    super.onResume();
}


public void sift() {

    Mat rgba = new Mat();
    Mat rgba2 = new Mat();
    Mat desc=new Mat();
    Mat desc2=new Mat();
    MatOfDMatch matches=new MatOfDMatch();
    Mat output=new Mat();

    Utils.bitmapToMat(inputImage, rgba);
    Utils.bitmapToMat(inputImage2, rgba2);
    Utils.bitmapToMat(out, output);

    MatOfKeyPoint keyPoints = new MatOfKeyPoint();
    MatOfKeyPoint keyPoints2 = new MatOfKeyPoint();

    Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGBA2GRAY);
    Imgproc.cvtColor(rgba2, rgba2, Imgproc.COLOR_RGBA2GRAY);

    detector.detect(rgba, keyPoints);
    detector.detect(rgba2, keyPoints2);

    descriptorExtractor.compute(rgba,keyPoints,desc);
    descriptorExtractor.compute(rgba2,keyPoints2,desc2);
    descriptorMatcher.match(desc,desc2,matches);

    Features2d.drawMatches(rgba,keyPoints,rgba2,keyPoints2,matches,output);
       Utils.matToBitmap(output, out);
       imageView.setImageBitmap(out);
    }


  }

我还需要知道筛选是否是实时检测复杂物体的最佳选择?

编辑1 :我现在注意到我收到以下错误

  

OpenCV错误:断言失败(0< = roi.x&& 0< = roi.width&& roi.x + roi.width< = m.cols&& 0< ; = roi.y&& 0< = roi.height&& roi.y + roi.height< = m.rows)

     

致命信号11(SIGSEGV),代码2,故障地址0x12ce08e0 in tid 9971

1 个答案:

答案 0 :(得分:1)

问题解决了:
我提到的错误是由于某种原因阻止图像显示(可能是图像视图和我们想要显示的图像之间的属性差异让我们说某些情况下的尺寸
实际上我不确定前一点,但是在那里提到了,解决方案是根据图像设置图像视图动态调整大小,但这对我不起作用)
我的问题通过替换这些说明解决了:

 Features2d.drawMatches(rgba,keyPoints,rgba2,keyPoints2,matches,output);
 Utils.matToBitmap(output, out);
 imageView.setImageBitmap(out);

以下内容:

Scalar RED = new Scalar(255,0,0);
Scalar GREEN = new Scalar(0,255,0);
MatOfByte drawnMatches = new MatOfByte();
 Features2d.drawMatches(rgba,keyPoints,rgba2,keyPoints2,matches,output,GREEN, RED,  drawnMatches, Features2d.NOT_DRAW_SINGLE_POINTS);

Bitmap imageMatched = Bitmap.createBitmap(output.cols(), output.rows(), Bitmap.Config.RGB_565);//need to save bitmap
Utils.matToBitmap(output, imageMatched);
imageView.setImageBitmap(imageMatched);

我实际上没有区别,所以进一步帮助描述这将是非常有帮助的,但希望这可以暂时帮助人们遇到同样的问题