Ask Your Question
1

I'm trying to detect edges/walls on an image of a room?

asked 2017-09-26 12:58:15 -0600

Splintersfury gravatar image

updated 2020-12-09 08:31:05 -0600

Basically, I have done up this code which runs a blur to smooth out the noise, and then canny edge followed by houghlinesp and I only drew lines that intersect. This is my result but im missing out some main edges and drew some useless ones too. How do I go about making this more accurate?

Here is my code:

#include <iostream>
#include <string>
#include "opencv2/core/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;

//Rect bound(200,100,200,200);
Rect bound(0,0,700,700);
Mat blur, gray, grad;

Point2f computeIntersect(Vec4i a, Vec4i b)
{
    int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3];
    int x3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3];

    if (float d = ((float)(x1-x2) * (y3-y4)) - ((y1-y2) * (x3-x4)))
    {
        Point2f pt;
        pt.x = ((x1*y2 - y1*x2) * (x3-x4) - (x1-x2) * (x3*y4 - y3*x4)) / d;
        pt.y = ((x1*y2 - y1*x2) * (y3-y4) - (y1-y2) * (x3*y4 - y3*x4)) / d;
        return pt;
    }
    else
        return Point2f(-1, -1);
}

int main( int argc, char** argv )
{
  Mat src = imread("empty2.jpg");
  Mat roi = src(bound);
  Mat blury, grayy, thresh, dst;
  int scale = 1;
  int delta = 0;
  int ddepth = CV_16S;
  Mat grad_x, grad_y;
  Mat abs_grad_x, abs_grad_y;
  Mat finals, finals2;


  //bilateralFilter( roi, blury, 50, 5, BORDER_DEFAULT );
  cvtColor( roi, grayy, COLOR_BGR2GRAY );
  //threshold(grayy,thresh,127,255,THRESH_BINARY);
  //dilate( thresh, thresh, MORPH_RECT );
  //dilate( thresh, thresh, MORPH_RECT );
  //dilate( thresh, thresh, MORPH_RECT );
  //Sobel( grayy, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
  //Sobel( grayy, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
  //convertScaleAbs( grad_x, abs_grad_x );
  //convertScaleAbs( grad_y, abs_grad_y );
  //addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
  Canny( grayy, finals, 50, 150, 3 );
  dilate( finals, finals, MORPH_RECT );
  dilate( finals, finals, MORPH_RECT );
  dilate( finals, finals, MORPH_RECT );
  cvtColor(finals, finals2, COLOR_GRAY2BGR);
  vector<Vec4i> lines;
  HoughLinesP(finals, lines, 1, CV_PI/180, 30, 50, 10 );

  vector<Point2f> corners;
  vector<Vec4i> edgedetect;
  for (int i = 0; i < lines.size(); i++)
  {
      for (int j = i+1; j < lines.size(); j++)
      {
          Point2f pt = computeIntersect(lines[i], lines[j]);
          Vec4i l = lines[i];
          Vec4i h = lines[j];
          if (pt.x >= 0 && pt.y >= 0)
                  edgedetect.push_back(l);
                  edgedetect.push_back(h);
                  for (int r = 0; r < lines.size(); r += 2)
                      for(int k = r + 1; k < lines.size(); k++)
                      {
                          Vec4i e = edgedetect[i];
                          Vec4i f = edgedetect[j];
                          line( finals2, Point(e[0], e[1]), Point(e[2], e[3]), Scalar(0,0,255), 3, LINE_AA);
                          line( finals2, Point(f[0], f[1]), Point(f[2], f[3]), Scalar(0,0,255), 3, LINE_AA);
                      }
      }
  }


  namedWindow("original",WINDOW_AUTOSIZE);
  imshow("original",src);
  namedWindow("test",WINDOW_AUTOSIZE);
  imshow("test", finals2);



  waitKey(0);
  return 0;
}

Original image: Original Detected edges: Detected edges

edit retag flag offensive close merge delete

Comments

try to change 50, 150 in 10, 50 (canny params)

LBerger gravatar imageLBerger ( 2017-09-26 15:12:41 -0600 )edit

Hi, I have changed the parameters to 10,50 and my eclipse took forever to compile haha. Not sure if it'll help

Splintersfury gravatar imageSplintersfury ( 2017-09-26 19:58:01 -0600 )edit
LBerger gravatar imageLBerger ( 2017-09-27 01:00:54 -0600 )edit

Yeap ive read it, was thinking if I should do a preprocessing step to make the edges more defined (especially for the wall on the right)

Splintersfury gravatar imageSplintersfury ( 2017-09-27 06:51:27 -0600 )edit
1

try to use this sample to find good threshold

LBerger gravatar imageLBerger ( 2017-09-27 07:02:12 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-09-27 18:04:02 -0600

Splintersfury gravatar image

Thank you so much, it totally improved my image by adding this portion

Scharr(blurImage,dx,CV_16S,1,0);
Scharr(blurImage,dy,CV_16S,0,1);

but now, i have intersection points that are close to one another, how do I make it such that I would want to find the intersection point with 3 lines touching an area close to it?

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-26 12:58:15 -0600

Seen: 6,932 times

Last updated: Sep 26 '17