I want to detect the corners in a video.
To do so,I first grayscaled the video frames and applied the corner detection method there.
However, I got the following error:
E/cv::error(): OpenCV(4.6.0) Error: Assertion failed (src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4) in Java_org_opencv_android_Utils_nMatToBitmap2
The code I wrote is below.The language is Kotlin. Could anyone please explain the solution? Thank you.
class MainActivity : AppCompatActivity() {
@RequiresApi(Build.VERSION_CODES.P)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
OpenCVLoader.initDebug()
val videoView = findViewById<VideoView>(R.id.videoView)
val imgView = findViewById<ImageView>(R.id.imageView)
//set Video to videoView
val uri = Uri.parse("android.resource://" getPackageName() "/" R.raw.video_name)
videoView.setVideoURI(uri)
videoView.start();
val retriever = MediaMetadataRetriever()
var bitmap = MediaMetadataRetriever.BitmapParams()
val context = applicationContext
retriever.setDataSource(context, uri)
//get frame
val frameNum =
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_FRAME_COUNT)
thread(){
try{
for (i in 1..frameNum!!.toInt()) {
//get frame
var bitframe = retriever.getFrameAtIndex(i)
val f_width = bitframe!!.width
val f_height = bitframe!!.height
//Mat
var srcMat: Mat = Mat.zeros(f_height, f_width, CV_8UC1)
var grayMat: Mat = Mat.zeros(f_height, f_width, CV_8UC1)
var bitMat: Mat = Mat.zeros(f_height, f_width, CV_8UC1)
var cornerMat:Mat = Mat.zeros(f_height, f_width, CV_8UC1)
//bitframe to srcMat
Utils.bitmapToMat(bitframe, srcMat)
//grayScale
Imgproc.cvtColor(srcMat, grayMat, Imgproc.COLOR_BGR2GRAY)
Imgproc.cornerHarris(grayMat,cornerMat,3,5,0.5)
Utils.matToBitmap(cornerMat, bitframe)
Handler(Looper.getMainLooper()).post {
imgView.setImageBitmap(bitframe)
}
}
}
activity_main.xml is below.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<VideoView
android:id="@ id/videoView"
android:layout_width="396dp"
android:layout_height="258dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@ id/imageView"
android:layout_width="382dp"
android:layout_height="244dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/videoView"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
CodePudding user response:
Thank you for your cooperation.
Finally,I wiped data and thr error was gone.
CodePudding user response:
for (i in 1..frameNum!!.toInt())
The method call getFrameAtIndex(i)
may not be 1-indexed, try replacing
with getFrameAtIndex(i-1)?