diff --git a/Yash/Oops_assgn/Search algo/BFS.cpp b/Yash/Oops_assgn/Search algo/BFS.cpp new file mode 100644 index 0000000..d8e8f0c --- /dev/null +++ b/Yash/Oops_assgn/Search algo/BFS.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace cv; +using namespace std; +struct Pixel +{ + int x,y; +}; + +int main( int argc, char** argv ) +{ + + Mat image,grey,bin; + int **visited,i,j,a,b,k,l,count=0,s=0,ht,wd; + queue pix_queue; + // cout << "Success" << s; s++; + // getchar(); + + image = imread("/home/yash/fuerte_workspace/sandbox/beginner_tutorials/algo/img.png", CV_LOAD_IMAGE_COLOR); + + if(! image.data ) + { + cout << "Could not open or find the image" << std::endl ; + return -1; + } + ht=image.rows; + wd=image.cols; + cvtColor(image,grey,CV_BGR2GRAY); + imwrite("grey_img.png",grey); + namedWindow( "Display window", WINDOW_AUTOSIZE ); + imshow( "Display window", grey ); + + waitKey(0); + threshold(grey, bin, 100, 255, CV_THRESH_BINARY); + + imshow("Display window",bin); + waitKey(0); + visited=new int* [ht]; + for(i=0;i<=ht;i++) + visited[i]=new int [wd]; + for(i=0;i<=ht;i++) + for(j=0;j<=wd;j++) + visited[i][j]=-1; + ht--; wd--; + for(i=1;i<603;i++) + for(j=1;j<1429;j++) + { + if(bin.at(i,j)==255) + { + if(visited[i][j]==-1) + { + count++; + Pixel pix; + Pixel temp; + pix.x=i; + pix.y=j; + pix_queue.push(pix); + temp=pix_queue.front(); + a=temp.x; + b=temp.y; + printf("a=%d,b=%d",a,b); + getchar(); + while(!pix_queue.empty()) + { + + Pixel t; + t=pix_queue.front(); + a=t.x; + b=t.y; + pix_queue.pop(); + for(k=a-1;k<=a+1;k++) + for(l=b-1;l<=b+1;l++) + { + if(bin.at(k,l)==255 && visited[k][l]==-1) + { + printf("k=%d,l%d",k,l); + getchar(); + pix.x=k; + pix.y=l; + pix_queue.push(pix); + visited[k][l]=count; + } + } + visited[a][b]=count; + } + printf("got out"); + getchar(); + + } + + } + } + + cout< +#include +#include +using namespace std; +struct node +{ + int i; + int cweight; +}; + +class CompareWeight +{ + public: + bool operator() (node& lhs, node& rhs) + { + if (lhs.cweight>rhs.cweight) return true; + return false; + } +}; + +int main() +{ + int j,l,n,s,e,present; + node start,current,next; + n=8; + int k[n][n], past[n],flag[n]; + for(j=0;j, CompareWeight> pq; + + cout<<"Start="; + cin>>s; + cout<<"End="; + cin>>e; + for(j=0;jcurrent.cweight||flag[current.i]==-1) + { + past[current.i]=current.cweight; + flag[current.i]=1; + } + for(j=0;j=next.cweight||flag[next.i]==-1) + pq.push(next); + } + } + cout<<"Success"; + } +present=e; +l=past[e]; +cout<<'\n'<<"Backtrace End("< +#include +#include +#include +#include +#include +#include +#include +using namespace cv; +using namespace std; +struct Pixel +{ + int x,y; +}; + +int main( int argc, char** argv ) +{ + + Mat image,grey,bin; + int **visited,i,j,a,b,k,l,count=0,s=0,ht,wd; + stack pix_stack; + // cout << "Success" << s; s++; + // getchar(); + + image = imread("/home/yash/fuerte_workspace/sandbox/beginner_tutorials/algo/img.png", CV_LOAD_IMAGE_COLOR); + + if(! image.data ) + { + cout << "Could not open or find the image" << std::endl ; + return -1; + } + ht=image.rows; + wd=image.cols; + cvtColor(image,grey,CV_BGR2GRAY); + imwrite("grey_img.png",grey); + namedWindow( "Display window", WINDOW_AUTOSIZE ); + imshow( "Display window", grey ); + + waitKey(0); + threshold(grey, bin, 100, 255, CV_THRESH_BINARY); + + imshow("Display window",bin); + waitKey(0); + visited=new int* [ht]; + for(i=0;i<=ht;i++) + visited[i]=new int [wd]; + for(i=0;i<=ht;i++) + for(j=0;j<=wd;j++) + visited[i][j]=-1; + ht--; wd--; + for(i=1;i<603;i++) + for(j=1;j<1429;j++) + { + if(bin.at(i,j)==255) + { + if(visited[i][j]==-1) + { + count++; + Pixel pix; + Pixel temp; + pix.x=i; + pix.y=j; + pix_stack.push(pix); + temp=pix_stack.top(); + a=temp.x; + b=temp.y; + printf("a=%d,b=%d",a,b); + getchar(); + while(!pix_stack.empty()) + { + + Pixel t; + t=pix_stack.top(); + a=t.x; + b=t.y; + pix_stack.pop(); + for(k=a-1;k<=a+1;k++) + for(l=b-1;l<=b+1;l++) + { + if(bin.at(k,l)==255 && visited[k][l]==-1) + { + printf("k=%d,l%d",k,l); + getchar(); + pix.x=k; + pix.y=l; + pix_stack.push(pix); + visited[k][l]=count; + } + } + visited[a][b]=count; + } + printf("got out"); + getchar(); + + } + + } + } + + cout< myqueue1; + queue myqueue2; + cv::Mat img=imread(image.jpg); + int ht=img->cols; + int wd=img->rows; + Mat gray; + cvtCor( image, gray, CV_BGR2GRAY ); + imwrite( "Gray_Image.jpg", gray); + cv::Scalar intensity; + + + int **visited; + visited = (int**)malloc(768*sizeof(int*)); + for(i=0;icols;i++) + for(j=0;jrows;j++) + { + intensity= grey.at(i, j); + if (intensity>127) + grey.at(i, j)=255; + else + grey.at(i, j)=0; + } + + + + + + for(i=0;i(i, j)==0) + { + if(visited[i][j]==-1) + { + *count=*count+1; + + myqueue1.front=NULL; + myqueue1.push(i); + myqueue1.front(&x); + myqueue2.front=NULL; + myqueue2.push(j); + myqueue2.front(&y); + + + } + while(!isEmpty()) + { + myqueue1.front(&x); + myqueue1.pop(); + myqueue2.front(&y); + myqueue2.pop(); + for(k=x-1;k<=x+1;k++) + for(l=y-1;l<=y+1;l++) + { + intensity=grey.at(i, j); + if(intensity==0 && visited[k][l]==-1) + { + myqueue1.push(k); + myqueue2.push(l); + visited[k][l]=*count; + } + } + visited[x][y]=*count; + } + } + } + + return visited; +} + +void centroid(int** output,int count,Circle* c) +{ + int i,j; + double count_pixel[count+1]; + cv::Mat img=cv::imread(image.jpg); + for(i=0;i(&c[i].y, &c[i].x); + if(intensity.val[0]>200) c[i].color=1; + else if (intensity.val[1]>200) c[i].color=2; + else (intensity.val[2]>200) c[i].color=3; + } +} +return_data board() +{ + return_data a; + int **output=NULL; + int count; + output=blobDetect(&count); + Circle c[100]; + centroid(output,count,c); + a.circle=c; + a.cnt=count; + return a; +} + + diff --git a/Yash/Oops_assgn/board_game/Board.h b/Yash/Oops_assgn/board_game/Board.h new file mode 100644 index 0000000..36a61b9 --- /dev/null +++ b/Yash/Oops_assgn/board_game/Board.h @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +class Circle +{ + public: + int x; + int y; + int color; +}; + +class return_data +{ + public: + Circle* circle; + int cnt; +}; + +class Board +{ +private: + Circle* c; +public: + int** blobDetect(int*); + void centroid(int** ,int ,Circle* ); + Circle* board(); + int getCount(); +}; diff --git a/Yash/Oops_assgn/board_game/Board.h.gch b/Yash/Oops_assgn/board_game/Board.h.gch new file mode 100644 index 0000000..192b14c Binary files /dev/null and b/Yash/Oops_assgn/board_game/Board.h.gch differ diff --git a/Yash/Oops_assgn/board_game/Dice.cpp b/Yash/Oops_assgn/board_game/Dice.cpp new file mode 100644 index 0000000..715ffcb --- /dev/null +++ b/Yash/Oops_assgn/board_game/Dice.cpp @@ -0,0 +1,9 @@ + +#include "Dice.h" + +using namespace std; + +int dice() +{ + return rand()%6 + 1; +} \ No newline at end of file diff --git a/Yash/Oops_assgn/board_game/Dice.h b/Yash/Oops_assgn/board_game/Dice.h new file mode 100644 index 0000000..1e8b181 --- /dev/null +++ b/Yash/Oops_assgn/board_game/Dice.h @@ -0,0 +1,11 @@ +#include +#include +#include + +using namespace std;; + +class Dice +{ + public: + int dice(); +}; diff --git a/Yash/Oops_assgn/board_game/Dice.h.gch b/Yash/Oops_assgn/board_game/Dice.h.gch new file mode 100644 index 0000000..616b667 Binary files /dev/null and b/Yash/Oops_assgn/board_game/Dice.h.gch differ diff --git a/Yash/Oops_assgn/board_game/Distance.cpp b/Yash/Oops_assgn/board_game/Distance.cpp new file mode 100644 index 0000000..df6c659 --- /dev/null +++ b/Yash/Oops_assgn/board_game/Distance.cpp @@ -0,0 +1,7 @@ +#include "Distance.h" +using namespace std; + +float dist(int x, int y, int a, int b) +{ + return sqrt((x-a)*(x-a)+(y-b)*(y-b)); +} \ No newline at end of file diff --git a/Yash/Oops_assgn/board_game/Distance.h b/Yash/Oops_assgn/board_game/Distance.h new file mode 100644 index 0000000..db0bb7a --- /dev/null +++ b/Yash/Oops_assgn/board_game/Distance.h @@ -0,0 +1,10 @@ +#include +#include + +using namespace std; + +class Distance +{ + public: + float dist(int, int, int, int); +}; diff --git a/Yash/Oops_assgn/board_game/Distance.h.gch b/Yash/Oops_assgn/board_game/Distance.h.gch new file mode 100644 index 0000000..192b14c Binary files /dev/null and b/Yash/Oops_assgn/board_game/Distance.h.gch differ diff --git a/Yash/Oops_assgn/board_game/Game.cpp b/Yash/Oops_assgn/board_game/Game.cpp new file mode 100644 index 0000000..870cd60 --- /dev/null +++ b/Yash/Oops_assgn/board_game/Game.cpp @@ -0,0 +1,24 @@ + +#include "Game.h" +using namespace std; + +int game(Player p[]) +{ + return_data a; + int th; + Dice d; + Board* bd; + cv::Mat img=cv::imread(image.jpg); + a = bd->board(); + int i; + while(0) + { + for (i=0;i<2;i++) + { + th=d.dice(); + p[i].newStatus(th,return_data a); + if(p[i].x>img->width||p[i].y>img->height) + return i; + } + } +} \ No newline at end of file diff --git a/Yash/Oops_assgn/board_game/Game.h b/Yash/Oops_assgn/board_game/Game.h new file mode 100644 index 0000000..b1f03ea --- /dev/null +++ b/Yash/Oops_assgn/board_game/Game.h @@ -0,0 +1,13 @@ +#include +#include "Player.h" +#include "Board.h" +#include "Dice.h" + + +using namespace std; + +class gameplay +{ + public: + int game(Player p[]); +}; diff --git a/Yash/Oops_assgn/board_game/Game.h.gch b/Yash/Oops_assgn/board_game/Game.h.gch new file mode 100644 index 0000000..192b14c Binary files /dev/null and b/Yash/Oops_assgn/board_game/Game.h.gch differ diff --git a/Yash/Oops_assgn/board_game/Main.cpp b/Yash/Oops_assgn/board_game/Main.cpp new file mode 100644 index 0000000..25dee67 --- /dev/null +++ b/Yash/Oops_assgn/board_game/Main.cpp @@ -0,0 +1,21 @@ +#include +#include "Player.h" + +using namespace std; + +int main(int argc, char* argv[]) +{ + Player p[2]; + gameplay game1; + int j; + for (j=0;j<2;j++) + { + p[j].x=0; + p[j].y=0; + p[j].score=0; + } + int i=game1.game(p) + cout<<"Player"<getCount();i++) + { + if(p.xd.dist(x,y,circle[i].x,circle[i].y)) + { + j=i; + min=d.dist(x,y,circle[i].x,circle[i].y); + } + } + p.x=circle[i].x; + p.y=circle[i].y; +} + + +void Player::newStatus(int th, return_data a) +{ + if(th%2==0) + { + p.updtPos(1,a.circle, a.cnt); + p.score+=2*th; + return; + } + if(th==1) + { + p.updtPos(2,a.circle, a.cnt); + score+=10; + return; + } + else + { + p.updtPos(3,a.circle,a.cnt); + p.score+=th; + return; + } +} + + diff --git a/Yash/Oops_assgn/board_game/Player.h b/Yash/Oops_assgn/board_game/Player.h new file mode 100644 index 0000000..3a2b96d --- /dev/null +++ b/Yash/Oops_assgn/board_game/Player.h @@ -0,0 +1,15 @@ +#include +#include +#include +#include "Distance.h" +#include "Board.h" +using namespace std; + +class Player +{ + private: + int x,y,score; + public: + void newStatus(int , Circle*,return_data ); + void updtPos(int , Circle*, int ); +}; diff --git a/Yash/Oops_assgn/board_game/Player.h.gch b/Yash/Oops_assgn/board_game/Player.h.gch new file mode 100644 index 0000000..192b14c Binary files /dev/null and b/Yash/Oops_assgn/board_game/Player.h.gch differ diff --git a/Yash/Oops_assgn/board_game/blob.cpp b/Yash/Oops_assgn/board_game/blob.cpp new file mode 100644 index 0000000..22ac3cb --- /dev/null +++ b/Yash/Oops_assgn/board_game/blob.cpp @@ -0,0 +1,367 @@ +#include +#include +#include +#include +#include +#include + +#define PIX(img,i,j,k) (((uchar*)img->imageData)[(i)*img->widthStep+(j)*img->nChannels+(k)]) +typedef struct node +{ +int x; +int y; +struct node *next; +}node; + +node *rear,*front; + +int isEmpty() +{ +if(front==NULL) +return 1; +else return 0; +} +void enqueue(int x,int y) +{ +node* t; +t=(node*)malloc(sizeof(node)); +t->x=x; +t->y=y; +t->next=NULL; +if(isEmpty()) +{ +front=t; +rear=front; +} +else{ +rear->next=t; +rear=t; +rear->next=NULL; +} +} +void queuefront(int *x,int *y) +{ +*x=front->x; +*y=front->y; +return; +} +void dequeue() +{ +node* t; +if(isEmpty()) +{ +printf("Queue Underflow\n"); +return; +} +t=front; +front=t->next; +free(t); +} + +IplImage* img; +IplImage* gray; + +int** blobDetection(IplImage *img,int *count) +{ +int i,j,k,l,x,y; +*count=0; +int height = img->height; +int width = img->width; +int **visited; +visited = (int**)malloc(height*sizeof(int*)); +for(i=0;iheight;i++) +visited[i]=(int*)malloc(img->width*sizeof(int)); +for(i=0;iheight;i++) +for(j=0;jwidth;j++) +visited[i][j]=-1; +for(i=1;iheight-1;i++) +for(j=1;jwidth-1;j++) +{ +if(PIX(img,i,j,0)==0) +{ +if(visited[i][j]==-1) +{ +*count=*count +1; +front=NULL; +enqueue(i,j); +queuefront(&x,&y); +while(!isEmpty()) +{ +queuefront(&x,&y); +//printf("(%d,%d),(%d)",x,y,visited[x][y]); +dequeue(); +for(k=x-1;k<=x+1;k++) +for(l=y-1;l<=y+1;l++) +{ +if(PIX(img,k,l,0)==0 && visited[k][l]==-1) +{ +enqueue(k,l); +visited[k][l]=*count; +} +} +visited[x][y]=*count; +} +} +} +} +//printf("%d\n",*count); +return visited; +} + + +void hough() +{ +IplImage *frame,*output; +//cvWaitKey(10000); +int thresh=127; +//cvNamedWindow("win",CV_WINDOW_AUTOSIZE); +cvNamedWindow("window",CV_WINDOW_AUTOSIZE); +cvShowImage("window",gray); +output=cvCreateImage(cvGetSize(gray),IPL_DEPTH_8U,1); +int height,width; +height=gray->height; +width=gray->width; +int** mat; +int diag; +diag=(int)(sqrt(height*height + width*width)); +int angle=360; +int step=2; +int i,j; +CvScalar pixel; + +mat=(int**)(malloc(diag*sizeof(int*))); +for(i=0;iheight;i++) +{ +for(j=0;jwidth;j++) +{ +CvScalar pixel; +pixel=cvGet2D(gray,i,j); +if(pixel.val[0]==0) +{ +for(temp=0;temp<360;temp++) +{ +distance=(int)(fabs(j*cos(temp*3.14/180.0)+i*sin(temp*3.14/180.0))); +mat[distance][temp]++; +} +} +} +} + +int x; +int count=0; +thresh=100; +cvCreateTrackbar("Thresh","window",&thresh,500); +while(1) +{ +pixel.val[0]=0; +for(x=0;xwidth;x++) +{ +int y; +for(y=0;yheight;y++) +{ +cvSet2D(output,y,x,pixel); +} +} +pixel.val[0]=255; +for(i=0;ithresh) +{ +count++; +//printf("%d %d\n",i,j); +for(x=0;xwidth;x++) +{ +int y; +y=(int)(-(1.0)/tan(j*3.14/180.0)*x + i/sin(j*3.14/180.0)); +if(y>=0 && yheight) +{ +//printf("%d %d\n",y,x); +cvSet2D(output,y,x,pixel); +} +} +} +} +} +cvShowImage("my_win",output); +if(cvWaitKey(33)==27 || thresh==20) +break; +} +//cvReleaseImage(&output); +//printf("%d\n",count); +//cvShowImage("my_win",output); +//cvWaitKey(0); +cvDestroyAllWindows(); +return; +} +void function(int **data,int counts, double count_x[], double count_y[]) //function to detect the shapes +{ + +cvNamedWindow("win",0); //create window in function +int k,l,count; +CvScalar pixel,pix,pix2; //structure to store four values +int i,j,t; +for(count=1;count<=counts;count++) +{ +int c=0; +gray=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1); +for(i=1;iheight-1;i++) +{ +for(j=1;jwidth-1;j++) +{ +pixel.val[0]=255; +cvSet2D(gray,i,j,pixel); +int min=count; +int max=-1; +for(k=i-1;k<=i+1;k++) +{ +for(l=j-1;l<=j+1;l++) +{ +//pix=cvGet2D(img,k,l); //Getting pixel from loaded image +/* cvGet2D(,,) */ +if(min>data[k][l]) +min=data[k][l]; +if(data[k][l]<=count && max,,, ) */ +} +} +cvShowImage("win",gray); +//hough(); +//Use contours to detect number of edges +CvSeq* contours; //hold the pointer to a contour in the memory block +CvSeq* result; //hold sequence of points of a contour +CvMemStorage *storage = cvCreateMemStorage(0); //storage area for all contours + +//finding all contours in the image +cvFindContours(gray, storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0)); + +//iterating through each contour + +result = cvApproxPoly(contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0); + + +printf("No. of contours is %d \n",result->total); +if(result->total==3) +printf("Triangle\n"); +else if(result->total==4) +printf("Square\n"); +else if (result->total==8) +printf("Circle\n"); +else +printf("Arbit\n"); + + +//shaped(count_x[count],count_y[count],count); +cvReleaseImage(&gray); +cvWaitKey(0); +} + +} + +void binary_convert(IplImage *img) //Convert Image to binary +{ +int i,j; +CvScalar pixel; +for(i=0;iheight;i++) +{ +for(j=0;jwidth;j++) +{ +pixel=cvGet2D(img,i,j); +if(pixel.val[0]>100) +{ +PIX(img,i,j,0)=255; +} +else +{ +PIX(img,i,j,0)=0; +} +} +} +//cvNamedWindow("win",0); +//cvShowImage("win",img); +//cvWaitKey(0); +} + +void centroid(int** output,int count,double count_x[],double count_y[]) //Centroid +{ +int i,j; + +double count_pixel[count+1]; + +for(i=0;iheight;i++) +{ +for(j=0;jwidth;j++) +{ +int value; +value=output[i][j]; +if(value!=-1) +{ +count_x[value]+=j; +count_y[value]+=i; +count_pixel[value]++; +} +} +} +for(i=1;idepth,1); +binary_convert(img); //Convert image to binary + +output=blobDetection(img,&count); //Blob detection + +printf("count = %d\n",count); +cvShowImage("window2",img); +double* count_x; +double* count_y; + +count_x=(double* )(malloc(count*sizeof(double))); +count_y=(double* )(malloc(count*sizeof(double))); +centroid(output,count, count_x, count_y); +function(output,count,count_x, count_y); //Centroid Callback function +cvWaitKey(0); +return 0; +} \ No newline at end of file