5 Replies - 4945 Views - Last Post: 17 April 2012 - 10:24 PM Rate Topic: ***** 1 Votes

#1 simagen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 10-November 10

Restoring Shuffled Image (Binary Img)

Posted 16 April 2012 - 07:32 AM

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

The program will be used to restore the original image from a standard, and also the same image with noise this time.

One thing at a time though so it doesn't get too complicated; I have created the following code that reads in a text file and outputs a .PGM file that can be read as an image using the software ImageJ. Download link here:

http://rsbweb.nih.gov/ij/download.html

My Code (which is not much use to you as the point is getting it from a PGM file to a un-shuffled image. Just showing where I am at now):

#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;
}


So what I need help with is how to un-shuffle a PGM image (4x4 pixels) back to the original picture.

Similarity and Dissimilarity techniques is a good start I would imagine?

Any help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Restoring Shuffled Image (Binary Img)

#2 turboscrew  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,047
  • Joined: 03-April 12

Re: Restoring Shuffled Image (Binary Img)

Posted 16 April 2012 - 07:57 AM

Quote

The program will be used to restore the original image from a standard, and also the same image with noise this time.


Are you writing a median filtering?
Was This Post Helpful? 0
  • +
  • -

#3 simagen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 10-November 10

Re: Restoring Shuffled Image (Binary Img)

Posted 16 April 2012 - 08:05 AM

View Postturboscrew, on 16 April 2012 - 02:57 PM, said:

Quote

The program will be used to restore the original image from a standard, and also the same image with noise this time.


Are you writing a median filtering?


I am not sure what that is, but I was told using various similarity and dissimilarity measures is the way to go about this?
Was This Post Helpful? 0
  • +
  • -

#4 turboscrew  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,047
  • Joined: 03-April 12

Re: Restoring Shuffled Image (Binary Img)

Posted 16 April 2012 - 10:23 PM

I asked, because I'm not sure what the program is supposed to do.

To me "suffled image" sounds like pixels in wrong order.
Median is a good filter for removing random noise from a signal or picture.
Was This Post Helpful? 0
  • +
  • -

#5 simagen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 10-November 10

Re: Restoring Shuffled Image (Binary Img)

Posted 17 April 2012 - 12:25 PM

Yes, shuffled image is simply the 4x4 pixels that have been broken up in a random order.

I have:

1 Original Image
1 Shuffled Image
1 Original Image with Noise this time

What im trying to make the program do is take the shuffled.pgm image file and compare it to the original.pgm image file and for it to run some sort of similarity algorithms/measures on it to get it back to the original.

Edit:

If the median filtering will work to put it back to all black and white again, then that would be the solution for that part! (thanks)

This post has been edited by simagen: 17 April 2012 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 turboscrew  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,047
  • Joined: 03-April 12

Re: Restoring Shuffled Image (Binary Img)

Posted 17 April 2012 - 10:24 PM

The similarity could be checked with cross-correlation (just 2 dimensional signal). It shows how close you are, but doesn't give you which pixels are wrong.

You'll probably find better ways to handle the shuffled image if you search the net with words "image" and "encryption".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1