Could you check the error, please?
I find the problem, the problem is the pointer problem for (*rdatap)[i][j]
Anyone can tell me why and how to solve this?
The problem is the evaluation from matrix to pointer. The values are changed.
Example:
-----the 0 row---0----colomn---
R=253 G=253 B=253
changed to
-----the 0 row---0----colomn---
R=253 G=12 B=12
This is a simple C++ program to read the data from RGB image, and then output the image into another file without any processing. For this image, the R, G, B values are equal to render the image gray scale.But the result is that they are not equal
Through debug, I find that the problem is in main() after the call of the function ::matrix2Pointer(),
Thank you for your reading and help!
Also, I find that the evaluation changed the value of (*gdatap)[0][0] into 12 when it execute the sentence (*gdatap)[192][0] = ImageData[192][0][1].
In addition, I find the latter evaluation begins to change the previous values after the row 165, from row 166
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
|
#define X_SIZE 272
#define Y_SIZE 173
#define BYTESPERPIXEL 3
#include "FileFuncRGB.h"
using namespace std;
int main()
{
char *s;
int **rdata;
int **gdata;
int **bdata;
unsigned char imagedata[X_SIZE][Y_SIZE][BYTESPERPIXEL];
s ="images/pcb.raw";
readMatrix(imagedata, s);
matrix2Pointer(imagedata, &rdata, &gdata, &bdata);
/*for(int i =0;i<X_SIZE; i++){
for(int j =0; j<Y_SIZE; j++){
imagedata[i][j][0] = 0;
imagedata[i][j][1] = 0;
imagedata[i][j][2] = 0;
cout<<rdata[i][j]<<" "<<gdata[i][j]<<" "<<bdata[i][j]<<endl;
}
cout<<endl;
}*/
s = "images/pcb_out_1.raw";
pointer2Matrix(imagedata, rdata, gdata, bdata);
writeMatrix(imagedata, s);
return 0;
}
|
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#ifndef FILEFUNCRGB_H_
#define FILEFUNCRGB_H_
#ifdef X_SIZE
#ifdef Y_SIZE
#ifdef BYTESPERPIXEL
#include <iostream>
#include <stdio.h>
#include "../src/def.h"
using namespace std;
void readMatrix(unsigned char ImageData[X_SIZE][Y_SIZE][BYTESPERPIXEL], char *filename){
FILE *file;
file=fopen(filename,"rb");
cout<<"----------reach the file "<<filename<<" fopen------------"<<endl;
// Read image (filename specified by first argument) into image data matrix
if (file==NULL) {
cout << "Cannot open file: "<<filename <<endl;
exit(1);
}
fread(ImageData, sizeof(unsigned char), (X_SIZE)*(Y_SIZE)*(BYTESPERPIXEL), file);
fclose(file);
cout<<"----------file open is over ------------"<<endl;
}
void matrix2Pointer(unsigned char ImageData[X_SIZE][Y_SIZE][BYTESPERPIXEL], int ***rdatap,
int ***gdatap, int ***bdatap){
(*rdatap)= new int *[Y_SIZE];
(*gdatap)= new int *[Y_SIZE];
(*bdatap)= new int *[Y_SIZE];
for(int i=0;i<X_SIZE;i++){
(*rdatap)[i] = new int[Y_SIZE];
(*gdatap)[i] = new int[Y_SIZE];
(*bdatap)[i] = new int[Y_SIZE];
}
for(int i=0;i<X_SIZE;i++){
for(int j=0;j<Y_SIZE;j++){
//cout<<"-----the "<<i<<" row---"<<j<<"----colomn---"<<endl;
(*rdatap)[i][j] = (signed int)ImageData[i][j][0];
(*gdatap)[i][j] = (signed int)ImageData[i][j][1];
(*bdatap)[i][j] = (signed int)ImageData[i][j][2];
if((*rdatap)[i][j]<0){
(*rdatap)[i][j]=0;
}
if((*gdatap)[i][j]<0){
(*gdatap)[i][j]=0;
}
if((*bdatap)[i][j]<0){
(*bdatap)[i][j]=0;
}
if((*rdatap)[i][j]>=L_LUM){
(*rdatap)[i][j]=L_LUM-1;
}
if((*gdatap)[i][j]>=L_LUM){
(*gdatap)[i][j]=L_LUM-1;
}
if((*bdatap)[i][j]>=L_LUM){
(*bdatap)[i][j]=L_LUM-1;
}
//cout<<(*rdatap)[i][j];
}
//cout<<endl;
}
//cout<<endl;
}
void writeMatrix(unsigned char ImageData[X_SIZE][Y_SIZE][BYTESPERPIXEL], char *filename){
FILE *file;
file=fopen(filename,"wb");
cout<<"----------reach the file "<<filename<<" fwrite------------"<<endl;
// Read image (filename specified by first argument) into image data matrix
if (file==NULL) {
cout << "Cannot open file: "<<filename <<endl;
exit(1);
}
fwrite(ImageData, sizeof(unsigned char), X_SIZE*Y_SIZE*BYTESPERPIXEL, file);
fclose(file);
cout<<"----------file write is over ------------"<<endl;
}
void pointer2Matrix(unsigned char ImageData[X_SIZE][Y_SIZE][BYTESPERPIXEL], int **rdata,
int **gdata, int **bdata){
for(int i=0;i<X_SIZE;i++){
//cout<<"-----the "<<i<<" row------"<<endl;
for(int j=0;j<Y_SIZE;j++){
ImageData[i][j][0] = (unsigned char)rdata[i][j];
ImageData[i][j][1] = (unsigned char)gdata[i][j];
ImageData[i][j][2] = (unsigned char)bdata[i][j];
//cout<<rdata[i][j]<<" "<<gdata[i][j]<<" "<<bdata[i][j]<<endl;
}
//cout<<endl;
}
//cout<<endl;
}
void deletePointer(int **rdata, int **gdata, int **bdata){
for(int i=0;i<X_SIZE;i++){
delete [] rdata[i];
delete [] gdata[i];
delete [] bdata[i];
}
delete [] rdata;
delete [] gdata;
delete [] bdata;
}
#endif
#endif
#endif
#endif /* FILEFUNCRGB_H_ */
|