My first OpenCV program is the results of someone else, huh, huh.
A simple image processing function, more programs, will gradually add my study.
// cvmedianfilterdlg.cpp: importation file //
#include "stdafx.h" #include "cvmedianfilter.h" #include "cvmedianfilterdlg.h"
#ifdef _debug # define new debug_new # undef this_filestatic char this_file [] = __file __; # ENDIF
/// Caboutdlg Dialog Used for app about # include "cvaux.h" #include "cv.h" #include "highgui.h" // # include "ijl.h" ???? # include "cvcam.h" #include "math.h" // # include "ipl.h" #include
#pragma comment (lib, "cvaux.lib") # Pragma Comment (lib, "cvcam.lib") // # pragma comment (lib, "ijl15.lib") ???? # pragma comment (lib, "CV .lib ") # Pragma Comment (lib," highgui.lib ")
#define ORIG_WIN_SIZE 24static CvMemStorage * storage1 = 0; static CvHidHaarClassifierCascade * hid_cascade = 0; // on_mouse (int event, int x, int y, int flags); IplImage * detect_and_draw (IplImage * image); char filename [] = "D : //Sample256-1.bmp ";
Class Caboutdlg: public cdialog {public: capoutdlg ();
// Dialog Data // {{AFX_DATA (Caboutdlg) Enum {IDD = IDD_ABOUTBOX}; //}} AFX_DATA
// classwizard generated virtual function overrides // {{AFX_VIRTUAL (Caboutdlg) protected: Virtual Void DodataExchange (CDATAEXCHANGE * PDX); // DDX / DDV Support //}} AFX_VIRTUAL
// ImplementationProtace: // {{AFX_MSG (Caboutdlg) //}} AFX_MSG DECLARE_MESSAGE_MAP ()};
CAboutDlg :: CAboutDlg (): CDialog (CAboutDlg :: IDD) {// {{AFX_DATA_INIT (CAboutDlg) //}} AFX_DATA_INIT} void CAboutDlg :: DoDataExchange (CDataExchange * pDX) {CDialog :: DoDataExchange (pDX); // {{AFX_DATA_MAP (Caboutdlg) //}} AFX_DATA_MAP}
Begin_MESSAGE_MAP (Caboutdlg, cdialog) // {{AFX_MSG_MAP (Caboutdlg) // No Message Handlers //}} AFX_MSG_MAPEND_MESSAGE_MAP ()
/// ccvmedianfilterdlg Dialog
CCvmedianfilterDlg :: CCvmedianfilterDlg (CWnd * pParent / * = NULL * /): CDialog (CCvmedianfilterDlg :: IDD, pParent) {// m_num = 0; // {{AFX_DATA_INIT (CCvmedianfilterDlg) // NOTE: the ClassWizard will add member initialization Here //}} AFX_DATA_INIT // Note That Loadicon Does Not Require A Subsequent Destroyicon in Win32 M_Hicon = AFXGetApp () -> Loadicon (iDR_MAINFRAME);
void CCvmedianfilterDlg :: DoDataExchange (CDataExchange * pDX) {CDialog :: DoDataExchange (pDX); // DDX_Text (pDX, IDC_NUM, m_num); // {{AFX_DATA_MAP (CCvmedianfilterDlg) // NOTE: the ClassWizard will add DDX and DDV calls Here //}} AFX_DATA_MAP}
BEGIN_MESSAGE_MAP (CCvmedianfilterDlg, CDialog) // {{AFX_MSG_MAP (CCvmedianfilterDlg) ON_WM_SYSCOMMAND () ON_WM_PAINT () ON_WM_QUERYDRAGICON () ON_BN_CLICKED (IDC_gray, Ongray) ON_BN_CLICKED (IDC_medfilter, Onmedfilter) ON_BN_CLICKED (IDC_singreen, Onsingreen) ON_BN_CLICKED (IDC_noise, Onnoise) ON_BN_CLICKED (IDC_saltnoise , Onsaltnoise) ON_BN_CLICKED (IDC_BUTTON1, OnCanny) ON_BN_CLICKED (IDC_BUTTON2, OnThreshold) ON_BN_CLICKED (IDC_Laplace, OnLaplace) ON_BN_CLICKED (IDC_Log, OnLog) ON_BN_CLICKED (IDC_Tough, OnTough) //}} AFX_MSG_MAPEND_MESSAGE_MAP ()
/// ccvmedianfilterdlg Message Handlers
Bool ccvmedianfilterdlg :: oninitdialog () {cdialog :: oninitdialog ();
// Add "About ..." Menu Item to System Menu.
// IDM_ABOUTBOX must be in the system command range ASSERT ((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);. ASSERT (IDM_ABOUTBOX <0xF000); CMenu * pSysMenu = GetSystemMenu (FALSE); if (! PSysMenu = NULL) {CString strAboutMenu; StraboutMenu.LoadString (ids_aboutbox); if (! straboutmenu.isempty ()) {psysmenu-> appendmenu (mf_separator); psysmenu-> appendmenu (mf_string, idm_aboutbox, straboutmenu);}}
// set the icon for this dialog. The framework does this automatic or //en the application's main window is not a dialog seticon (m_hicon, true); // set big icon seticon (m_hicon, false); // set small icon / / TODO: ADD Extra Initialization Here Return True; // Return True Unless 1 The focus to a control}
void CCvmedianfilterDlg :: OnSysCommand (UINT nID, LPARAM lParam) {if ((nID & 0xFFF0) == IDM_ABOUTBOX) {CAboutDlg dlgAbout; dlgAbout.DoModal ();} else {CDialog :: OnSysCommand (nID, lParam);}}
// if you add a minimize button to your dialog, you will need the code Below // to draw the icon. For mfc Applications Using the document / view model, // this is automaticly done for you by the framework.
Void ccvmedianfilterdlg :: onpaint () {if (isiconic ()) {cpaintdc dc (this); // device context for Painting
SendMessage (WM_ICONERASEBKGND, (WPARAM) dc.getsafehdc (), 0);
// Center icon in client rectangle int cxIcon = GetSystemMetrics (SM_CXICON); int cyIcon = GetSystemMetrics (SM_CYICON); CRect rect; GetClientRect (& rect); int x = (rect.Width () - cxIcon 1) / 2; int y = (Rect.height () - Cyicon 1) / 2;
// Draw the icon dc.drawicon (x, y, m_hicon);} else {cdialog :: onpaint ();}}
// The system calls this to obtain the cursor to display while the user drags // the minimized window.HCURSOR CCvmedianfilterDlg :: OnQueryDragIcon () {return (HCURSOR) m_hIcon;} void CCvmedianfilterDlg :: Ongray () {// TODO: Add Your Control Notification Handler Code Here Updatedata (TRUE); char filename [] = "d: //sample256-1.bmp"; // char filename [] = "f: // Switty // OpenCV Lu Bin // newImage008. DIB "; //" F: // xhl // funny picture //rt.jpg "; char * wndname
IPLIMAGE * GRAY = 0, * Gray2 = 0; Gray = CVLoadImage (filename, 1); // gray2 = CVLoadImage (filename, 1);
gray2 = cvCreateImage (cvSize (gray-> width, gray-> height), IPL_DEPTH_8U, 1); wndname = "GRAY2BGR"; cvNamedWindow (wndname, 1); cvCvtColor (gray, gray2, CV_BGR2GRAY); // rgb converted to gray CVShowImage (Wndname, Gray2); CvwaitKey (0); CVRELEASEIMAGE (& Gray); CVRELEASEIMAGE (& gray2);
CvDestroyWindow (WndName);
}
Void ccvmedianfilterdlg :: OnMedfilter () {// Todo: add your control notification handler code her filename2 [] = "d: //sample256-1.bmp"; char filename1 [] = "D: // Sample256-1. BMP "; char filename [] =" d: //sample256-1.bmp ";
char * wndname; IplImage * gray, * gray2, * dst, * dst3; gray = cvLoadImage (filename, 1); gray2 = cvCreateImage (cvSize (gray-> width, gray-> height), IPL_DEPTH_8U, 1); dst = CvcreateImage (CVSIZE (Gray-> Width, Gray-> Height), IPL_DEPTH_8U, 4); DST3 = CVCReateImage (CVSIZE (Gray-> Width, Gray-> Height), IPL_DEPTH_8U, 3); // WndName = "with bitmap save image format "; // cvSaveImage (filename1, dst3); // cvSaveImage (filename1, dst3); // cvNamedWindow (wndname, 1); // gray1 = cvLoadImage (filename1,1); // cvShowImage (wndname, gray1 ); If (num == 0) {wndname = "original map"; cvnamedwindow (wndname, 1); cvshowimage (wndname, gray); wndname = "grayscale image"; cvnamedwindow (wndname, 1); CVCVTCOLOR (Gray, Gray2, CV_BGR2GRAY; CVCVTCOLOR (Gray2, DST3, 8); CVShowImage (WndName, DST3);
CvwaitKey (0); // CVRELEASEIMAGE (& Gray); // CVRELEASEIMAGE (& gray2); // CVRELEASEIMAGE (& DST3); // CVRELEASEIMAGE (& DST); // CvDestroyWindow (Wndname); Num = 1;} else {
CDC * pDC = GetDC (); CDC dcMem; CBitmap myBitmap; myBitmap.CreateCompatibleBitmap (pDC, gray-> height, gray-> width); dcMem.CreateCompatibleDC (pDC); dcMem.SelectObject (& myBitmap); // char * rvalue = 0, * bvalue = 0, * gvalue = 0; DWORD IFILTERH = 3; // Filter high DWORD IFILTERW = 3; // - Filter Width DWORD IFILTERMX = 1; // - Central Element of Filter X Coordinate DWORD IFILTERMY = 1; // - Filter center element y coordinate dword ifilterlen = ifilterh * ifilterw; // Window size DWORD I, J, K, L, M, N; DWORD TEMP, TEMP1; BYTE R, R1, G, G1, B, B1, R2, G2, B2; BYTE BTEMP; BYTE RVALUE [9], BVALUE [9], GValue [9];
For (i = ifiltermy; i <(gray-> height iFiltermy 1); i ) for (j = ifiltermx; j <(gray-> width-ifilterw iFiltermx 1); J ) {TEMP = i * ( Gray-> width * 3 j * 3; r = (Byte) (Gray-> ImageData [Temp]); g = (Byte) (Gray-> ImageData [Temp 1]); b = (byte) Gray-> imagedata [Temp 2]); for (k = 0; k
R2 = rvalue [(iFilterlen 1) / 2];
For (m = 0; M
B2 = BVALUE [(iFilterlen 1) / 2];
For (m = 0; M
G2 = gvalue [(iFilterlen 1) / 2];
// b2 = (Byte) getMediannum (BValue, iFilterh * ifilterw); // g2 = (byte) getMedianNum (gvalue, iFilterh * ifilterw); // r2 = (byte) getMediannum (Rvalue, iFilterh * ifilterw); * / DcMem.Setpixel (J, I, RGB (B2, G2, R2));} PDC-> Stretchblt (0, 0, Gray-> Width, Gray-> Height, & DCMEM, 0, 0, Gray-> Width, GRAY -> Height, Srccopy;
ReleaseDC (pDC); ReleaseDC (& dcMem); // cvReleaseImage (& gray1); // cvDestroyWindow (wndname);} cvReleaseImage (& gray); cvReleaseImage (& gray2); cvReleaseImage (& dst3); cvReleaseImage (& dst); cvDestroyWindow (wndname); }
Void ccvmedianfilterdlg :: onok () {// Todo: add extra validation here cdialog :: onok ();}
Void ccvmedianfilterdlg :: Onsen () // Open source image {// Todo: add your control notification handler code hendename [] = "d: //sample256-1.bmp"; char * wndname; wndname = "original image "
IPLIMAGE * image = 0; Image = CVLoadImage (filename, 1);
CvnamedWindow (WndName, 1); CvShowImage (Wndname, Image); CvwaitKey (0); CVRELEASEIMAGE (& image);
Void ccvmedianfilterdlg :: ONNOISE () {// Todo: add your control notification handler code here char filename [] = "d: //sample256-1.bmp"; char * wndname; wndname = "noise image";
IPLIMAGE * Gray; Gray = CVLoadImage (FileName, 1); cvnamedwindow (wndname, 1); cvshowimage (wndname, gray);
} / * void ccvmedianfilterdlg :: onsaltnoise () {// Todo: add your control notification handler code here char filename [] = "d: //sample256-1.bmp"; char * wndname; iPlimage * Gray = 0, * Gray2 = 0, * Gray3 = 0, * DST = 0, * DST3 = 0; Gray = CVLoadImage (FileName, 1); Gray2 = CvcreateImage (CVSIZE (Gray-> Width, Gray-> Height), IPL_Depth_8u, 1); gray3 = cvCreateImage (cvSize (gray-> width, gray-> height), 8,3); dst3 = cvCreateImage (cvSize (gray-> width, gray-> height), IPL_DEPTH_8U, 3); dst = cvCreateImage (cvSize ( Gray-> width, gray-> height), IPL_Depth_8u, 4); WndName = "Gray2BGR"; CVCVTCOLOR (Gray, Gray2, CV_BGR2GRAY); CVCVTCOLOR (GRAY2, DST3, 8); GRAY3 = CVCloneImage (DST3); // below Add salt and pepper noise CDC * pDC = GetDC (); CDC dcMem; CBitmap myBitmap; myBitmap.CreateCompatibleBitmap (pDC, gray3-> height, gray3-> width); dcMem.CreateCompatibleDC (pDC); dcMem.SelectObject (& myBitmap);
DWORD I, J; //, K, L, M, N; DWORD TEMP; //, Temp1; Byte R, G, B Download Adobe Reader
SRAND (NULL)); // Generate a pseudo-random seed for (j = 0; j
// wndname = "noisegray3"; // cvnamedwindow (wndname, 1); // cvshowimage (wndname, gray3); // cvwaitkey (0); // PDC-> ReleaseDC (PDC); CVRELEASEIMAGE (& gray3);} * / void ccvmedianfilterdlg :: onsaltnoise () {// Todo: add your control notification handler code here char filename [] = "d: //sample256-1.bmp"; char * wndname; iPlimage * Gray = 0, * Gray2 = 0, * gray3 = 0; gray = cvLoadImage (filename, 1); gray2 = cvCreateImage (cvSize (gray-> width, gray-> height), IPL_DEPTH_8U, 1); gray3 = cvCreateImage (cvSize (gray-> width, gray -> Height, 8, 3);
// CVCopy (gray, gray3); // add noise in color images
// CVCVTCOLOR (Gray, Gray3, CV_BGR2GRAY); // Why don't you write in grayscale image?
CVCVTCOLOR (Gray, Gray2, CV_BGR2GRAY); // In grayscale image add noise CVCVTCOLOR (Gray2, Gray3, CV_GRAY2BGR);
DWORD I, J; SRAND (NULL)); // Generate a pseudo-random seed for (j = 0; j
}
Void ccvmedianfilterdlg :: oncanny () // Edge Detection {// Todo: Add Your Control Notification Handler Code Here
Char filename [] = "d: //sample256-1.bmp"; iPlimage * ww = 0, * WW1 = 0, * DST = 0; ww = cvloadimage (filename, 1); WW1 = CVCReateImage (CVSIZE (WW- > width, ww-> height, IPL_DEPTH_8U, 1); DST = CvcreateImage (CVSIZE (WW-> Width, WW-> Height), IPL_Depth_8u, 1); CVCVTCOLOR (WW, WW1, CV_BGR2GRAY); CVCANNY (WW1, DST , 100, 200, 3); // Cvcanny source image must be grayscale image // Cvcanny (WW1, DST, 100, 200, 5); // Cvcanny (WW1, DST, 100, 200, 7);
CvnamedWindow ("Canny Operator (Nuclear = 3)", 1); CVSHOWIMAGE ("Canny Operator (Nuclear = 3)", DST);
CVRELEASEIMAGE (& WW); CVRELEASEIMAGE (& DST); CVRELEASEIMAGE (& WW);
Void ccvmedianfilterdlg :: ONTHRESHOLD () {// Todo: Add Your Control Notification Handler Code Here
IPLIMAGE * log = 0, * pyr = 0, * log1 = 0, * log2 = 0; iplimage * laplace = 0, * LAPLACE1 = 0, * DST = 0, * THR = 0;
Log = CVLoadImage (FileName, 1); pyr = cvcreateImage (CVSIZE (log-> width / 2, log-> height / 2), 8, 3);
Laplace = CvcreateImage (CVSIZE (log-> width, log-> height), 8, 3);
CvPyRDown (log, pyr, cv_gaussian_5x5); cvpyrup (pyr, laplace, cv_gaussian_5x5);
laplace1 = cvCreateImage (cvSize (log-> width, log-> height), IPL_DEPTH_8U, 1); dst = cvCreateImage (cvSize (log-> width, log-> height), IPL_DEPTH_16S, 1); cvCvtColor (laplace, laplace1, CV_BGR2GRAY; CVLAPLACE (Laplace1, DST, 3);
THR = CvcreateImage (cvsize (log-> width, log-> height), IPL_DEPTH_8U, 1); // wty
CVTHRESHOLD (DST, THR, 25, 255, CV_THRESH_BINARY); // WTY binarized threshold enhancement
CVMean (THR, LAPLACE1);
CvnamedWindow ("LOG-binarization", 1); CVSHOWIMAGE ("LOG-bisal", THR);
CVRELEASEIMAGE (& THR); // Wty
CVRELEASEIMAGE (& log1); CVRELEASEIMAGE (& log2);
CVRELEASEIMAGE (& LOG); CVRELEASEIMAGE (& PYR);
CVRELEASEIMAGE (& Laplace1); CVRELEASEIMAGE (& Laplace);} void ccvmedianfilterdlg :: online () {// Todo: add your control notification handler code here
IplImage * laplace = 0, * laplace1 = 0, * dstlaplace = 0; laplace = cvLoadImage (filename, 1); laplace1 = cvCreateImage (cvSize (laplace-> width, laplace-> height), IPL_DEPTH_8U, 1); dstlaplace = cvCreateImage (cvsize (laplace-> width, laplace-> height), IPL_DEPTH_16S, 1);
cvCvtColor (laplace, laplace1, CV_BGR2GRAY); cvLaplace (laplace1, dstlaplace, 3); // cvLaplace (laplace1, dstlaplace, 5); // or // cvLaplace (laplace1, dstlaplace, 7); // or cvNamedWindow ( "Laplace ", 1); CvShowImage; CVRELEASEIMAGE (& Laplace1); CVRELEASEIMAGE (& Dstlaplace); CVRELEASEIMAGE (& LAPLACE);
Void ccvmedianfilterdlg :: ONLOG () {// Todo: add your control notification handler code her iplimage * log = 0, * pyr = 0, * log1 = 0, * log2 = 0; iPlimage * Laplace = 0, * laplace1 = 0 , * DST = 0; log = CVLoadImage (FileName, 1); pyr = cvcreateImage (cvsize (log-> width / 2, log-> height / 2), 8, 3);
laplace = cvCreateImage (cvSize (log-> width, log-> height), 8,3); cvPyrDown (log, pyr, CV_GAUSSIAN_5x5); cvPyrUp (pyr, laplace, CV_GAUSSIAN_5x5); // Gaussian laplace1 = cvCreateImage (cvSize (log -> width, log-> height), IPL_DEPTH_8U, 1); // laplace dst = cvCreateImage (cvSize (log-> width, log-> height), IPL_DEPTH_16S, 1); cvCvtColor (laplace, laplace1, CV_BGR2GRAY); cvLaplace (Laplace1, DST, 3); // CVLAPLACE (LAPLACE1, DSTLAPLACE, 5); // CVLAPLACE (LAPLACE1, DSTLAPLACE, 7);
CvnamedWindow ("LOG Operator (Nuclear = 3)", 1); CVSHOWIMAGE ("LOG Operator (Nuclear = 3)", DST);
CVRELEASEIMAGE (& log1); CVRELEASEIMAGE (& log2);
CVRELEASEIMAGE (& LOG); CVRELEASEIMAGE (& LAPLACE1); CVRELEASEIMAGE (& DST); CVRELEASEIMAGE (& LAPLACE);
Void ccvmedianfilterdlg :: ONTOUGH () {// Todo: add your control notification handler code here char * wndname
Wndname = "original map"; cvnamedwindow (wndname, 1);
IPLIMAGE * WW = 0, * WW1 = 0, * DST = 0, * DST1 = 0; WW = CVLoadImage (filename, 1);
CvShowImage (WNDNAME, WW);
WW1 = CVCREATEIMAGE (CVSIZE (WW-> Width, WW-> Height), IPL_DEPTH_8U, 1);
DST = CvcreateImage (CVSIZE (WW-> Width, WW-> Height), IPL_Depth_8u, 1); DST1 = CVCReateImage (CVSIZE (WW-> Width, WW-> Height), IPL_Depth_8u, 3); CVCVTCOLOR (WW, DST, CV_BGR2GRAY);
CVMemStorage * Storage = CVCreateMemStorage (0); CVSEQ * lines = 0; INT i; Cvcanny (DST, WW1, 50, 200, 3); CVCVTCOLOR (WW1, DST1, CV_GRAY2BGR);
#if 1 lines = Cvhoughlines2 (WW1, Storage, CV_HOUGH_STANDARD, 1, CV_PI / 180, 150, 3, 0);
For (i = 0; i
CvwaitKey (0); CVRELEASEIMAGE (& DST);
CVRELEASEIMAGE (& WW); CVRELEASEIMAGE (& WW1);
CVRELEASEIMAGE (& DST1);