1 | initial version |
Here is a very simple approach by comparing the contour and Point in the image. You can adapt this code in what ever language you want! Hope this helps you!
#include <iostream>
#include "opencv2/photo.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/ximgproc/edge_filter.hpp"
#include "opencv2/core.hpp"
#include <iostream>
#include <stdlib.h>
using namespace std;
using namespace cv;
Mat mInput;
static void onMouse( int event, int x, int y, int, void* )
{
if( event != EVENT_LBUTTONDOWN )
return;
Point g_ptMousePoint = Point(x,y);
Mat mGray,mThres,mThresPad,mResult;
mResult= mInput.clone();
RNG rng(12345);
//Convert to Gray Scale
cvtColor(mInput,mGray,COLOR_BGR2GRAY);
//Threshold only darker part
threshold(mGray,mThres,230,255,THRESH_BINARY_INV);
//imshow("mThres Wo Border",mThres);
//Do you need to detect contours at borders? Then add a border
bool detectOpenAreas=true;
if(detectOpenAreas)
{
Rect rectImage(0,0,mInput.size().width,mInput.size().height);
rectangle(mThres,rectImage.tl(),rectImage.br(),Scalar(255),4);
//imshow("mThres",mThres);
}
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Find contours
findContours( mThres.clone(), contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
for( size_t i = 0; i< contours.size(); i++ )
{
//Filter out the Text contours
if(contourArea(contours[i])>100)
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( mResult, contours, (int)i, color, 1, 8, hierarchy, 0, Point() );
}
}
//imshow("Result",mResult);
Mat mCMask(mInput.size(),CV_8UC1,Scalar(0)),mPtMask(mInput.size(),CV_8UC1,Scalar(0)),mResultMask(mInput.size(),CV_8UC1,Scalar(0));
for( size_t i = 0; i< contours.size(); i++ )
{
mCMask.setTo(0);
mPtMask.setTo(0);
//Filter out the Text contours by Area & Make sure it does not have any child!
//you add few more filter to this loop like distance from contour mass centre etc...
if(contourArea(contours[i])> 100 && hierarchy[i][3] != -1 )
{
//Check if Point line on the contour by creating a Mask
drawContours( mCMask, contours, (int)i, Scalar(255), -1, 8, hierarchy, 0, Point() );
circle(mPtMask,g_ptMousePoint,1,Scalar(255),-1);
bitwise_and(mCMask,mPtMask,mResultMask);
//Check for Non Zero Pixels if > 1 match found
if(countNonZero(mResultMask)>=1)
{
drawContours( mResult, contours, (int)i, Scalar(0,255,0), -1, 8, hierarchy, 0, Point() );
}
}
}
circle(mResult,g_ptMousePoint,2,Scalar(255),-1);
imshow("Result",mResult);
}
int main( int, char** )
{
mInput= imread("input.jpg",1);
namedWindow("Result",WINDOW_AUTOSIZE);
imshow("Result",mInput);
setMouseCallback( "Result", onMouse, 0 );
cout<<"Please Pick a point!";
for(;;)
{
int c = waitKey(0);
if( c == 27 )
{
cout << "App Exit ...\n";
break;
}
}
destroyAllWindows();
waitKey(0);
return 0;
}