void CMyImageView::OnBianyuan() { // TODO: 在此添加命令处理程序代码 CMyImageDoc* pDoc = GetDocument();//获取文档指针 ASSERT_VALID(pDoc); suanzi dlginputyuzhi;//定义阈值对话框类对象 dlginputyuzhi.DoModal();//弹出对话框 UpdateData(TRUE); if (pDoc->m_pDib == NULL) { AfxMessageBox("Error!"); return; } int mcols = pDoc->m_pDib->m_nCols; int mrows = pDoc->m_pDib->m_nRows; unsigned char* m_pData = NULL; m_pData = pDoc->m_pDib->pImgData; unsigned char* m_pDataOldPos = m_pData; unsigned char* a; int* b = NULL; int irows, jcols; int max = 0; int min = 255; vector<int>v; for (irows = 0; irows < mrows; irows++) { for (jcols = 0; jcols < mcols; jcols++) { int j = 0; int p = 0, q = 0; m_pData += irows * mcols + jcols; j += irows * mcols + jcols; if (j > mcols && (j + 1) % mcols != 0 && j < (mrows * (mcols - 1))) { p = 4 * (*m_pData) - *(m_pData + 1) - *(m_pData - 1) - *(m_pData - mcols) - *(m_pData + mcols); if (p > dlginputyuzhi.suanzi_1) { v.push_back(0); } else { v.push_back(255); } } m_pData = m_pDataOldPos; } } int aq = 0; for (irows = 0; irows < mrows; irows++) { for (jcols = 0; jcols < mcols; jcols++) { int j = 0; int p = 0, q = 0; m_pData += irows * mcols + jcols; j += irows * mcols + jcols; if (j > mcols && (j + 1) % mcols != 0 && j < (mrows * (mcols - 1))) { *m_pData = v[aq]; aq++; } m_pData = m_pDataOldPos; } } for (irows = 0; irows < mrows; irows++) { for (jcols = 0; jcols < mcols; jcols++) { int j = 0; int p = 0, q = 0; m_pData += irows * mcols + jcols; j += irows * mcols + jcols; if (irows>0&&irows<mrows-1&&jcols>0&&jcols<mcols-1) { if (*m_pData==0) { if (*(m_pData + 1) == 0 || *(m_pData - 1) == 0 || *(m_pData - mcols) == 0 || *(m_pData + mcols) == 0) { } else { *m_pData = 255; } } } m_pData = m_pDataOldPos; } } pDoc->UpdateAllViews(NULL); }仿照此代码,将其中的sobel算子改成Robert算子
时间: 2024-04-01 13:34:52 浏览: 59
void CMyImageView::OnBianyuan() {
// TODO: 在此添加命令处理程序代码
CMyImageDoc* pDoc = GetDocument();//获取文档指针
ASSERT_VALID(pDoc);
suanzi dlginputyuzhi;//定义阈值对话框类对象
dlginputyuzhi.DoModal();//弹出对话框
UpdateData(TRUE);
if (pDoc->m_pDib == NULL) {
AfxMessageBox("Error!");
return;
}
int mcols = pDoc->m_pDib->m_nCols;
int mrows = pDoc->m_pDib->m_nRows;
unsigned char* m_pData = NULL;
m_pData = pDoc->m_pDib->pImgData;
unsigned char* m_pDataOldPos = m_pData;
unsigned char* a;
int* b = NULL;
int irows, jcols;
int max = 0;
int min = 255;
vector<int>v;
for (irows = 0; irows < mrows; irows++) {
for (jcols = 0; jcols < mcols; jcols++) {
int j = 0;
int p = 0, q = 0;
m_pData += irows * mcols + jcols;
j += irows * mcols + jcols;
if (j > mcols && (j + 1) % mcols != 0 && j < (mrows * (mcols - 1))) {
// Robert算子
p = abs(*m_pData - *(m_pData + mcols + 1)) + abs(*(m_pData + 1) - *(m_pData + mcols));
if (dlginputyuzhi.suanzi_1 != 0 && p > dlginputyuzhi.suanzi_1) {
v.push_back(0);
}
else {
v.push_back(255);
}
}
m_pData = m_pDataOldPos;
}
}
int aq = 0;
for (irows = 0; irows < mrows; irows++) {
for (jcols = 0; jcols < mcols; jcols++) {
int j = 0;
int p = 0, q = 0;
m_pData += irows * mcols + jcols;
j += irows * mcols + jcols;
if (j > mcols && (j + 1) % mcols != 0 && j < (mrows * (mcols - 1))) {
*m_pData = v[aq];
aq++;
}
m_pData = m_pDataOldPos;
}
}
for (irows = 0; irows < mrows; irows++) {
for (jcols = 0; jcols < mcols; jcols++) {
int j = 0;
int p = 0, q = 0;
m_pData += irows * mcols + jcols;
j += irows * mcols + jcols;
if (irows > 0 && irows < mrows - 1 && jcols > 0 && jcols < mcols - 1) {
if (*m_pData == 0) {
if (*(m_pData + 1) == 0 || *(m_pData - 1) == 0 || *(m_pData - mcols) == 0 || *(m_pData + mcols) == 0) {
}
else {
*m_pData = 255;
}
}
}
m_pData = m_pDataOldPos;
}
}
pDoc->UpdateAllViews(NULL);
}
阅读全文