⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 segment.cpp

📁 the code write C++. it is in image processing field. this code Read TIFF Image then it segment image
💻 CPP
字号:
// Segment.cpp: implementation of the Segment class.
//
//////////////////////////////////////////////////////////////////////

#include "Segment.h"
#include <Math.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<io.h>
#include <ctype.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Segment::Segment()
{

}

Segment::~Segment()
{

}

//////////////////////////////////////////////////////////////////////
void Segment::LTransformation(float *OutputImage, int NBL, int NBC)
{
//////////////////////////////////////////////////////////////////////
	int i,j,k,l;
	float min = 0, max = 255;
//////////////////////////////////////////////////////////////////////
	for (i=0; i<NBL; i++)
		for (j=0; j<NBC; j++)
		{
			if (min > OutputImage[j+(i*NBL)]) min = OutputImage[j+(i*NBL)];
			if (max < OutputImage[j+(i*NBL)]) max = OutputImage[j+(i*NBL)];
		}


	float a = 255 / max-min;
	float b = -a * min;

		if (max > 255)
		{
			for (k=0; k<NBL; k++)
				for (l=0; l<NBC; l++)
				{
					OutputImage[(k*NBL)+l] = a * (OutputImage[(k*NBL)+l]) + b;
				}
		}

}


//////////////////////////////////////////////////////////////////////

void Segment::Histogram(float *InputImage, int NBL, int NBC)
{
//////////////////////////////////////////////////////////////////////
	int i,j;
//////////////////////////////////////////////////////////////////////
	for (i=0; i < 256; i++ )
		H[i]=0;
		
	for (j=0; j< NBL*NBC; j++ )
		H[int(InputImage[j])] += 1;

	

}

//////////////////////////////////////////////////////////////////////

void Segment::SegmentWithM(float *InputImage, float *OutputImage, int NBL, int NBC)
{
//////////////////////////////////////////////////////////////////////

	int M;
	int *T;
	float R;

	cout << "input the value of modes";
	cin >> M;

	int count_Threshold= M - 1;
	float intensit=0.0;
	float intensit_Dif = (float)255.0 / (float)count_Threshold; 

	T = new int [count_Threshold];
//////////////////////////////////////////////////////////////////////

	cout <<"input " << count_Threshold <<"  integer number ofthresholds.\n";

	for (int i=0 ; i<count_Threshold ; i++)
	{
		cout << "input threshold  value ";
		cin >> T[i];
	}
	

	for (i=0; i < NBL*NBC; i++)
	{
		R = InputImage[i];

		if (R <= ((float)T[0]))
		{
			OutputImage[i] = 0;
		}
		else if (count_Threshold != 1)
		{
			intensit = intensit_Dif;
			for (int j=1; j<count_Threshold; j++)
			{
				intensit += intensit_Dif;
				if (R > ((float)T[j-1]) && R <= ((float)T[j]))
				{
					OutputImage[i] = intensit;
					break;
				}
			}
			if(R > ((float)T[count_Threshold-1]))
				OutputImage[i] = 255;
		}
		else
		{
			OutputImage[i] = 255;
		}
	}


}
//////////////////////////////////////////////////////////////////////
void Segment::OtsuThreshold(float *InputImage, float *OutputImage, int NBL, int NBC)
{

//////////////////////////////////////////////////////////////////////
	int i,t;
	float Mean1 = 0.0, Mean2 = 0.0,TotalMean = 0.0;               
	float threshold = 0, Tnew;
	float weight1 =0.0, weight2=0.0, temp1=0.0, temp2=0.0;	
	float Sum = 0.0, SumG = 0.0, SumH[256], SumHG[256];
	bool flag = false;
//////////////////////////////////////////////////////////////////////

		Histogram(InputImage, NBL, NBC);

		for (i=0; i<=255; i++)
	{

		Sum += H[i];
		SumH[i] = Sum;
		
		SumG += H[i] * i; 
		SumHG[i] = SumG;

	}

	TotalMean = SumHG[255] / SumH[255];

	cout <<"==========================================="<< endl;
	cout <<"The total of means " << TotalMean << endl;


	threshold = TotalMean;
	t = threshold;


	while (!flag) 
	{
		weight1 = SumH[t];
		weight2 = SumH[255] - SumH[t];

		if (SumH[t] != 0.0)
			Mean1 = SumHG[t] / SumH[t];
		else 
			Mean1 = 0.0;

		if ((SumH[255]-SumH[t]) != 0.0)
			Mean2 = (SumHG[255]-SumHG[t]) / (SumH[255]-SumH[t]);
		else
			Mean2 = 0.0;

		Tnew = (Mean1 + Mean2) / 2;

		cout << "threshold : " << threshold << ", Tnew :  " << Tnew << endl;


				if (abs(threshold - Tnew) < 1.0)
		{
			cout << "\nFound optimal threshold value: " << t << endl << endl;
			flag = true;
		}
		else
		{
			threshold = Tnew;
			t = Tnew;
		}
	
	}


	float R;

	for (i=0; i < NBL*NBC; i++)
	{
		R = InputImage[i];

		if (R <= t)
		{
			OutputImage[i] = 0;

		}
		else
		{
			OutputImage[i] = 255;
		}
	}


}
//////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -