赞
踩
用于处理XCT断层扫描数据图,原图为32位的位图,由于我还不会处理,先转成了8位的位图,然后用以下代码二值化,并重建成三维结构,输出成为文件
很多的没必要的代码我并没有删除,都是程序调试时,实验OpenCV函数的,自己看哪个有用,去查手册比较好
由于绝大多数代码都是用来测试,并没有写的特别规整,请见谅
比较重要的函数imread和imshow,以及基本数据结构Mat,阈值函数threshold和adaptiveThreshold
代码千万不要像我写的这样,这么乱
#include <iostream> #include <cstdio> #include <fstream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> int main(int argc,char *argv[]) { if (argc != 5) { std::cout << "have some mistake in parameter\n"; std::cout << "the struct of input\n"; std::cout << "1. the compare block of MEAN\n"; std::cout << "2. the parameter C of MEAN\n"; std::cout << "3. the compare block of GAUSSIAN\n"; std::cout << "4. the parameter C of GAUSSIAN\n"; exit(EXIT_FAILURE); } int MEANblock=atoi(argv[1]); int MEANc=atoi(argv[2]); int GAUSSIANblock=atoi(argv[3]); int GAUSSIANc=atoi(argv[4]); cv::Mat image; cv::Mat result1,result2; std::string inputName; std::ofstream foutMean; foutMean.open("Mean.dat", std::ios::ate); std::ofstream foutGaussian; foutGaussian.open("Gaussian.dat", std::ios::ate); int sumMean=0; int sumGaussian=0; int total=0; for(int t=25;t<80;t++){ std::string str1="sli-00"; std::string str2=".png"; std::ostringstream oss; oss<<str1<<t<<str2; //std::cout<<oss.str()<<std::endl; image=cv::imread(oss.str(), cv::IMREAD_GRAYSCALE); oss.clear(); //cv::threshold(image,result,105,255,cv::THRESH_TOZERO); //cv::adaptiveThreshold(image, result1, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 9, -3); //cv::adaptiveThreshold(image, result2, 255, cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY, 11, -2); cv::adaptiveThreshold(image, result1, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, MEANblock, MEANc); cv::adaptiveThreshold(image, result2, 255, cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY, GAUSSIANblock, GAUSSIANc); int rows=image.rows; int cols=image.cols; int center=rows/2; int radius=rows/2-10; for (int i=0; i<rows ; i++) { for (int j=0; j<cols ; j++) { //std::cout<<(int)result1.at<uchar>(i, j)<<"\t"; int meanvalue=(int)result1.at<uchar>(i, j); int gaussianvalue=(int)result2.at<uchar>(i, j); if(std::pow(i-center,2)+std::pow(j-center,2)<std::pow(radius,2)){ foutMean<<meanvalue<<"\t"; foutGaussian<<gaussianvalue<<"\t"; total++; sumMean+=meanvalue/255; sumGaussian+=gaussianvalue/255; }else{ foutMean<<0<<"\t"; foutGaussian<<0<<"\t"; } } } } //printf("the Mean data voidage:%d,%d,%f\n",sumMean,total,1-(double)sumMean/(double)total); //printf("the Gaussian data voidage:%d,%d,%f\n",sumGaussian,total,1-(double)sumGaussian/(double)total); printf("the Mean data voidage:%f\n",1-(double)sumMean/(double)total); printf("the Gaussian data voidage:%f\n",1-(double)sumGaussian/(double)total); cv::namedWindow("origin"); cv::imshow("origin",image); cv::namedWindow("mean"); cv::imshow("mean",result1); cv::namedWindow("gaussian"); cv::imshow("gaussian",result2); cv::waitKey(); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。