2021-04-14 17:56:53 -0600
| received badge | ● Famous Question
(source)
|
2020-04-08 00:57:11 -0600
| received badge | ● Notable Question
(source)
|
2019-09-25 00:48:15 -0600
| received badge | ● Popular Question
(source)
|
2019-08-25 09:41:34 -0600
| received badge | ● Popular Question
(source)
|
2019-06-18 09:01:14 -0600
| received badge | ● Popular Question
(source)
|
2019-04-03 14:22:27 -0600
| received badge | ● Popular Question
(source)
|
2018-03-30 09:50:02 -0600
| marked best answer | SVM predict Assertion failed For now I am trying to predict only 1 or -1 the error is OpenCV Error: Assertion failed (samples.cols == var_count && samples.type() == CV_32F) in predict
my code public static Mat getMat(String path) {
Mat img = new Mat();
Mat convert_to_gray = Imgcodecs.imread(path, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
convert_to_gray.convertTo(img, CvType.CV_8U);
return img;
}
public static void test() {
System.out.println("Testing..");
Mat input = Imgcodecs.imread(new File(FILE_TEST).getAbsolutePath(),0);
Mat output = new Mat();
input.convertTo(output,CvType.CV_32SC1);
output = output.reshape(1, 1);
System.out.println(output);
System.out.println(clasificador.predict(output)); <-the error points here
}
public static void trains() {
System.out.println("Training...");
v_descriptors.copyTo(trainingData);
trainingData.convertTo(trainingData, CvType.CV_32F);
trainingLabels.copyTo(classes);
classes.convertTo(classes, CvType.CV_32S);
clasificador.setType(SVM.C_SVC);
clasificador.setKernel(SVM.LINEAR);
clasificador.setGamma(0.5);
clasificador.setNu(0.5);
clasificador.setC(1);
clasificador.setTermCriteria(S);
clasificador.train(trainingData, Ml.ROW_SAMPLE, classes);
System.out.println("Done Training");
}
public static void trainPositives() {
MatOfFloat descriptorsValues = new MatOfFloat();
int count = 0;
for (File file : new File(PATH_POSITIVE).listFiles()) {
count++;
Mat img = getMat(file.getAbsolutePath());
HOGDescriptor d = new HOGDescriptor(new Size(32, 16), new Size(8, 8), new Size(4, 4), new Size(4, 4), 9);
d.compute(img, descriptorsValues);
Mat labelsMat = new Mat(1, 1, CvType.CV_32SC1, new Scalar(1));
v_descriptors.push_back(descriptorsValues.reshape(1, 1));
trainingLabels.push_back(labelsMat);
}
System.out.println(v_descriptors);
System.out.println(trainingLabels);
}
public static void trainNegatives() {
int count = 0;
MatOfFloat descriptorsValues2 = new MatOfFloat();
for (File file : new File(PATH_NEGATIVE).listFiles()) {
count++;
Mat img = getMat(file.getAbsolutePath());
HOGDescriptor d = new HOGDescriptor(new Size(32, 16), new Size(8, 8), new Size(4, 4), new Size(4, 4), 9);
d.compute(img, descriptorsValues2);
Mat labelsMat = new Mat(1, 1, CvType.CV_32SC1, new Scalar(-1));
v_descriptors.push_back(descriptorsValues2.reshape(1, 1));
trainingLabels.push_back(labelsMat);
}
System.out.println(v_descriptors);
System.out.println(trainingLabels);
}
|
2017-09-24 12:02:27 -0600
| marked best answer | detect the plate i cant seem to detect the plate using the findcontour method here is my code i am using openCV java api Mat image = Imgcodecs.imread("/home/tomna/NetBeansProjects/main/src/main/Dadcar.png");
Mat image_gray = new Mat();
Mat image_sobel = new Mat();
Mat image_threshold = new Mat();
Mat strel = getStructuringElement(MORPH_RECT, new Size(20,36));
Imgproc.cvtColor(image, image_gray, Imgproc.COLOR_BGR2GRAY); // make image gray
Imgproc.blur(image_gray, image_gray, new Size(5, 5));
Imgproc.Sobel(image_gray, image_sobel, CvType.CV_8U, 1, 0, 3, 1, 0);
Imgproc.threshold(image_sobel, image_threshold, 0, 255, Imgproc.THRESH_OTSU + Imgproc.THRESH_BINARY);
Imgproc.morphologyEx(image_threshold, image_threshold, Imgproc.MORPH_CLOSE, strel);
double[] centers = {image_gray.width() / 2, image_gray.height() / 2};
Point image_center = new Point(centers);
Mat hierarchy = new Mat();
Imgproc.findContours(image_threshold, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
Rect rect_min = new Rect();
for (MatOfPoint contour : contours) {
Rect rec = Imgproc.boundingRect(contour);
if (rec.height > image.height() & rec.width > image.width())
{
continue;
}
for (int i = 0; i < contours.size(); i++) {
Imgproc.drawContours(image, contours, i, new Scalar(0, 0, 255), 1, 8, hierarchy, 0, new Point());
Imgproc.rectangle(image, rec.tl(), rec.br(), new Scalar(0, 0, 255), 2);
}
}
|
2017-06-04 04:01:23 -0600
| commented question | ANN_MLP recognition |
2017-06-03 13:18:17 -0600
| asked a question | ANN_MLP recognition so i have been successful with making ANN_MLP work and output results since i had a problem with the sigmoid not initialized however i am still not sure if there is a problem with having not enough training images or i have something wrong with my code making the results not true every time with different results is there a possible way to check if the problem is with the data set i have ? so my input is this image and the dataset used for training so the result should be 6.0 but what i get is 5.0 [0.52028143,
0.44897014, 0.54281932, 0.50860959, 0.47123247, 0.54682046, 0.48321274, 0.54106969, 0.45284992] and when input this image the result should be 0.0 but it gives me also 5.0 [0.52415699, 0.44889876, 0.54271841, 0.51714647, 0.48295537, 0.54325002, 0.48789713, 0.53129482, 0.45263588] so without further due here is the code String PATH_numbers = "/home/tomna/NetBeansProjects/Parking-tracking-system/src/mainclass/chdataset/Numb";
Mat img = Imgcodecs.imread("/home/tomna/NetBeansProjects/Parking-tracking-system/src/mainclass/characters/8.jpg");
Mat trainingData = new Mat();
Mat trainingLabels = new Mat();
ANN_MLP knn = ANN_MLP.create();
TermCriteria s = new TermCriteria(TermCriteria.MAX_ITER + TermCriteria.EPS, 100, 0.00001);
knn.setTrainMethod(ANN_MLP.BACKPROP, 0.5, 0.5);
knn.setBackpropWeightScale(0.05f);
knn.setBackpropMomentumScale(0.05f);
knn.setTermCriteria(s);
Mat layers = new Mat(3, 1, CvType.CV_32SC1);
layers.row(0).setTo(new Scalar(4440));
layers.row(1).setTo(new Scalar(2000));
layers.row(2).setTo(new Scalar(9));
knn.setLayerSizes(layers);
knn.setActivationFunction(ANN_MLP.SIGMOID_SYM, 0.1, 0.1);
trainingLabels = new Mat(45, 9, CvType.CV_32F, Scalar.all(0));
for (File file : new File(PATH_numbers).listFiles()) {
Mat image = getMat(file.getAbsolutePath());
image.convertTo(image, CvType.CV_32F); // Convert to float
Imgproc.resize(image, image, new Size(60, 74));
Imgproc.GaussianBlur(image, image, new Size(5, 5), 0);
Core.addWeighted(image, 0.5, image, 0, 0, image);
Mat imgresized = image.reshape(1, 1); // make continous
trainingData.push_back(imgresized);
}
int index = 0;
int numImgPerClass = 5;
int rowstart = 0;
for (int i = 0; i < 9; i++) {
trainingLabels.submat(rowstart, numImgPerClass, i, i + 1).setTo(new Scalar(1));
rowstart += 5;
numImgPerClass += 5;
}
knn.train(trainingData, Ml.ROW_SAMPLE, trainingLabels);
Mat test = new Mat();
Imgproc.cvtColor(img, test, Imgproc.COLOR_RGB2GRAY);
Imgproc.resize(test, test, new Size(60, 74));
test.convertTo(test, CvType.CV_32FC1);
Mat results = new Mat();
test = test.reshape(1, 1);
float label = knn.predict(test.reshape(1, 1), results, 0);
System.out.println(label + " " + results.dump());
|
2017-06-01 08:46:53 -0600
| commented answer | ANN_MLP bad argument error |
2017-06-01 08:42:18 -0600
| commented answer | ANN_MLP bad argument error @StevenPuttemans no i am not asking you to write me code i am asking for some explanation on why the output is this way |
2017-06-01 07:10:51 -0600
| commented answer | ANN_MLP bad argument error @StevenPuttemans could you check the updated code and see if you can help me understand why i get a 0 as an output no matter what the user input image is ? i mean the trainig part and going down and thank you :) |
2017-06-01 07:04:15 -0600
| commented answer | ANN_MLP bad argument error this means Mat layers = new Mat(3, 1, CvType.CV_32SC1);
layers.row(0).setTo(new Scalar(4440));
layers.row(1).setTo(new Scalar(2000));
layers.row(2).setTo(new Scalar(9)); <---- here
knn.setLayerSizes(layers);
trainingLabels = new Mat(45, 9, CvType.CV_32F, Scalar.all(0)); <----here
int index = 0;
int numImgPerClass = 5;
int rowstart = 0;
for (int i = 0; i < 9; i++) {
trainingLabels.submat(rowstart, numImgPerClass, i, i + 1).setTo(new Scalar(1));
rowstart += 5;
numImgPerClass += 5;
}
|
2017-06-01 06:07:24 -0600
| commented answer | ANN_MLP bad argument error @StevenPuttemans i do understand how it works but the thing is i have 9 charachter and only 5 classess so my only solution is to increase the number of classes to 9 which i am not sure if correct since the layers output is 5 [1,0,0,0,0,0,0,0,0; //image 1
[1,0,0,0,0,0,0,0,0; //image 1
[1,0,0,0,0,0,0,0,0; //image 1
[0,1,0,0,0,0,0,0,0; //image 2
[0,1,0,0,0,0,0,0,0; //image 2
[0,1,0,0,0,0,0,0,0; //image 2
|
2017-06-01 05:10:01 -0600
| commented answer | ANN_MLP bad argument error @berak mm ok this means the code above is correct.. could you check the UPDATE cuz it outputs an error if i added a reshaper and convert to the output to the training labels but if i dont the output is 0 |
2017-06-01 04:51:25 -0600
| commented answer | ANN_MLP bad argument error what i did in a loop outside and this output just fine int index = 0;
int numImgPerClass = 5;
int rowstart = 0;
for (int i = 0; i < 9; i++) {
trainingLabels.submat(rowstart, numImgPerClass, i, i + 1).setTo(new Scalar(1));
rowstart += 5;
numImgPerClass += 5;
}
|
2017-06-01 04:50:08 -0600
| commented answer | ANN_MLP bad argument error @berak this outputs an error OpenCV Error: Assertion failed (0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows) in Mat, file /home/tomna/Desktop/opencv-3.1.0/modules/core/src/matrix.cpp, line 469
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/tomna/Desktop/opencv-3.1.0/modules/core/src/matrix.cpp:469: error: (-215) 0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows in function Mat
]
|
2017-06-01 04:49:21 -0600
| commented answer | ANN_MLP bad argument error @StevenPuttemans i do understand that i should make a row for each training sample but what i dont understand is based on what the 1 will be placed at which column ? so far what i see is its index like 0 then index 0 will contain the 1 |
2017-05-31 12:50:04 -0600
| commented question | ANN_MLP bad argument error @berak i just updated the the question check the UPDATE and i really wanna know how close am i from fininshing this and thank you |
2017-05-31 09:36:44 -0600
| commented answer | ANN_MLP bad argument error @ StevenPuttemans any idea what to do? |
2017-05-31 08:49:18 -0600
| answered a question | ANN_MLP bad argument error @berak so there is something i just noticed if according to your code Mat labels = new Mat(numImages, numClasses, CV_32F, Scalar.all(0));
Mat labels = new Mat( 45, 5 , CV_32F, Scalar.all(0)); <-- this means that the output of the labels will be something like that [1, 0, 0, 0, 0;
1, 0, 0, 0, 0;
1, 0, 0, 0, 0;
1, 0, 0, 0, 0;
1, 0, 0, 0, 0;
0, 1, 0, 0, 0;
0, 1, 0, 0, 0;
0, 1, 0, 0, 0;
0, 1, 0, 0, 0;
0, 1, 0, 0, 0;
0, 0, 1, 0, 0;
0, 0, 1, 0, 0;
0, 0, 1, 0, 0;
0, 0, 1, 0, 0;
0, 0, 1, 0, 0;
0, 0, 0, 1, 0;
0, 0, 0, 1, 0;
0, 0, 0, 1, 0;
0, 0, 0, 1, 0;
0, 0, 0, 1, 0;
0, 0, 0, 0, 1;
0, 0, 0, 0, 1;
0, 0, 0, 0, 1;
0, 0, 0, 0, 1;
0, 0, 0, 0, 1;
0, 0, 0, 0, 0; <<--- it wont assign anymore
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0]
|
2017-05-31 08:02:29 -0600
| commented answer | ANN_MLP bad argument error @berak mm ok good and now there is a problem with part two i cant seem to update the parameters of submat for (File file : new File(PATH_numbers).listFiles()) {
int numImgPerClass = 5;
int rowstart = 0;
trainingLabels.submat(rowstart, numImgPerClass, index, index + 1).setTo(new Scalar(1));
index++;
rowstart += 5;
numImgPerClass += 5;
}
System.out.println(trainingLabels.dump());
[1, 0, 0, 0, 0;
1, 0, 0, 0, 0;
1, 0, 0, 0, 0;
1, 0, 0, 0, 0;
1, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;] it out puts the 45 all "0"
|
2017-05-31 06:42:44 -0600
| commented answer | ANN_MLP bad argument error @berak ok soo i understood the second part the one-hot encoded but the first part setting the network layers is what i dont understand according to your explanation this is how the network will look like ? Mat layers = new Mat(3,1,CvType.CV_32SC1);
layers.row(0).setTo(new Scalar(4440));
layers.row(1).setTo(new Scalar(2000));
layers.row(2).setTo(new Scalar(5));
|
2017-05-31 06:42:44 -0600
| received badge | ● Commentator
|
2017-05-31 06:22:02 -0600
| commented question | how to Use KNN for OCR @StevenPuttemans ya i know but i need to implement this using opencv so ya this is why i am using neural network ANN_MLP now |
2017-05-31 06:08:29 -0600
| commented question | how to Use KNN for OCR @StevenPuttemans do you have any links on how to implement it using java and opencv lib ? |
2017-05-30 15:44:56 -0600
| commented question | ANN_MLP bad argument error @berak man sorry for tagging you in every question i post but man you seem like the only one who understand image processing here once again sorry and hope you help here :) |
2017-05-30 15:42:43 -0600
| asked a question | ANN_MLP bad argument error i am going straigt to the point i am doing a neural network and after setting the ANN i get this error saying that OpenCV Error: Bad argument (input
training data should be a
floating-point matrix with the number
of rows equal to the number of
training samples and the number of
columns equal to the size of 0-th
(input) layer) in prepare_to_train,
file
/home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp,
line 668 Exception in thread "main"
CvException
[org.opencv.core.CvException:
cv::Exception:
/home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp:668: error: (-5) input training data should
be a floating-point matrix with the
number of rows equal to the number of
training samples and the number of
columns equal to the size of 0-th
(input) layer in function
prepare_to_train ] but i dont get why cuz the numbers seems to be okay or what i think they are soo here is the code void run() throws IOException {
String PATH_numbers = "/home/tomna/NetBeansProjects/Parking-tracking-system/src/mainclass/chdataset/Numb";
Mat img = Imgcodecs.imread("/home/tomna/NetBeansProjects/Parking-tracking-system/src/mainclass/characters/4.jpg");
Mat trainingData = new Mat();
Mat trainingLabels = new Mat();
//// here i prepare the neural network//////////
ANN_MLP knn = ANN_MLP.create();
TermCriteria s = new TermCriteria(TermCriteria.MAX_ITER + TermCriteria.EPS, 100, 0.00001);
knn.setTrainMethod(ANN_MLP.BACKPROP);
knn.setBackpropWeightScale(0.05f);
knn.setBackpropMomentumScale(0.05f);
knn.setTermCriteria(s);
" ///////////here i identify that 3 layer 1 input 5 hidden 1 output ////not sure here how to put the input since i have only 45 images for training so need help here and i think 5 is also a not good example for hidden so yaa as well here "
Mat layers = new Mat(3,1,CvType.CV_32SC1);
layers.row(0).setTo(new Scalar(1));
layers.row(1).setTo(new Scalar(5));
layers.row(2).setTo(new Scalar(1));
knn.setLayerSizes(layers);
int i = 0;
for (File file : new File(PATH_numbers).listFiles()) {
Mat image = getMat(file.getAbsolutePath());
Imgproc.GaussianBlur(image, image, new Size(5, 5), 0);
Core.addWeighted(image, 1.5, image, 0, 0, image);
image.convertTo(image, CvType.CV_32F); // Convert to float
Imgproc.resize(image, image, new Size(60, 74));
Mat imgresized = image.reshape(1, 1); // make continous
trainingData.push_back(imgresized);
int nImagesPerClass = 5;
int id = 1 + (i / nImagesPerClass);
trainingLabels.push_back(new Mat(1, 1, CvType.CV_32SC1, new Scalar(id)));
i++;
System.out.println(trainingData);
}
Mat response = new Mat();
Mat tmp;
tmp = trainingLabels.reshape(1, 1); // make continuous
tmp.convertTo(response, CvType.CV_32F); // Convert to float
System.out.println(trainingData.cols());
knn.train(trainingData, Ml.ROW_SAMPLE, response); <<-- "error point here when i output trainiData.rows=45 and trainiData.rows.cols = 4440 so i guess the bad argument is for the cols ? "
Mat test = new Mat();
Imgproc.cvtColor(img, test, Imgproc.COLOR_RGB2GRAY);
Imgproc.GaussianBlur(test, test, new Size(5, 5), 0);
Core.addWeighted(test, 0.5, test, 0, 0, test);
Imgproc.resize(test, test, new Size(60, 74));
test.convertTo(test, CvType.CV_32F);
Mat results = new Mat();
int label ... (more) |
2017-05-30 09:13:43 -0600
| commented question | how to Use KNN for OCR @berak so the training label will be something like this
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9] and after changing this to this for both the training images and test image Imgproc.resize(image, image, new Size(74, 60));
Imgproc.resize(image, image, new Size(60, 74));
now what processing could be done ? |
2017-05-30 08:45:39 -0600
| commented question | how to Use KNN for OCR @berak do you mean i use the code before the edit but the i/5+1 where will this be exactly |
2017-05-30 05:02:51 -0600
| edited question | how to Use KNN for OCR so iv had this question every where and no answers are provided i am trying to recognize a digit (image) and the training data set is the result is 35 from the KNN.findNearest so my question is what does this number mean or how could i use it to say that the input data is equal to ٢ here is my code void run() throws IOException {
String PATH_numbers = "/home/tomna/NetBeansProjects/Parking-tracking-system/src/mainclass/chdataset/Numb";
Mat img = Imgcodecs.imread("/home/tomna/NetBeansProjects/Parking-tracking-system/src/mainclass/characters/6.jpg");
Mat trainingData = new Mat();
Mat trainingLabels = new Mat();
int i=1;
int j=1;
for (File file : new File(PATH_numbers).listFiles()) {
Mat image = getMat(file.getAbsolutePath());
image.convertTo(image, CvType.CV_32F); // Convert to float
Imgproc.resize(image, image, new Size(74, 60));
Mat imgresized = image.reshape(1, 1); // make continous
trainingData.push_back(imgresized);
trainingLabels.push_back(new Mat(1, 1, CvType.CV_32SC1, new Scalar(i)));
if(j%5==0){
i++;
}
j++;
}
Mat response = new Mat();
Mat tmp;
tmp = trainingLabels.reshape(1, 1); // make continuous
tmp.convertTo(response, CvType.CV_32F); // Convert to float
KNearest knn = KNearest.create();
knn.train(trainingData, Ml.ROW_SAMPLE, tmp);
System.out.println(tmp.dump());
Mat test = new Mat();
Imgproc.cvtColor(img, test, Imgproc.COLOR_RGB2GRAY);
Imgproc.resize(test, test, new Size(74, 60));
test.convertTo(test, CvType.CV_32F);
Mat results = new Mat();
int label = (int) knn.findNearest(test.reshape(1, 1), 1, results);
System.out.println(label);
/// this part is an old part when i had only two images for each digit so i thought that the output correspond to number of images (trainingdata) ex if label if 1 means that the nearest neighbor is from image 1.. not sure
// if(label==1||label==2){
// System.out.println("٠");
// }else if(label==3||label==4){
// System.out.println("١");
// }else if(label==4||label==5){
// System.out.println("٢");
// }else if(label==6||label==7){
// System.out.println("٣");
// }else if(label==8||label==9){
// System.out.println("٤");
// }else if(label==10||label==11){
// System.out.println("٥");
// }else if(label==12||label==13){
// System.out.println("٦");
// }else if(label==14||label==15){
// System.out.println("٧");
// }else if(label==16||label==17){
// System.out.println("٨");
// }else if(label==18||label==19){
// System.out.println("٩");
// }else{
// System.out.println("Sorry");
// }
}
edit after using moment and hue moment with findcontours there will be sure some code wrong or logically wrong but i am new to this so bare with me and point it out. the output is always 1 no matter what the input user image is here is the code void run() throws IOException {
String PATH_numbers = "/home/tomna/NetBeansProjects/Parking-tracking-system/src/mainclass/chdataset/Numb";
Mat img = Imgcodecs.imread("/home/tomna/NetBeansProjects/Parking-tracking-system/src/mainclass/characters/4.jpg");
Mat trainingData = new Mat();
Mat trainingLabels = new Mat(); for (File file : new File(PATH_numbers).listFiles()) {
Mat image = getMat(file.getAbsolutePath());
Imgproc.resize(image, image, new Size(74, 60));
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(image, contours ... (more) |