I am using OpenCV for Android Version 3.1.0. I tried to use AKAZE detector and AKAZE descriptor. When I run the code on my Emulator am getting the below error.
OpenCV Error: Assertion failed (scn + 1 == m.cols) in void CV:perspectiveTransform(cv::InputArray, cv::OutputArray, cv::InputArray),
file/Volumes/Linux/builds/master_pack-android/opencv/modules/core/src/matmul.cpp, line 2125
core::perspectiveTransform_10() caught cv::Exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/core/src/matmul.cpp:2125:
error: (-215) scn + 1 == m.cols in function void CV:perspectiveTransform(cv::InputArray, cv::OutputArray, cv::InputArray)
I am using
private final Mat firstCorners = new Mat(4,1, CvType.CV_32FC2)
private final Mat secondCorners = new Mat(4,1, CvType.CV_32FC2)
final Mat homography = Calib3d.findHomography(first, second, Calib3d.RANSAC, 1)
Core.perspectiveTransform(firstCorners, secondCorners, homography)
I tried new Mat(4,1, CvType.CV_32FC1) but of no use
any help on this.
Related
Good morning everyone. I am currently working on a project in which I have to detect object coordinates within an image with high precision. I have tried using a normal chessboard for camera calibration but the reprojection error was too high so I decided to use the Charuco calibration pattern. I am working with OpenCv 3.4 and Java (project constraint). Since the Aruco function are not included in OpenCv for Java I created a new package in my project which includes the necessary classes. The Aruco code is the one that you can find in the following link :
Aruco Code Github
The code that I'm executing is the following:
protected void captureImagesCharuco() {
int squaresX = 5;
int squaresY = 7;
float squareLength = (float) 37.0;
float markerLength = (float) 22.0;
int calibrationFlags = 0;
float aspectRatio = 1;
DetectorParameters detectParams = DetectorParameters.create();
detectParams.set_adaptiveThreshWinSizeMin(3);
detectParams.set_adaptiveThreshWinSizeMax(23);
detectParams.set_adaptiveThreshWinSizeStep(10);
detectParams.set_adaptiveThreshConstant(7);
detectParams.set_minMarkerPerimeterRate(0.03);
detectParams.set_maxMarkerPerimeterRate(4.0);
detectParams.set_polygonalApproxAccuracyRate(0.05);
detectParams.set_minCornerDistanceRate(10);
detectParams.set_minDistanceToBorder(3);
detectParams.set_minMarkerDistanceRate(0.05);
detectParams.set_cornerRefinementWinSize(5);
detectParams.set_cornerRefinementMaxIterations(30);
detectParams.set_cornerRefinementMinAccuracy(0.1);
detectParams.set_markerBorderBits(1);
detectParams.set_perspectiveRemovePixelPerCell(8);
detectParams.set_perspectiveRemoveIgnoredMarginPerCell(0.13);
detectParams.set_maxErroneousBitsInBorderRate(0.04);
detectParams.set_minOtsuStdDev(5.0);
detectParams.set_errorCorrectionRate(0.6);
Dictionary dictionary = Aruco.getPredefinedDictionary(0);
CharucoBoard charucoBoard = CharucoBoard.create(squaresX, squaresY, squareLength, markerLength, dictionary);
List<List<Mat>> charucoCorners = new ArrayList<>();
List<Mat> charucoIds = new ArrayList<>();
List<Mat> validImgs = new ArrayList<>();
Size imgSize = new Size();
int nFrame = 0;
File[] listImages = imageDirectory.listFiles();
for(File file : listImages) {
String src = file.getAbsolutePath();
Mat imgRead = Imgcodecs.imread(src,Imgcodecs.IMREAD_COLOR);
imgSize = imgRead.size();
Mat imgCopy = new Mat();
Mat ids = new Mat();
List<Mat> rejectedCorners = new ArrayList<>();
List<Mat> corners = new ArrayList<>();
if (!imgRead.empty()){
Aruco.detectMarkers(imgRead, dictionary, corners, ids);
Aruco.refineDetectedMarkers(imgRead, (Board)charucoBoard, corners, ids, rejectedCorners);
Mat currentCharucoCorners = new Mat();
Mat currentCharucoIds = new Mat();
int idsSize = ids.rows()*ids.cols();
if(idsSize>0) {
Aruco.interpolateCornersCharuco(corners, ids, imgRead, charucoBoard, currentCharucoCorners, currentCharucoIds);
}
imgRead.copyTo(imgCopy);
if(idsSize<0) {
Aruco.drawDetectedCornersCharuco(imgCopy, currentCharucoCorners);
}
if(currentCharucoCorners.total()>0) {
Aruco.drawDetectedCornersCharuco(imgCopy, currentCharucoCorners, currentCharucoIds, new Scalar(255,0,0));
}
charucoCorners.add(corners);
charucoIds.add(currentCharucoIds);
validImgs.add(imgRead);
nFrame++;
}
}
intrinsic.put(0, 0, 1);
intrinsic.put(1, 1, 1);
List<Mat> allCharucoCorners = new ArrayList<>();
List<Mat> allCharucoIds = new ArrayList<>();
for(int i =0;i<nFrame;i++) {
Mat currentCharucoCorners = new Mat();
Mat currentCharucoIds = new Mat();
Aruco.interpolateCornersCharuco(charucoCorners.get(i), charucoIds.get(i), validImgs.get(i), charucoBoard, currentCharucoCorners, currentCharucoIds,intrinsic,distCoeffs,4);
allCharucoCorners.add(currentCharucoCorners);
allCharucoIds.add(currentCharucoIds);
}
double repError = Aruco.calibrateCameraCharuco(allCharucoCorners, charucoIds, charucoBoard, imgSize, intrinsic, distCoeffsCharuco, rvecs, tvecs, calibrationFlags);
System.out.println("reprojection error : " + repError);
}
I then simply execute the captureImagesCharuco() in the main program. However when I do so I get the following error :
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.UnsatisfiedLinkError: application.DetectorParameters.create_0()J
at application.DetectorParameters.create_0(Native Method)
at application.DetectorParameters.create(DetectorParameters.java:24)
at application.CameraCalibrate.captureImagesCharuco(CameraCalibrate.java:115)
at application.Main.main(Main.java:64)
... 11 more
Exception running application application.Main
I have tried searching for how to solve this error (UnsatisfiedLinkError) and I found that it is usually caused when you're using a library that isn't included in the Build Path or the project (Even though I ma not sure). I guess the library in question here is the Aruco package but I don't know how I can include a package in the build path of the project.
Any kind of help will be more than welcome ! Thank you ! :)
The error indicates that you have opencv package without special module you're using;
In order to fix that you'd need to either find a prebuilt opencv with module you need (in your exactly case it's lcoated in contrib library, so this probably helps.
In case you want to build it from sources - you should enable the module you want in cmake properties. For contrib - you'd need to cmake contrib project first & then enable contrib in main opencv makefile. For building opencv & contirb - please follow official documentation.
I am trying to connect java with R using Rserve
Java: 1.8.0_151
R: 3.5.0
OS: Mac 10.13.4 HighSierra
To connect R with Java, I typed the following on RStudio
install.packages("Rserve")
library(Rserve)
Rserve(args="--no-save")
things went smooth and I was so happy about it.
Then I jumped back to Java (Java Eclipse so to speak) and continued typing. Here is what I've done on Eclipse
package rserve;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.REngineException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
public class WordCloud1 {
public static void main(String[] args) throws REngineException,
REXPMismatchException {
RConnection c = new RConnection();
String path = "/Users/JinhoShin/Desktop/study/R/r_temp2";
String file = "seoul_new.txt";
c.parseAndEval("library(KoNLP)");
c.parseAndEval("useSejongDic()");
c.parseAndEval("library(wordcloud)");
c.parseAndEval("library(RColorBrewer)");
c.parseAndEval("setwd('" + path + "')");
c.parseAndEval("data1=readLines('" + file + "')");
c.parseAndEval("data2 = sapply(data1,extractNoun,USE.NAMES=F)");
c.parseAndEval("data3 = unlist(data2)");
c.parseAndEval("data3=gsub('seoul','',data3)");
c.parseAndEval("data3=gsub('request','',data3)");
c.parseAndEval("data3=gsub('place','',data3)");
c.parseAndEval("data3=gsub('transportation','',data3)");
c.parseAndEval("data3=gsub(' ','',data3)");
c.parseAndEval("data3=gsub('-','',data3)");
c.parseAndEval("data3=gsub('OO','',data3)");
c.parseAndEval("write(unlist(data3),'seoul_2.txt')");
c.parseAndEval("data4 = read.table('seoul_2.txt')"); ########this is what blows me up
c.parseAndEval("wordcount=table(data4)");
c.parseAndEval("palete = brewer.pal(9,'Set3')");
c.parseAndEval(
"wordcloud(names(wordcount),freq = wordcount,scale=c(5,1),rot.per=0.25, min.freq = 1," +
" random.order=F, random.color = T, colors=palete)");
c.parseAndEval("savePlot('0517seoul.png', type = 'png')");
c.parseAndEval("dev.off()");
c.close();
}
}
as you notice from the code
c.parseAndEval("data4 = read.table('seoul_2.txt')"); => at rserve.WordCloud1.main(WordCloud1.java:30)
I have no idea why it can't read my text file despite the fact that it could write that file.
This is what Java Eclipse console keeps showing me
Exception in thread "main" org.rosuda.REngine.REngineException: eval failed
at org.rosuda.REngine.Rserve.RConnection.parseAndEval(RConnection.java:499)
at org.rosuda.REngine.REngine.parseAndEval(REngine.java:108)
at rserve.WordCloud1.main(WordCloud1.java:30)
Caused by: org.rosuda.REngine.Rserve.RserveException: eval failed
at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:261)
at org.rosuda.REngine.Rserve.RConnection.parseAndEval(RConnection.java:497)
... 2 more
and this is what RStudio keeps showing me
Error: long vectors not supported yet: qap_encode.c:36
Fatal error: unable to initialize the JIT
I tried everything I could do to resolve this issue, but still I am on the same spot.
I have built a simple project in android studio, and included OpenCV in order to train either an SVM (support vector machine) or an ANN (artificial neural network). Everything seems to go well, including data creation, training and inspection of trained data, except for saving. Whenever I save a opencv ml-object (like ann.save(...) or svm.save(...)), android studio crashes.
SVM -
When I extract supportvectors using the line
classifier.getSupportVectors()
the numbers seem sain. However, the app crashes when I move past a breakpoint placed at
classifier.save("C:\\foo\\trentsvm.txt");
In logCat I dig up the following feedback:
07-04 14:36:10.939 25258-25258/com.example.tbrandsa.opencvtest A/libc:
Fatal signal 11 (SIGSEGV), code 2, fault addr 0x7f755f53f0 in tid
25258 (ndsa.opencvtest) [ 07-04 14:36:10.942 439: 439 W/]
debuggerd: handling request: pid=25258 uid=10227 gid=10227 tid=25258
I get a similar error if i instead try to save an artificial neural network (ANN), see update far below.
I have tried saving the file as XML and txt, and as "C:\trentsvm.someformat", and as "trentsvm.someformat". I also get the same error in my Eclipse java project. High pain, no gain. Full code is below. Could you help?
PS: I use OpenCv version 3.2.0. and Android Studio 2.3.2
// I based this code on stuff i found online. Not sure if all is as important or good.
// Purpose: multilabel classification - digit recognition for android app.
// Create data and labels for a digit recognition algorithm
int numTargets = 10; // (0-9 => 10 types of labels)
int totalSamples = 100; // Could have been number of images of digits
int totalIndicators = 10; // Could have been number of properites per digit image.
Mat labels = new Mat(totalSamples,1, CvType.CV_16S);
Mat data = new Mat(totalSamples, totalIndicators,CvType.CV_16S);
// Fill with dummy values:
for (int s = 0; s<totalSamples; s++)
{
int someLabel = s%numTargets;
labels.put(s,0, (double)someLabel);
for (int m = 0; m<totalIndicators; m++)
{
int someDataValue = (s%numTargets)*totalIndicators + m;
data.put(s, m, (double)someDataValue);
}
}
data.convertTo(data, CvType.CV_32F);
labels.convertTo(labels, CvType.CV_32S);
SVM classifier = SVM.create();
TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER,100,0.1);
classifier.setKernel(SVM.LINEAR);
classifier.setType(SVM.C_SVC); //We choose here the type CvSVM::C_SVC that can be used for n-class classification (n >= 2).
classifier.setGamma(0.5);
classifier.setNu(0.5);
classifier.setC(1);
classifier.setTermCriteria(criteria);
classifier.train(data, Ml.ROW_SAMPLE, labels);
// Check how trained SVM predicts the training data
Mat estimates = new Mat(totalSamples, 1, CvType.CV_32F);
classifier.predict(data, estimates, StatModel.RAW_OUTPUT);
for (int i = 0; i<totalSamples; i++)
{
double l = labels.get(i, 0)[0];
double e = estimates.get(i, 0)[0];
System.out.print("\n fact: "+l+", estimat: "+e);
}
Mat suppV = classifier.getSupportVectors();
try {
if (classifier.isTrained()){
// It crashes at the next line!
classifier.save("C:\\foo\\trentsvm.txt");
}
}
catch (Exception e)
{
}
Update july 5th: As suggested by ZdaR, I tried the to use an in-phone adress, but it did not solve the problem.
String address = Environment.getExternalStorageDirectory().getPath()+"/trentsvm.xml";
// address now has value "storage/emulated/0/trentsvm.xml"
classifier.save(address);
In logcat:
07-05 14:50:12.420 11743-11743/com.example.tbrandsa.opencv2 A/libc:
Fatal signal 11 (SIGSEGV), code 2, fault addr 0x7d517f1990 in tid
11743 (brandsa.opencv2)
[ 07-05 14:50:12.424 3134: 3134 W/ ] debuggerd: handling
request: pid=11743 uid=10319 gid=10319 tid=11743
Update july 6th:
When I run the same script in eclipse and use a debugger (JUnit 4, VM arguments: -Djava.library.path=C:\Users\tbrandsa\Downloads\opencv\build\java\x64;src\test\jniLibs, ) debugging on the pc without device, the caught exception "e" says the following
cause= Exception,
detailMessage= "Unknown Exception" ,
stackTrace=> StackTraceElement[0] ,
suppressedExeptions= Collections$UnmodifiableRandomAccessList,
Update july 13th:
I just tried with an artificial neural network (ANN) too, and it crashes when trying to save.
Error:
Fatal signal 11 (SIGSEGV), code 1, fault addr 0x15a57e688000c in tid
8507 (brandsa.opencv2) debuggerd: handling request: pid=8507
uid=10319 gid=10319 tid=8507
Code:
// Mat data is of size 100*20*CV_32FC1,
// Mat labels is of size 100*1*CV_32FC1
// layerSizes is of size 3*1*CV_8UC1
int[] hiddenLayers = {10};
Mat layerSizes = new Mat(2 + hiddenLayers.length,1,CvType.CV_8U);
layerSizes.put(0, 0, data.width());
for (int l = 0; l< hiddenLayers.length; l++){
layerSizes.put(1 + l, 0,hiddenLayers[l]);}
layerSizes.put(1 + hiddenLayers.length, 0,labels.width());
ANN_MLP ann = ANN_MLP.create();
ann.setLayerSizes(layerSizes);
ann.setActivationFunction(ANN_MLP.SIGMOID_SYM);
ann.train(data, Ml.ROW_SAMPLE , labels);
ann.save("/storage/emulated/0/Pictures/no.rema.priceagent.test/trentann.xml");
To reduce the noise in an image, I am trying to get the average of 10 images.
Mat imgMain = new Mat(n_height, n_width, CvType.CV_32FC4);
Mat imgFin = new Mat(n_height, n_width, CvType.CV_32FC4);
for(int i=1; i <= 10; i++) {
//Crop the image
image.recycle();
image = null;
image = BitmapFactory.decodeFile("/storage/sdcard0/DCIM/A" + String.valueOf(i) + ".jpg");
pimage = Bitmap.createBitmap(image, leftOff1, topOff1, n_width, n_height);
Utils.bitmapToMat(pimage, imgMain);
scaleAdd(imgMain, 0.1, imgFin, imgFin);
}
Running the application, I get the following msg:
Caused by: CvException [org.opencv.core.CvException: cv::Exception:
/hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/matmul.cpp:2079:
error: (-215) src1.type() == src2.type() in function void
cv::scaleAdd(cv::InputArray, double, cv::InputArray, cv::OutputArray)
]
at org.opencv.core.Core.scaleAdd_0(Native Method)
at org.opencv.core.Core.scaleAdd(Core.java:6690)
at MainActivity.imageAnalysis(MainActivity.java:123)
where line 123 is scaleAdd(imgMaing, 0.1, imgFin, imgFin);
According to the reference, src1, src2 and dst MAT should be of same size and type. However, I get this error when I set imgFin type to 32FC4 but do not get any errors when imgFin is set to 8UC4. Any experience of this kind? I need to keep the floating numbers in imgFin which is why I can't go with 8UC4.
// this line will overwrite your imgMain,
// the type will be CV_8UC4, regardless, what you said before.
Utils.bitmapToMat(pimage, imgMain);
// so, convert to float:
imgMain.convertTo(imgMain, CvType.CV_32FC4);
// now add float images, to avoid precision loss:
scaleAdd(imgMain, 0.1, imgFin, imgFin);
I'm working on eclipse + opencv 2.4.6 + java api.
Now i have this compilation error :
OpenCV Error: Assertion failed (ptnum > 3) in unknown function, file ..\..\..\src\opencv\modules\imgproc\src\contours.cpp, line 1969 Exception in thread "main" CvException [org.opencv.core.CvException: ..\..\..\src\opencv\modules\imgproc\src\contours.cpp:1969: error: (-215) ptnum > 3 ]
at org.opencv.imgproc.Imgproc.convexityDefects_0(Native Method)
at org.opencv.imgproc.Imgproc.convexityDefects(Imgproc.java:3170)
The partial code is:
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
Imgproc.findContours(imgframe,contours , hierarchy,Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE,new Point(0,0));
for(int i=0;i<contours.size();i++) {
Imgproc.drawContours(imgframe, contours,i,new Scalar(255,0,255),2,8,hierarchy,0,new Point());
MatOfInt hull_=new MatOfInt();
MatOfInt4 convexityDefects=new MatOfInt4();
Imgproc.convexHull(contours.get(0), hull_);
Imgproc.convexityDefects(contours.get(0), hull_, convexityDefects);
}
Can you help me??
Thank's
I think the problem lies in the number of points in your convex hull. It should have at least 3 points to make using the convexityDefect() possible. It can easily be checked using an if in the for loop:
if(hull_.rows() >= 3){
Imgproc.convexityDefects(contours.get(0), hull_, convexityDefects);
}