Ask Your Question

BenNG's profile - activity

2019-10-29 04:32:02 -0600 received badge  Notable Question (source)
2019-02-01 04:12:52 -0600 received badge  Popular Question (source)
2017-02-20 02:16:01 -0600 commented answer remove small object faster

great to see that ! thank you for pointing this to me ! I finished my project but I will test it again 3.2 and see how it goes !! I hope it's not going to be painful !!! thanks again :)

2017-02-13 09:40:47 -0600 received badge  Self-Learner (source)
2017-02-13 08:44:37 -0600 answered a question build OpenCV 3.1 for android with openCL support

Turn out that there is a python script call build_sdk.py located at: /opencv-3.1.0/platforms/android.
With this script you will be able to created your own android sdk.
Before started I had to install ant (a build tool in the java ecosystem).
For the NDK I used the version r10e.

I wanted to turn ON OpenCL to have some performance improvement. So inside the script look for DWITH_OPENCL and change the flag from OFF to ON. I also had to turn off DBUILD_ANDROID_EXAMPLES and DINSTALL_ANDROID_EXAMPLES because I had some issue with them.

I created a folder opencv-build-android and inside this folder I invoke the script like this:

../opencv-3.1.0/platforms/android/build_sdk.py --ndk_path $ANDROID_NDK --sdk_path $ANDROID_SDK . ../opencv-3.1.0

be careful to the . before ../opencv-3.1.0

I hope It will help you !

2017-02-10 09:14:05 -0600 asked a question build OpenCV 3.1 for android with openCL support

Hi All!

I tried OpenCL on my OpenCV project and the result is a huge improvement in terms of performance so I would like to try OpenCL on android.

I use this article to guide me. Here is my cmake command:

cmake -GNinja -DCMAKE_MAKE_PROGRAM="/usr/bin/ninja" -DCMAKE_TOOLCHAIN_FILE="../opencv-3.1.0/platforms/android/android.toolchain.cmake" -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON  -DWITH_OPENCL=YES -DANDROID_ABI="armeabi-v7a" ../opencv-3.1.0

and I use this command

ninja install/strip

I have no errors neither on compilation nor installation. And an install folder has been created. I think all is perfectly fine except that I can't find the libopencv_java3.so file !!!!! so:

  1. Do you know how I can generate libopencv_java3.so ?
  2. Do you know how I can target several abis ? I tried -DANDROID_ABI="armeabi-v7a" -DANDROID_ABI="x86" and -DANDROID_ABI="armeabi-v7a x86" but it is not correct.

Thank you !

2017-02-09 09:43:59 -0600 commented answer remove small object faster

Yes I tried this one but it breaks my behavior. I'm stuck with contourArea(contours[i]) unfortunately ...

2017-02-08 04:33:52 -0600 commented answer remove small object faster

Hi. I stuck with contourArea(contours[i]) unfortunately ...

2017-02-03 08:54:41 -0600 commented question Which resolution will you choose ?

Thank you for your answer! What resolution will you choose for image processing ?

2017-02-03 06:52:31 -0600 asked a question Which resolution will you choose ?

Hi all !

I have some general question about image processing. I have a project that process an image in real time. It extract a puzzle, write on it and merge the puzzle in the original image. If you start a project right now with real time image processing which resolution will you choose for:

The camera (in my case android) ?
The image that is going to be modified (puzzle in my case) ?

At the beginning of the project I chose 640x360 (don't ask me why ...) for the image that being processed The camera is 1920*1080 so I must resize the image in order to process it and now my Algo is not accurate anymore. well I think my question is do you have some advice ?

Thank you !

2017-02-01 07:47:48 -0600 received badge  Student (source)
2017-02-01 07:42:03 -0600 commented answer remove small object faster

Thank you a lot for your answer ! I had to think a lot on how I preprocess my picture. Unfortunatly I think it is not possible to "reuse" the result of findContour because.
- I preprocess the original picture to extract the puzzle
- I extract the cell on the puzzle
- I preprocess the cell to extract the number

I realize that I never apply findContour on the same image or part of image so unfortunately for me I can't apply what you advise me :( and unfortunately for me again

rectangle(output, cv::boundingRect(contours[i]), color, -1, 8);

breaks the behavior of my app so zero optimization for me today !!

But thanks because it makes me think about my process again.

2017-01-31 11:39:51 -0600 commented question remove small object faster
2017-01-31 11:38:58 -0600 asked a question remove small object faster

Hello all !

In my project I have a function that find contour within an image and if this contour is too small then the contour is set to the color of the background in other words I remove it. here is the function

Mat removeTinyVolume(Mat input, int area, Scalar color)
{
// we draw to the color of the background
Mat output = input.clone();
vector<vector<Point>> contours;
findContours(input, contours, RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

// cout << "contours : " << contours.size() << endl;

for (int i = 0; i < contours.size(); i++)
{
    if (contourArea(contours[i]) < area)
    {
        drawContours(output, contours, i, color, -1, 8);
    }
}
return output;
}

It accurate I'm happy but it is really slow so I was wondering if you guys have some tips&ticks for that ? Thank you a lot ;) By the way this function is part of an open source project that you can find in comments.

2017-01-31 09:16:40 -0600 commented question switching from knn to svm (opencv 3.1)

My grabNumbers function use to take ~110ms now it's ~40ms this is huge !!!

2017-01-31 09:12:29 -0600 commented question switching from knn to svm (opencv 3.1)

I had edited the code and now it works ! Thank you Berak !

2017-01-31 08:24:03 -0600 commented question switching from knn to svm (opencv 3.1)

I made some modification I hope it is better now ! by the wat this is an open source project, you can check it out https://github.com/BenNG/sudoku-recognizer (here)

2017-01-31 02:57:50 -0600 commented question switching from knn to svm (opencv 3.1)

The error is weird because I think that my Mat labels has integer inside !

2017-01-31 02:55:59 -0600 commented question switching from knn to svm (opencv 3.1)

Hi! I simplify a bit my code and change the Mat labels to be "svm compliant" it still work for my knn algo. It still not work but I have a new error :

OpenCV Error: Bad argument (in the case of classification problem the responses must be categorical; either specify varType when creating TrainData, or pass integer responses) in train, file /home/benng/bin/opencv_working_dir/opencv-3.1.0/modules/ml/src/svm.cpp, line 1618
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/benng/bin/opencv_working_dir/opencv-3.1.0/modules/ml/src/svm.cpp:1618: error: (-5) in the case of classification problem the responses must be categorical; either specify varType when creating TrainData, or pass integer responses in function train
2017-01-30 11:36:29 -0600 asked a question switching from knn to svm (opencv 3.1)

Hello !

I would like to switch from knn to svm in order to see how performance goes:
This is the "script" I use to generate a file call "raw-features.yml".

int main(int argc, char **argv)
{

// data to return
Mat features(nbrOfCells, normalizedSizeForCell * normalizedSizeForCell, CV_8UC1);
Mat labels(1, nbrOfCells, CV_8UC1);
Mat svm_labels(nbrOfCells, 1, CV_32S);


// Ptr<ml::KNearest> knn(ml::KNearest::create());
std::map<int, std::map<int, int>> knownCellValues(cellValues());
int value;

string fileName;
Mat raw, sudoku;
ExtractionInformation extractInfo;

string raw_features_path("./../assets/raw-features.yml");
cv::FileStorage raw_features(raw_features_path, cv::FileStorage::WRITE); // open the classifications file

int incrCell = 0; // --> 1184
Mat roi, normalized;
for (int i = 0; i <= lastTrainingPuzzle; i++)
{
    // cout << i << endl;
    stringstream ss;
    ss << "./../assets/puzzles/s";
    ss << i;
    ss << ".jpg";
    string fileName(ss.str());

    raw = imread(fileName, CV_LOAD_IMAGE_GRAYSCALE);

    vector<Point> biggestApprox = findBiggestBlob(raw);
    extractInfo = extractPuzzle(raw, biggestApprox);
    Mat sudoku = recursiveExtraction(extractInfo.image);


    for (int k = 0; k < 81; k++)
    {
        roi = extractRoiFromCell(sudoku, k);
        if (!roi.empty())
        {
            value = knownCellValues[i][k];
            Mat feat = roi.reshape(1, 1);

            feat.copyTo(features.row(incrCell));
            labels.at<unsigned char>(0, incrCell) = value;
            svm_labels.at<int>(incrCell, 0) = value;


            incrCell++;
        }
    }
}

features.convertTo(features, CV_32F);   
labels.convertTo(labels, CV_32F);

raw_features << "features" << features;
raw_features << "labels" << labels;
raw_features << "svm_labels" << svm_labels;
raw_features.release();

return 0;
}

Once "raw-features.yml" is ready, I can use it like that !

I use this the "getKnn" function from my project to train the knn algo.

Ptr<ml::KNearest> getKnn(cv::FileStorage raw_features)
{
int trainingNbr = nbrOfCells * 0.9;
int testingNbr = nbrOfCells - trainingNbr;
Mat features(nbrOfCells, normalizedSizeForCell * normalizedSizeForCell, CV_8UC1);
Mat labels(1, nbrOfCells, CV_8UC1);
Ptr<ml::KNearest> knn(ml::KNearest::create());

if (raw_features.isOpened() == false)
{
    throw std::logic_error("error, unable to open training classifications file, exiting program\n\n");
}

raw_features["features"] >> features;
raw_features["labels"] >> labels;
raw_features.release();

Mat sub_features = features(cv::Range(0, trainingNbr), cv::Range::all());
Mat sub_labels = labels(cv::Range::all(), cv::Range(0, trainingNbr));

knn->train(sub_features, ml::ROW_SAMPLE, sub_labels);

return knn;
}

I'm really happy of it, it works perfectly but I have a problem of performance so I would like to test other algo.

I created the same function for SVM:

Ptr<ml::SVM> getSvm(FileStorage raw_features)
{
Mat features(nbrOfCells, normalizedSizeForCell * normalizedSizeForCell, CV_32F);
Mat svm_labels(nbrOfCells, 1, CV_32S);
raw_features["features"] >> features;
raw_features["svm_labels"] >> svm_labels;
raw_features.release();

Ptr<ml::SVM> svm = ml::SVM::create();
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::POLY);
svm->setDegree(3); // I had to put it if not it fails
svm->setGamma(3);

Ptr<ml::TrainData> tData = ml::TrainData::create(features, ml::SampleTypes::ROW_SAMPLE, svm_labels);
svm->train(tData);
return svm;
}

I use it like that:

string grabNumbers(Mat extractedPuzzle, Ptr<ml::SVM> svm)
{
Mat roi, response, dist;
stringstream ss;
int K = 1;

for (int k = 0; k < 81; k++)
{
    roi = extractRoiFromCell(extractedPuzzle, k);
    if (!roi.empty())
    {
        roi.convertTo(roi, CV_32F);
        float res = svm->predict(roi.reshape(1, 1));
        ss << res;
    }
    else
    {
        ss << "0";
    }
}

return ss.str();
}

But I have this weird error I say weird because I already done the conversion !

    Gtk-Message: Failed ...
(more)
2017-01-05 04:19:16 -0600 commented question What is the reverse of warpPerspective ?

By using this:

    warpPerspective(sudoku, original, trans_mat33, input.size(), WARP_INVERSE_MAP, BORDER_TRANSPARENT);

I managed to re introduce the sudoku I had modified

Thank you !

2017-01-05 03:35:11 -0600 commented question What is the reverse of warpPerspective ?

It does the job yes but the background is black :(

2017-01-05 03:05:48 -0600 commented question What is the reverse of warpPerspective ?

Somebody ask the same question here without answer:

2017-01-05 03:03:11 -0600 asked a question What is the reverse of warpPerspective ?

Hi all !

At the bottom of this link, a transformation extract the sudoku in "full screen".

I would like to draw some text on the extracted sudoku and re introduce it in the big picture. Do you know how can I re introduce it ?

Here is the code I use:

/**
coordinates is a std::vector<Point2f> with inside:
[topLeft, topRight, bottomLeft, bottomRight]  

original is the original picture (the left one in the link) and sudoku is the right one

*/

float w = (float)input.cols;
float h = (float)input.rows;
float hw = w / 2.0f;
float hh = h / 2.0f;

// to points
dst_p[0] = Point2f(0.0f, 0.0f);
dst_p[1] = Point2f(w, 0.0f);
dst_p[2] = Point2f(w, h);
dst_p[3] = Point2f(0.0f, h);

// create perspective transform matrix
Mat trans_mat33 = getPerspectiveTransform(coordinates, dst_p); //CV_64F->double
Mat inv_trans_mat33 = getPerspectiveTransform(dst_p, coordinates);
// perspective transform operation using transform matrix
warpPerspective(input, sudoku, trans_mat33, input.size(), INTER_LINEAR);

sudoku = writeOnPuzzle(sudoku, "012345000000000000000000000000000000000000000000000000000000000000000000000000000");

invert(trans_mat33, inv_trans_mat33);
warpPerspective(sudoku, original, trans_mat33, input.size(), WARP_INVERSE_MAP);

Thank you !

2016-12-14 03:36:26 -0600 asked a question How to use my cpp project on Android ?

Hi guys !

I finally finished a big millestone on my project here but it's not 100% finished.

I would like to port my project on android (ios ?) and I need your experience to win a bit of time because I start to be a little bit out of it...

Could you tell me:

  • How do I need to package my lib to be eatable by android studio ? ( my build here )

  • how to use a native lib which depends on OpenCV !

as usual Thank you for helping !

2016-10-26 13:19:40 -0600 commented question Why is my knn network is so bad ?

That's anoying I don't know what to do now ... It works but it sucks ... Is the knn efficient for that use case ?

2016-10-26 12:27:58 -0600 commented question Why is my knn network is so bad ?

Do you think I can train the cnn with a CV_8UC1 Mat ? I think there is a problem with the type of the trained data and the data I extract from a puzzle

2016-10-26 08:56:42 -0600 commented question Why is my knn network is so bad ?

thx a lot :)

2016-10-26 08:51:28 -0600 commented question Why is my knn network is so bad ?

I added some code !

2016-10-26 08:47:43 -0600 edited question Why is my knn network is so bad ?

Hello everyone !

I used a k-Nearest Neighbors algorithm (knn) and I trained it with the MNIST database
Here is the code for the training:

Ptr<ml::KNearest> getKnn()
{
Ptr<ml::KNearest> knn(ml::KNearest::create());

FILE *fp = fopen("/keep/Repo/USELESS/_sandbox/cpp/learning-cpp/sudoku/assets/train-images-idx3-ubyte", "rb");
FILE *fp2 = fopen("/keep/Repo/USELESS/_sandbox/cpp/learning-cpp/sudoku/assets/train-labels-idx1-ubyte", "rb");

if (!fp || !fp2)
{
    cout << "can't open file" << endl;
}

int magicNumber = readFlippedInteger(fp);
int numImages = readFlippedInteger(fp);
int numRows = readFlippedInteger(fp);
int numCols = readFlippedInteger(fp);
fseek(fp2, 0x08, SEEK_SET);

int size = numRows * numCols;

cout << "size: " << size << endl;
cout << "rows: " << numRows << endl;
cout << "cols: " << numCols << endl;

Mat_<float> trainFeatures(numImages, size);
Mat_<int> trainLabels(1, numImages);

BYTE *temp = new BYTE[size];
BYTE tempClass = 0;
for (int i = 0; i < numImages; i++)
{
    fread((void *)temp, size, 1, fp);
    fread((void *)(&tempClass), sizeof(BYTE), 1, fp2);

    trainLabels[0][i] = (int)tempClass;

    for (int k = 0; k < size; k++)
    {
        trainFeatures[i][k] = (float)temp[k];
    }
}
knn->train(trainFeatures, ml::ROW_SAMPLE, trainLabels);

return knn;

}

When I test the algorithm with the 10k images file MNIST provide I have: Accuracy: 96.910000 which is a good news :) The code to test the knn trained is here:

