Sei sulla pagina 1di 34

Digital Image Processing

Using Visual C++

Muhammad Salman Habib


BCS 6th Semester
Chapter 4, 5, 6
• Main Review
– Functions Applied in these chapters on Images are
• Reverse Image
• Contrast +
• Contrast –
• Histogram
• Binirization
• Dynamic Binirization
• Histogram Equal
• Histogram Stretch
• Histogram UpStretch
Reverse Image
Reverse the Image Pixels
• void CWinTestDoc::OnReverseImg()
• {
• for(int i=0; i<256; i++)
• {
• for(int j=0; j<256; j++) m_OutImg[i][j] = 255-m_InImg[i][j];
• }
• UpdateAllViews(NULL);
• }
Contrast +
Increase the Image Contrast

• void CWinTestView::OnConstAdd()
• {
• // TODO: Add your command handler code here
• CWinTestDoc* pDoc = GetDocument();
• ASSERT_VALID(pDoc);
• for(int i=0; i<height; i++)
• {
• for(int j=0; j<width; j++)
• {
• int tempVal = pDoc->m_InImg[i][j]+60;
• tempVal = tempVal > 255 ? 255: tempVal;
• tempVal = tempVal < 0 ? 0: tempVal;
• pDoc->m_OutImg[i][j] = (unsigned char)tempVal;
• }
• }
• Invalidate(FALSE); //
• }
Contrast +
Contrast -
• void CWinTestView::OnConstSub()
• {
• // TODO: Add your command handler code here
• CWinTestDoc* pDoc = GetDocument();
• ASSERT_VALID(pDoc);
• for(int i=0; i<height; i++)
• {
• for(int j=0; j<width; j++)
• {
• int tempVal = pDoc->m_InImg[i][j]-60;
• tempVal = tempVal > 255 ? 255: tempVal;
• tempVal = tempVal < 0 ? 0: tempVal;
• pDoc->m_OutImg[i][j] = (unsigned
char)tempVal;
• }
• }
• Invalidate(FALSE);
• }
Contrast -
Histogram
Generates the Histogram Graph of Image
void CWinTestDoc::m_ImgHisto(int height, int width)
• {
• int i,j,vmax,vmin;
• for(i=0; i<256; i++) m_HistoArr[i]=0;

• for(i=0; i<height; i++)


• {
• for(j=0; j<width; j++)
• {
• int gv = (int)m_InImg[i][j];
• m_HistoArr[gv]++;
• }
• }
• vmin = 1000000; vmax =0;
• for(i=0; i<256; i++)
• {
• if(m_HistoArr[i]<=vmin) vmin = m_HistoArr[i];
• if(m_HistoArr[i]>=vmax) vmax = m_HistoArr[i];
• }
• if(vmax==vmin) return;
• float vd = (float)(vmax-vmin);
• for(i=0; i<256; i++)
Histogram
• {
• m_HistoArr[i] = (int)( ((float)m_HistoArr[i]-
vmin)*255.0/vd);
• }

• for(i=0; i<height; i++)


• for(j=0; j<width; j++) m_OutImg[i][j] = 255;

• for(j=0; j<width; j++)


• {
• for(i=0; i<m_HistoArr[j]; i++) m_OutImg[255-i][j] = 0;
• }
• }
• void CWinTestDoc::m_BinThres(int height, int width, int binThres)
• {
• for(int i=0; i<height; i++)
• {
• for(int j=0; j<width; j++)
• {
• if(m_InImg[i][j]>binThres) m_OutImg[i][j] = 255;
• else m_OutImg[i][j] = 0;
• }
• }
• }
Histogram
Binirization
• void CWinTestView::OnBinarization()
• {
• // TODO: Add your command handler code here
• CWinTestDoc* pDoc = GetDocument();

• ASSERT_VALID(pDoc);

• for(int i=0; i<height; i++)


• {
• for(int j=0; j<width; j++)
• {
• if(pDoc->m_InImg[i][j]>100) pDoc->m_OutImg[i][j]=255;
• else pDoc->m_OutImg[i][j]=0;
• }
• }

• Invalidate(FALSE);
• }
Binirization
Dynamic Binirization
• void CWinTestView::OnBinDynamic()
• {
• // TODO: Add your command handler code here
• CBinCntrlDlg pbinCtrlDlg;
• pbinCtrlDlg.DoModal();
• }
Histogram Equal
• void CWinTestView::OnHistoEqual()
• {
• // TODO: Add your command handler code here
• CWinTestDoc* pDoc = GetDocument();
• ASSERT_VALID(pDoc);

• pDoc->m_HistoEqual(256,256);

• Invalidate(FALSE);
• }
Histogram Equal
Histogram Stretch
• void CWinTestView::OnHistoStretch()
• {
• // TODO: Add your command handler code here
• CWinTestDoc* pDoc = GetDocument();
• ASSERT_VALID(pDoc);
• pDoc->m_HistoStretch(256,256);
• Invalidate(FALSE);
• }
Histogram UpStretch
• void CWinTestView::OnHistoUpstretch()
• {
• // TODO: Add your command handler code here
• CWinTestDoc* pDoc = GetDocument();
• ASSERT_VALID(pDoc);
• pDoc->m_HistoUpStretch(256,256,20,20);
• Invalidate(FALSE);
• }
Chapter 7
• Main Review
– Functions Applied in this chapter on Images
are
• Smoothing Box
• Smoothing (Gaussian)
• Sharpening (Laplacian)
• Edge (Prewitt)
• Edge (Sobel)
Smoothing Box
• void CWinTestDoc::m_SmoothingBox(int height, int width)
• {
• int MaskBox[3][3]={{1,1,1}, {1,1,1}, {1,1,1}};
• int heightm1=height-1;
• int widthm1=width-1;
• int mr,mc;
• int newValue;
• int i,j;
• for(i=0;i<height;i++)
• for(j=0;j<width;j++)
• m_OutImg[i][j]=0;
• for(i=1; i<heightm1; i++)
• {
• for(j=1; j<widthm1; j++)
• {
• newValue=0; -
• for(mr=0;mr<3;mr++)
• for(mc=0;mc<3;mc++)
• newValue += (MaskBox[mr][mc]*m_InImg[i+mr-1][j+mc-1]);
• newValue /= 9;
• m_OutImg[i][j]=(BYTE)newValue;
• }
• }
• }
Smoothing Box
Smoothing (Gaussian)
• void CWinTestDoc::m_SmoothingGaussian(int height, int width)
• {
• int MaskGaussian[3][3]={{1,2,1}, {2,4,2}, {1,2,1}};
• int heightm1=height-1;
• int widthm1=width-1;
• int mr,mc;
• int newValue;
• int i,j;
• for(i=0;i<height;i++)
• for(j=0;j<width;j++)
• m_OutImg[i][j]=0;
• for(i=1; i<heightm1; i++)
• {
• for(j=1; j<widthm1; j++)
• {
• newValue=0;
• for(mr=0;mr<3;mr++)
• for(mc=0;mc<3;mc++)
• newValue += (MaskGaussian[mr][mc]*m_InImg[i+mr-1][j+mc-1]);
• newValue /= 20;
• m_OutImg[i][j]=(BYTE)newValue;
• }
• }
• }
Smoothing (Gaussian)
Sharpening (Laplacian)
• void CWinTestDoc::m_SharpeningLaplacian(int height, int width)
• {
• int MaskBox[3][3]={{-1,-1,-1}, {-1,8,-1}, {-1,-1,-1}};
• int heightm1=height-1;
• int widthm1=width-1;
• int mr,mc;
• int newValue;
• int i,j;
• int *pTmpImg;
• int min,max;
• float constVal1,constVal2;
• pTmpImg=new int[height*width];
• for(i=0;i<height;i++)
• for(j=0;j<width;j++)
• {
• m_OutImg[i][j]=0;
• pTmpImg[i*width+j]=0;
• }

• for(i=1; i<heightm1; i++)


• {
• for(j=1; j<widthm1; j++)
• {
• newValue=0; //0À¸·Î ÃʱâÈ-
• for(mr=0;mr<3;mr++)
• for(mc=0;mc<3;mc++)
• newValue += (MaskBox[mr][mc]*m_InImg[i+mr-1][j+mc-1]);
• if(newValue<0)
• newValue=-newValue;
• pTmpImg[i*width+j]=newValue;
• }
• }
• min=(int)10e10;
• max=(int)-10e10;
• for(i=1; i<heightm1; i++)
• {
• for(j=1; j<widthm1; j++)
• {
• newValue=pTmpImg[i*width+j];
• if(newValue<min)
• min=newValue;
• if(newValue>max)
• max=newValue;
• }
• }
• constVal1=(float)(255.0/(max-min));
• constVal2=(float)(-255.0*min/(max-min));
• for(i=1; i<heightm1; i++)
• {
• for(j=1; j<widthm1; j++)
• {
• newValue=pTmpImg[i*width+j];
• newValue=constVal1*newValue+constVal2;
• m_OutImg[i][j]=(BYTE)newValue;
• }
• }
• delete [] pTmpImg;
• }
Sharpening (Laplacian)

