gpt4 book ai didi

java - void cv::cvtColor(cv::InputArray Android) 断言失败(scn == 3 || scn == 4)

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:00:19 27 4
gpt4 key购买 nike

我正在尝试将 Mat 转换为灰度值,以便获得像素的数值。我在 void cv::cvtColor(cv::InputArray error) 上收到“断言失败(scn == 3 || scn == 4)”。我知道我错过了一些东西,但我不知道它是什么.

代码

import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.hardware.camera2.CameraCaptureSession;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.JavaCameraView;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.MatOfPoint;
import org.opencv.imgproc.Imgproc;
import org.opencv.highgui.Highgui;


public class testActivity extends AppCompatActivity implements CvCameraViewListener2 {

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status){
switch (status){
case LoaderCallbackInterface.SUCCESS:
{
mOpenCvCameraView.enableView();
break;
}
default:
{
super.onManagerConnected(status);
}
}
}

};
private JavaCameraView mOpenCvCameraView;
Mat bgrPixel;
Mat grayPixel;
Mat rgbMat;

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

mOpenCvCameraView = (JavaCameraView) findViewById(R.id.testVideoView);
mOpenCvCameraView.setMaxFrameSize(240,320);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
getSupportActionBar().hide();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}



public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}

public void onResume() {
super.onResume();
if (!OpenCVLoader.initDebug()) {
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallback);
} else {
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}



@Override
public void onPause() {
super.onPause();

if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
if(rgbMat == null){
rgbMat = inputFrame.rgba();
bgrPixel= rgbMat.submat(1,1,2,2).clone();
grayPixel = new Mat();
Imgproc.cvtColor(bgrPixel, grayPixel, Imgproc.COLOR_BGR2GRAY,1);
}
rgbMat= inputFrame.rgba();
return rgbMat;
}


@Override
public void onCameraViewStarted(int width, int height) {

}

@Override
public void onCameraViewStopped() {
bgrPixel.release();
grayPixel.release();
rgbMat.release();
}


}

日志猫

08-18 14:44:37.649  17571-17884/bluemage.systems.displaylatencytester E/cv::error()﹕ OpenCV Error: Assertion failed (scn == 3 || scn == 4) in void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/color.cpp, line 3737
08-18 14:44:37.650 17571-17884/bluemage.systems.displaylatencytester E/org.opencv.imgproc﹕ imgproc::cvtColor_10() caught cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
08-18 14:44:37.659 17571-17884/bluemage.systems.displaylatencytester E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-4081
Process: bluemage.systems.displaylatencytester, PID: 17571
CvException [org.opencv.core.CvException: cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
]
at org.opencv.imgproc.Imgproc.cvtColor_0(Native Method)
at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:4244)
at bluemage.systems.displaylatencytester.testActivity.onCameraFrame(testActivity.java:117)
at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:387)
at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:328)
at java.lang.Thread.run(Thread.java:818)

最佳答案

if(rgbMat == null){
rgbMat = inputFrame.rgba();
bgrPixel= rgbMat.submat(1,1,2,2).clone();
grayPixel = new Mat();
Imgproc.cvtColor(bgrPixel, grayPixel, Imgproc.COLOR_BGR2GRAY,1);
}

我在这里看到两个错误。我猜你正在尝试提取第一个像素。我不知道你的方法是否是最好的方法,但是 submat()(参见 [此处]( http://docs.opencv.org/java/org/opencv/core/Mat.html#submat(int , int, int, int)))需要 rowStart,行结束、列开始、列结束。所以你应该将其更改为 submat(1,2,1,2)

另外,请注意 rgba() 返回一个 RGBA 图像,因此您应该使用 Imgproc.COLOR_RGBA2GRAY

关于java - void cv::cvtColor(cv::InputArray Android) 断言失败(scn == 3 || scn == 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32080533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com