int main(int argc, char **argv) // argv are the command-line arguments { char infname[512], outfname[512]; // file names for input and output unsigned char imagetypein, imagetypeout; IMAGE *data; IMAGE *target; if(argc<5) // too few command-line arguments? { printf("Command-line usage:\n"); printf(" %s (inf) (outf) (x) (y)\n",argv[0]); printf(" (inf) is the input file name\n"); printf(" (outf) is the output file name\n"); printf(" (x), (y), are the image dimensions\n"); exit(0); } // Allocate local memory for struct pointers data = malloc(sizeof(IMAGE)); // Handle Command Line args strcpy(infname,nextargs); // read input file name strcpy(outfname,nextargs); // read output file name data->xmax = nextargi; // Read image dimensions data->ymax = nextargi; data->zmax = 1; // params set image data types in and out imagetypein = FLOATIMAGE; imagetypeout = UCHARIMAGE; // Read Image into Mem printf("Reading image %s with dimensions %d, %d, %d\n",infname,data->xmax,data->ymax,data->zmax); if(read_raw(infname, data)){ printf("Error reading file\n"); exit (1); } // Set target params, Allocate local memory target = malloc(sizeof(IMAGE)); if(copyimage(target, data, imagetypeout)){ fprintf(stderr,"Could not Create Image: target\n"); exit(-1); } /* Image Processing calls here */ printf(" Converting Data Types...\n"); //switch case with command line parameters to specify data type conversions if(float2uchar(target, data)){ printf("Error Converting\n"); exit(3); } // Write Image to File printf("Writing processed image %s with dimensions %d, %d, %d\n",outfname,target->xmax,target->ymax,target->zmax); if(write_raw(outfname, target)){ printf("Error writing file\n"); exit (4); } // Free All Memory removeimage(data,imagetypein); removeimage(target,imagetypeout); printf("Program completed successfully\n"); exit (0); }
Mat OPert_ima(Mat& ori_ima,int type) { if( (type<0) || (type>6) ) { cout<<"请输入正确的type(0,1,2,3,4,5,6)!"<<endl; Sleep(2000); exit(-1); } float operat[][9] = {{1.0/9, 1.0/9, 1.0/9, 1.0/9, 1.0/9, 1.0/9, 1.0/9, 1.0/9, 1.0/9}, {0, -1.0/4, 0, -1.0/4, 1.0, -1.0/4, 0, -1.0/4, 0}, {-1, 0, 1, -1, 0, 1, -1, 0, 1}, {-1, -1, -1, 0, 0, 0, 1, 1, 1}, {-3, 0, 3, -10, 0, 10, -3, 0, 3}, {-3, -10, -3, 0, 0, 0, 3, 10, 3}, {0, 0, 0, 0, 1, 0, 0, 0, 0}}; Mat new_ima(ori_ima.rows, ori_ima.cols, CV_8U, Scalar(0)); // 最终输出图像 Mat float_ima(ori_ima.rows, ori_ima.cols, CV_32F, Scalar(0)); // 中间float图像,保证精度 //float_ima=uchar2float(ori_ima); // uchar型图像转换为float型图像 for (int i=1; i<ori_ima.rows-1; i++) { float* data = float_ima.ptr<float>(i); uchar* data0 = ori_ima.ptr<uchar>(i-1); uchar* data1 = ori_ima.ptr<uchar>(i); uchar* data2 = ori_ima.ptr<uchar>(i+1); for (int j=1; j<ori_ima.cols; j++) { data[j] = data0[j-1] * operat[type][0] + data0[j] * operat[type][1] + data0[j+1] * operat[type][2] + data1[j-1] * operat[type][3] + data1[j] * operat[type][4] + data1[j+1] * operat[type][5] + data2[j-1] * operat[type][6] + data2[j] * operat[type][7] + data2[j+1] * operat[type][8]; } } new_ima=float2uchar(float_ima); // float型图像转换为uchar型图像 return new_ima; }