📄 mssb1dlg.cpp
字号:
tempT.y = 309 - point.y;
LinePoint[0].x = LinePoint[0].y = 0;
LinePoint[1].x = LinePoint[1].y = 0;
//////得到权矢量W1,W2,W3的值////////
GetW(LineNum);
}
}
}
CDialog::OnLButtonUp(nFlags, point);
}
void CMSSB1Dlg::OnButton3()
{
// TODO: Add your control notification handler code here
readBmp(); //读取bmp图片rgb值
UpdateData();
CClientDC dc(this);
for(int i=0;i<332;i++)
{
for(int j=0;j<234;j++)
{
BYTE r= GetRValue(color[i][j]);
BYTE g= GetGValue(color[i][j]);
BYTE b= GetBValue(color[i][j]);
if(m_sel3ClassQuestion == 0) //第一种情况
{
if(m_selRGB == 0) //RG坐标
{
float m1 = w[0][0]*r + w[0][1]*g + w[0][2];
float m2 = w[1][0]*r + w[1][1]*g + w[1][2];
float m3 = w[2][0]*r + w[2][1]*g + w[2][2];
if(m1 > 0 && m2 <0 && m3 <0)
{
dc.SetPixel(i+663,j+50,RGB(255,0,0));
}
else if(m2 < 0 && m1 < 0 && m3 > 0)
{
dc.SetPixel(i+663,j+50,RGB(0,255,0));
}
else if(m3 < 0 && m1 < 0 && m2 > 0)
{
dc.SetPixel(i+663,j+50,RGB(0,0,255));
}
else dc.SetPixel(i+663,j+50,RGB(0,0,0));
}
if(m_selRGB == 1) //RB坐标
{
float m1 = w[0][0]*r + w[0][1]*b + w[0][2];
float m2 = w[1][0]*r + w[1][1]*b + w[1][2];
float m3 = w[2][0]*r + w[2][1]*b + w[2][2];
if(m1 > 0 && m2 < 0 && m3 < 0)
{
dc.SetPixel(i+663,j+50,RGB(255,0,0));
}
else if(m2 < 0 && m1 < 0 && m3 > 0)
{
dc.SetPixel(i+663,j+50,RGB(0,255,0));
}
else if(m3 < 0 && m1 < 0 && m2 > 0)
{
dc.SetPixel(i+663,j+50,RGB(0,0,255));
}
else dc.SetPixel(i+663,j+50,RGB(0,0,0));
}
if(m_selRGB == 2) //GB坐标
{
float m1 = w[0][0]*g + w[0][1]*b + w[0][2];
float m2 = w[1][0]*g + w[1][1]*b + w[1][2];
float m3 = w[2][0]*g + w[2][1]*b + w[2][2];
if(m1 > 0 && m2 < 0 && m3 < 0)
{
dc.SetPixel(i+663,j+50,RGB(255,0,0));
}
else if(m2 < 0 && m1 < 0 && m3 > 0)
{
dc.SetPixel(i+663,j+50,RGB(0,255,0));
}
else if(m3 < 0 && m1 < 0 && m2 > 0)
{
dc.SetPixel(i+663,j+50,RGB(0,0,255));
}
else dc.SetPixel(i+663,j+50,RGB(0,0,0));
}
}
if(m_sel3ClassQuestion == 1) //第二种情况
{
if(m_selRGB == 0) //RG坐标
{
float m1 = w[0][0]*r + w[0][1]*g + w[0][2];
float m2 = w[1][0]*r + w[1][1]*g + w[1][2];
float m3 = w[2][0]*r + w[2][1]*g + w[2][2];
if(m1 > 0 && m2 > 0)
{
dc.SetPixel(i+663,j+50,RGB(255,0,0));
}
else if(m2 < 0 && m3 < 0)
{
dc.SetPixel(i+663,j+50,RGB(0,255,0));
}
else if(m3 > 0 && m1 < 0)
{
dc.SetPixel(i+663,j+50,RGB(0,0,255));
}
else dc.SetPixel(i+663,j+50,RGB(0,0,0));
}
if(m_selRGB == 1) //RB坐标
{
float m1 = w[0][0]*r + w[0][1]*b + w[0][2];
float m2 = w[1][0]*r + w[1][1]*b + w[1][2];
float m3 = w[2][0]*r + w[2][1]*b + w[2][2];
if(m1 > 0 && m2 > 0)
{
dc.SetPixel(i+663,j+50,RGB(255,0,0));
}
else if(m2 < 0 && m3 < 0)
{
dc.SetPixel(i+663,j+50,RGB(0,255,0));
}
else if(m3 > 0 && m1 < 0)
{
dc.SetPixel(i+663,j+50,RGB(0,0,255));
}
else dc.SetPixel(i+663,j+50,RGB(0,0,0));
}
if(m_selRGB == 2) //GB坐标
{
float m1 = w[0][0]*g + w[0][1]*b + w[0][2];
float m2 = w[1][0]*g + w[1][1]*b + w[1][2];
float m3 = w[2][0]*g + w[2][1]*b + w[2][2];
if(m1 > 0 && m2 > 0)
{
dc.SetPixel(i+663,j+50,RGB(255,0,0));
}
else if(m2 < 0 && m3 < 0)
{
dc.SetPixel(i+663,j+50,RGB(0,255,0));
}
else if(m3 > 0 && m1 < 0)
{
dc.SetPixel(i+663,j+50,RGB(0,0,255));
}
else dc.SetPixel(i+663,j+50,RGB(0,0,0));
//else dc.SetPixel(j+23,550-i,RGB(0,0,0));
}
}
}
}
}
void CMSSB1Dlg::OnButton4()
{
// TODO: Add your control notification handler code here
readBmp();
CClientDC dc(this);
int N = 10; DWORD e = 5; int L = 0;
for(int i=0;i<3;i++) //初始聚类中心
{
z[0][i].r = GetRValue(color[100*i][100*i]);
z[0][i].g = GetGValue(color[100*i][100*i]);
z[0][i].b = GetBValue(color[100*i][100*i]);
z[1][i].r = 0; z[1][i].g = 0; z[1][i].b = 0;
}
while (L < N)
{
TheClosestClass();//按最近邻原则分类
DWORD d1 = sqrt((z[1][0].r-z[0][0].r)*(z[1][0].r-z[0][0].r) + (z[1][0].g-z[0][0].g)*(z[1][0].g-z[0][0].g)
+ (z[1][0].b-z[0][0].b)*(z[1][0].b-z[0][0].b));
DWORD d2 = sqrt((z[1][1].r-z[0][1].r)*(z[1][1].r-z[0][1].r) + (z[1][1].g-z[0][1].g)*(z[1][1].g-z[0][1].g)
+ (z[1][1].b-z[0][1].b)*(z[1][1].b-z[0][1].b));
DWORD d3 = sqrt((z[1][2].r-z[0][2].r)*(z[1][2].r-z[0][2].r) + (z[1][2].g-z[0][2].g)*(z[1][2].g-z[0][2].g)
+ (z[1][2].b-z[0][2].b)*(z[1][2].b-z[0][2].b));
if (d1 > e || d2 > e || d3 > e)
{
L++;
for(int i=0;i<3;i++)
z[0][i].r = z[1][i].r;
z[0][i].g = z[1][i].g;
z[0][i].b = z[1][i].b;
}
else L = N;
}
//显示
for (i=0;i<332;i++)
{
for(int j=0;j<234;j++)
{
float m1,m2,m3;
m1 = (GetRValue(color[i][j]) - z[0][0].r)*(GetRValue(color[i][j]) - z[0][0].r)
+(GetGValue(color[i][j]) - z[0][0].g)*(GetGValue(color[i][j]) - z[0][0].g)
+(GetBValue(color[i][j]) - z[0][0].b)*(GetBValue(color[i][j]) - z[0][0].b);
m2 = (GetRValue(color[i][j]) - z[0][1].r)*(GetRValue(color[i][j]) - z[0][1].r)
+(GetGValue(color[i][j]) - z[0][1].g)*(GetGValue(color[i][j]) - z[0][1].g)
+(GetBValue(color[i][j]) - z[0][1].b)*(GetBValue(color[i][j]) - z[0][1].b);
m3 = (GetRValue(color[i][j]) - z[0][2].r)*(GetRValue(color[i][j]) - z[0][2].r)
+(GetGValue(color[i][j]) - z[0][2].g)*(GetGValue(color[i][j]) - z[0][2].g)
+(GetBValue(color[i][j]) - z[0][2].b)*(GetBValue(color[i][j]) - z[0][2].b);
if (m1<=m2 && m1<=m3)
{ dc.SetPixel(i+663,j+50,RGB(255,0,0)); }
if (m2<=m1 && m2<=m3)
{ dc.SetPixel(i+663,j+50,RGB(255,255,255)); }
if (m3<=m1 && m3<=m2)
{ dc.SetPixel(i+663,j+50,RGB(0,0,255)); }
}
}
}
void CMSSB1Dlg::OnButton5()
{
// TODO: Add your control notification handler code here
for(int i=0;i<3;i++) {ShowWindow(1005+i,info); ShowWindow(1005+i,info);}
Invalidate();
/////清空数据//////
LineNum = 0;
info1.Empty();
info2.Empty();
info3.Empty();
classOne1.clear();
classTwo1.clear();
classThree1.clear();
}
void CMSSB1Dlg::ShowWindow(int Num,CString info)
{
CEdit*HInfo=(CEdit*)GetDlgItem(Num);
HInfo->SetWindowText(info);
HInfo->LineScroll(HInfo->GetLineCount());
}
void CMSSB1Dlg::DrawXYZ() //画坐标
{
CClientDC dc(this);
dc.MoveTo(368,309);
dc.LineTo(635,309);
dc.MoveTo(368,309);
dc.LineTo(368,45);
dc.MoveTo(635,309);
dc.LineTo(625,307);
dc.MoveTo(635,309);
dc.LineTo(625,311);
dc.MoveTo(368,45);
dc.LineTo(366,55);
dc.MoveTo(368,45);
dc.LineTo(370,55);
}
////////得到权矢量/////////
void CMSSB1Dlg::GetW(int a)
{
if(tempF.x - tempT.x != 0)
{
w[a-1][0] = (float)(tempF.y - tempT.y)/(float)(tempF.x - tempT.x);
w[a-1][1] = (float)(tempT.x - tempF.x)/(float)(tempF.x - tempT.x);
w[a-1][2] = (-w[a-1][1]*(float)tempF.y - w[a-1][0]*(float)tempF.x)/(float)(tempF.x - tempT.x);
}
else
{
w[a-1][0] = (float)(tempF.y - tempT.y)/(float)(tempF.y - tempT.y);
w[a-1][1] = (float)(tempT.x - tempF.x)/(float)(tempF.y - tempT.y);
w[a-1][2] = (-w[a-1][1]*(float)tempF.y - w[a-1][0]*(float)tempF.x)/(float)(tempF.y - tempT.y);
}
CString s;
s.Format("%f. ",w[a-1][0]);
info4+=s;
s.Format("%f. ",w[a-1][1]);
info4+=s;
s.Format("%f ",w[a-1][2]);
info4+=s;
ShowWindow(1004+a,info4);
info4.Empty();
}
void CMSSB1Dlg::DrawA(int a, int b)
{
CClientDC dc(this);
for(int i=0;i<5;i++)
{
dc.SetPixel(366+a+i,309-b,RGB(255,0,0));
}
for(int j=0;j<5;j++)
{
dc.SetPixel(368+a,307-b+j,RGB(255,0,0));
}
}
void CMSSB1Dlg::DrawB(int a, int b)
{
CClientDC dc(this);
for(int i=0;i<5;i++)
{
dc.SetPixel(366+a+i,309-b,RGB(0,0,0));
}
for(int j=0;j<5;j++)
{
dc.SetPixel(368+a,307-b+j,RGB(0,0,0));
}
}
void CMSSB1Dlg::DrawC(int a, int b)
{
CClientDC dc(this);
for(int i=0;i<5;i++)
{
dc.SetPixel(366+a+i,309-b,RGB(0,0,255));
}
for(int j=0;j<5;j++)
{
dc.SetPixel(368+a,307-b+j,RGB(0,0,255));
}
}
void CMSSB1Dlg::OnSelchangeCombo1()
{
// TODO: Add your control notification handler code here
int selectRGB = pCombox1->GetCurSel();
m_selRGB = selectRGB;
Invalidate();
}
void CMSSB1Dlg::OnSelchangeCombo2()
{
// TODO: Add your control notification handler code here
int select3class = pCombox2->GetCurSel();
m_sel3ClassQuestion = select3class;
}
void CMSSB1Dlg::TheClosestClass()
{
for (int i=0;i<332;i++)
{
for(int j=0;j<234;j++)
{
float m1,m2,m3;
m1 = (GetRValue(color[i][j]) - z[0][0].r)*(GetRValue(color[i][j]) - z[0][0].r)
+(GetGValue(color[i][j]) - z[0][0].g)*(GetGValue(color[i][j]) - z[0][0].g)
+(GetBValue(color[i][j]) - z[0][0].b)*(GetBValue(color[i][j]) - z[0][0].b);
m2 = (GetRValue(color[i][j]) - z[0][1].r)*(GetRValue(color[i][j]) - z[0][1].r)
+(GetGValue(color[i][j]) - z[0][1].g)*(GetGValue(color[i][j]) - z[0][1].g)
+(GetBValue(color[i][j]) - z[0][1].b)*(GetBValue(color[i][j]) - z[0][1].b);
m3 = (GetRValue(color[i][j]) - z[0][2].r)*(GetRValue(color[i][j]) - z[0][2].r)
+(GetGValue(color[i][j]) - z[0][2].g)*(GetGValue(color[i][j]) - z[0][2].g)
+(GetBValue(color[i][j]) - z[0][2].b)*(GetBValue(color[i][j]) - z[0][2].b);
if (m1<=m2 && m1<=m3)
{ classOne.push_back(color[i][j]); }
if (m2<=m1 && m2<=m3)
{ classTwo.push_back(color[i][j]); }
if (m3<=m1 && m3<=m2)
{ classThree.push_back(color[i][j]); }
}
}
for(DWORD* p=classOne.begin();p<classOne.end();++p)
{
COLORREF m = *p;
z[1][0].r += GetRValue(m); z[1][0].g += GetGValue(m);z[1][0].b += GetBValue(m);
}
for(p=classTwo.begin();p<classTwo.end();++p)
{
COLORREF m = *p;
z[1][1].r += GetRValue(m); z[1][1].g += GetGValue(m);z[1][1].b += GetBValue(m);
}
for(p=classThree.begin();p<classThree.end();++p)
{
COLORREF m = *p;
z[1][2].r += GetRValue(m); z[1][2].g += GetGValue(m);z[1][2].b += GetBValue(m);
}
z[1][0].r /= classOne.size(); z[1][0].g /= classOne.size(); z[1][0].b /= classOne.size();
z[1][1].r /= classTwo.size(); z[1][1].g /= classTwo.size(); z[1][1].b /= classTwo.size();
z[1][2].r /= classThree.size(); z[1][2].g /= classThree.size(); z[1][2].b /= classThree.size();
classOne.clear(); classTwo.clear(); classThree.clear();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -