📄 inrcast.cpp
字号:
// Compute false colors if (a_falsecolors) { std::fprintf(stderr,"\n- Compute false colors\t"); std::fflush(stderr); cimglist_for(src,l) { CImg<unsigned char> tmp = src[l].get_normalize((T)0,(T)255); cimg_forXYZV(tmp,x,y,z,k) src[l](x,y,z,k) = (T)cimg::ror((unsigned char)tmp(x,y,z,k),4); } } // Compute image histogram if (a_histo) { std::fprintf(stderr,"\n- Compute histogram(s)\t"); std::fflush(stderr); cimglist_for(src,l) src[l] = src[l].get_histogram(a_histo); } // Image break if (a_break) { int nb = 0; std::sscanf(a_break,"%*[xyzvXYZV]%d",&nb); std::fprintf(stderr,"\n- Break image along axis '%c'\t",a_break[0]); std::fflush(stderr); CImgList<T> tmp; cimglist_for(src,l) tmp.insert(src[l].get_split(a_break[0],nb)); src = tmp; } // Images append if (a_append!='\0') { std::fprintf(stderr,"\n- Append images along axis '%c'\t",a_append); std::fflush(stderr); src = CImgList<T>(src.get_append(a_append,'p')); } // User-selected crop if (a_ucrop) { std::fprintf(stderr,"\n- User crop : Please select the image region to crop.\t"); std::fflush(stderr); int selection[6] = {-1,-1,-1,-1,-1,-1}; src[0].feature_selection(selection,2); if (selection[0]<0 || selection[1]<0 || selection[2]<0 || selection[3]<0 || selection[4]<0 || selection[5]<0) std::fprintf(stderr,".. Aborted.."); else { std::fprintf(stderr,"-> Crop [%d,%d,%d]-[%d,%d,%d]", selection[0],selection[1],selection[2], selection[3],selection[4],selection[5]); cimglist_for(src,l) src[l].crop(selection[0],selection[1],selection[2],selection[3],selection[4],selection[5]); } } // Apply logarithm, square root or square if (a_log) cimglist_for(src,l) (src[l]+=(T)1e-10f).log(); if (a_sqrt) cimglist_for(src,l) src[l].sqrt(); if (a_sqr) cimglist_for(src,l) src[l].pow(2.0); // Unroll image if (a_unroll) { std::fprintf(stderr,"\n- Unroll image along axis '%c'.\t",a_unroll); std::fflush(stderr); cimglist_for(src,l) src[l].unroll(a_unroll); } // Permute image axes if (a_permute) { std::fprintf(stderr,"\n- Permute axes with permutation '%s'.\t",a_permute); std::fflush(stderr); cimglist_for(src,l) src[l].permute_axes(a_permute); } // Annotate images if (a_annot) { std::fprintf(stderr,"\n- Annotate image with \"%s\".\t",a_annot); std::fflush(stderr); cimglist_for(src,l) { const CImg<T> col(src[l].dimv(),1,1,1,(T)CImgStats(src[l],false).max); src[l].draw_text(a_annot,5,5,col.ptr()); } } // Reverse image list if (a_reverse) { std::fprintf(stderr,"\n- Reverse image list"); std::fflush(stderr); src.reverse(); } // Convert image to destination type std::fprintf(stderr,"\n- Conversion to '%s' pixels\t",cimg::type<t>::id()); std::fflush(stderr); CImgList<t> dest(src); std::fprintf(stderr,"\n- Output images list : %d image(s)\n",dest.size); {cimglist_for(dest,l) { const CImgStats stats(dest[l]); std::fprintf(stderr,"\t> [%d] : size=%dx%dx%dx%d, min=%g, mean=%g, max=%g, var=%g\n", l,dest[l].dimx(),dest[l].dimy(),dest[l].dimz(),dest[l].dimv(), stats.min,stats.mean,stats.max,stats.variance); }} // Save destination image if (file_o) { std::fprintf(stderr,"\n- Save image(s) to '%s'\t",file_o); std::fflush(stderr); dest.save(file_o); } // Display destination image if (a_visu || !file_o) { std::fprintf(stderr,"\n- Visualize image(s)\n"); std::fflush(stderr); if ( dest.size==1 && ( ( dest[0].dimy()==1 && dest[0].dimz()==1 ) || ( dest[0].dimx()==1 && dest[0].dimz()==1 ) || ( dest[0].dimx()==1 && dest[0].dimy()==1 ) || ( dest[0].dimx()==1 && dest[0].dimy()==1 && dest[0].dimz()==1))) { // 1D plot representation CImg<t> resized_img; const CImg<t>& img = dest[0].dimv()<=8?dest[0]:(resized_img=dest[0].get_resize(-100,-100,-100,8,0)); CImgStats stats(img,false); CImg<unsigned char> palette = CImg<unsigned char>(3,img.dimv(),1,1,0).noise(300,1); const unsigned char gray[3]={128,128,128},white[3]={255,255,255}; unsigned long trame = 0x0F0F0F0F; char message[1024]; palette[0]=255; palette[1]=0; palette[2]=0; if (img.dimv()>1) { palette[3]=0; palette[4]=255; palette[5]=0; } if (img.dimv()>2) { palette[6]=0; palette[7]=0; palette[8]=255; } CImgDisplay disp(500,300,"1D plot of the output image",0); CImg<unsigned char> visu0; do { if (!visu0.data || disp.is_resized) { // plot graphics disp.resize(); visu0 = CImg<unsigned char>(disp.dimx(),disp.dimy(),1,3,0); if (img.dimv()==1) visu0.draw_graph(img,palette.ptr(),1,stats.max,stats.min,0.4f); cimg_forV(img,k) visu0.draw_graph(img.get_shared_channel(k),&palette(0,k),0,stats.max,stats.min); visu0.draw_axis(CImg<unsigned int>::sequence(1+visu0.width/60,0,(int)img.size()-1), CImg<double>::sequence(1+visu0.height/60,stats.max,stats.min), gray); } CImg<unsigned char> visu(visu0); if (disp.mouse_x>=0) { visu.draw_line(disp.mouse_x,0,disp.mouse_x,visu.dimy()-1,gray,trame,0.5); trame = cimg::rol(trame); const unsigned x = disp.mouse_x*img.size()/(img.dimv()*disp.dimx()); std::sprintf(message,"x=%d\n[ ",x); cimg_forV(img,k) std::sprintf(message+std::strlen(message),"%s%g%s",k==0?"":" ", (float)img(x,0,0,k),k==img.dimv()-1?"":"\n"); std::sprintf(message+std::strlen(message)," %s]",dest[0].dimv()<=8?"":"\n..."); visu.draw_text(message,disp.mouse_x-20,10,white,NULL,1); } else visu=visu0; disp.display(visu).wait(40); } while (!disp.is_closed && !disp.key); } else dest.display("inrcast"); }}//---------------------------------------------------------// Read input image and do conversion to output pixel type//---------------------------------------------------------template<typename T> void input_file(int argc, char **argv, T& pixel_type) { pixel_type = 0; // Read input image list std::fprintf(stderr,"- Load image(s)...\t"); std::fflush(stderr); CImgList<T> src; CImgList<char> filename; bool opt = false; for (int k=1; k<argc; k++) if (argv[k][0]=='-' && argv[k][1]!='\0' && argv[k][1]!='.') opt=true; else { if (!opt || !cimg::strcasecmp(argv[k-1],"-h") || !cimg::strcasecmp(argv[k-1],"-orient") || !cimg::strcasecmp(argv[k-1],"-norm") || !cimg::strcasecmp(argv[k-1],"-equalize") || !cimg::strcasecmp(argv[k-1],"-crop") || !cimg::strcasecmp(argv[k-1],"-plot") || !cimg::strcasecmp(argv[k-1],"-visu") || !cimg::strcasecmp(argv[k-1],"-get_size") || !cimg::strcasecmp(argv[k-1],"-get_width") || !cimg::strcasecmp(argv[k-1],"-get_height") || !cimg::strcasecmp(argv[k-1],"-get_depth") || !cimg::strcasecmp(argv[k-1],"-get_dim") ) { std::fprintf(stderr,"\n\t> Loading [%d]='%s'\t",k-1,argv[k]); std::fflush(stderr); CImgList<T> tmpl; const char *ext = cimg::filename_split(argv[k]); if (!cimg::strcasecmp(ext,"yuv")) { unsigned int w=352,h=288; const char *geom = cimg_option("-g","352x288",NULL); if (geom) std::sscanf(geom,"%u%*c%u",&w,&h); unsigned int frame_first=0; int frame_last=-1; const char *frames = cimg_option("-yuvframes",(const char*)NULL,"YUV range of frames"); if (frames) std::sscanf(frames,"%u%*c%d",&frame_first,&frame_last); tmpl.load_yuv(argv[k],w,h,frame_first,frame_last,true); } else { if (!cimg::strcasecmp(ext,"raw")) { unsigned int w=0,h=1,d=1,v=1; const char *geom = cimg_option("-g",(char*)NULL,NULL); if (geom) std::sscanf(geom,"%u%*c%u%*c%u%*c%u",&w,&h,&d,&v); else { std::fprintf(stderr,"\ninrcast : You must specify an input geometry (option -g) for .raw images\n"); std::exit(0); } const bool multiplexed = cimg_option("-multiplexed",false,"Use multiplexed mode"); const bool endian_swap = cimg_option("-endian",false,"Big/Little endian mode"); tmpl = CImgList<T>(CImg<T>::get_load_raw(argv[k],w,h,d,v,multiplexed,endian_swap)); } else { if (!cimg::strcasecmp(ext,"rgb")) { unsigned int w=0,h=1; const char *geom = cimg_option("-g",(char*)NULL,NULL); if (geom) std::sscanf(geom,"%u%*c%u",&w,&h); else { std::fprintf(stderr,"\ninrcast : You must specify an input geometry (option -g) for .rgb images\n"); std::exit(0); } tmpl = CImgList<T>(CImg<T>::get_load_rgb(argv[k],w,h)); } else { if (!cimg::strcasecmp(ext,"rgba")) { unsigned int w=0,h=1; const char *geom = cimg_option("-g",(char*)NULL,NULL); if (geom) std::sscanf(geom,"%u%*c%u",&w,&h); else { std::fprintf(stderr,"\ninrcast : You must specify an input geometry (option -g) for .rgba images\n"); std::exit(0); } tmpl = CImgList<T>(CImg<T>::get_load_rgba(argv[k],w,h)); } else tmpl = CImgList<T>(argv[k]); } } } const CImg<char> name(argv[k],cimg::strlen(argv[k])+1); if (tmpl.size==1) filename.insert(name); else cimglist_for(tmpl,l) { char s[1024]; std::sprintf(s,"%s [%d]",name.data,l); filename.insert(CImg<char>(s,cimg::strlen(s)+1)); } src.insert(tmpl); } opt=false; } std::fputc('\n',stderr); // Convert to output pixel type (if specified) const char *typei = cimg_option("-i","float",NULL); const char *typeo = cimg_option("-t",typei,NULL); bool saved = false; if (!saved && !cimg::strcasecmp(typeo,"bool")) { bool t=false; output_file(argc,argv,src,filename,t); saved=true; } if (!saved && !cimg::strcasecmp(typeo,"char")) { char t=0; output_file(argc,argv,src,filename,t); saved=true; } if (!saved && !cimg::strcasecmp(typeo,"uchar")) { unsigned char t=0; output_file(argc,argv,src,filename,t); saved=true; } if (!saved && !cimg::strcasecmp(typeo,"ushort")) { unsigned short t=0; output_file(argc,argv,src,filename,t); saved=true; } if (!saved && !cimg::strcasecmp(typeo,"short")) { short t=0; output_file(argc,argv,src,filename,t); saved=true; } if (!saved && !cimg::strcasecmp(typeo,"uint")) { unsigned int t=0; output_file(argc,argv,src,filename,t); saved=true; } if (!saved && !cimg::strcasecmp(typeo,"int")) { int t=0; output_file(argc,argv,src,filename,t); saved=true; } if (!saved && !cimg::strcasecmp(typeo,"ulong")) { unsigned long t=0; output_file(argc,argv,src,filename,t); saved=true; } if (!saved && !cimg::strcasecmp(typeo,"long")) { long t=0; output_file(argc,argv,src,filename,t); saved=true; } if (!saved && !cimg::strcasecmp(typeo,"float")) { float t=0; output_file(argc,argv,src,filename,t); saved=true; } if (!saved && !cimg::strcasecmp(typeo,"double")) { float t=0; output_file(argc,argv,src,filename,t); saved=true; } if (!saved) { std::fprintf(stderr,"inrcast : Cannot save image with pixel type '%s' (see help)",typeo); std::exit(0); }}//----------------// Main procedure//----------------int main(int argc,char **argv) { cimg_usage("1D/2D/3D Image(s) converter and visualizer.\n\t\tusage : inrcast [options] image_in1 [image_in2] [...] [-o image_out]"); if (argc==1) { std::fprintf(stderr,"Try '%s -h' to get help\n",cimg::basename(argv[0])); std::exit(0); } // Read input image and call converter bool casted = false; const char *typei = cimg_option("-i","float",NULL); if (!casted && !cimg::strcasecmp(typei,"bool")) { bool t=false; input_file(argc,argv,t); casted=true; } if (!casted && !cimg::strcasecmp(typei,"char")) { char t=0; input_file(argc,argv,t); casted=true; } if (!casted && !cimg::strcasecmp(typei,"uchar")) { unsigned char t=0; input_file(argc,argv,t); casted=true; } if (!casted && !cimg::strcasecmp(typei,"ushort")) { unsigned short t=0; input_file(argc,argv,t); casted=true; } if (!casted && !cimg::strcasecmp(typei,"short")) { short t=0; input_file(argc,argv,t); casted=true; } if (!casted && !cimg::strcasecmp(typei,"uint")) { unsigned int t=0; input_file(argc,argv,t); casted=true; } if (!casted && !cimg::strcasecmp(typei,"int")) { int t=0; input_file(argc,argv,t); casted=true; } if (!casted && !cimg::strcasecmp(typei,"ulong")) { unsigned long t=0; input_file(argc,argv,t); casted=true; } if (!casted && !cimg::strcasecmp(typei,"long")) { long t=0; input_file(argc,argv,t); casted=true; } if (!casted && !cimg::strcasecmp(typei,"float")) { float t=0; input_file(argc,argv,t); casted=true; } if (!casted && !cimg::strcasecmp(typei,"double")) { float t=0; input_file(argc,argv,t); casted=true; } if (!casted) { std::fprintf(stderr,"Cannot load image pixel type '%s' (see help)",typei); std::exit(0); } // Return to shell std::fprintf(stderr,"Done !\n"); std::exit(0); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -