Ask Your Question

Nesbit's profile - activity

2018-09-24 08:49:34 -0500 received badge  Popular Question (source)
2017-12-05 07:29:04 -0500 commented question DIS Optical Flow crashes when running twice

@berak Thanks.

2017-12-05 05:51:06 -0500 commented question DIS Optical Flow crashes when running twice

@berak Just in case: you got TWO optical flows, right? (In the code I display the flow image each time, and one needs to

2017-12-05 05:49:06 -0500 edited question DIS Optical Flow crashes when running twice

DIS Optical Flow crashes when running twice Hello, When running DIS Optical Flow twice (as in the example below), it cr

2017-12-05 05:47:12 -0500 commented question DIS Optical Flow crashes when running twice

@berak Thanks. Are you using the latest version of OpenCV and the latest build of opencv_contrib? I'm on Linux Mint 18.3

2017-12-05 05:35:41 -0500 commented question DIS Optical Flow crashes when running twice

Also crashed with "opencv/samples/data/Megamind.avi". I have updated the full code for the ease of reproduction. The err

2017-12-05 05:34:14 -0500 edited question DIS Optical Flow crashes when running twice

DIS Optical Flow crashes when running twice Hello, When running DIS Optical Flow twice (as in the example below), it cr

2017-12-05 04:59:10 -0500 received badge  Enthusiast
2017-12-04 14:31:13 -0500 asked a question DIS Optical Flow crashes when running twice

DIS Optical Flow crashes when running twice Hello, When running DIS Optical Flow twice (as in the example below), it cr

2017-07-24 03:02:24 -0500 received badge  Famous Question (source)
2017-07-24 03:02:24 -0500 received badge  Notable Question (source)
2017-07-24 03:02:24 -0500 received badge  Popular Question (source)
2016-02-29 18:25:27 -0500 asked a question How to include OpenCV source code in C++ project to make it portable?

In my C++ project I use (3.1) OpenCV Mat for (only) storing and operating matrices (thus only the core module is used).

Now I would like to make the code portable: if a user (running Linux or Windows or OS X) takes my code then he can compile and run it, without installing the whole OpenCV pack.

Moreover, I would like the user to run the fastest version of the code (i.e. compiled in Release mode, Optimizations, etc...).

Could you please suggest a way to do that?

I copied the source code from modules/cores to the project directory but not sure how to proceed. I guess I have to modify the original OpenCV CMakeLists to take into account only the core module, but the file looks complicated for me :(

Thank you in advance for any suggestions!

2016-02-15 08:44:00 -0500 answered a question How to do Delaunay Triangulation and return an adjacency matrix?
2016-02-12 23:50:06 -0500 asked a question How to do Delaunay Triangulation and return an adjacency matrix?

I am trying to do Delaunay Triangulation for a set of points in OpenCV, but encountered a problem.

The function takes a matrix of coordinates and return an adjacency matrix. (If there is and edge connecting the point i and the point j, then adj(i,j) = 1, otherwise 0.)

I didn't get it working. The code below give strange results.

Could you please help?

An example of Delaunay Triangulation is given here.

Thank you in advance.

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

using namespace std;
using namespace cv;

Mat delaunay(const Mat& points, int imRows, int imCols)
/// Return the Delaunay triangulation, under the form of an adjacency matrix
/// points is a Nx2 mat containing the coordinates (x, y) of the points
{
    Mat adj(points.rows, points.rows, CV_32S, Scalar(0));

    /// Create subdiv and insert the points to it
    Subdiv2D subdiv(Rect(0,0,imCols,imRows));
    for(int p = 0; p < points.rows; p++)
    {
        float xp = points.at<float>(p, 0);
        float yp = points.at<float>(p, 1);
        Point2f fp(xp, yp);
        subdiv.insert(fp);
    }

    /// Get the number of edges
    vector<Vec4f> edgeList;
    subdiv.getEdgeList(edgeList);
    int nE = edgeList.size();

    /// Check adjacency
    for(int e = 1; e <= nE; e++)
    {
        int p = subdiv.edgeOrg(e); // Edge's origin
        int q = subdiv.edgeDst(e); // Edge's destination

        if(p < points.rows && q < points.rows)
            adj.at<int>(p, q) = 1;
//        else
//        {
//            cout<<p<<", "<<q<<endl;
//            assert(p < points.rows && q < points.rows);
//        }
    }
    return adj;
}



int main()
{
    Mat points = Mat(100, 2, CV_32F);
    randu(points, 0, 99);

    int rows = 100, cols = 100;
    Mat im(rows, cols, CV_8UC3, Scalar::all(0));
    Mat adj = delaunay(points, rows, cols);

    for(int i = 0; i < points.rows; i++)
    {
        int xi = points.at<float>(i,0); 
        int yi = points.at<float>(i,1);
        /// Draw the edges
        for(int j = i+1; j < points.rows; j++)
        {
            if(adj.at<int>(i,j) > 0)
            {
                int xj = points.at<float>(j,0); 
                int yj = points.at<float>(j,1);
                line(im, Point(xi,yi), Point(xj,yj), Scalar(255,0,0), 1);
            }
        /// Draw the nodes
        circle(im, Point(xi, yi), 1, Scalar(0,0,255), -1);
        }
    }
    namedWindow("im", CV_WINDOW_NORMAL);
    imshow("im",im);
    waitKey();
    return 0;
}
2015-11-24 13:49:26 -0500 received badge  Famous Question (source)
2014-10-19 09:36:24 -0500 received badge  Notable Question (source)
2014-09-19 06:52:12 -0500 commented answer How to use parallel_for?

Thanks, @Daniil.

2014-09-18 23:02:46 -0500 asked a question Parallel for loop: What's wrong with my code?

Update: solved. There are overlaps among the M1(Rect(x,y,r,r)).

For the purpose of testing the parallel for loop in OpenCV, I created the following code, which just takes a matrix and does some manipulations and outputs another matrix. The parallel code is supposed to give the same results as given by the serial code. However, it is not the case.

Before compiling the code, make sure that you have already had TBB properly enabled and installed in OpenCV. (Otherwise the parallelization will not be taken into account and will be treated as serial code, thus you'll obviously obtain the same results.)

#include <opencv2/core/core.hpp>
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>    

using namespace cv;
using namespace std;    

class Parallel_process : public cv::ParallelLoopBody
{

private:
    const Mat& im;
    Mat& out;
    int r;

public:
    Parallel_process(const Mat& inputIm, int radius, Mat& outputIm)
        : im(inputIm), out(outputIm), r(radius){}

    virtual void operator()(const cv::Range& range) const
    {
        //cout << "start=" << range.start<< " end=" << range.end<<endl;
        //cout << endl;
        for(int y = range.start; y < range.end; y++)
        {
            //cout << " " << y;
            for(int x=0; x< im.cols - r; x++)
            {
                out(Rect(x,y,r,r)) = im(Rect(x,y,r,r))*im(Rect(x,y,r,r));
                //cout<<im(Rect(x,y,r,r))<<endl;
            } 
        }
    }
};


int main(int , char** )
{

    double start, timeSec;

    int n = 5, r = 2;

    /// Define a matrix
    Mat M = Mat(n, n, CV_32F);
    //randu(M, 0, 1);
    for(int y=0; y< M.rows; y++)
    {
        for(int x=0; x< M.cols; x++)
        {
            M.at<float>(x,y) = abs(x-y);    
        } 
    } 

    //cout<<M<<endl;

    Mat M1 = M.clone();
    Mat M2 = M.clone();    


    /// Serial loop
    start = (double)getTickCount();
    for(int y=0; y< M1.rows - r; y++)
    {
        for(int x=0; x< M1.cols - r; x++)
        {
            M1(Rect(x,y,r,r)) = M(Rect(x,y,r,r))*M(Rect(x,y,r,r));  
        } 
    } 
    timeSec = (getTickCount() - start) / getTickFrequency();
    cout << "Non parallel time: " << timeSec << "s" << endl;
    //cout<<M1<<endl;        


    /// Parallel loop
    start = (double)getTickCount();
    parallel_for_(Range(0,(int)M2.rows-r), Parallel_process(M,r,M2));
    timeSec = (getTickCount() - start) / getTickFrequency();
    cout << "Parallel time: " << timeSec << "s" << endl;    
    //cout<<M2<<endl;    

    /// Check the results
    cout << "Check: norm(M1-M2)=" << norm(M1-M2) << endl;
    return 0;
}

By executing the obtained binary, I got very random results (even with the same binary, i.e. compiled once but executed several times).

Hope somebody can help to figure it out. Thanks in advance.

enter image description here

2014-09-18 20:29:07 -0500 commented answer How to use parallel_for?

Hi @alexcv and @Michael. Is it possible to specify the number of processors? Thanks.

2014-09-02 08:37:22 -0500 commented question Missing Icons in Highgui Toolbar and wrong color values

+1. I installed 2.4.9 with Qt5 and have the same problem, the toolbar icons are invisible.

2014-09-02 07:04:09 -0500 received badge  Supporter (source)
2014-05-19 20:39:08 -0500 received badge  Student (source)
2014-04-30 04:27:49 -0500 received badge  Popular Question (source)
2013-06-20 11:44:58 -0500 answered a question How can I convert cv::Mat of type CV_8UC1 to a QImage in Qt?

Normalize and then convert cv::Math src to QImage qim:

int w=src.cols;
int h=src.rows;
QImage qim(w,h,QImage::Format_RGB32);
QRgb pixel;
Mat im;
normalize(src.clone(),im,0.0,255.0,CV_MINMAX,CV_8UC1);
for(int i=0;i<w;i++)
{
    for(int j=0;j<h;j++)
    {
        int gray = (int)im.at<unsigned char>(j, i);
        pixel = qRgb(gray,gray,gray);
        qim.setPixel(i,j,pixel);
    }
}
2013-06-19 12:05:03 -0500 commented question Fast adjacency matrix computation from watershed

I've answered myself but have to wait 2 days to be able to post it.

2013-06-19 11:54:59 -0500 commented question Fast adjacency matrix computation from watershed

Actually, M is the adjacency matrix and need to be computed.

2013-06-18 10:16:02 -0500 asked a question Fast adjacency matrix computation from watershed

I would like to know if there is a faster way, than what I have done below, to compute the region adjacency matrix from a watershed image.

Input: watershed image with N regions labeled from 1 to N.

Output: adjacency matrix of these N regions.

1. For each region, compute the corresponding mask and put all masks into a vector:

vector<Mat> masks;    
for(int i = 0; i < N; i++ )
{    
// Create the corresponding mask
Mat mask;    
compare(wshed, i+1, mask, CMP_EQ);

// Dilate to overlap the watershed line (border)
dilate(mask, mask, Mat());

// Add to the list of masks
masks.push_back(mask);    
}

2. Define a function to check if two regions are adjacent:

bool areAdjacent(const Mat& mask1, const Mat& mask2)
{
    // Get the overlapping area of the two masks
    Mat m;
    bitwise_and(mask1, mask2, m);

    // Compute the size of the overlapping area
    int size = countNonZero(m);

    // If there are more than 10 (for example) overlapping pixels, then the two regions are adjacent
    return (size > 10);
}

3. Compute the adjacency matrix M: if the i-th region and the j-th region are adjacent, then M[i][j] = M[j][i] =1, otherwise they are equal to 0.

Mat M = Mat::zeros(N, N, CV_8U);
for(int i = 0; i < N-1; i++)
    {
        for(int j = i+1; j < N; j++)
        {
            if(areAdjacent(masks[i], masks[j]))
            {
                M.at<uchar>(i,j) = 1;
                M.at<uchar>(j,i) = 1;
            }
        }
    }
    return M;
2013-04-09 08:01:00 -0500 commented answer cvtcolor RGB to XYZ

You misunderstood the question. Please read carefully any questions before answering.

2013-04-09 07:59:33 -0500 received badge  Editor (source)
2013-04-09 07:29:09 -0500 commented answer << operator

More exactly, a << b means a*2^b. (http://en.wikipedia.org/wiki/Binary_shift#Bit_shifts)

2013-04-09 07:18:19 -0500 commented answer << operator

Thanks a lot ! :D

2013-04-09 07:18:01 -0500 received badge  Scholar (source)
2013-04-09 05:10:20 -0500 asked a question << operator

I saw something like this in some source files of OpenCV:

(double)(1<<16)

What does the operator << do?

Thanks.

2013-04-09 04:55:10 -0500 asked a question cvtcolor RGB to XYZ

In modules/imgproc/src/color.cpp, line 678:

static const int coeffs0[] =
        {
            1689,    1465,    739,
            871,     2929,    296,
            79,      488,     3892
        };

Could anybody tell me where this matrix comes from?

I guess it was somehow computed from

image description

and

image description

(see http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html)

but how and why?

Thank you for your help.

Update (posted this as an answer but not allowed because I'm a new member):

OK I hope somebody can have a better answer.

It is actually

image description

(correction: 2^12 instead of 2^16, sorry. In the code, 12 is defined as the shift)

This is for defining cv::RGB2XYZ_i that will be used in the case the input data are integer ('i' for integer).

I guess the main reason for such a conversion is PRECISION (for cv::RGB2XYZ_f, this conversion is not needed, please refer to the source code for details).