|
Newbie
Join Date: May 2008
Posts: 6
Rep Power: 0 
|
Re: image processing
ok so i decided to not go the mathematical route for blurring/sharpening, something which i know is the dumb BS way out, but i just couldnt get the algorithim to work after an entire night of trying. here is what i have so far:
//---------------------------------------------------------------------- // SECTION 1: Preprocessor Commands //---------------------------------------------------------------------- // Include Win32 API Libraries (headers) #include <windows.h> #include <windowsx.h> #include <commctrl.h> #include <string.h> #include <stdio.h> // Define the maximum number of letters that text control objects can have #define MAXLETTERS 32000 #define MaxSize 512 // Define event-ID numbers for the control objects #define ID_BTTN 1001 #define ID_FNAME 1002 #define ID_FOPEN 2001 #define ID_FEXIT 2002 #define ID_INV 2003 #define ID_ROTATE 2004 #define ID_BLUR 2005 #define ID_FLIP 2006 #define ID_Sharp 2007 //---------------------------------------------------------------------- // SECTION 2: Computational Code //---------------------------------------------------------------------- // Declare global variables: flowdata is available to all subsequent functions unsigned char imgdata[MaxSize][MaxSize]; int imgheight, imgwidth; // Function DrawData(): draws data from flowdata array onto screen void DrawData(HWND window) { // Declare local variables int col, row; unsigned char shade; HDC drawing_context = GetDC(window); // Draw the pixels for (row = 0 ; row < imgheight ; row++) for (col = 0 ; col < imgwidth ; col++) { shade = imgdata[row][col]; SetPixel(drawing_context, col+50, row+50, RGB(shade, shade, shade)); } } // flips data from flowdata array onto screen void Inv(HWND window) { // Declare local variables int col, row; unsigned char shade; HDC drawing_context = GetDC(window); // Draw the pixels for (row = 0 ; row < imgheight ; row++) for (col = 0 ; col < imgwidth ; col++) { shade = imgdata[row][col]; SetPixel(drawing_context, col+50, 195-row, RGB(shade, shade, shade)); } } //Function to blur the image void Blur(HWND window) { // Declare local variables int col, row; unsigned char shade; unsigned char shade0; unsigned char shade1; unsigned char shade2; unsigned char shade3; unsigned char shade4; unsigned char shade5; unsigned char shade6; unsigned char shade7; unsigned char shadeBlur; HDC drawing_context = GetDC(window); // Draw the pixels for (row = 0 ; row < imgheight ; row++) for (col = 0 ; col < imgwidth ; col++) { shade0 = imgdata[row-4][col-4]; shade1 = imgdata[row-3][col-3]; shade2 = imgdata[row-2][col-2]; shade3 = imgdata[row-1][col-1]; shade = imgdata[row][col]; shade4 = imgdata[row+1][col+1]; shade5 = imgdata[row+2][col+2]; shade6 = imgdata[row+4][col+3]; shade7 = imgdata[row+4][col+4]; shadeBlur = (shade + shade0 + shade1 + shade2 + shade3 + shade4 + shade5 + shade5 + shade6 + shade7 )/9; SetPixel(drawing_context, col+50, row+50, RGB(shadeBlur, shadeBlur, shadeBlur)); } }//end blur void Sharp(HWND window) { // Declare local variables int col, row; unsigned char shade; unsigned char shade0; unsigned char shade1; unsigned char shade2; unsigned char shade3; unsigned char shade4; unsigned char shade5; unsigned char shade6; unsigned char shade7; unsigned char shadeSharp; HDC drawing_context = GetDC(window); // Draw the pixels for (row = 0 ; row < imgheight ; row++) for (col = 0 ; col < imgwidth ; col++) { shade0 = imgdata[row-4][col-4]; shade1 = imgdata[row-3][col-3]; shade2 = imgdata[row-2][col-2]; shade3 = imgdata[row-1][col-1]; shade = imgdata[row][col]; shade4 = imgdata[row+1][col+1]; shade5 = imgdata[row+2][col+2]; shade6 = imgdata[row+4][col+3]; shade7 = imgdata[row+4][col+4]; shadeSharp = (shade + shade0 + shade1 + shade2)*1.3; SetPixel(drawing_context, col+50, row+50, RGB(shadeSharp, shadeSharp, shadeSharp)); } } //Function to rotate image 90 degrees void Rotate90Degrees(HWND window) { // Declare local variables int col, row; unsigned char shade; HDC drawing_context = GetDC(window); // Draw the pixels for (row = 0 ; row < imgwidth ; row++) for (col = 0 ; col < imgheight ; col++) { shade = imgdata[col][row]; SetPixel(drawing_context, col+50, row-23, RGB(shade, shade, shade)); } }//end Rotate90Degrees // Function LoadData(): loads data from file name in file name box int LoadData(HWND window) { // Declare local variables HWND file_name_box; char filename[MAXLETTERS], imgformat[80]; FILE * infile; int row, col, imgdepth; // Get the input file's name from the File Text Box file_name_box = GetDlgItem(window, ID_FNAME); // get file box handle GetWindowText(file_name_box, filename, MAXLETTERS); // get file name from file box // Open the input file, for reading infile = fopen( filename , "r"); if (!infile) { SetWindowText(file_name_box, "ERROR!"); // signal if file does not exist return( -1 ); } // Read-in image information from the input file fscanf(infile,"%s", imgformat); fscanf(infile,"%d", &imgwidth); fscanf(infile,"%d", &imgheight); fscanf(infile,"%d", &imgdepth); fgetc(infile); // Read-in the image data from the input file for (row = 0 ; row < imgheight ; row++) for (col = 0 ; col < imgwidth ; col++) imgdata[row][col] = fgetc(infile); // close the input file fclose(infile); return(0); } // Function GetFileName(): opens a file window and gets user-selected file name void GetFileName(HWND window) { // Declare local variables OPENFILENAME open_file_dialog; char filename[ MAXLETTERS ] = ""; HWND file_name_box; // Initialize the Open File Dialog Data Structure ZeroMemory(&open_file_dialog, sizeof(open_file_dialog)); // is this really needed? open_file_dialog.lStructSize = sizeof(open_file_dialog); open_file_dialog.hwndOwner = window; // name of parent window open_file_dialog.lpstrFile = filename; // name of file name variable open_file_dialog.nMaxFile = MAXLETTERS; // max No of letters in file name open_file_dialog.lpstrFilter = "PGM Files (*.pgm)\0*.pgm\0All Files (*.*)\0*.*\0"; // type open_file_dialog.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST; // style open_file_dialog.lpstrDefExt = "pgm"; // file extension // Open the File dialog, get the file name from the user and process it if non-NULL if ( GetOpenFileName(&open_file_dialog) ) { file_name_box = GetDlgItem(window, ID_FNAME); // get file box handle SetWindowText(file_name_box, filename); // put file name in file box } } //---------------------------------------------------------------------- // SECTION 3: GUI Building //---------------------------------------------------------------------- // Function MakeControls(): builds control objects on a window void MakeControls(HWND window) { // Create the Label for the file name text box CreateWindowEx(WS_EX_STATICEDGE, "Static", "File Name:", WS_CHILD | WS_VISIBLE, 10, 260, 50, 50, window, NULL, NULL, NULL); // Create the File Name Text box CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", NULL, WS_CHILD | WS_VISIBLE, 70, 260, 250, 50, window, (HMENU) ID_FNAME, NULL, NULL); // Create the Draw button CreateWindowEx(0, "Button", "Draw", WS_CHILD | WS_VISIBLE, 330, 260, 60, 50, window, (HMENU) ID_BTTN, NULL, NULL); } // Function MakeMenu(): builds menu objects on a window void MakeMenu(HWND window) { // Declare local variables HMENU filemenu, subfilemenu; HMENU imagemenu, subimagemenu; // Create a blank file menu filemenu = CreateMenu(); imagemenu = CreateMenu(); // Create a blank sub-menu (popup menu) for the file menu subfilemenu = CreatePopupMenu(); subimagemenu = CreatePopupMenu(); // Place Open and Exit items onto the blank popup sub-menu AppendMenu(subfilemenu, MF_STRING, ID_FOPEN, "&Open"); AppendMenu(subfilemenu, MF_STRING, ID_ROTATE, "&Rotate"); AppendMenu(subfilemenu, MF_STRING, ID_BLUR, "&Blur"); AppendMenu(subfilemenu, MF_STRING, ID_FLIP, "&Flip"); AppendMenu(subfilemenu, MF_STRING, ID_FEXIT, "&Exit"); AppendMenu(subfilemenu, MF_STRING, ID_Sharp, "&Sharpen"); // Place the popup sub-menu on the file menu AppendMenu(filemenu, MF_STRING | MF_POPUP, (UINT)subfilemenu, "&File"); AppendMenu(imagemenu, MF_STRING | MF_POPUP, (UINT)subimagemenu, "&Image"); // Place the file menu on the Main Window of the program SetMenu(window, filemenu); //InsertMenuItem(window, 0,FALSE, imagemenu); //http://msdn.microsoft.com/en-us/library/ms647616(VS.85).aspx } //---------------------------------------------------------------------- // SECTION 4: Event Dispatcher //---------------------------------------------------------------------- // Function ProcessMessages(): Specifies how to respond to user interactions LRESULT CALLBACK ProcessMessages(HWND window, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_CREATE: // what to do when the window is created MakeControls( window ); MakeMenu( window ); break; case WM_DESTROY: // what to do when the window is X-ed PostQuitMessage(0); break; case WM_COMMAND: // what to do if a control was activated switch(LOWORD( wParam)) { case ID_FEXIT: // control was Exit submenu? PostMessage(window, WM_CLOSE, 0, 0); break; case ID_FOPEN: // control was Open submenu? GetFileName( window ); break; case ID_BTTN: // control was Draw button? if ( !LoadData(window) ) DrawData( window ); break; case ID_INV: // control was Flip button? if ( !LoadData(window) ) Inv( window ); break; case ID_BLUR: //Control Blur submenu Blur( window ); break; case ID_ROTATE: //Control Rotate submenu Rotate90Degrees(window); break; case ID_FLIP: //Control Flip submenu Inv(window); break; case ID_Sharp: Sharp(window); break; } default: // what to do otherwise ( default ) return DefWindowProc( window, message, wParam, lParam); } return( 0 ); } //---------------------------------------------------------------------- // SECTION 5: Main Window and Application Code //---------------------------------------------------------------------- // The main program loop starts here int WINAPI WinMain( HINSTANCE thisprogram, HINSTANCE dummy1, LPSTR dummy2, int startmode) { // Define local variables HWND mainwindow; DWORD style; unsigned int dflt = CW_USEDEFAULT; MSG message; WNDCLASSEX wc; // Define the class for the main window wc.hInstance = thisprogram; // what program this class applies to wc.lpszClassName = "Main"; // the name of this class wc.lpfnWndProc = ProcessMessages; // the message processor wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); // the icon (large) wc.hIconSm = NULL; // the icon (small) wc.hCursor = LoadCursor( NULL, IDC_ARROW ); // the type of cursor wc.hbrBackground = ( HBRUSH )( COLOR_WINDOW ); // background color wc.cbSize = sizeof( WNDCLASSEX ); wc.style = 0; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.lpszMenuName = NULL; // Register this "Main" window class with the OS RegisterClassEx( &wc ); // Define the main window's style style = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN; // Create the main window mainwindow = CreateWindowEx(0, "Main", "ENBE 241: Imager", style, dflt, dflt, 400, 370, NULL, NULL, NULL, NULL); // Display the main window and all of its visible children ShowWindow( mainwindow, startmode ); // Continuously get and process messages while (GetMessage(&message, NULL, 0, 0) > 0) { TranslateMessage( &message ); DispatchMessage( &message ); } // That's It! return( 0 ); }
I realize that the sharpening isnt as good as it should be, but i couldnt think of any other way to do it. I still need to write parts for thresholding and coarsening the image, which i would really appreciate help with. thanks
|