Extract coordinate of intersection of Hough lines and get data into Notepad or Excel

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

I was able to get the strings and based on my research on this site I found a post that explains how to get the coordinates, however due to my limited understanding I was unable to get the code to work from my Hough source code and get the coordinates of the intersection points into the 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 would like to put in the source code:

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

Currently my source code is drawing Houghlines and exporting the image (as result.bmp) and simultaneously displaying the image in a new window.

I just need to figure out how and where to put the new code plus some additional code to get the raw coordinate data into an output file like Notepad, most preferably in the same folder as result.bmp (the output file name can be anything, just need to be there).

Sorry if this sounds like a newbie question (I really am) and any help is greatly appreciated. Thank you very much in advance.

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

EDIT: These are all three codes (Hough, Coordinate extract and Exporting data to notepad), but as a complete newbie, I don't know how to get them to work in the same 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;

}

      

+3


source to share


1 answer


It is very easy to export data to notepad or excel file. Here is the code to export the math to a csv file. Format your string with the data you want to export the data you want.



/*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();

      

+1


source







All Articles