Solved

Problem in storing coordinates of a pixel

Posted on 2003-11-19
4
259 Views
Last Modified: 2010-04-01
hi! all
      i'm writng a code for image compression in which  i have to read the data from file and store it in a matrix form. Second step involves sorting of data....data can be sort but i've to store their positions not the values in an array (acc. to the sorted values).

Suppose 64(3rd row & 5th col) is the max value in the matrix...then i've to store 3,5 position of 64 . Now problem is how do i store that. i think it's pretty simple but i'm not bale to get it.
This is done so that value's original location is not lost...since it's an image & after whatever operation we need to send that vlue back to its original position so as to get the original image back.

What kind of structure do i need to use.
Please if someone cud help me solving this prob.
thanks in advance
0
Comment
Question by:s_t_1979
4 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 9777947
Is this homework?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 9778110
Try this:

int* sortArray(int* array, int size)
{
   // check for minimal two items
   if (size <= 1)
       return NULL;

   // create integer array for the sorted indices
   int*     idxArr  = new int[size];  
   int      nn      = 0;       // nn is the number of already sorted items
   int      n1      = 0;       // n1 is the lower range index when searching binary
   int      n2      = 0;       // n2 is the upper range index when searching binary
   int      n       = 0;       // n is the middle of the binary range
   int      i;                 // loop counter

   // loop all items and build a sorted index array  
   for (i = 0; i < size; i++)
   {
       // i1 is the new item which index must be inserted to the index array
       int i1 = array[i];
       // init the binary range to the current size of the already sorted index array
       n1    = 0;
       n2    = nn-1;

       // binary search
       while (n1 <= n2)
       {
           // compute the middle of the current range for dividing the current range into two parts
           n     = (n1 + n2 + 1)/2;
           // get the item to compare
           int i2 = array[idxArr[n]];
           // compare both items
           // if the left item is less we make the lower part current
           if (i1 < i2)
               n2   = n - 1;
           // if the left item is greater or equal we make the upper part current
           else
               n1 = n + 1;
       }

       // at end of the previous loop n1 is the new position where the current item index (i)
       // has to be inserted to get the index array sorted

       // if this position is not outside the current size (that means we have not to change the order)
       // we move all indices on the right side for one position using memmove
       if (n1 < nn)
       {
           memmove(&idxArr[n1+1], &idxArr[n1], (nn-n1)*sizeof(int));    
       }
       idxArr[n1] = i;
       nn++;
   }
   return idxArr;
}

#define MAXX 100
#define MAXY 100

int main(int argc, char* argv[])
{
    int matrix[MAXY][MAXX] = { 0 };

    // read matrix from somewhere

    if (readMatrix(matrix, ...))
    {
        int* array  = (int*)matrix;
        int  size   = MAXX*MAXY;

        int* idxArr = sortArray(array, size);

        // now you may store the index array to a file
        // it is as big as the original image file (if the values to sort are integers)

        writeSortedIndex(idxArr, ...);

        // if you need the coordinates e. g. of the biggest value you may calculate
        int idx = idxArr[size-1];

        int x      = idx % MAXX;
        int y      = idx / MAXX;

        // then, you got the max value by
        int maxInt = matrix[x][y];
       
    }
}

I assumed that your values are integers.

Hope, that helps

Alex
0
 
LVL 4

Accepted Solution

by:
dhyanesh earned 100 total points
ID: 9779128
Hi

You can use an array of structures. Suppose you have M rows and N columns, then

struct
{
     int x;
     int y;
}array[M*N];

Thus this array then can be accessed easily as array[0].x or array[0].y which will give x and y of first element.

Thus you can store the x and y values easily and access them.

Dhyanesh
0
 

Author Comment

by:s_t_1979
ID: 9785128
thanks a lot Dhyanesh...i knew ans will be simple...but was not able to do it. sometimes u tend to forget basic things. Once again thanks a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Modify static variables within a class in VC++ 3 95
std::atomic with custom class 6 189
Visual c++ and text files 7 60
Grammars for C C++ and java 1 114
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now