OpenCV Android Error - CVException - android

I'm trying transform perspective automatically, I'm using a manual from HERE
Exactly the chapter 4: Flexible perspective transform
When I get the image to transform I get the next error:
Error
11-04 13:34:25.759: E/cv::error()(17332): OpenCV Error: Assertion failed (count >= 0 && (depth == CV_32F || depth == CV_32S)) in double cv::arcLength(cv::InputArray, bool), file /home/maksim/workspace/android-pack/opencv/modules/imgproc/src/shapedescr.cpp, line 301
11-04 13:34:25.759: E/org.opencv.imgproc(17332): imgproc::arcLength_10() caught cv::Exception: /home/maksim/workspace/android-pack/opencv/modules/imgproc/src/shapedescr.cpp:301: error: (-215) count >= 0 && (depth == CV_32F || depth == CV_32S) in function double cv::arcLength(cv::InputArray, bool)
11-04 13:34:25.759: E/AndroidRuntime(17332): FATAL EXCEPTION: main
11-04 13:34:25.759: E/AndroidRuntime(17332): Process: com.example.alvaro.opencv, PID: 17332
11-04 13:34:25.759: E/AndroidRuntime(17332): CvException [org.opencv.core.CvException: cv::Exception: /home/maksim/workspace/android-pack/opencv/modules/imgproc/src/shapedescr.cpp:301: error: (-215) count >= 0 && (depth == CV_32F || depth == CV_32S) in function double cv::arcLength(cv::InputArray, bool)
11-04 13:34:25.759: E/AndroidRuntime(17332): ]
The code
Mat gray = new Mat();
Imgproc.cvtColor(sampledImage, gray, Imgproc.COLOR_RGB2GRAY, 0);
Imgproc.GaussianBlur(gray, gray, new Size(7, 7), 0);
Mat edgeImage = new Mat();
Imgproc.Canny(gray, edgeImage, 100, 200);//1500,300
Mat lines = new Mat();
int threshold = 100;//200
Imgproc.HoughLinesP(edgeImage, lines, 1, Math.PI / 180, threshold, 60, 10);//100,60
ArrayList<org.opencv.core.Point> corners = new ArrayList<org.opencv.core.Point>();
//Find the intersection of the four lines to get the four corners
for (int i = 0; i < lines.cols(); i++)
{
for (int j = i + 1; j < lines.cols(); j++)
{
org.opencv.core.Point intersectionPoint = getLinesIntersection(lines.get(0, i), lines.get(0, j));
if (intersectionPoint != null)
{
Log.i(TAG, "intersectionPoint: " + intersectionPoint.x + " " + intersectionPoint.y);
corners.add(intersectionPoint);
}
}
}
MatOfPoint2f approxCorners=new MatOfPoint2f();
MatOfPoint2f cornersMat=new MatOfPoint2f();
cornersMat.fromList(corners);
//ERROR LINE on "Imgproc.arcLength"
double approxDistance = Imgproc.arcLength(cornersMat, true) * 0.02;
Imgproc.approxPolyDP(cornersMat, approxCorners,approxDistance, true);
What I tried
I tried change image 32bits to 8bits (or another one) without results
To use another method to her edges.
I found HERE the same issue, but... really I can't found the solution...
I'm really don't know how I can solve it.

Related

OpenCV haarCascade whit Optical flow tracking

