1 | initial version |
I have add some lines to this program
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
map <int ,vector<Point > > arrayNeighbour;
int thresh = 100,valRef=255;
int max_thresh = 255;
RNG rng(12345);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
vector<vector<Point> >hull( contours.size() );
Mat imThresh;
/** @function thresh_callback */
void thresh_callback(int, void* )
{
Mat src_copy = src.clone();
Mat threshold_output;
/// Detect edges using Threshold
threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
imThresh =threshold_output.clone();
/// Find contours
findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
/// Find the convex hull object for each contour
hull.resize( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{ convexHull( Mat(contours[i]), hull[i], false ); }
/// Draw contours + hull results
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
drawContours( drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
}
/// Show in a window
namedWindow( "Hull demo", CV_WINDOW_AUTOSIZE );
imshow( "Hull demo", drawing );
}
void InitMapDist(Mat src)
{
for (int i=-src.rows/2;i<src.rows/2;i++)
for (int j = -src.cols / 2; j < src.cols / 2; j++)
{
if (arrayNeighbour.find(i*i + j*j) == arrayNeighbour.end())
{
vector<Point> v;
arrayNeighbour.insert(make_pair(i*i+j*j,v));
}
arrayNeighbour[i*i + j*j].push_back(Point(i,j));
}
}
int LookForNearest(Mat imgThresh, Mat src, int l, int c, int nb)
{
int nbFound=0;
vector<int> pixel;
int dist=1;
map<int,vector<Point> >::iterator it=arrayNeighbour.begin();
float mean=0;
while (nbFound < nb && it!=arrayNeighbour.end())
{
for (int i = 0; i < it->second.size(); i++)
{
Point p=Point(l,c)+it->second[i];
if (p.x >= 0 && p.x < imgThresh.cols && p.y >= 0 && p.y < imgThresh.rows)
{
if (imgThresh.at<uchar>(p.y, p.x) == valRef)
{
nbFound++;
mean+=src.at<uchar>(p.y, p.x);
}
}
}
it++;
}
return mean/nbFound;
}
int main( int argc, char** argv )
{
/// Load source image and convert it to gray
src_gray = imread( "c:/Users/Laurent.PC-LAURENT-VISI/Downloads/14425608212127298.jpeg", CV_LOAD_IMAGE_GRAYSCALE );
blur( src_gray, src_gray, Size(3,3) );
InitMapDist(src_gray);
waitKey();
/// Create Window
char* source_window = "Source";
namedWindow( source_window, CV_WINDOW_AUTOSIZE );
imshow( source_window, src_gray );
createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
thresh_callback( 0, 0 );
waitKey();
Mat img=255*Mat::ones(src_gray.size(),CV_8UC1);
Mat res=Mat::zeros(src_gray.size(),CV_8UC1);
double areaMax=0;
int iMax=0;
for (int i = 1; i<contours.size();i++)
{
double area=contourArea(contours[i],false );
if (area>areaMax)
{
iMax=i;
areaMax=area;
}
}
drawContours(img,hull, iMax,0,CV_FILLED);
imshow("HuLL",img);
imshow("imThresh",imThresh);
for (int i = 0; i<img.rows; i++)
{
for (int j=0;j<img.cols;j++)
if (img.at<uchar>(i,j)==0)
if (imThresh.at<uchar>(i,j)==valRef)
res.at<uchar>(i, j) = src_gray.at<uchar>(i,j);
else
res.at<uchar>(i, j) = LookForNearest(imThresh,src_gray,i,j,5);
}
imshow("res",res);
waitKey();
return(0);
}