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









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










share|improve this question























  • 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















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










share|improve this question























  • 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













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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


















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20