473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Pgm Image processing in c++.

Hello,
In the following code ,i am able to shrink an image using 2d pointer,but when writing the image to a file ,the file is not fully shrink(the pixels are cropped).

Could anyone please help me to sort out the problem in my code.



Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string>
  4. #include<conio.h>
  5. using namespace std;
  6.  
  7. void ReadPGM(ifstream& ipgm, string &f_name, string &f_type ,int **&ptr,int &rows,int &columns,int &maxgray,string &comment1);
  8. void Shrinkimage(ifstream& ipgm,int **&ptr,int &rows,int &columns,int **&ptr2); 
  9. //void writePGM(ofstream &opgm,ifstream&ipgm, string &ftype,string &f_type,int **&ptr,int &rows,int &columns,int &maxgray,string &comment1);
  10. void writePGM(ofstream &opgm,ifstream &ipgm,string &f_type,string &comment1,int &rows,int &columns,int &maxgray,int **&ptr2);
  11. int main (){
  12.  
  13.     ifstream ipgm;
  14.     ofstream opgm;
  15.     string f_name;
  16.     string f_type;
  17.     int userinput;
  18.     int rows,columns,maxgray;
  19.     string comment1,comment2;
  20.     int **ptr;
  21.     int **ptr2;
  22.  
  23.     cout<<"Enter the name of you pgm file(with extension):"<<endl;
  24.     //fflush(stdin);
  25.     getline(cin,f_name);
  26.  
  27.     ReadPGM(ipgm,f_name,f_type,ptr,rows,columns,maxgray,comment1);
  28.     cout<<"Enter ""1"" if you want to shrink a file:"<<endl;
  29.     cin>>userinput;
  30.     if(userinput==1)
  31.     {
  32.     Shrinkimage(ipgm,ptr,rows,columns,ptr2);
  33.     writePGM(opgm,ipgm,f_type,comment1,rows,columns,maxgray,ptr2); 
  34.     }
  35.  
  36.  
  37.     system("pause");
  38.     return 0;
  39.  
  40. }
  41.  
  42.  
  43. void ReadPGM(ifstream& ipgm, string &f_name, string &f_type ,int **&ptr,int &rows,int &columns,int &maxgray,string &comment1){
  44.  
  45.  
  46.                                     ipgm.open(f_name.c_str());
  47.                                     if(!ipgm) //if file not found
  48.                                     {
  49.                                     cout<<"File not found!"<<endl;
  50.                                     system("pause");
  51.                                     }
  52.  
  53.                                     getline(ipgm, f_type);
  54.  
  55.                                     getline(ipgm ,comment1);
  56.                                     //getline(ipgm ,comment2);
  57.  
  58.                                     ipgm>>columns; //width
  59.                                     ipgm>>rows; //height  
  60.                                     ipgm>>maxgray;
  61.                                     cout<<f_type<<endl<<comment1<<"    "<<columns<<"    "<<rows<<"    "<<maxgray<<endl;
  62.  
  63.                                    ptr =new int*[rows];
  64.  
  65.                                    for(int i=0;i<rows;i++)
  66.                                    {
  67.                                    ptr[i]=new int[columns];  
  68.                                    }
  69.  
  70.                                    for(int j=0; j<rows; j++)
  71.                                    {
  72.                                            for(int k=0;k<columns;k++)
  73.                                            {
  74.                                             ipgm>> ptr[j][k];
  75.                                             //cout << ptr[j][k];
  76.  
  77.                                            }
  78.                                    }
  79.  
  80. }
  81.  
  82. void writePGM(ofstream &opgm,ifstream &ipgm,string &f_type,string &comment1,int &rows,int &columns,int &maxgray,int **&ptr2){
  83.  
  84.  
  85.     string type1;
  86.     int rows2=rows/2;
  87.     int columns2=columns/2;
  88.     int **ptr3;
  89.     ptr3=new int * [rows];
  90.     for(int i=0;i<rows;i++)
  91.     {
  92.             ptr3[i]=new int [columns];
  93.     }
  94.  
  95.     //For shrink
  96.     opgm.open("Shrink.PGM");
  97.     opgm<<f_type<<endl;
  98.     opgm<<comment1<<endl;
  99.     opgm<<columns2<<" ";
  100.     opgm<<rows2<<endl;
  101.     opgm<<maxgray<<endl;
  102.  
  103.       for(int j=0,x=0; j<rows2; j+=2,x++)
  104.                 {
  105.                         for(int k=0,y=0;k<columns2;k+=2,y++)
  106.                         {
  107.                         opgm << ptr2[x][y]<<" "<<endl;
  108.  
  109.                         //cout << ptr2[x][y];
  110.  
  111.  
  112.  
  113.                         }
  114.                         opgm <<"\n ";
  115.                 }
  116.  
  117. }
  118.  
  119.  
  120.  
  121. void Shrinkimage(ifstream& ipgm,int **&ptr,int &rows,int &columns,int **&ptr2){
  122.      int x=0,y=0;
  123.      int rows2=rows/2;
  124.      int columns2=columns/2;
  125.      //int **ptr2;
  126.  
  127.      ptr2 = new int* [rows2];
  128.  
  129.      for(int i=0; i<rows2; i++)
  130.      {
  131.              ptr2[i] = new int [columns2];        
  132.      }
  133.  
  134.                 for(int j=0,x=0; j<rows2; j+=2,x++)
  135.                 {
  136.                         for(int k=0,y=0;k<columns2;k+=2,y++)
  137.                         {
  138.                         ptr2[x][y] = ptr[j][k];
  139.                        // cout << ptr2[x][y];
  140.  
  141.  
  142.                         }
  143.                 }
  144.  
  145. }
  146.  