i'm trying to track objects with Optical flow in android after using a Haar Cascade detection like in the code below and i have this error can anyone help me with this
E/cv::error(): OpenCV(3.4.12) Error: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0) in virtual void cv::{anonymous}::SparsePyrLKOpticalFlowImpl::calc(cv::InputArray, cv::InputArray, cv::InputArray, cv::InputOutputArray, cv::OutputArray, cv::OutputArray), file /build/3_4_pack-android/opencv/modules/video/src/lkpyramid.cpp, line 1259
E/org.opencv.video: video::calcOpticalFlowPyrLK_15() caught cv::Exception: OpenCV(3.4.12) /build/3_4_pack-android/opencv/modules/video/src/lkpyramid.cpp:1259: error: (-215:Assertion failed) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function 'virtual void cv::{anonymous}::SparsePyrLKOpticalFlowImpl::calc(cv::InputArray, cv::InputArray, cv::InputArray, cv::InputOutputArray, cv::OutputArray, cv::OutputArray)'
E/AndroidRuntime: FATAL EXCEPTION: Thread-2
Process: opencv.org, PID: 31380
CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.12) /build/3_4_pack-android/opencv/modules/video/src/lkpyramid.cpp:1259: error: (-215:Assertion failed) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function 'virtual void cv::{anonymous}::SparsePyrLKOpticalFlowImpl::calc(cv::InputArray, cv::InputArray, cv::InputArray, cv::InputOutputArray, cv::OutputArray, cv::OutputArray)'
]
enter code hereMatOfRect cars = new MatOfRect();
if (mDetectorType == JAVA_DETECTOR) {
if (mJavaDetector != null) {
mJavaDetector.detectMultiScale(matGray, cars,1.1, 2, 2,
// TODO: objdetect.CV_HAAR_SCALE_IMAGE
new Size(mAbsoluteCarSize, mAbsoluteCarSize), new Size());
}
}
else {
Log.e(TAG, "Detection method is not selected!");
}
Rect[] carsArray = cars.toArray();
ArrayList<Point> CurrentCars = new ArrayList<>();
for (int i = 0; i < carsArray.length; i++)
{
Imgproc.rectangle(matRGB, carsArray[i].tl(), carsArray[i].br(),
CAR_RECT_COLOR, 3);
xCenter = (carsArray[i].x + carsArray[i].width + carsArray[i].x) / 2;
yCenter = (carsArray[i].y + carsArray[i].y + carsArray[i].height) / 2;
Point center = new Point(xCenter, yCenter);
CurrentCars.add(center);
Imgproc.putText(matRGB, "[" + center.x + "," + center.y + "]",
new Point(center.x + 20, center.y + 20),
Core.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(255, 255, 255,
255));
}
if (previousPoints.empty()){
previousPoints.fromList(CurrentCars);
matGray.copyTo(matPrevGray);
}
currentPoints = new MatOfPoint2f();
MatOfByte status = new MatOfByte();
MatOfFloat err = new MatOfFloat();
TermCriteria criteria = new TermCriteria(TermCriteria.COUNT + TermCriteria.EPS,10,0.03);
Video.calcOpticalFlowPyrLK(matPrevGray,matGray,previousPoints,currentPoints,status,err);
byte StatusArr[] = status.toArray();
Point p0Arr[] = previousPoints.toArray();
Point p1Arr[] = currentPoints.toArray();
ArrayList<Point> good_new = new ArrayList<>();
for (int i = 0; i<StatusArr.length ; i++ ) {
if (StatusArr[i] == 1) {
good_new.add(p1Arr[i]);
Imgproc.line(matRGB, p1Arr[i], p0Arr[i], new Scalar(255,255,0,0),2);
Imgproc.circle(matRGB, p1Arr[i],5, new Scalar(255,255,0,0),-1);
}
}
currentPoints.copyTo(previousPoints);
matGray.copyTo(matPrevGray);
matPrevGray is empty. that's what it's saying.

getting error while using findFundamentalMat in android opencv and not able to resolve it

