I have a keras network model that takes INDArray but I don't know how to convert a Mat to an INDArray. I know that this is the same question of How to convert Mat (opencv) to INDArray (DL4J)? but it did not help me. Is there some API that performs this task? Thanks
You can use a NativeImageLoader for the conversion.
import org.datavec.image.loader.NativeImageLoader;
(...)
Mat cvImage();
// Fill in your Mat with something
NativeImageLoader nil = new NativeImageLoader();
INDArray image = nil.asMatrix(cvImage).div;
Make sure you have the dependency for datavec in your pom.xml. What error do you get?
Related
I am using javacpp-presets/opencv which is a java wrapper for opencv. My question is how to get pixel value in Mat object or how to convert Mat object to multi-dimension java array?
I am using tensorflow java api for model inference.It needs float[][][][] java array as input argument.
Solved see this
Mat bgr = new Mat();
UByteIndexer rgbaIdx = bgr.createIndexer();
System.out.print(rgbaIdx.get(0, 0, 0))
I saved ORB and SIFT descriptors in xml.
I tried use Mat descriptors = imread("descriptor.xml")
but it didn't work.
it's blank.
Let say that you have stored the SIFT descriptor in descriptor.xml. Use the following code to assign the saved descriptor back to a Mat variable.
Mat sift_descriptor;
FileStorage fs("descriptor.xml", FileStorage::READ);
fs["descriptor"] >> sift_descriptor;
fs.release();
You have to save and load Mats that are not images with cv::FileStorage.
I have a 3x3x1000 OpenCV Mat matrix created using
int sz[] = {3,3,1000};
Mat bigCube(3, sz, CV_8U);
I want to do matrix operations on the 1000 separate 3x3 sub-matrices. But I can not find a way to do this.
The most obvious would be in a for-loop using Range, something like;
bigCube(Range(0,3),Range(0,3),Range(i,i+1));
//Do some operations...
But this won't compile. Is there a way to do this?
I have a folder of images of a car from every angle. I want to use the bag of words approach to train the system in recognizing the car. Once the training is done, I want that if an image of that car is given it should be able to recognize it.
I have been trying to learn the BOW function in opencv in order to make this work and have come at a level where I do not know what to do now and some guidance would be appreciated.
Here is my code that I used to make the bag of words:
Ptr<FeatureDetector> features = FeatureDetector::create("SIFT");
Ptr<DescriptorExtractor> descriptors = DescriptorExtractor::create("SIFT");
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
//defining terms for bowkmeans trainer
TermCriteria tc(MAX_ITER + EPS, 10, 0.001);
int dictionarySize = 1000;
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);
BOWImgDescriptorExtractor bowDE(descriptors, matcher);
//training data now
Mat features;
Mat img = imread("c:\\1.jpg", 0);
Mat img2 = imread("c:\\2.jpg", 0);
vector<KeyPoint> keypoints, keypoints2;
features->detect(img, keypoints);
features->detect(img2,keypoints2);
descriptor->compute(img, keypoints, features);
Mat features2;
descripto->compute(img2, keypoints2, features2);
bowTrainer.add(features);
bowTrainer.add(features2);
Mat dictionary = bowTrainer.cluster();
bowDE.setVocabulary(dictionary);
This is all based on the BOW documentation.
I think at this stage my system is trained. and the next step is predicting.
this is where I dont know what to do. If I use SVM or NormalBayesClassifier they both use the terms train and predict.
How do I predict and train after this? any guidance would be much appreciated. How do I connect the training of the classifier to my `bowDE`` function?
Your next step is to extract the actual bag of word descriptors. You can do this using the compute function from the BOWImgDescriptorExtractor. Something like
bowDE.compute(img, keypoints, bow_descriptor);
Using this function you create descriptors which you then gather into a matrix which serves as the input for the classifier functions. Maybe this tutorial can guide you a little bit.
Another thing I would like to mention is, that for classification you usually need at least 2 classes. So you also need some images which do not contain cars to train a classifier.
There is a IplImage and CvMat in OpenCV. What are the full names of them?
IPL in IplImage stands for Intel Processing Library, which is a remnant of when OpenCV was maintained by Intel.
CV in cvMat stands for Computer Vision Matrix, which is a data structure commonly used in graphics.
IplImage is an old structure, which I believe is internally converted into cv::Mat, or just Mat if you're in the cv namespace already. Likewise, cvMat is converted into Mat as well.
http://opencv.willowgarage.com/documentation/cpp/basic_structures.html?highlight=iplimage