Skip to content

Instantly share code, notes, and snippets.

@ahmetilgin
Last active August 25, 2016 16:53
Show Gist options
  • Save ahmetilgin/e1f13cd8dd29ef27fbd2f9eab7f2a87c to your computer and use it in GitHub Desktop.
Save ahmetilgin/e1f13cd8dd29ef27fbd2f9eab7f2a87c to your computer and use it in GitHub Desktop.
Trying fill inside polygon
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
bool sayac = false;
int kirmizi=0;
int ct=0;
vector<int>noktax;
vector<int>noktay;
vector<int>baslangicx;
vector<int>baslangicy;
vector<int>bitisx;
vector<int>bitisy;
int fillinside(int merkezx , int merkezy, Mat * ptr,int yon){
vector<int>toplamlar;
int buyuk,kucuk;
for(int i = 0 ; i < noktay.size();i++){
cout<<noktay[i]<<endl;
if(noktay[i]<noktay[i+1]){
buyuk = noktay[i+1];
}
}
for(int i = 0;i < (*ptr).rows;i++){
for(int j = 0 ; j < (*ptr).cols;j++){
if(i >= buyuk -2 && i<= buyuk+2){
break;
}
Vec3b & bgrPixel = (*ptr).at<Vec3b>(Point(j,i));
if(bgrPixel[2]==255){
sayac = true;
kirmizi++;
}
if(bgrPixel[2]==0 && sayac ==true){
ct++;
sayac =false;
}
if(ct % 2 == 1 && bgrPixel[2]==0){
bgrPixel[0]=255;
}
}
ct = 0;
}
for(int i = 0 ; i < (*ptr).rows ;i++){
for(int j = (*ptr).cols ; j> 0 ;j--){
Vec3b & bgrPixel = (*ptr).at<Vec3b>(Point(j,i));
if(bgrPixel[0]==255){
bgrPixel[0]=0;
}
if(bgrPixel[2]==255){
break;
}
}
}
}
void CallBackFunc(int event, int x, int y, int flags, void * ptr)
{ int a,b;
Mat * p = (Mat*)ptr;
int toplamx = 0;
int toplamy = 0;
if(event == EVENT_LBUTTONDOWN){
noktax.push_back(x);
noktay.push_back(y);
for(int i = x - 2; i < x + 2 ;i++){
for(int j = y - 2 ; j < y + 2; j++){
if((i< 2 || i > (*p).rows-2) || (j < 2 || j > (*p).cols -2)){
break;
}
Vec3b & bgrPixel = (*p).at<Vec3b>(Point(i,j));
bgrPixel[0]=0;
bgrPixel[1]=0;
bgrPixel[2]=255;
if((noktax[0] == i && noktay[0]==j)){
ct = ct + 1;
if(ct >= 2){
for(int i = 0 ; i < noktax.size()-1; i++){
a = noktax[i];
b = noktay[i];
Point nokta1 = Point(a,b);
a = noktax[i+1];
b = noktay[i+1];
Point nokta2 = Point(a,b);
line(*p, nokta1, nokta2,Scalar( 0, 0, 255 ),2,16);
//Vec3b & bgrPixel = (*p).at<Vec3b>(Point(b,a));
}
for(int i = 0; i<noktax.size()-1;i++){
cout<<noktax[i]<<" "<<" "<<noktay[i]<<endl;
toplamx = toplamx + noktax[i];
toplamy = toplamy + noktay[i];
}
int sonx = noktax.size()-1;
int sony = noktay.size()-1;
int merkezx = toplamx / sonx;
int merkezy = toplamy / sony;
fillinside(merkezx,merkezy,p,0);
for(int i = merkezx -5 ; i < merkezx+5;i++){
for(int j = merkezy-5;j<merkezy+5;j++){
Vec3b & bgrPixel = (*p).at<Vec3b>(Point(i,j));
bgrPixel[0]=255;
bgrPixel[1]=255;
bgrPixel[2]=255;
}
}
}
}
}
}
}
if(event == EVENT_RBUTTONDOWN){
for(int i = 0 ; i < noktax.size()-1; i++){
a = noktax[i];
b = noktay[i];
Point nokta1 = Point(a,b);
a = noktax[i+1];
b = noktay[i+1];
Point nokta2 = Point(a,b);
line(*p, nokta1, nokta2,Scalar( 0, 0, 255 ),2,16);
//Vec3b & bgrPixel = (*p).at<Vec3b>(Point(b,a));
}
}
if(event == EVENT_MBUTTONDOWN){
noktax.clear();
noktay.clear();
for(int i = 0 ; i < (*p).rows;i++){
for(int j = 0 ; j < (*p).cols;j++){
Vec3b & bgrPixel = (*p).at<Vec3b>(Point(j,i));
bgrPixel[0]=0;
bgrPixel[1]=0;
bgrPixel[2]=0;
}
}
}
}
int main(int argc, char *argv[])
{
Mat image(500, 500, CV_8UC3, Scalar(0,0,0));
while(true){
imshow("Vektor",image);
moveWindow("Vektor",100,100);
setMouseCallback("Vektor", CallBackFunc, &image);
waitKey(1);
}
return 0;
}
I have problem extramums points
https://www.youtube.com/watch?v=jjyXpeyGHe4&feature=youtu.be
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment