Restoring Shuffled Image (Binary Img)

I am (trying) creating a program with C++ to RESTORE A SHUFFLED IMAGE.

The program will be used to restore the original image from the standard original pic that has been shuffled, and will also be tested with the shuffled picture that has graphically edited with a NOISE effect this time.

I have created all the code to read in a text file and output the shuffled images etc etc, but need the Shuffled image to be arranged back to the original.

The shuffled image is 4x4 pixels.

Thanks in advance!
Do the opposite of what do did to shuffle it.
That is not possible as a text file is read in, then the code I created outputs it into a .PGM format image. If you have a way of reversing this shuffled image back into the original then Im all ears.. other than that, I believe similarity and/or dissimilarity measures are the way to go about this.. any ideas? Cheers
How are you 'shuffling' this image?

The 'opposite' of shuffling it depends on how the shuffle is being done.
this is my code snippet then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <sstream> // stringstream
#include <iostream> // cout, cerr
#include <fstream> // ifstream
#include <istream>

using namespace std;

// This is the code to read .txt file in, which will convert
// the contents to an array of numbers in double
double* readTXT(char *fileName, int sizeR, int sizeC)
{
  double* data = new double[sizeR*sizeC];
  int i=0;
  ifstream myfile (fileName);
  if (myfile.is_open())
  {
	 
	while ( myfile.good())
    {
       if (i>sizeR*sizeC-1) break;
		 myfile >> *(data+i);
        // cout << *(data+i) << ' '; // This line display the converted data on the screen, you may comment it out. 
	     i++;                                                             
	}
    myfile.close();
  }

  else cout << "Unable to open file"; 
  cout << i;

  return data;
}

// This is the code to write to .pgm image, which will convert the data in double to 
// an image in PGM format 
void WritePGM(char *filename, double *data, int& sizeR, int& sizeC, int Q)
{
 int i, j;
 unsigned char *image;
 ofstream myfile;

 image = (unsigned char *) new unsigned char [sizeR*sizeC];

 // convert the integer values to unsigned char
 
 for(i=0; i<sizeR*sizeC; i++)
	 image[i]=(unsigned char)data[i];

 myfile.open(filename, ios::out);

 if (!myfile) {
   cout << "Can't open file: " << filename << endl;
   exit(1);
 }

 myfile << "P5" << endl;
 myfile << sizeC << " " << sizeR << endl;
 myfile << Q << endl;

 myfile.write( reinterpret_cast<char *>(image), (sizeR*sizeC)*sizeof(unsigned char));

 if (myfile.fail()) {
   cout << "Can't write image " << filename << endl;
   exit(0);
 }

 myfile.close();

}

int main()
{
	// This part will show you how to use the two ReadIn functions.
	double* data = 0;
	int M=0; int N=0; 
	
	cout << endl;
	cout << "Data from text file -------------------------------------------" << endl;
	M = 768; N = 1024; //M and N represent the size of the image, e.g. for task 1, M = 512, N = 512
	char* fileName2 = "C:\\Users\\FileName\\Logo.txt"; 
	data = readTXT(fileName2, M, N);

	char* fileName = "C:\\Users\\FileName\\Logo.pgm";
	int Q = 255; // Q is the maximum intensity value for your image, 255 for greyscale and 1 for binary.
	// int Q = 1; // set Q to 1 if processing binary image
	WritePGM(fileName, data, M, N, Q); 

	return 0;
}


What this does is, Reads in my text file, then outputs it into PGM format which can be opened using the Software - ImageJ
I don't see any scrambling going on.

But I do see some data loss. Specifically you are casting doubles to unsigned chars, so if the value contains any floating point portion, or if it's below 0 or above 255 you will lose information. (15.3576 in the text file will be written as 15 in the pgm file).

There is no way to recover that data that is lost. You can however reconstruct a similar text file that will produce the exact same pgm file. You can do this by doing the opposite of what you're doing now.

What you're doing now:
- Reading doubles (as text) from a text file
- Writing them as unsigned chars (as binary) to a pgm file


To reverse it:
- Read unsigned chars (as binary) from the pgm file
- Write them (as text) to a text file
Yes, your right, it depends on the text file how the image.pgm looks like.. (noise or shuffled) and I can see how your method can work.
However, Im wanting to go out on a limb here and try a different way to this as it just seems a bit unchallenging and unrewarding dont you think? Could you think of any other way Disch?
For the Noise Image, I found that using Median Filtering works best to restore it back to the original image.

Im still worried about the shuffled image and how to restore this back to the original though.
Topic archived. No new replies allowed.