void testKnn(Ptr<ml::KNearest> knn, bool debug)
{
int totalCorrect = 0;

FILE *fp = fopen("/keep/Repo/USELESS/_sandbox/cpp/learning-cpp/sudoku/assets/t10k-images-idx3-ubyte", "rb");
FILE *fp2 = fopen("/keep/Repo/USELESS/_sandbox/cpp/learning-cpp/sudoku/assets/t10k-labels-idx1-ubyte", "rb");

int magicNumber = readFlippedInteger(fp);
int numImages = readFlippedInteger(fp);
int numRows = readFlippedInteger(fp);
int numCols = readFlippedInteger(fp);
fseek(fp2, 0x08, SEEK_SET);

int size = numRows * numCols;

Mat_<float> testFeatures(numImages, size);
Mat_<int> expectedLabels(1, numImages);

BYTE *temp = new BYTE[size];
BYTE tempClass = 0;

int K = 1;
Mat response, dist, m;

for (int i = 0; i < numImages; i++)
{

    if (i % 1000 == 0 && i != 0)
    {
        cout << i << endl;
    }

    fread((void *)temp, size, 1, fp);
    fread((void *)(&tempClass), sizeof(BYTE), 1, fp2);

    expectedLabels[0][i] = (int)tempClass;

    for (int k = 0; k < size; k++)
    {
        testFeatures[i][k] = (float)temp[k];
    }

    // test to verify if createMatFromMNIST and createMatToMNIST are well.
    m = testFeatures.row(i);

    knn->findNearest(m, K, noArray(), response, dist);

    if (debug)
    {
        cout << "response: " << response << endl;
        cout << "dist: " << dist << endl;
        Mat m2 = createMatFromMNIST(m);
        showImage(m2);
        // Mat m3 = createMatToMNIST(m2);
        // showImage(m3);
    }

    if (expectedLabels[0][i] == response.at<float>(0))
    {
        totalCorrect++;
    }
}
printf("Accuracy: %f ", (double)totalCorrect * 100 / (double)numImages);

}

By the way, you can test the knn I have implemented in my project here: (see the actions part) https://bitbucket.org/BenNG/sudoku-recognizer

But when it comes to use my own data against the algo, it has a bad behavior.
What is the data I give to the algo ?

To answer that I will present a bit my project. My project is a sudoku grabber. So on a picture that holds a sudoku, I'm able to find the sudoku and extract it. Then I'm able to extract every cell in the puzzle. Each cell is preprocessed before I send it to the knn. By ... (more)

2016-10-26 08:26:07 -0600 commented question Why is my knn network is so bad ?

Well you are right for the code it's better to have it on the website but It's started to be big and I thought It was better for the reader to have all the code. For your answer I don't understand what it means

2016-10-26 08:09:53 -0600 commented question Why is my knn network is so bad ?

I double check and it is accessible here ! https://bitbucket.org/BenNG/sudoku-re... even if you dont have an account !