Dialation/Erosion on 2d array of chars c++

Multi tool use
up vote
0
down vote
favorite
Im trying to figure out why my function for dialating an image wont work.
My goal is to turn something like this:
.........
.........
....x....
.........
.........
into this:
.........
....x....
...xxx...
....x....
.........
for each cycle of dilation.
i havent thought of it but i aslo need to to the reverse for the erosion.
This is what ive come up with so far (the program takes in input from the user in the command line using argv) :
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
//look up line by line parsing
using namespace std;
void replacee(vector<vector<char>> &vec, char oldd, char neww)
for (vector<char> &v : vec) // reference to innver vector
replace(v.begin(), v.end(), oldd, neww); // standard library
algorithm
void dialationn(vector<vector<char>> & vec, char suspect)
for (int i = 0; i < vec.size(); i ++)
for (int j = 0; j < vec[i].size(); j++)
if (vec[i][j] == suspect)
if (i > 0)
vec[i-1][j] = suspect;
if (j > 0)
vec[i][j-1] = suspect;
if (i + 1<vec.size())
vec[i+1][j] = suspect;
if (j + 1<vec[i].size())
vec[i][j+1] = suspect;
int main(int argc, char* argv)
fstream fin; char ch;
string name (argv[1]); //File Name.
vector<vector<char>> data;
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
fin.open(name.c_str(),ios::in);
// Assume name as an arbitary file.
string argument2 (argv[2]);
string argument3 (argv[3]);
string argument4 (argv[4]);
while(fin)
ch = fin.get();
if(ch!='n')
temp.push_back(ch);
else
data.push_back(temp);
temp.clear();
if (argument2 == "replace")
replacee(data, argument3[0], argument4[0]);
for (int i = 0; i < data.size(); i ++)
for (int j = 0; j < data[i].size(); j++)
cout << data[i][j];
cout << endl;
else if (argument2 == "dialate")
dialationn(data, argument3[0]);
for (int i = 0; i < data.size(); i ++)
for (int j = 0; j < data[i].size(); j++)
cout << data[i][j];
cout << endl;
fin.close();
the dialationn function ive implemented so far uses a double for loop to cycle through the 2d array, and when it finds the character that is suppoesd to be dialated, it checks if a border is near by and sets the coordinate accordingly.
edit: fixed == typo's
c++ image-morphology
|
show 5 more comments
up vote
0
down vote
favorite
Im trying to figure out why my function for dialating an image wont work.
My goal is to turn something like this:
.........
.........
....x....
.........
.........
into this:
.........
....x....
...xxx...
....x....
.........
for each cycle of dilation.
i havent thought of it but i aslo need to to the reverse for the erosion.
This is what ive come up with so far (the program takes in input from the user in the command line using argv) :
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
//look up line by line parsing
using namespace std;
void replacee(vector<vector<char>> &vec, char oldd, char neww)
for (vector<char> &v : vec) // reference to innver vector
replace(v.begin(), v.end(), oldd, neww); // standard library
algorithm
void dialationn(vector<vector<char>> & vec, char suspect)
for (int i = 0; i < vec.size(); i ++)
for (int j = 0; j < vec[i].size(); j++)
if (vec[i][j] == suspect)
if (i > 0)
vec[i-1][j] = suspect;
if (j > 0)
vec[i][j-1] = suspect;
if (i + 1<vec.size())
vec[i+1][j] = suspect;
if (j + 1<vec[i].size())
vec[i][j+1] = suspect;
int main(int argc, char* argv)
fstream fin; char ch;
string name (argv[1]); //File Name.
vector<vector<char>> data;
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
fin.open(name.c_str(),ios::in);
// Assume name as an arbitary file.
string argument2 (argv[2]);
string argument3 (argv[3]);
string argument4 (argv[4]);
while(fin)
ch = fin.get();
if(ch!='n')
temp.push_back(ch);
else
data.push_back(temp);
temp.clear();
if (argument2 == "replace")
replacee(data, argument3[0], argument4[0]);
for (int i = 0; i < data.size(); i ++)
for (int j = 0; j < data[i].size(); j++)
cout << data[i][j];
cout << endl;
else if (argument2 == "dialate")
dialationn(data, argument3[0]);
for (int i = 0; i < data.size(); i ++)
for (int j = 0; j < data[i].size(); j++)
cout << data[i][j];
cout << endl;
fin.close();
the dialationn function ive implemented so far uses a double for loop to cycle through the 2d array, and when it finds the character that is suppoesd to be dialated, it checks if a border is near by and sets the coordinate accordingly.
edit: fixed == typo's
c++ image-morphology
Can you add more details on what you mean by "won't work"? And I see some typos where you're using the comparison==
instead of the assignment=
.
– 1201ProgramAlarm
Nov 10 at 1:42
1
How about skipping over the command-line stuff and file reading, and just make a direct call to the function in question with hard-coded data?
– PaulMcKenzie
Nov 10 at 1:46
3
Ever hear of "unit testing"? Your function should work regardless of where the data comes from. It would be much easier to have hard-coded data and call the function directly. Even in your question, you have a diagram of hard-coded data, and you say your program doesn't work. So test that data, not some random stuff from the command-line or file. Once you see the data you posted work, then you introduce file reading and whatever other frills.
– PaulMcKenzie
Nov 10 at 1:51
1
@xannax159 Here is an example. No need for any file reading or command-line arguments. The vector is set up exactly as in your question, and I made some call to one of the functions using whatever characters I'm testing with for the command-line argument.
– PaulMcKenzie
Nov 10 at 1:58
2
@xannax159 -- Take that as a lesson -- never waste time writing fancy input or output routines if you haven't tested and vetted the functions that are actually important in getting your program to work correctly.
– PaulMcKenzie
Nov 10 at 2:11
|
show 5 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Im trying to figure out why my function for dialating an image wont work.
My goal is to turn something like this:
.........
.........
....x....
.........
.........
into this:
.........
....x....
...xxx...
....x....
.........
for each cycle of dilation.
i havent thought of it but i aslo need to to the reverse for the erosion.
This is what ive come up with so far (the program takes in input from the user in the command line using argv) :
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
//look up line by line parsing
using namespace std;
void replacee(vector<vector<char>> &vec, char oldd, char neww)
for (vector<char> &v : vec) // reference to innver vector
replace(v.begin(), v.end(), oldd, neww); // standard library
algorithm
void dialationn(vector<vector<char>> & vec, char suspect)
for (int i = 0; i < vec.size(); i ++)
for (int j = 0; j < vec[i].size(); j++)
if (vec[i][j] == suspect)
if (i > 0)
vec[i-1][j] = suspect;
if (j > 0)
vec[i][j-1] = suspect;
if (i + 1<vec.size())
vec[i+1][j] = suspect;
if (j + 1<vec[i].size())
vec[i][j+1] = suspect;
int main(int argc, char* argv)
fstream fin; char ch;
string name (argv[1]); //File Name.
vector<vector<char>> data;
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
fin.open(name.c_str(),ios::in);
// Assume name as an arbitary file.
string argument2 (argv[2]);
string argument3 (argv[3]);
string argument4 (argv[4]);
while(fin)
ch = fin.get();
if(ch!='n')
temp.push_back(ch);
else
data.push_back(temp);
temp.clear();
if (argument2 == "replace")
replacee(data, argument3[0], argument4[0]);
for (int i = 0; i < data.size(); i ++)
for (int j = 0; j < data[i].size(); j++)
cout << data[i][j];
cout << endl;
else if (argument2 == "dialate")
dialationn(data, argument3[0]);
for (int i = 0; i < data.size(); i ++)
for (int j = 0; j < data[i].size(); j++)
cout << data[i][j];
cout << endl;
fin.close();
the dialationn function ive implemented so far uses a double for loop to cycle through the 2d array, and when it finds the character that is suppoesd to be dialated, it checks if a border is near by and sets the coordinate accordingly.
edit: fixed == typo's
c++ image-morphology
Im trying to figure out why my function for dialating an image wont work.
My goal is to turn something like this:
.........
.........
....x....
.........
.........
into this:
.........
....x....
...xxx...
....x....
.........
for each cycle of dilation.
i havent thought of it but i aslo need to to the reverse for the erosion.
This is what ive come up with so far (the program takes in input from the user in the command line using argv) :
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
//look up line by line parsing
using namespace std;
void replacee(vector<vector<char>> &vec, char oldd, char neww)
for (vector<char> &v : vec) // reference to innver vector
replace(v.begin(), v.end(), oldd, neww); // standard library
algorithm
void dialationn(vector<vector<char>> & vec, char suspect)
for (int i = 0; i < vec.size(); i ++)
for (int j = 0; j < vec[i].size(); j++)
if (vec[i][j] == suspect)
if (i > 0)
vec[i-1][j] = suspect;
if (j > 0)
vec[i][j-1] = suspect;
if (i + 1<vec.size())
vec[i+1][j] = suspect;
if (j + 1<vec[i].size())
vec[i][j+1] = suspect;
int main(int argc, char* argv)
fstream fin; char ch;
string name (argv[1]); //File Name.
vector<vector<char>> data;
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
fin.open(name.c_str(),ios::in);
// Assume name as an arbitary file.
string argument2 (argv[2]);
string argument3 (argv[3]);
string argument4 (argv[4]);
while(fin)
ch = fin.get();
if(ch!='n')
temp.push_back(ch);
else
data.push_back(temp);
temp.clear();
if (argument2 == "replace")
replacee(data, argument3[0], argument4[0]);
for (int i = 0; i < data.size(); i ++)
for (int j = 0; j < data[i].size(); j++)
cout << data[i][j];
cout << endl;
else if (argument2 == "dialate")
dialationn(data, argument3[0]);
for (int i = 0; i < data.size(); i ++)
for (int j = 0; j < data[i].size(); j++)
cout << data[i][j];
cout << endl;
fin.close();
the dialationn function ive implemented so far uses a double for loop to cycle through the 2d array, and when it finds the character that is suppoesd to be dialated, it checks if a border is near by and sets the coordinate accordingly.
edit: fixed == typo's
c++ image-morphology
c++ image-morphology
edited Nov 10 at 1:43
asked Nov 10 at 1:40


