您的位置:首页 > 其它

图像处理步骤

2013-12-08 20:00 141 查看
在stdafx.h里

打开 #define SELF_ANALY宏定义,可以知道数据原始操作

即可以知道一般图像处理步骤为

原始图片--> 灰度处理 ----> 边缘化

R,G,B --->Gray = R*0.299 + G*0.587 + B*0.114 ---->将所有的图像数据变成 0或0xff(可以称为开关量)

void CopImgDlg::OnBnClickedButtonPregray()

{

// TODO: 在此添加控件通知处理程序代码

if(pSrcImg)

cvReleaseImage(&pSrcImg);

pSrcImg = cvLoadImage("test.bmp"); //显示图片

if(pSrcImg)

{

debug_printf("open ok!\r\n");

#ifndef SELF_ANALY

CRect rect;

int cx,cy;

cx = pSrcImg->width;

cy = pSrcImg->height;

GetDlgItem(IDC_STATIC_CV)->GetWindowRect(&rect); //相对srceen坐标值

// debug_printf("left=%d\r\n",rect.left);

// debug_printf("top=%d\r\n",rect.top);

// debug_printf("width=%d\r\n",rect.right-rect.left);

// debug_printf("height=%d\r\n",rect.bottom-rect.top);

ScreenToClient(&rect); //相对于对话框的坐标值

// debug_printf("left=%d\r\n",rect.left);

// debug_printf("top=%d\r\n",rect.top);

// debug_printf("width=%d\r\n",rect.right-rect.left);

// debug_printf("height=%d\r\n",rect.bottom-rect.top);

GetDlgItem(IDC_STATIC_CV)->MoveWindow(rect.left,rect.top,cx,cy,true);//调整大小

CDC *pDC = GetDlgItem(IDC_STATIC_CV)->GetDC();

HDC hDC= pDC->GetSafeHdc();

GetDlgItem(IDC_STATIC_CV)->GetClientRect(&rect); //相对于控件坐标值

debug_printf("left=%d\r\n",rect.left);

debug_printf("top=%d\r\n",rect.top);

debug_printf("width=%d\r\n",rect.right-rect.left);

debug_printf("height=%d\r\n",rect.bottom-rect.top);

CvvImage cimg;

cimg.CopyOf(pSrcImg); // 复制图片

cimg.DrawToHDC(hDC,&rect); // 将图片绘制到显示控件的指定区域内

ReleaseDC(pDC);

#else //自己的取数据画图

CRect rect;

int cx,cy;

cx = pSrcImg->width;

cy = pSrcImg->height;

GetDlgItem(IDC_STATIC_CV)->GetWindowRect(&rect); //相对srceen坐标值

ScreenToClient(&rect); //相对于对话框的坐标值

GetDlgItem(IDC_STATIC_CV)->MoveWindow(rect.left,rect.top,cx,cy,true);//调整大小

CWnd *pWnd;

CDC *pDC;

pWnd=GetDlgItem(IDC_STATIC_CV);

pDC=pWnd->GetDC();

pWnd->Invalidate();

pWnd->UpdateWindow();

CPoint picPoint;

CRect picRect;

LONG picWidth,picHeigt;

pWnd->GetClientRect(&picRect);

picWidth=(picRect.right-picRect.left);

picHeigt=(picRect.bottom-picRect.top);

picPoint.x = 0;

picPoint.y = 0;

int i,j;

int widBytes;

widBytes=pSrcImg->widthStep;

debug_printf("width : %d \r\n",pSrcImg->width);

debug_printf("height : %d \r\n",pSrcImg->height);

debug_printf("widBytes : %d \r\n",widBytes);

BYTE *ptr = new BYTE[widBytes];

for(i=0;i<pSrcImg->height;i++)

{

pDC->MoveTo(picPoint);

memcpy(ptr,pSrcImg->imageData+i*widBytes,widBytes);

if(i == (pSrcImg->height/2))

debug_printf("dat : %d %d %d \r\n",ptr[0],ptr[1],ptr[2]);

for(j=0;j<pSrcImg->width;j++)

{

pDC->SetPixel(picPoint,RGB(ptr[j*3+2],ptr[j*3+1],ptr[j*3]));

picPoint.x ++;

}

picPoint.x = 0;

picPoint.y ++;

}

pWnd->ReleaseDC(pDC);

delete[] ptr;

#endif

}

else

{

debug_printf("open failed!\r\n");

}

}

void CopImgDlg::OnBnClickedButtonGray()

