Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added OOPs assignment. Trying to remove the errors. #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions Yash/Oops_assgn/Search algo/BFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <math.h>
#include <queue>
#include <new>
#include <stdio.h>
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<Pixel> 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<uchar>(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<uchar>(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<<count;
return 0;
}
95 changes: 95 additions & 0 deletions Yash/Oops_assgn/Search algo/Dijkstra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <iostream>
#include <queue>
#include <stdio.h>
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<n;j++)
flag[j]=-1;
priority_queue <node, vector<node>, CompareWeight> pq;

cout<<"Start=";
cin>>s;
cout<<"End=";
cin>>e;
for(j=0;j<n;j++)
{
for(l=0;l<n;l++)
{
k[j][l]=-1;
}
}

k[0][1]=20;
k[0][3]=80;
k[0][6]=90;
k[1][5]=10;
k[2][3]=10;
k[2][5]=50;
k[2][7]=20;
k[3][2]=10;
k[3][6]=20;
k[4][1]=50;
k[4][6]=30;
k[5][2]=10;
k[5][3]=40;
k[6][0]=20;
start.i=s;
start.cweight=0;
pq.push(start);
while(!pq.empty())
{
current=pq.top();
pq.pop();
if(past[current.i]>current.cweight||flag[current.i]==-1)
{
past[current.i]=current.cweight;
flag[current.i]=1;
}
for(j=0;j<n;j++)
{
if(k[current.i][j]!=-1)
{
next.i=j;
next.cweight=current.cweight+k[current.i][j];
if(past[next.i]>=next.cweight||flag[next.i]==-1)
pq.push(next);
}
}
cout<<"Success";
}
present=e;
l=past[e];
cout<<'\n'<<"Backtrace End("<<e<<") <- ";
while(present!=s)
{
for(j=0;j<n;j++)
if(k[j][present]!=-1 && past[present]-k[j][present]==past[j])
{
present=j;
cout<<j<<" <- ";
}
}
cout<<'\n';
return 0;
}
102 changes: 102 additions & 0 deletions Yash/Oops_assgn/Search algo/dfs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <math.h>
#include <stack>
#include <new>
#include <stdio.h>
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<Pixel> 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<uchar>(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<uchar>(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<<count;
return 0;
}
Loading