Edge (Prewitt)
void CWinTestDoc::m_EdgePrewitt(int height, int width)
• {
• int MaskPrewittX[3][3]={{-1,0,1}, {-1,0,1}, {-1,0,1}};
• int MaskPrewittY[3][3]={{1,1,1}, {0,0,0}, {-1,-1,-1}};
• int heightm1=height-1;
• int widthm1=width-1;
• int mr,mc;
• int newValue;
• int i,j;
• int *pImgPrewittX,*pImgPrewittY;
• int min,max,where;
• float constVal1,constVal2;
• pImgPrewittX=new int[height*width];
• pImgPrewittY=new int[height*width];
• for(i=0;i<height;i++)
• for(j=0;j<width;j++)
• {
• m_OutImg[i][j]=0;
• where=i*width+j;
• pImgPrewittX[where]=0;
• pImgPrewittY[where]=0;
• }
• for(i=1; i<heightm1; i++)
• {
• for(j=1; j<widthm1; j++)
• {
• newValue=0;
• for(mr=0;mr<3;mr++)
• for(mc=0;mc<3;mc++)
• newValue +=
(MaskPrewittX[mr][mc]*m_InImg[i+mr-1][j+mc-1]);
• pImgPrewittX[i*width+j]=newValue;
• }
• }
• for(i=1; i<heightm1; i++)
• {
• for(j=1; j<widthm1; j++)
• {
• newValue=0;
• for(mr=0;mr<3;mr++)
• for(mc=0;mc<3;mc++)
• newValue += (MaskPrewittY[mr][mc]*m_InImg[i+mr-1][j+mc-1]);
• pImgPrewittY[i*width+j]=newValue;
• }
• }
• for(i=1;i<heightm1;i++)
• for(j=1;j<widthm1;j++)
• {
• where=i*width+j;
• constVal1=pImgPrewittX[where];
• constVal2=pImgPrewittY[where];
• if(constVal1<0)
• constVal1=-constVal1;
• if(constVal2<0)
• constVal2=-constVal2;
• pImgPrewittX[where]=constVal1+constVal2;
• }
• min=(int)10e10;
• max=(int)-10e10;
• for(i=1; i<heightm1; i++)
• {
• for(j=1; j<widthm1; j++)
• {
• newValue=pImgPrewittX[i*width+j];
• if(newValue<min)
• min=newValue;
• if(newValue>max)
• max=newValue;
• }
• }
• constVal1=(float)(255.0/(max-min));
• constVal2=(float)(-255.0*min/(max-min));
• for(i=1; i<heightm1; i++)
• {
• for(j=1; j<widthm1; j++)
• {
• newValue=pImgPrewittX[i*width+j];
• newValue=constVal1*newValue+constVal2;
• m_OutImg[i][j]=(BYTE)newValue;
• }
• }
• delete [] pImgPrewittX;
• delete [] pImgPrewittY;
• }
Edge (Prewitt)
Edge (Sobel)
• void CWinTestDoc::m_EdgeSobel(int height, int width)
• {
• int MaskSobelX[3][3]={{-1,0,1}, {-2,0,2}, {-1,0,1}};
• int MaskSobelY[3][3]={{1,2,1}, {0,0,0}, {-1,-2,-1}};
• int heightm1=height-1;
• int widthm1=width-1;
• int mr,mc;
• int newValue;
• int i,j;
• int *pImgSobelX,*pImgSobelY;
• int min,max,where;
• float constVal1,constVal2;
• pImgSobelX=new int[height*width];
• pImgSobelY=new int[height*width];
• for(i=0;i<height;i++)
• for(j=0;j<width;j++)
• {
• m_OutImg[i][j]=0;
• where=i*width+j;
• pImgSobelX[where]=0;
• pImgSobelY[where]=0;
• }
• for(i=1; i<heightm1; i++)

• {
• for(j=1; j<widthm1; j++)
• {
• newValue=0;
• for(mr=0;mr<3;mr++)
• for(mc=0;mc<3;mc++)
• newValue += (MaskSobelX[mr][mc]*m_InImg[i+mr-1][j+mc-1]);
• pImgSobelX[i*width+j]=newValue;
• }
• }
• for(i=1; i<heightm1; i++)
• {
• for(j=1; j<widthm1; j++)
• {
• newValue=0;
• for(mr=0;mr<3;mr++)
• for(mc=0;mc<3;mc++)
• newValue += (MaskSobelY[mr][mc]*m_InImg[i+mr-1][j+mc-1]);
• pImgSobelY[i*width+j]=newValue;
• }
• }
• for(i=1;i<heightm1;i++)
• for(j=1;j<widthm1;j++)
• {
• where=i*width+j;
• constVal1=pImgSobelX[where];
• constVal2=pImgSobelY[where];
• if(constVal1<0)
• constVal1=-constVal1;
• if(constVal2<0)
• constVal2=-constVal2;
• pImgSobelX[where]=constVal1+constVal2;
• }
• min=(int)10e10;
• max=(int)-10e10;
• for(i=1; i<heightm1; i++)
• {
• for(j=1; j<widthm1; j++)
• {
• newValue=pImgSobelX[i*width+j];
• if(newValue<min)
• min=newValue;
• if(newValue>max)
• max=newValue;
• }
• }
• //º¯È¯½Ã »ó¼ö°ªÀ» ¹Ì¸® °è»ê
• constVal1=(float)(255.0/(max-min));
• constVal2=(float)(-255.0*min/(max-min));
• for(i=1; i<heightm1; i++)
• {
• for(j=1; j<widthm1; j++)
• {
• //[min,max]»çÀÌÀÇ °ªÀ» [0,255]°ªÀ¸·Î º¯È¯
• newValue=pImgSobelX[i*width+j];
• newValue=constVal1*newValue+constVal2;
• m_OutImg[i][j]=(BYTE)newValue;
• }
• }

• //µ¿Àû ÇÒ´ç ¸Þ¸ð¸® ÇØÁ¦


• delete [] pImgSobelX;
• delete [] pImgSobelY;
• }
Edge (Sobel)
Thank you….

Potrebbero piacerti anche