xannax159
448
448
Can you add more details on what you mean by "won't work"? And I see some typos where you're using the comparison==
instead of the assignment=
.
– 1201ProgramAlarm
Nov 10 at 1:42
1
How about skipping over the command-line stuff and file reading, and just make a direct call to the function in question with hard-coded data?
– PaulMcKenzie
Nov 10 at 1:46
3
Ever hear of "unit testing"? Your function should work regardless of where the data comes from. It would be much easier to have hard-coded data and call the function directly. Even in your question, you have a diagram of hard-coded data, and you say your program doesn't work. So test that data, not some random stuff from the command-line or file. Once you see the data you posted work, then you introduce file reading and whatever other frills.
– PaulMcKenzie
Nov 10 at 1:51
1
@xannax159 Here is an example. No need for any file reading or command-line arguments. The vector is set up exactly as in your question, and I made some call to one of the functions using whatever characters I'm testing with for the command-line argument.
– PaulMcKenzie
Nov 10 at 1:58
2
@xannax159 -- Take that as a lesson -- never waste time writing fancy input or output routines if you haven't tested and vetted the functions that are actually important in getting your program to work correctly.
– PaulMcKenzie
Nov 10 at 2:11
|
show 5 more comments
Can you add more details on what you mean by "won't work"? And I see some typos where you're using the comparison==
instead of the assignment=
.
– 1201ProgramAlarm
Nov 10 at 1:42
1
How about skipping over the command-line stuff and file reading, and just make a direct call to the function in question with hard-coded data?
– PaulMcKenzie
Nov 10 at 1:46
3
Ever hear of "unit testing"? Your function should work regardless of where the data comes from. It would be much easier to have hard-coded data and call the function directly. Even in your question, you have a diagram of hard-coded data, and you say your program doesn't work. So test that data, not some random stuff from the command-line or file. Once you see the data you posted work, then you introduce file reading and whatever other frills.
– PaulMcKenzie
Nov 10 at 1:51
1
@xannax159 Here is an example. No need for any file reading or command-line arguments. The vector is set up exactly as in your question, and I made some call to one of the functions using whatever characters I'm testing with for the command-line argument.
– PaulMcKenzie
Nov 10 at 1:58
2
@xannax159 -- Take that as a lesson -- never waste time writing fancy input or output routines if you haven't tested and vetted the functions that are actually important in getting your program to work correctly.
– PaulMcKenzie
Nov 10 at 2:11
Can you add more details on what you mean by "won't work"? And I see some typos where you're using the comparison
==
instead of the assignment =
.– 1201ProgramAlarm
Nov 10 at 1:42
Can you add more details on what you mean by "won't work"? And I see some typos where you're using the comparison
==
instead of the assignment =
.– 1201ProgramAlarm
Nov 10 at 1:42
1
1
How about skipping over the command-line stuff and file reading, and just make a direct call to the function in question with hard-coded data?
– PaulMcKenzie
Nov 10 at 1:46
How about skipping over the command-line stuff and file reading, and just make a direct call to the function in question with hard-coded data?
– PaulMcKenzie
Nov 10 at 1:46
3
3
Ever hear of "unit testing"? Your function should work regardless of where the data comes from. It would be much easier to have hard-coded data and call the function directly. Even in your question, you have a diagram of hard-coded data, and you say your program doesn't work. So test that data, not some random stuff from the command-line or file. Once you see the data you posted work, then you introduce file reading and whatever other frills.
– PaulMcKenzie
Nov 10 at 1:51
Ever hear of "unit testing"? Your function should work regardless of where the data comes from. It would be much easier to have hard-coded data and call the function directly. Even in your question, you have a diagram of hard-coded data, and you say your program doesn't work. So test that data, not some random stuff from the command-line or file. Once you see the data you posted work, then you introduce file reading and whatever other frills.
– PaulMcKenzie
Nov 10 at 1:51
1
1
@xannax159 Here is an example. No need for any file reading or command-line arguments. The vector is set up exactly as in your question, and I made some call to one of the functions using whatever characters I'm testing with for the command-line argument.
– PaulMcKenzie
Nov 10 at 1:58
@xannax159 Here is an example. No need for any file reading or command-line arguments. The vector is set up exactly as in your question, and I made some call to one of the functions using whatever characters I'm testing with for the command-line argument.
– PaulMcKenzie
Nov 10 at 1:58
2
2
@xannax159 -- Take that as a lesson -- never waste time writing fancy input or output routines if you haven't tested and vetted the functions that are actually important in getting your program to work correctly.
– PaulMcKenzie
Nov 10 at 2:11
@xannax159 -- Take that as a lesson -- never waste time writing fancy input or output routines if you haven't tested and vetted the functions that are actually important in getting your program to work correctly.
– PaulMcKenzie
Nov 10 at 2:11
|
show 5 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235304%2fdialation-erosion-on-2d-array-of-chars-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hh,O7 tfK T3QzhDA K10m OhpXJGNsZUd,etPy Kfx56zmD KHTkUB6MyxSL1 oT,LI
Can you add more details on what you mean by "won't work"? And I see some typos where you're using the comparison
==
instead of the assignment=
.– 1201ProgramAlarm
Nov 10 at 1:42
1
How about skipping over the command-line stuff and file reading, and just make a direct call to the function in question with hard-coded data?
– PaulMcKenzie
Nov 10 at 1:46
3
Ever hear of "unit testing"? Your function should work regardless of where the data comes from. It would be much easier to have hard-coded data and call the function directly. Even in your question, you have a diagram of hard-coded data, and you say your program doesn't work. So test that data, not some random stuff from the command-line or file. Once you see the data you posted work, then you introduce file reading and whatever other frills.
– PaulMcKenzie
Nov 10 at 1:51
1
@xannax159 Here is an example. No need for any file reading or command-line arguments. The vector is set up exactly as in your question, and I made some call to one of the functions using whatever characters I'm testing with for the command-line argument.
– PaulMcKenzie
Nov 10 at 1:58
2
@xannax159 -- Take that as a lesson -- never waste time writing fancy input or output routines if you haven't tested and vetted the functions that are actually important in getting your program to work correctly.
– PaulMcKenzie
Nov 10 at 2:11