Mar 11 '12 #1
2 3617
weaknessforcats
9,208 Expert Mod 8TB
I don't believe you will find anyone to debug ths code. There are no comments and no statement of what the code is to do and how it is to do it. Understanding a problem from the code only is like looking at an elephant from two inches. You only see that it is gray.

Have you stepped through each function with your debugger to confirm that the function works?
Mar 11 '12 #2
Thanks, for your advice i debugged my functions and found that error.Next time i'll care for debugging.
Mar 11 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Alexandre Fayolle | last post by:
Hello, I'm about to start a project which will involve some greyscale image processing using morphological operators (erosion, dilation, distance transforms...), and I was wondering if these...
0
by: Andreas Håkansson | last post by:
Hiya! I'm currently looking for a good image processig component (classes) to use in a project. I do not want to controls (winform or webform) since I will be doing this with code. I actually...
2
by: Maxwell2006 | last post by:
Hi, I am developing a simple image upload asp.net page and I am looking for a simple image processing component to be able to change the image resolution of jpg images and also reduce the...
10
by: stonny | last post by:
Hi, I am converting my matlab program into C/C++. I need to change some image processing toolbox functions into C/C++, such as edge detection, mathematical morphology. Is there any place that I...
5
by: edurand | last post by:
Hi, We are are pleased to announce the version 3.0 of the image processing library 'Filters'. You can use it in Python, and we have provided tutorials and samples in Python, with for exemple...
3
by: birensubudhi | last post by:
hey guys,can anyone tell me what is image processing, how to do it using C. IN KSHITIJ 2007 held at IIT kgp a que by INFOSYS is asked, they hav given an image then cut in different orientation...
26
by: mohangupta13 | last post by:
can anyone help me where to find the best image processing library for c++ mohan gupta
5
by: whisk3rs | last post by:
Hello, I have a conceptual question. I need to write a program that will take as input a list of images and then process each image individually (extract useful features from the image) ...
0
by: tavares | last post by:
(Our apologies for cross-posting. We appreciate if you kindly distribute this information by your co- workers and colleagues.) ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.