i am capturing the continuous images and extracting the fast features and further matching these features to get the get the Rotational and
translation matrix. that will help me in 3d scene reconstruction but getting
error when i am using Calib3d.findFundamentalMat command and not able to resolve it.
MatOfDMatch matches = new MatOfDMatch();
descriptorM.match(firstImgDescriptors, secondImgDescriptors, matches);
Calib3d calib = new Calib3d();
MatOfPoint2f imgpts1 = getMatOfPoint2fFromDMatches(matches, keyPoints1, 0);
MatOfPoint2f imgpts2 = getMatOfPoint2fFromDMatches(matches, keyPoints2, 1);
Mat F = Calib3d.findFundamentalMat(imgpts1, imgpts2, Calib3d.FM_RANSAC,3, 0.99);
private static MatOfPoint2f getMatOfPoint2fFromDMatches(MatOfDMatch matches,
MatOfKeyPoint keyP, int tipo) {
/* 0 para query, 1 para train*/
DMatch dm[] = matches.toArray();
List<Point> lp = new ArrayList<Point>(dm.length);
KeyPoint tkp[] = keyP.toArray();
if(tipo == 0){
for (int i = 0; i < dm.length; i++) {
DMatch dmm = dm[i];
//if (dmm.queryIdx < tkp.length)
lp.add(tkp[dmm.queryIdx].pt);
}
}
if (tipo == 1) {
for (int i = 0; i < dm.length; i++) {
DMatch dmm = dm[i];
// if (dmm.trainIdx < tkp.length)
lp.add(tkp[dmm.trainIdx].pt);
}
}
return new MatOfPoint2f(lp.toArray(new Point[0]));
}
the Logcat window show the following error
E/cv::error(): OpenCV Error: Bad argument (The input arrays should be 2D or 3D point sets) in cv::Mat cv::findFundamentalMat(cv::InputArray, cv::InputArray, int, double, double, cv::OutputArray), file /Volumes/Linux/builds/master_pack-android/opencv/modules/calib3d/src/fundam.cpp, line 724
09-11 15:29:04.578 13625-13625/io.rpng.calibration E/org.opencv.calib3d: calib3d::findFundamentalMat_11() caught cv::Exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/calib3d/src/fundam.cpp:724: error: (-5) The input arrays should be 2D or 3D point sets in function cv::Mat cv::findFundamentalMat(cv::InputArray, cv::InputArray, int, double, double, cv::OutputArray)
09-11 15:29:04.579 13625-13625/io.rpng.calibration D/AndroidRuntime: Shutting down VM
09-11 15:29:04.588 13625-13625/io.rpng.calibration E/AndroidRuntime: FATAL EXCEPTION: main
Process: io.rpng.calibration, PID: 13625
CvException [org.opencv.core.CvException: cv::Exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/calib3d/src/fundam.cpp:724: error: (-5) The input arrays should be 2D or 3D point sets in function cv::Mat cv::findFundamentalMat(cv::InputArray, cv::InputArray, int, double, double, cv::OutputArray)
]
at org.opencv.calib3d.Calib3d.findFundamentalMat_1(Native Method)
at org.opencv.calib3d.Calib3d.findFundamentalMat(Calib3d.java:153)
at com.pradeep.calibration.activities.MainActivity$5.onImageAvailable(MainActivity.java:529)
at android.media.ImageReader$ListenerHandler.handleMessage(ImageReader.java:648)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
The error is telling you that your imgpoints1 and/or imgpoints2 are not proper 2D or 3D point matrices.
Try printing the dimensions of these directly before calling Calib3d.findFundamentalMat(...)

How can I superimpose an image on the frame of the camera (in real time)?

I am developing an object recognition program with opencv and android, the program basically makes the moment the real time camera recognizes the object displays an image with information of the object in augmented reality form on the frame of the camera.
The problem is that it shows an error in the addition of the images how can I successfully do the addition?
The images are not of the same size.
public Mat onCameraFrame(Mat aInputFrame) {
// Crear una imagen en escala de grises
Imgproc.cvtColor(aInputFrame, grayscaleImage, Imgproc.COLOR_RGBA2RGB);
Mat imagen = Highgui.imread(ruta, Highgui.CV_LOAD_IMAGE_UNCHANGED);
Rect roi = new Rect(150, 200, 40, 60);
MatOfRect faces = new MatOfRect();
// Utilice el clasificador para detectar rostros
if (cascadeClassifier != null) {
cascadeClassifier.detectMultiScale(grayscaleImage, faces, 1.1, 2, 2,
new Size(absoluteFaceSize, absoluteFaceSize), new Size());
}
Mat copia = aInputFrame.clone();
Mat imgroi = imagen.submat(roi);
Mat salidaroi = copia.submat(roi);
Core.addWeighted(imgroi, 0.5, imagen, 0.2, 1, salidaroi);
return salidaroi;
}
Error
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x
+ roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat(const cv::Mat&, const Rect&),
file
/hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/matrix.cpp,
line 323

