void CChildView::OnEdge() // edge detection based on Sobel operators
{
{
int operx[3][3]={-1,0,1,-2,0,2,-1,0,1};//Sobel operators
int opery[3][3]={1,2,1,0,0,0,-1,-2,-1};//Sobel operators
int i,j;
int p=0;
int smx=0,smy=0;
//CImage img_tem;
//img_tem.Create(img_width,img_hight,24,0);
for(i=0;i<img_hight;i++)
{
for(j=0;j<img_width;j++)
{
COLORREF cf = MyRGB(0,0,0);
img_final.SetPixel(j,i,cf); //img_final is a member of this class, this is to reset it to zeros
}
}
for(i=0;i<img_hight-2;i++)
{
for(j=0;j<img_width-2;j++)
{
smx=0;
smy=0;
for (int m=0; m<3; m++)
{
for (int n=0; n<3; n++)
{
COLORREF rgb=img_new.GetPixel(j+n,i+m);
int p=MyGetRValue(rgb);
smx=smx+p*operx[m][n];
smy=smy+p*opery[m][n];
}
}
float G=smx*smx+smy*smy;
int GD=(int)sqrt(G);
if(GD>100) // threshold to determine whether it is an edge
{
COLORREF cf = MyRGB(255,255,255);
img_final.SetPixel(j+1,i+1,cf);
}
}
}
Invalidate(); //call the OnPaint to show the result
}
I love tennis and programming. I am not genius, so I collect the knowledge piece by piece.
Showing posts with label image processing. Show all posts
Showing posts with label image processing. Show all posts
Monday, May 27, 2013
edge detection based on sobel operator in MFC
basic image processing in MFC
Everybody knows that MFC+OpenCV is very convenient for image processing. But how about only using MFC? Most of the operation for image processing is based on bitmap in previous MFC version, say VC++6.0.
In VS2008, there is already CImage class, which could greatly facilitate the image processing.
(1) get basic info of one image
(2) get the r,g,b value from the COLORREF value:
(3) convert the 24bit color image into gray image:
In VS2008, there is already CImage class, which could greatly facilitate the image processing.
(1) get basic info of one image
#include <atlimage.h>
...
CImage img; //image object
img.Load(FilePathName); // load a local image
img_hight=img.GetHeight();//get hight
img_width=img.GetWidth();//get width
img_bits=img.GetBPP();//get bit counts for each pixel, say 8,24,32...
COLORREF rgb=img.GetPixel(10,10);// get the pixel color
CImage img_new; //another image object
img_new.Create(img_width, img_hight, 24, 0);//create a image with the same size
img.SetPixel(10,10,rgb);// set the pixel color
(2) get the r,g,b value from the COLORREF value:
#define MyGetRValue(rgb) ((BYTE)(rgb))
#define MyGetGValue(rgb) ((BYTE)(((WORD)(rgb)) >> 8))
#define MyGetBValue(rgb) ((BYTE)((rgb)>>16))
COLORREF rgb=img.GetPixel(j,i);
int r = MyGetRValue(rgb);
int g=MyGetGValue(rgb);
int b=MyGetBValue(rgb);
(3) convert the 24bit color image into gray image:
#define MyRGB(r,g,b) ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)))
BYTE cl = (BYTE)(0.299 * r + 0.587 * g + 0.114 * b);
int clr=(int)cl;
COLORREF cf = MyRGB(clr,clr,clr);
img_gray.SetPixel(j,i,cf);
Subscribe to:
Posts (Atom)

