Ask Your Question

dinhtuyen's profile - activity

2020-03-22 10:35:44 -0600 received badge  Notable Question (source)
2017-06-14 11:25:29 -0600 received badge  Popular Question (source)
2016-07-05 03:18:57 -0600 received badge  Teacher (source)
2015-04-02 09:44:52 -0600 asked a question round, sin, cos, sqrt, atan2, mod, floor... functions in OpenCV!

In matlab we have many function to perform an procedure for per element of matrix, example round, sin, cos, sqrt, atan2, mod, floor ......... here is an example:

A = [1.23 2.34 3; 5 6.78 7.89; 4.111 34.56 56.78]

A =

1.2300    2.3400    3.0000
5.0000    6.7800    7.8900
4.1110   34.5600   56.7800

B = round(A)

B =

 1     2     3
 5     7     8
 4    35    57

can you please give me some recommend to perform those function above.

example: cv::OutputArray round(cv::InputArray _src) { }

thank alot!

2015-03-29 10:05:55 -0600 received badge  Scholar (source)
2015-03-29 10:05:49 -0600 received badge  Supporter (source)
2015-03-29 03:26:36 -0600 received badge  Editor (source)
2015-03-28 21:28:02 -0600 answered a question extract a mat which have address in a mat from another mat

Thank you for your help! I have written a function to "extract a mat which have address in a mat from another mat":

cv::Mat extract(cv::Mat SRC, cv::Mat IDX)
{
    cv::Mat src = SRC.clone();
    cv::Mat idx = IDX.clone();
    cv::Mat dst = cv::Mat(idx.size(), src.type());
    src = src.t();
    idx.convertTo(idx, 4);
    for (int i = 0; i < idx.total(); i++)
    {
    //  dst.at<>(i) = src.at<>(idx.at<int>(i));
        switch (src.type())
            {
            case 0:
                dst.at<uchar>(i) = src.at<uchar>(idx.at<int>(i));
                break;
            case 1:
                dst.at<char>(i) = src.at<char>(idx.at<int>(i));
                break;
            case 2:
                dst.at<ushort>(i) = src.at<ushort>(idx.at<int>(i));
                break;
            case 3:
                dst.at<short>(i) = src.at<short>(idx.at<int>(i));
                break;
            case 4:
                dst.at<int>(i) = src.at<int>(idx.at<int>(i));
                break;
            case 5:
                dst.at<float>(i) = src.at<float>(idx.at<int>(i));
                break;
            default:
                dst.at<double>(i) = src.at<double>(idx.at<int>(i));
                break;
            }
    }
    return dst;
}

and call it in main function:

cv::Mat src = (cv::Mat_<double>(3, 3) << 4, 9, 8, 10, 3, 7, 5, 8, 4);
cv::Mat idx = (cv::Mat_<int>(2, 2) << 1, 2, 3, 4);

cv::Mat dst = extract(src, idx);
cout << "src = " << endl << " " << src << endl;
cout << "idx = " << endl << " " << idx << endl;
cout << "dst = " << endl << " " << dst << endl;

and result is similar to your program but i don't know my function had Optimized??? Can you please improve my function? Thank a lot !!!!

2015-03-27 20:06:21 -0600 received badge  Enthusiast
2015-03-23 10:39:00 -0600 answered a question Import Matlab Matrix to OpenCv

I have writen a function in matlab to write a matrix to a file .yml. after in opencv i read this file!!! Matlab function:

function TH0001_Matlab2OpenCV( variable, fileName, flag)

[rows cols] = size(variable);

% Beware of Matlab's linear indexing
variable = variable';

% Write mode as default
if ( ~exist('flag','var') )
    flag = 'w'; 
end

if ( ~exist(fileName,'file') || flag == 'w' )
    % New file or write mode specified 
    file = fopen( fileName, 'w');
    fprintf( file, '%%YAML:1.0\n');
else
    % Append mode
    file = fopen( fileName, 'a');
end

% Write variable header
fprintf( file, '    %s: !!opencv-matrix\n', inputname(1));
fprintf( file, '        rows: %d\n', rows);
fprintf( file, '        cols: %d\n', cols);
fprintf( file, '        dt: f\n');
fprintf( file, '        data: [ ');

% Write variable data
for i=1:rows*cols
    fprintf( file, '%.6f', variable(i));
    if (i == rows*cols), break, end
    fprintf( file, ', ');
    if mod(i+1,4) == 0
        fprintf( file, '\n            ');
    end
end

fprintf( file, ']\n');
fclose(file);

%==============================================================

% call this function in matlab

TH0001_Matlab2OpenCV( img11, 'img01.yml', 'img01');

//======================================================= //Function read file .yml in opencv

cv::Mat LoadDatafromymlfile(char* ymlfilename, char* varriablestring)
{
    cv::Mat temp;
    cv::FileStorage fs(ymlfilename, cv::FileStorage::READ);
    fs[varriablestring] >> temp;
    fs.release();
    return temp;
}

// Call this function

char *filenameyml = "D://img01.yml"; //location of file .yml
cv::Mat img11 = LoadDatafromymlfile(filenameyml, "img11");
2015-03-22 05:07:21 -0600 asked a question extract a mat which have address in a mat from another mat

Example in matlab:

A =

     4     9     8
    10     3     7
     5     8     4

>> b = [2 3; 4 5]

b =

     2     3
     4     5

>> A(b)

ans =

    10     5
     9     3

how to find A(b) in OpenCV???? Thank!!!!