3

I need help to get the coordinates of the lines that HoughLines produced and extract it to an output file (Notepad, Excel, or any other output files).

I managed to obtain the lines and based on my research on this site I found a post that tells how to obtain the coordinates, however due to my limited understanding I could not get the code to run along my original Hough code and get the intersection points coordinate onto an output file.

Here is my original Hough code:

#pragma once
#include <C:\OpenCV2.2\include\opencv\cv.h>
#include <C:\OpenCV2.2\include\opencv\highgui.h>
#include <C:\OpenCV2.2\include\opencv2\core\core.hpp>
#include <C:\OpenCV2.2\include\opencv2\imgproc\imgproc.hpp>
#include <C:\OpenCV2.2\include\opencv2\highgui\highgui.hpp>

#include <stdio.h>
#include <math.h>

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char* argv[])

{

cv::Mat dst_img, gray_img, contour_img, contrast_img;
cv::Mat src_img = cv::imread("C:\\Frame-1.bmp"); //Source image path
dst_img = src_img.clone();
dst_img.convertTo(contrast_img, -1, 1.5, 0);
cv::cvtColor(contrast_img, gray_img, CV_BGR2GRAY);
cv::Canny(gray_img, contour_img, 75, 225, 3);

vector<Vec2f> lines_;

HoughLines(contour_img, lines_, 1, CV_PI/180, 200);

for( size_t i = 0; i < lines_.size(); i++ )
{
float rho = lines_[i][0];
float theta = lines_[i][1];

double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;

Point pt1(cvRound(x0 + 1000*(-b)),
    cvRound(y0 + 1000*(a)));
Point pt2(cvRound(x0 - 1000*(-b)),
    cvRound(y0 - 1000*(a)));

cv::clipLine(gray_img.size(), pt1, pt2);

if(!dst_img.empty())
    line( dst_img, pt1, pt2, Scalar(0, 0, 255), 1, CV_AA);

cv::imwrite("result.bmp", dst_img);
}

namedWindow("My Image");
imshow("My Image", dst_img);

waitKey(0);

return 0;

}

And here is the link to the code that I wanted to put into my original code:

I am struck at finding the point of intersection of most lines in an image

Right now my original code draws Houghlines and exports the image (as result.bmp) and at the same time displays the image on a new window.

I just need to figure how and where to put the new code plus an additional code to obtain the raw data of the coordinates onto an output file like Notepad, most desirably in the same folder as result.bmp (the name of the output file could be anything, just needed it to be there).

Sorry if this question sounds like a beginner`s question (I really am) and any help is much appreciated. Many thanks in advance.

Additional information: I am using OpenCV 2.2 and Microsoft Visual Studio Academic 2010

EDIT: This is all three codes (Hough, Coordinate extraction, and Exporting data to notepad) but as a complete beginner I don`t know to make them all work in a single code.

#pragma once
#include <C:\OpenCV2.2\include\opencv\cv.h>
#include <C:\OpenCV2.2\include\opencv\highgui.h>
#include <C:\OpenCV2.2\include\opencv2\core\core.hpp>
#include <C:\OpenCV2.2\include\opencv2\imgproc\imgproc.hpp>
#include <C:\OpenCV2.2\include\opencv2\highgui\highgui.hpp>

#include <stdio.h>
#include <math.h>

#include <opencv2/opencv.hpp>
#include <iostream>

#define PointMinusPoint(P,Q,R)      {(P).x = (Q).x - (R).x; (P).y = (Q).y - (R).y;}
#define PointCross(P,Q)             (((P).x*(Q).y)-((P).y*(Q).x))
#define SIGN(X)             (((X)>=0)? 1:-1 )
#define ABS(a)              ((a) >= 0 ? (a) : (-(a)))
#define ROUND(a)            ((SIGN(a)) * ( ( int )( ABS(a) + 0.5 ) ) ) 

typedef struct{
   int x,y;
} MYintPOINT;

typedef struct {
    MYintPOINT  pStart;
    MYintPOINT  pEnd;
} MyLine;

using namespace std;
using namespace cv;

int main(int argc, char* argv[])