Opencv4Android getPerspectiveTransform error

I want to use getPerspectiveTransform function, but it only accepts Mat as arguments and I have coordinate data in an array. So I convert them into points and then to Mat as follows:
List<Point> l = new ArrayList<Point>();
for (int i = 0; i < pts_1.length; i++) {
Point pt = new Point(pts_1[0][i], pts_1[1][i]);
l.add(i,pt);
}
Mat mat1 = Converters.vector_Point2f_to_Mat(l);
for (int i = 0; i < pts_2.length; i++) {
Point pt = new Point(pts_2[0][i], pts_2[1][i]);
l.add(i,pt);
}
Mat mat2 = Converters.vector_Point2f_to_Mat(l);
Mat perspectiveTransform = Imgproc.getPerspectiveTransform(mat1,mat2);
But when I run my app, then it gives me error 'Something went wrong', and in logcat I am getting the following error:
E/cv::error(): OpenCV Error: Assertion failed (src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4) in cv::Mat cv::getPerspectiveTransform(cv::InputArray, cv::InputArray), file /build/master_pack-android/opencv/modules/imgproc/src/imgwarp.cpp, line 6748
E/org.opencv.imgproc: imgproc::getPerspectiveTransform_10() caught cv::Exception: /build/master_pack-android/opencv/modules/imgproc/src/imgwarp.cpp:6748: error: (-215) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function cv::Mat cv::getPerspectiveTransform(cv::InputArray, cv::InputArray)
I am new to OpenCV4Android so I don't understand why this is happening. How to fix it? Is there any other better way to do this?
Thanks for help!
Note: I know that a similar procedure is followed here: Can't get OpenCV's warpPerspective to work on Android but he is not getting this error, so I am posting it here.
As mentioned in the comment discussion on OP's post (all credit to them), the problem lies with the list l:
List<Point> l = new ArrayList<Point>();
for (int i = 0; i < pts_1.length; i++) {
Point pt = new Point(pts_1[0][i], pts_1[1][i]);
l.add(i,pt);
}
Mat mat1 = Converters.vector_Point2f_to_Mat(l);
If we take a look at the contents of the List<Point> l:
for (Point pt : l)
System.out.println("(" + pt.x + ", " + p.ty + ")");
(x0, y0)
(x1, y1)
(x2, y2)
(x3, y3)
And moving onto the next matrix:
for (int i = 0; i < pts_2.length; i++) {
Point pt = new Point(pts_2[0][i], pts_2[1][i]);
l.add(i,pt);
}
Mat mat2 = Converters.vector_Point2f_to_Mat(l);
Taking another look at the contents of the List<Point> l:
for (Point pt : l)
System.out.println("(" + pt.x + ", " + p.ty + ")");
(x4, y4)
(x5, y5)
(x6, y6)
(x7, y7)
(x0, y0)
(x1, y1)
(x2, y2)
(x3, y3)
So this is the culprit; your second matrix will have 8 points in it.
From the Java docs for ArrayList:
Parameters: index - index at which the specified element is to be inserted
Using l.add will insert and not overwrite the old list. So the solution would be to make a new list for each transformation matrix.

converting a Mat into Bitmap causes an error

