Ask Your Question
0

how to detect object and pattern and retrieve the angle of orientation

asked 2017-11-23 09:16:15 -0600

AdhilIqbal gravatar image

I am new to OpenCV I want to know how i can detect an object regardless of its orientation for example. the following is a reference picture

Reference Picture:

image description

2.How to detect the object in difference orientations, how to know the angle of its orientation?

image description

  1. How to detect the same object eventhough it has a spot, and point out where the spot is

image description

My development platform is VS2010 with opencv 2.4.10, much appreciate if you can help me. Color is not essential here.

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
3

answered 2017-11-24 02:31:09 -0600

The already proposed solution will only work when having objects with circles. If not this will fail dramatically. What I suggest is, given these very simple conditions.

  • Perform a segmentation of the object since the background is quite constant. My guess OTSU thresholding will work quite well on a grayscale image.
  • Then do a findContours and get the contour of the object
  • Apply minAreaRect on the found contour to get a rotatedRect associated to this object
  • Retrieve the angle of the rotatedRect and warp it back with warpAffine
edit flag offensive delete link more

Comments

do you have any code, I can start on as i am new to opencv, I am sorry if you find such a request contemptuous... but please help a bro out.

AdhilIqbal gravatar imageAdhilIqbal ( 2017-11-24 07:07:06 -0600 )edit

I'm all for learning, so I'll try to tackle this second approach. Try this code in the meantime: https://docs.opencv.org/2.4/doc/tutor...

sjhalayka gravatar imagesjhalayka ( 2017-11-24 07:59:34 -0600 )edit

@AdhilIqbal I get you are new to OpenCV but I am of the principle that people should learn to use a library, rather than being able to clone some code from others. My approach is quite detailed and gives you the corresponding function you need and can look up in the documentation.

StevenPuttemans gravatar imageStevenPuttemans ( 2017-11-24 08:44:59 -0600 )edit
2

@shalayka - for the future, please do not recommend outdated 2,4 links, but whatever is current (3.3.1 as of now)

berak gravatar imageberak ( 2017-11-24 12:36:21 -0600 )edit

OK, sounds good.

sjhalayka gravatar imagesjhalayka ( 2017-11-24 12:37:11 -0600 )edit

thanks guys for your help, do you have any tuts on how to build binaries for vs2010

AdhilIqbal gravatar imageAdhilIqbal ( 2017-11-25 09:49:31 -0600 )edit

@AdhilIqbal Just wondering... have you tried using the prebuilt 3.3.1 .lib/.h/.dll files from the Win pack (https://opencv.org/releases.html), as an experiment? If so, what errors are you getting?

I notice that you have to add the Windows SDK's x64 compilers (https://www.mathworks.com/matlabcentr...), because VS 2010 doesn't include them by default. Or, are you forced to use the x86 .lib/.h/.dll files, which is why you have to use version 2.x?

sjhalayka gravatar imagesjhalayka ( 2017-11-25 13:53:05 -0600 )edit
0

answered 2017-11-23 11:19:06 -0600

sjhalayka gravatar image

updated 2017-11-24 21:24:08 -0600

To find the angle, use this (simpler) code:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

#include <iostream>
#include <cmath>
using namespace std;


int main(void)
{
    Mat frame = imread("rot.png");

    cvtColor(frame, frame, CV_BGR2GRAY);

    // Find the two circles.
    vector<Vec3f> circles;

    // Parameter values are very much obtained by trial and error
    HoughCircles(frame, // input
        circles, // output
        CV_HOUGH_GRADIENT, // method
        1, // dp
        5, // minimum distance
        5, // param1
        24, // param2
        0, // minimum radius
        13); // maximum radius

    if (circles.size() != 2)
    {
        cout << "Found " << circles.size() << " circles. You should find only two." << endl;
        return 0;
    }

    Point2d centre1(circles[0][0], circles[0][1]);
    Point2d centre2(circles[1][0], circles[1][1]);

    circle(frame, centre1, 10, Scalar(0, 0, 0), 3);
    circle(frame, centre2, 10, Scalar(0, 0, 0), 3);

    // Calculate angle
    double rise = circles[1][1] - circles[0][1];
    double run = circles[1][0] - circles[0][0];
    double slope = rise / run;
    double angle_radians = atan(slope);
    double angle_degrees = angle_radians*180.0 / CV_PI;

    imshow("Hough Circles", frame);

    waitKey(0);
    return 0;
}
edit flag offensive delete link more

Comments

hi, my version of OpenCV does not support opencv_world331.lib as its version is 2.4.10, I can only use this version as my VS is VS2010

AdhilIqbal gravatar imageAdhilIqbal ( 2017-11-24 07:02:35 -0600 )edit

I'm not sure how to link in version 2.x, sorry. Try this URL: https://docs.opencv.org/2.4/doc/tutor...

sjhalayka gravatar imagesjhalayka ( 2017-11-24 07:49:39 -0600 )edit
1

That is actually incorrect @AdhilIqbal you CAN use OpenCV 3.x in VS2010, you just need to build binaries yourself...

StevenPuttemans gravatar imageStevenPuttemans ( 2017-11-24 08:48:27 -0600 )edit
1

What’s stopping you from upgrading your IDE/compiler? Capitalism? If not, then you should have no problem upgrading to the free Visual Studio Community 2017, which is provided at: https://www.visualstudio.com/vs/commu...

It really does pay to use the latest version of things, in terms of dealing with bugs and language compliance. Visual Studio Community 2017 works fine with the OpenCV 3.3.1 prebuilt x64 .lib/.h/.dll files, which are provided in the Win pack from: https://opencv.org/releases.html

sjhalayka gravatar imagesjhalayka ( 2017-11-24 18:23:08 -0600 )edit

@AdhilIqbal . You can get free upgrading to VS2017 and also kindly, upgrade to OpenCV 3.3.1

supra56 gravatar imagesupra56 ( 2017-11-25 08:35:51 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-11-23 09:16:15 -0600

Seen: 13,569 times

Last updated: Nov 24 '17