Ask Your Question

tarmizi_adam2005's profile - activity

2017-09-18 01:27:26 -0500 received badge  Notable Question (source)
2017-01-06 07:32:07 -0500 received badge  Popular Question (source)
2014-12-03 01:15:30 -0500 asked a question Write data to text file

Hi, I am trying to read a ".csv" file that contains double valued data like this:

1   0.0922279   0.0977822   0.0567845   0.0308806   0.0491464   0.0436114   
1   0.277745    0.130502    0.118348    0.0365346   0.0669739   0.00924114  
1   0.137181    0.140128    0.116455    0.122226    0.151289    0.129282    
1   0.182825    0.0810142   0.0811148   0.0588809   0.147732    0.0410181

The above is just an example. The real ".csv" file is 269 row by 3780 column (269 x 3780).

I try to read the csv file, put the values in a Mat and then write that Mat to another file called "data.txt".

However, when I open the data.txt nothing is written except a value of 1. This is obviously wrong as, the data to the file would have contained the same values of the original "csv" file. This is the code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/ml/ml.hpp> // for CvMLData
#include <iostream>
#include <fstream>

using namespace std;
using namespace cv;

int main()
{

    ofstream data("data.txt"); // 
    CvMLData mlData;
    mlData.read_csv("HOG_data.csv"); // File containing 269 row by 3780 column

    if(mlData.get_values() == nullptr)
    {
        cout << "Unable to open file !" << endl;
        return 1;
    }

    //const Mat m = mlData.get_values();  <---Also tried this;
    const CvMat *m = mlData.get_values();

    Mat dataset;
    dataset = m;

    for(int i = 0; i < dataset.rows; i++)
    {
        for(int j  =0; j < dataset.cols; j++)
        {
            data << dataset.at<float>(i,j) << " ";
        }

        data << endl;
    }

    waitKey();
    return 0;
}

Any suggestions or something i've probably overlooked ?

2014-11-18 00:36:22 -0500 asked a question Computing Histogram of Oriented Gradients

Hi,

In order to understand the Histogram of Oriented Gradients (HOG) features proposed by Dalal and Triggs, I have opted to hard code it without using openCV's HOGDescriptor. Here is some relevant code that i tried to implement HOG:

void hog::hog_process(Mat &direction) // direction is the gradient direction matrix
{

  Size blockSize(8,8);
  Size cellSize(4,4);

  vector<Mat> block;
  Size s =  direction.size();
  Mat cell_eightPx;
  Mat cell_fourPx;

// Essentially split the image into 8 by 8 cells. HOG processing of each block should be essiantially here.
// the 8 by 8 cells are then split again into 4 by 4 cells
   for(int col = 0; col < direction.rows; col += blockSize.height)
    {
        for(int row = 0; row < direction.cols; row += blockSize.width)
        {
            Rect rect= Rect(row, col,blockSize.width,blockSize.height);

            cell_eightPx = Mat(direction,rect); // Get a 8 by 8 cell from the gradient direction matrix.

            //**********COMPUTE 9 bin gradient direction histogram of the 8 by 8 cell here !!! ****
            for(int i = 0; i < cell_eightPx.rows; i += cellSize.height)
            {
                for(int j  = 0; j < cell_eightPx.cols; j += cellSize.width)
                {
                    Rect rect_fourPx = Rect(i,j,cellSize.width,cellSize.height); // 4 by 4 rectangle size
                    cell_fourPx = Mat(cell_eightPx,rect_fourPx); // create a 4 by 4 cell from gradient direction matrix (cell_eightPx)
                    gradientHist(cell_fourPx); // Calculate gradient histogram of the 4 by 4 cell. (Function call)
                    cell_fourPx.deallocate(); // clear the cell.
                }
            }
        }
    }
}

Here is the function gradientHist() to calculate the 9 bin histogram for the HOG features:

void hog::gradientHist(Mat &cell_fourPx)
{
    Mat hist;
    ofstream feature("Hist_Values.csv",std::ofstream::app);

    // create a 9 bin histogram with range from 0 t0 180 for HOG descriptors.
    int histSize = 9;
    float range[] = {0,180};
    const float *histRange = {range};
    bool uniform = true;
    bool accumulate = false;

    calcHist(&cell_fourPx, 1, 0,Mat(),hist, 1, &histSize, &histRange, uniform, accumulate); //Calculate the 9 bin histogram.

    normalize(hist, hist, 0, 0, NORM_MINMAX, -1, Mat());


    for(int i = 0; i < histSize; i++)
    {
       feature << hist.at<float>(i) << ","; // Output the value of HOG to a csv file             
    } 
}

However, OpenCV is telling me:

unsupported format or combination of formats () in calcHist, file....line 1219....histogram.cpp:1219:
error(-210) in function calcHist

perhaps I've overlooked something ? Any Suggestions/Ideas would be appreciated. Thanks in advance...

2014-11-17 01:50:42 -0500 asked a question Problem with computing Histogram of Oriented Gradients (HOG)

Hi,

In order to understand the mechanics of Histogram of Oriented Gradients proposed by Dalal and Triggs, I have tried to hard code the HOG algorithm. However I am having some problems. It says

OpenCV error: Unsupported format or combination of formats () in calcHist....
              error: (-210) in function calcHist

I am not quite sure what format of the function argument that is not right. This is the code:

void hog::hog_process(Mat &direction) // direction is a 128 by 64 gradient direction matrix.
{

  Size blockSize(8,8);
  Size cellSize(4,4);

  vector<Mat> block;
  Size s =  direction.size();
  Mat cell_eightPx;
  Mat cell_fourPx;

// Essentially split the image into 8 by 8 cells. HOG processing of each block should be essiantially here.
// the 8 by 8 cells are then split again into 4 by 4 cells
   for(int col = 0; col < direction.rows; col += blockSize.height)
    {
        for(int row = 0; row < direction.cols; row += blockSize.width)
        {
            Rect rect= Rect(row, col,blockSize.width,blockSize.height);

            cell_eightPx = Mat(direction,rect); // Get a 8 by 8 cell from the gradient direction matrix.

            //**********COMPUTE 9 bin gradient direction histogram of the 8 by 8 cell here !!! ****
            for(int i = 0; i < cell_eightPx.rows; i += cellSize.height)
            {
                for(int j  = 0; j < cell_eightPx.cols; j += cellSize.width)
                {
                    Rect rect_fourPx = Rect(i,j,cellSize.width,cellSize.height); // 4 by 4 rectangle size
                    cell_fourPx = Mat(cell_eightPx,rect_fourPx); // create a 4 by 4 cell from gradient direction matrix (cell_eightPx)
                    gradientHist(cell_fourPx); // Function call, Calculate gradient histogram of the 4 by 4 cell.
                    cell_fourPx.deallocate(); // clear the cell.
                }
            }
        }
    }
}

After partitioning the image with 4 by 4 cells as the above code the 9 bin histogram calculation is as follows:

void hog::gradientHist(Mat &cell_fourPx)
{
    Mat hist;
    ofstream feature("Hist_Values.csv",std::ofstream::app);

    // create a 9 bin histogram with range from 0 t0 180 for HOG descriptors.
    int histSize = 9;
    float range[] = {0,180};
    const float *histRange = {range};
    bool uniform = true;
    bool accumulate = false;

    calcHist(&cell_fourPx, 1, 0,Mat(),hist, 1, &histSize, &histRange, uniform, accumulate); //Calculate the 9 bin histogram.

    normalize(hist, hist, 0, 0, NORM_MINMAX, -1, Mat());

    for(int i = 0; i < histSize; i++)
    {
        feature << hist.at<float>(i) << ","; // Output the value of HOG to a csv file         
    }

}

Any suggestions/ directions are very much welcomed. I may have overlooked something. Thanks in advance.

2014-10-16 02:13:25 -0500 received badge  Scholar (source)
2014-10-16 02:12:57 -0500 commented answer Computing gradient direction of image

Hi rwong,

Thank you very much for your reply and suggestions. I understand it now. After making some changes as you suggested I have managed to correct the problems. Thank again.

2014-10-15 03:14:56 -0500 asked a question Computing gradient direction of image

Hi,

I am trying to calculate the gradient direction of an image.

I tried the following function:

Mat computeArcTan(Mat &dst1, Mat &dst2)
{
   Mat angle;//(dst1.rows,dst1.cols,CV_32F);
   ofstream myFile("angle.csv");
   const double pi = 3.14159;

   for(int row = 0; row < dst1.rows; row++)
   {
       for(int col = 0; col < dst1.cols; col++ )
       {
           //angle.at<uchar>(row,col)=(double)atan2(dst1.at<uchar>(row,col),dst2.at<uchar>(row,col)*180/pi);
           angle.push_back((double)atan2(dst1.at<uchar>(row,col),dst2.at<uchar>(row,col))*180/pi);
           myFile << angle.at<double>(row,col) <<",";
       }

            myFile << endl;
   }
   return angle;
}

the dst1 and dst2 are the gradient of x and y direction. However, I believe I am not getting the correct direction values. The values printed in the csv file is a repetition of values of 0, 18.435, 45 and 71.3872. When i checked with matlab, it is totally wrong. I know openCV has the function cartToPolar() but I'm trying to hard code it just to understand the process behind it.

Any Ideas or direction/suggestion on what am i doing wrong ?

thanks,