another approach :
you can define each line as a cv::Rect
and you can find intersections of boundingRect of each contour by and operator ( & )
.
(note: you don't need to draw lines to find intersections)
the code below shows the way:
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
Mat src,gray;
src = imread(argv[1]);
if(src.empty())
return -1;
cvtColor( src, gray, COLOR_BGR2GRAY );
gray = gray > 127;
vector<Rect> rects_of_lines;
Rect rect_of_line;
rect_of_line.x = src.cols /4;
rect_of_line.y = 0;
rect_of_line.width = 1;
rect_of_line.height = src.rows;
rects_of_lines.push_back( rect_of_line );
rectangle( src, rect_of_line, Scalar(0,0,255));
rect_of_line.x = (src.cols /2) + src.cols /4;
rects_of_lines.push_back( rect_of_line );
rectangle( src, rect_of_line, Scalar(0,0,255));
rect_of_line.x = src.cols /2;
rects_of_lines.push_back( rect_of_line );
rectangle( src, rect_of_line, Scalar(0,0,255));
rect_of_line.x = 0;
rect_of_line.y = src.rows / 2;
rect_of_line.width = src.cols;
rect_of_line.height = 1;
rects_of_lines.push_back( rect_of_line );
rectangle( src, rect_of_line, Scalar(0,0,255));
vector<vector<Point> > contours;
findContours(gray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
Rect _boundingRect;
for (size_t i = 0; i < contours.size(); ++i)
{
if( contourArea( contours[i] ) > 20 )
{
_boundingRect = boundingRect( Mat(contours[i]) );
for( int j = 0; j < rects_of_lines.size(); j++ )
{
Rect intersection = _boundingRect & rects_of_lines[j];
if(intersection.height > 0)
{
// to centralize intersection rect
intersection.x = intersection.x + (intersection.width / 2 );
intersection.y = intersection.y + (intersection.height / 2 );
intersection.width = 1;
intersection.height = 1;
rectangle( src, intersection, Scalar(255,0,0),2);
putText(src, format("x = %d , y = %d",intersection.x,intersection.y),Point(intersection.x,intersection.y),CV_FONT_HERSHEY_COMPLEX,0.5,Scalar(0,255,0));
}
}
}
}
imshow("result", src);
waitKey(0);
return 0;
}
part of result image :
EDIT :
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
Mat src,gray;
src = imread(argv[1]);
if(src.empty())
return -1;
cvtColor( src, gray, COLOR_BGR2GRAY );
gray = gray > 127;
vector<Rect> rects_of_lines;
Rect rect_of_line;
rect_of_line.x = src.cols /4;
rect_of_line.y = 0;
rect_of_line.width = 1;
rect_of_line.height = src.rows;
rects_of_lines.push_back( rect_of_line );
rectangle( src, rect_of_line, Scalar(0,0,255));
rect_of_line.x = (src.cols /2) + src.cols /4;
rects_of_lines.push_back( rect_of_line );
rectangle( src, rect_of_line, Scalar(0,0,255));
rect_of_line.x = src.cols /2;
rects_of_lines.push_back( rect_of_line );
rectangle( src, rect_of_line, Scalar(0,0,255));
rect_of_line.x = 0;
rect_of_line.y = src.rows / 2;
rect_of_line.width = src.cols;
rect_of_line.height = 1;
rects_of_lines.push_back( rect_of_line );
rectangle( src, rect_of_line, Scalar(0,0,255));
vector<vector<Point> > contours;
findContours(gray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
Rect _boundingRect;
gray = 0;
for (size_t i = 0; i < contours.size(); ++i)
{
if( contourArea( contours[i] ) > 20 )
{
drawContours(gray,contours,i,Scalar(255),-1);
_boundingRect = boundingRect( Mat(contours[i]) );
for( int j = 0; j < rects_of_lines.size(); j++ )
{
Mat l = gray(rects_of_lines[j]);
vector<Point> pts;
findNonZero(l,pts);
_boundingRect = boundingRect( pts );
_boundingRect.x = rects_of_lines[j].x;
Rect intersection = _boundingRect & rects_of_lines[j];
if(intersection.height > 0)
{
putText(src, format("x = %d , y = %d",intersection.x,intersection.y),Point(intersection.x,intersection.y ...
(more)
Hi, I have one more question. today I have tried to work with an object that has many oblique. And that was also to be expected that I get such a result. You've probably even a piece of advice for me on how I can get good results here.
Img: http://www.pictureshack.ru/images/209...
result: http://www.pictureshack.ru/images/672...
You know how I can find the overall width of the object? My idea was that we can measure in steps 1 and where y> 388 (388 is the middle), and start counting again at the end where y = 388 finish. but this technique is too complex to calculations, the computer downloads and video shows even more. maybe try again with findсontours? or there may be other suitable function?