当前位置:   article > 正文

OpenCV自用二值化图像处理代码_opnecv二值化代码

opnecv二值化代码

OpenCV二值化

用于处理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;
}
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/954916
推荐阅读
相关标签
  

闽ICP备14008679号