{

cv::Mat dst_img, gray_img, contour_img, contrast_img;
cv::Mat src_img = cv::imread("C:\\Frame-1.bmp"); //Source image path
dst_img = src_img.clone();
dst_img.convertTo(contrast_img, -1, 1.5, 0);
cv::cvtColor(contrast_img, gray_img, CV_BGR2GRAY);
cv::Canny(gray_img, contour_img, 75, 225, 3);

vector<Vec2f> lines_;

HoughLines(contour_img, lines_, 1, CV_PI/180, 200);

for( size_t i = 0; i < lines_.size(); i++ )
{
float rho = lines_[i][0];
float theta = lines_[i][1];

double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;

Point pt1(cvRound(x0 + 1000*(-b)),
    cvRound(y0 + 1000*(a)));
Point pt2(cvRound(x0 - 1000*(-b)),
    cvRound(y0 - 1000*(a)));

cv::clipLine(gray_img.size(), pt1, pt2);

if(!dst_img.empty())
line( dst_img, pt1, pt2, Scalar(0, 0, 255), 1, CV_AA);

cv::imwrite("result.bmp", dst_img);
}

int findLinesIntersectionPoint(const MyLine*l1, const MyLine*l2, MYintPOINT *res){
    MYintPOINT  p  = l1->pStart;
    MYintPOINT  dp;
    MYintPOINT  q  = l2->pStart;
    MYintPOINT  dq;
    MYintPOINT  qmp;            // q-p
    int         dpdq_cross;     // 2 cross products
    int         qpdq_cross;     // dp with dq,  q-p with dq
    float       a;

PointMinusPoint(dp,l1->pEnd,l1->pStart);
PointMinusPoint(dq,l2->pEnd,l2->pStart);
PointMinusPoint(qmp,q,p);

dpdq_cross = PointCross(dp,dq);
if (!dpdq_cross){
    // Perpendicular Lines
    return 0;
}

qpdq_cross = PointCross(qmp,dq);
a = (qpdq_cross*1.0f/dpdq_cross);

res->x = ROUND(p.x+a*dp.x);
res->y = ROUND(p.y+a*dp.y);
return 1;
}

string FileName= FileName_S.c_str();
string::size_type Extension = FileName_S.find_last_of('.');                  // Find extension point

Mat mInputImg;
mInputImg= imread(FileName_S,1);
Size szInput= mInputImg.size();
const string DestinationFileName = FileName_S.substr(0, Extension) + "_ImageData.csv";   // Form the new name with container
ofstream myfile (DestinationFileName.c_str());
if (!myfile.is_open())
{
    MessageBox(L"Unable to Open File");
}
string Text= format("Row, Col , Pixel Data,\n");
myfile << Text;

for (int Row = 0; Row < szInput.height; Row++)
{
    for (int Col = 0; Col < szInput.width; Col++)
    {
        string Text= format("%d , %d , %d",Row,Col,mInputImg.at<uchar>(Row,Col));
        myfile << Text;
        myfile << "\n";
    }
}
myfile.close();

namedWindow("My Image");
imshow("My Image", dst_img);

waitKey(0);

return 0;

}
Community
  • 1
  • 1

1 Answers1

1

It is very easy to export your data to Notepad or excel file. Here is the code to Export a mat to a csv File. Format your String with your desired data to export your desired data.

/*Exporting a Mat to Excel(.csv) file*/
    string FileName= FileName_S.c_str();
    string::size_type Extension = FileName_S.find_last_of('.');                  // Find extension point

    Mat mInputImg;
    mInputImg= imread(FileName_S,1);
    Size szInput= mInputImg.size();
    const string DestinationFileName = FileName_S.substr(0, Extension) + "_ImageData.csv";   // Form the new name with container
    ofstream myfile (DestinationFileName.c_str());
    if (!myfile.is_open())
    {
        MessageBox(L"Unable to Open File");
    }
    string Text= format("Row, Col , Pixel Data,\n");
    myfile << Text;

    for (int Row = 0; Row < szInput.height; Row++)
    {
        for (int Col = 0; Col < szInput.width; Col++)
        {
            string Text= format("%d , %d , %d",Row,Col,mInputImg.at<uchar>(Row,Col));
            myfile << Text;
            myfile << "\n";
        }
    }
    myfile.close();
Balaji R
  • 1,805
  • 22
  • 41
  • Hi, thank you for the prompt reply. However I still could not figure out how to link the code above with the Houghline code and the Coordinate extraction code.I would like all three to be in one cohesive code but unfortunately I always get an undefined identifier error. I know it`s plain stupid to just copy and paste all three codes together but I need all of them to work together. – venopolymede Dec 15 '14 at 08:54
  • What have you tried? where it is failing? Can you show us some updated code? – Balaji R Dec 15 '14 at 09:20
  • I've updated my question with the latest source code I am currently using. I know the definition names and brackets are most likely off but I have no idea how to make them all work together (what files I haven`t define, what and where should I replace file names etc.) Any help is very much appreciated. – venopolymede Dec 15 '14 at 09:37
  • Sorry for not knowing much about this site. I just wanted to get the codes to work. If I offended you in any way, I sincerely apologize. – venopolymede Dec 15 '14 at 10:00
  • ha ha its not about offending! It is about trying a little by your own. You cannot expect others to do your complete job! So in future you can try to avoid these! Again take your time to fix your code!Happy coding! – Balaji R Dec 15 '14 at 10:25
  • Apologies but I still got errors on my code... if any help is possible I would greatly appreciate it. Having too much undefined errors... – venopolymede Dec 17 '14 at 03:59