I am working with OpenCV4Android version 2.4.11, and I am trying to detect cornrs in the image. For that purpose, I am using Harris corner detector.the problem i am facing is, after detecting the corners in the image as shown below
in the code I want to display the image that contains the detected corners after converting it into Bitmap, but then i receive the below posted error.
step 1 and step 2 are executed without errors, but when i run step 3 i get the posted logcat error.
Please let mek know why i am receiving this error and how to solve it?
code:
//step 1
this.mMatGray = new Mat();
Imgproc.cvtColor(this.mMatInputFrame, this.mMatGray, Imgproc.COLOR_BGR2GRAY);
//step 2
Imgproc.cornerHarris(mMatGray,mMatGray,3,3,3,1);
//step 3
final Bitmap bitmap = Bitmap.createBitmap(mMatInputFrame.cols(), mMatInputFrame.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mMatGray, bitmap);
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
mIVEdges.setImageBitmap(bitmap);
}
});
error:
OpenCV Error: Assertion failed (src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean), file /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp, line 98
E/org.opencv.android.Utils: nMatToBitmap catched cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:98: error: (-215) src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4 in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
E/AndroidRuntime: FATAL EXCEPTION: Thread-2477
Process: com.example.cornerdetection_00, PID: 22407
CvException [org.opencv.core.CvException: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:98: error: (-215) src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4 in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
]
at org.opencv.android.Utils.nMatToBitmap2(Native Method)
at org.opencv.android.Utils.matToBitmap(Utils.java:123)
at org.opencv.android.Utils.matToBitmap(Utils.java:132)
at com.example.cornerdetection_00.FragOpenCVCam.cornerHarris(FragOpenCVCam.java:204)
at com.example.cornerdetection_00.FragOpenCVCam.onCameraFrame(FragOpenCVCam.java:159)
at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:387)
at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:346)
update1:
now after testing the following code provided by "MIkka Marmik" i recieve the errors below
code:
Mat mMatGray = new Mat();
Imgproc.cvtColor(mMatGray, mMatGray, Imgproc.COLOR_BGR2GRAY);
//step 2
Imgproc.cornerHarris(mMatGray,mMatGray,3,3,3,1);
//step 3
final Bitmap bitmap = Bitmap.createBitmap(mMatGray.cols(), mMatGray.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mMatGray, bitmap);
error:
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int), file /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/imgproc/src/color.cpp, line 3739
E/org.opencv.imgproc: imgproc::cvtColor_11() caught cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/imgproc/src/color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
E/AndroidRuntime: FATAL EXCEPTION: Thread-4693
The createBitmap method is expecting an image which is either 1,3 or 4 channels. You must convert your output mMatGray to the format required for the createBitmap method. However, after checking the OpenCV documentation for the Harris corner algorithm, there are a few more steps involved to set up your image for corner detection. Try this out:
Mat src_gray = new Mat();
Mat dst = new Mat();
Mat dst_norm = new Mat();
Mat dst_norm_scaled = new Mat();
// Detector parameters
int blockSize = 2;
int apertureSize = 3;
double k = 0.04;
// Filter params
int thresh = 200;
int max_thresh = 255;
// Detecting corner
Imgproc.cvtColor(src, src_gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.cornerHarris(src_gray, dst, blockSize, apertureSize, k);
// Normalizing
Core.normalize(dst, dst_norm, 0, 255, Core.NORM_MINMAX);
Core.convertScaleAbs(dst_norm, dst_norm_scaled);
// Drawing a circle around corners
for (int j = 0; j < dst_norm.rows(); j++) {
for (int i = 0; i < dst_norm.cols(); i++) {
if (dst_norm.get(j, i)[0] > thresh) {
Core.circle(dst_norm_scaled, new Point(i, j), 5, new Scalar(255));
}
}
}
// Create bitmap
final Bitmap bitmap = Bitmap.createBitmap(dst_norm_scaled.cols(), dst_norm_scaled.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(dst_norm_scaled, bitmap);
try this
Mat mMatGray = new Mat();
mMatGray = Highgui.imread(path);
Imgproc.cvtColor(mMatGray, mMatGray, Imgproc.COLOR_RGB2BGR);
Imgproc.cvtColor(mMatGray, mMatGray, Imgproc.COLOR_BGR2GRAY);
//step 2
Imgproc.cornerHarris(mMatGray,mMatGray,3,3,3,1);
Core.normalize(mMatGray, mMatGray, 0, 255, Core.NORM_MINMAX, CvType.CV_32FC1, new Mat());
Core.convertScaleAbs(mMatGray, mMatGray);
//step 3
final Bitmap bitmap = Bitmap.createBitmap(mMatGray.cols(), mMatGray.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mMatGray, bitmap);

Categories

Resources