Thread: a 2-D array image (Prompt)

  1. #1
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49

    Question a 2-D array image (Prompt)

    Hi there,
    I think my main problem is that I can not fully understand the prompt of the assigned program..
    It says that I should choose a character such as '#' for the darkest character '0', and a blank for the brightest character, when reading a 2D array of numbers as every number represents brightness level '9'.. then what should we display for all the numbers between like '5' or '3' or '7' ??
    Also I can not get the part about correcting errors because it is not explained enough..

    That is the assigned problem Prompt, thank you.
    -Amy





    Photos taken in space by the Galileo spacecraft are sent back to earth as a stream of numbers. each number represents a level of brightness. A large number represents a high brightness level, and a small number represents a low level. Your job is to take a matrix (a two-dimensional array) of the numbers and print it as a picture.
    One approach to generating a picture is to print a dark character (such as #) when the brightness level is low, and to print a light character (such as a blank or a period) when the level is high. Unfortunately, errors in the transmission sometimes occur. Thus, your program should first attempt to find and correct these errors. Assume a value is in error if it differs by more than one from each of its four neighboring values, rounded to the nearest integer.
    Example

    |*|5 |*|
    |4| 2 |5 |
    |*|2 |*|


    The 2 would be regarded as an error and would be given a corrected value of 5
    Note that values on the corners or boundaries of the matrix have to be processed differently than the values on the interior. your program should print an image of the uncorrected picture and then an image of the corrected picture.


    Sample Input File:


    9897979989998997999688998997
    8221211996712212299892171239
    7915237789768181981969223198
    9822128988971612997679352399
    8712329897561231899289232687
    9753221622717115837899191299
    9712829273212382969829336198
    7823271213171723973989123498
    9815129966695162798989353297
    6632819976793223693779212197
    9527238878891372597699391798
    6219121967812191292661223129
    7976769565997866699798899787
    It ain't illegal until you get caught..

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    9 = brightest
    0 = lowest

    Since you are obviously not working with color or pixels they want you to use characters to represent the intensity levels. The errors in transmission (dumb way to put it if you ask me) can be fixed by a simple average filter or linear filter.

    Here is a possible solution that will output a greyscale image:

    I leave it to you to sort out the data type conversion issues as well as iterating through the 2D array.

    Code:
    float color_coef=current_value/9.0f;
    
    unsigned char red=(unsigned char)(255.0f*color_coef);
    unsigned char green=red;
    unsigned char blue=red;
    
    DWORD finalcolor=RGB(red,green,blue);

    4 way average filter
    ...1...
    2.X.3
    ...4...

    Final value of X = (1+2+3+4+X)/5
    or

    avg_value=(1+2+3+4)/4
    finalX=(X+avg_value)/2

    or

    avg_value=(1+2+3+4)/4
    finalX=LI(X,avg_value,bias);

    Linear filter (LI)
    value=v1+f1*(v2-v1)

    Bi linear filter (BI)
    value=v1+f1*(v2-v1)
    value2=v3+f1*(v4-v3)
    finalvalue=value+f2*(value2-value)

  3. #3
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49

    Question

    Sorry, but we have not studied displaying a greyscale image yet, and I did not understand the code at all, it sounds so advanced for me..

    And I did not get the filter thing either..
    Am sorry but can you explain it simpler? I do not think I am allowed to use this code because I do not understand it and we have not studied those functions/orders and I do not even know in which library they exist..

    Thanks,
    -Amy
    It ain't illegal until you get caught..

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    You basically want to read from the array. Then replace the numbers with characters you designate as bright or dark. You need to define 10 diff characters as bright or dark. You are then simply reading from the array, and where there is a 0 output your dark character, 9 your brightest characters.

    Try getting that done first, as the assignment says do both corrected and uncorrected. Once you get that, it is just a matter of figuring out how to correct it.

    Just take it one step at a time.

  5. #5
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49

    Question

    Well, I have worked on that after discovering that we need to display a bright character if the value is from 0 to 4, and a dark character if the value is from 5 to 9..
    So I have generated a code for that.. There is a small problem, the array does not go to fully print the last row.. Here is my code:

    Code:
    #include <iostream>
    #include <fstream.h>
    int main()
    {
    char image[13][28];
    char file[10] = "Image.txt";
    ifstream source;
    source.open(file); 
    for(int i=0; i<13; i++)
    {
    for(int j=0; j<28; j++)
    {
    source.get(image[i][j]);
    }
    }
     
    for(int m=0; m<13; m++)
    {
    for(int n=0; n<28; n++)
    {
    if(image[m][n]>=51 && image[m][n]<58 )
    	cout<<"#";
    else if(image[m][n]>=48 && image[m][n]<51)
    	cout<<" ";
    else
    	cout<<image[m][n];
    }
    }
    cout<<endl<<endl;
     
    return 0;
    }
    The following is the input file "Image.txt":

    9897979989998997999688998997
    8221211996712212299892171239
    7915237789768181981969223198
    9822128988971612997679352399
    8712329897561231899289232687
    9753221622717115837899191299
    9712829273212382969829336198
    7823271213171723973989123498
    9815129966695162798989353297
    6632819976793223693779212197
    9527238878891372597699391798
    6219121967812191292661223129
    7976769565997866699798899787


    I get the following output:
    www.geocities.com/amirahasanen1/output.JPG


    Thanks,
    -Amy
    It ain't illegal until you get caught..

  6. #6
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49

    Question

    Okay, so I think that characters' printing out problem is not that important, I would try to handle it..

    Now the main problem is that I can not understand this error part, on what basis did the example correct the number in the middle to be "5"? and when do we know that this number is an error??
    The following is the error part as stated in the problem prompt:


    Unfortunately, errors in the transmission sometimes occur. Thus, your program should first attempt to find and correct these errors. Assume a value is in error if it differs by more than one from each of its four neighboring values, rounded to the nearest integer.

    Example:
    --------

    |*|5 |*|
    |4| 2 |5 |
    |*|2 |*|

    The 2 would be regarded as an error and would be given a corrected value of 5
    Note that values on the corners or boundaries of the matrix have to be processed differently than the values on the interior.
    It ain't illegal until you get caught..

  7. #7
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Well, here is your problem.
    You have an array of char, but you have to do math, so you need an array of integers.

    Now, you can either convert your existing array, or redo it to type int and get one number at a time for each array position.

    You might ask your teacher about the error correction thing. Because how I read it and how it is done, well, it is stupid. It means whenever you find one error and fix it, you are creating another error that you will have to fix; it will just cascade with errors down the columns. But maybe I am reading it wrong; I do not claim to be a genius.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Image Processing
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2009, 12:10 PM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Array help
    By deedlit in forum C Programming
    Replies: 4
    Last Post: 11-05-2003, 10:55 AM