{

// TODO: 在此添加控件通知处理程序代码

if(!pSrcImg)

return;

//将颜色空间由RGB转化为Gray

pGrayImg=cvCreateImage(cvGetSize(pSrcImg),IPL_DEPTH_8U,1);

cvCvtColor(pSrcImg,pGrayImg,CV_RGB2GRAY);

#ifndef SELF_ANALY

CRect rect;

CDC *pDC = GetDlgItem(IDC_STATIC_CV)->GetDC();

HDC hDC= pDC->GetSafeHdc();

GetDlgItem(IDC_STATIC_CV)->GetClientRect(&rect);

CvvImage cimg;

cimg.CopyOf(pGrayImg); // 复制图片

cimg.DrawToHDC(hDC,&rect); // 将图片绘制到显示控件的指定区域内

ReleaseDC(pDC);

#else //自己的取数据画图

CWnd *pWnd;

CDC *pDC;

pWnd=GetDlgItem(IDC_STATIC_CV);

pDC=pWnd->GetDC();

pWnd->Invalidate();

pWnd->UpdateWindow();

CPoint picPoint;

CRect picRect;

LONG picWidth,picHeigt;

pWnd->GetClientRect(&picRect);

picWidth=(picRect.right-picRect.left);

picHeigt=(picRect.bottom-picRect.top);

picPoint.x = 0;

picPoint.y = 0;

int i,j;

int widBytes;

widBytes=pGrayImg->widthStep;

debug_printf("width : %d \r\n",pGrayImg->width);

debug_printf("height : %d \r\n",pGrayImg->height);

debug_printf("widBytes : %d \r\n",widBytes);

BYTE *ptr = new BYTE[widBytes];

for(i=0;i<pGrayImg->height;i++)

{

pDC->MoveTo(picPoint);

memcpy(ptr,pGrayImg->imageData+i*widBytes,widBytes);

if(i == pGrayImg->height/2)

debug_printf("dat : %d %d %d \r\n",ptr[0],ptr[1],ptr[2]);

for(j=0;j<pGrayImg->width;j++)

{

pDC->SetPixel(picPoint,RGB(ptr[j],ptr[j],ptr[j]));

picPoint.x ++;

}

picPoint.x = 0;

picPoint.y ++;

}

pWnd->ReleaseDC(pDC);

delete[] ptr;

#endif

}

void CopImgDlg::OnBnClickedButtonCanny()

{

// TODO: 在此添加控件通知处理程序代码

if(!pGrayImg)

return;

pCannyImg = cvCreateImage(cvGetSize(pGrayImg),IPL_DEPTH_8U,1);

cvCanny(pGrayImg, pCannyImg,(float)Thresh,(float)Thresh*3, 3);

#ifndef SELF_ANALY

CRect rect;

CDC *pDC = GetDlgItem(IDC_STATIC_CV)->GetDC();

HDC hDC= pDC->GetSafeHdc();

GetDlgItem(IDC_STATIC_CV)->GetClientRect(&rect);

CvvImage cimg;

cimg.CopyOf(pCannyImg); // 复制图片

cimg.DrawToHDC(hDC,&rect); // 将图片绘制到显示控件的指定区域内

ReleaseDC(pDC);

#else //自己的取数据画图

CWnd *pWnd;

CDC *pDC;

pWnd=GetDlgItem(IDC_STATIC_CV);

pDC=pWnd->GetDC();

pWnd->Invalidate();

pWnd->UpdateWindow();

CPoint picPoint;

CRect picRect;

LONG picWidth,picHeigt;

pWnd->GetClientRect(&picRect);

picWidth=(picRect.right-picRect.left);

picHeigt=(picRect.bottom-picRect.top);

picPoint.x = 0;

picPoint.y = 0;

int i,j;

int widBytes;

widBytes=pCannyImg->widthStep;

debug_printf("width : %d \r\n",pCannyImg->width);

debug_printf("height : %d \r\n",pCannyImg->height);

debug_printf("widBytes : %d \r\n",widBytes);

BYTE *ptr = new BYTE[widBytes];

for(i=0;i<pCannyImg->height;i++)

{

debug_printf("\r\ndat : ");

pDC->MoveTo(picPoint);

memcpy(ptr,pCannyImg->imageData+i*widBytes,widBytes);

// if(i == pCannyImg->height/2)

// debug_printf("dat : %d %d %d \r\n",ptr[0],ptr[1],ptr[2]);

for(j=0;j<pCannyImg->width;j++)

{

debug_printf(" %x",ptr[j]);

pDC->SetPixel(picPoint,RGB(ptr[j],ptr[j],ptr[j]));

picPoint.x ++;

}

picPoint.x = 0;

picPoint.y ++;

}

debug_printf("\r\n");

pWnd->ReleaseDC(pDC);

delete[] ptr;

#endif

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: