Overlapping image stitching technology based on multi-document window model in Visual C
Department of Computer Science and Technology, Fushun Petroleum College, Liaoning Province Li Zhigang
Summary:
Image splicing is a problem that is often encountered in the application of panoramic video system, geographic information system, etc., this article, this article, based on grid matching methods, the image of the boundary part has a more effective alignment algorithm, and Seamless splicing is achieved by smoothing factors. And applied the document window model to implement the algorithm, and complete the display, storage of bitmap files, and has a certain common meaning.
Key words:
Image splicing, algorithm, overlapping image, document window, bitmap file, image display
First, multi-document window model overview
The AppWizard of the MFC can generate three types of applications: Based on the dialog, single document application (SDI), and multi-document applications (MDI). In three applications, the multi-document application (MDI) is most complicated, and its functions are also stronger. When we generate a multi-document application with AppWizard, the system automatically generates a document class inherited from the CDocument class, a window class inherited from the CMDichildWnd class, from the CVIEW class class. When we establish a new document each time, the program generates a new instance according to the document template, which we can use the code to automatically generate code. But if we want to use multiple different document classes in the program, you need to create a document template and control the establishment of document instances. Suppose we have to add a TEST document to a multi-document-based project MDI. Specific steps are as follows:
1. Establish a framework class CTestFrame base class with ClassWizard to select cmdichildwnd.
2. Establish a document class ctestdoc base class to select CDocument with ClassWizard.
3. Create a document class CTestView base class to select CView with ClassWizard.
4, add three classes of header files to the application class cmdiapp.
5. Create a new document template, add the following code in the cmdiapp :: initInstance () function
CMULTIDEMPLATE * PDOCTEMPLATE;
PDOCTEMPLATE = New CMULTIDOCTEMPLATE
IDR_TESTTYPE,
Runtime_class (ctestdoc),
Runtime_class (ctestframe),
Runtime_class (ctestView));
AddDDOCTemplate (pdoctemplate);
6. Define a menu item ID number to ID_Newtest, use classwizard to add its handle function to the application class (or the main frame class), add the following code in its handset cmdiapp :: onnewtest () function
Position CurtemplatePos = getFirstDoCtemplatePosition ();
While (CURTEMPLATEPOS! = NULL)
{
// Remove a document template pointer
CDOCTemplate * CURTemplate = getNextdDoCtemplate (CURTEMPLATEPOS);
CString Str;
Curtemplate-> getDocstring (str, cdoctemplate :: DOCNAME); // Take a document name
IF (str == _t ("test")) // Determine if the current document document TEST class
{
CURTEMPLATE-> OpenDocumentFile (null); // Create a new document instance
Return;
}
}
This way we have established a new document class. Note that we use a document type resource idR_testType when you create a document template in 5, which is defined in the resource file (not included in the icon and menu): StringTable PrelioAd DiscArse
Begin
........
IDR_TESTTYPE "/ntest/ntest/n/nmdi.document/ntest document"
End
The document type identifier includes seven substrings, including window titles, document names, file extensions, and more. In 6 Curtemplate-> getDocstring (str, cdoctemplate :: DOCNAME); take the second substring, document name. After the document is established, we can operate it. Of course, communications between documents and window classes, document classes, and main window classes, and different document classes are more complicated. It is not a few words. If you are not familiar with the reader of the document window, please refer to other relevant information.
Second, overlapping image stitching technology
1. Algorithm ideology
In the process of achieving panoramic video systems, geographic information systems (GIS), and other applications, we usually encounter such a problem, that is to put several small images into a large image. . In order to allow computer to automatically align the image. We require that the image boundary to be spliced is partially overlapping, the computer is using this information to match alignment. The overall idea of matching algorithm is to ensure the accuracy of the alignment, but also to ensure that the volume is not too large. Here the algorithm uses the own characteristics of the image, both in the general image, the gradation value of adjacent pixel points is not large. Therefore, you can take a grid on the boundary of the second image, then move the grid on the first image, calculate the difference between the two images of all grid points correspond to the difference in the RGB value of the pixel point. with. Record the smallest value of the grid position, that is, it is considered to be the best matching position. (Figure 1) In order to reduce the amount of operation, we divide the match into two steps, the first step is rough, and the grid spacing is moved horizontally or vertically at this stage. After completing a rough match, we perform precise match at the current best matching point, at which the current best match point is centered, the grid is down, and the left and right moves a small step. The initial step length is half the step of moving the steps when the rough splicing is half a grid spacing. Constantly compare the current minimum square, if excellent is excellent than the current value, replace the current best match point. The loop is reduced for each step, until the horizontal step and the vertical step is 0.
(a) stitching the front two image (b) post-synthesis image
Figure 1 Grid matches
2. Algorithm Description
Procedure ImageMatching, PROCEDUREMAGEMATCHING
{
Enter FirstImage;
Enter SecondImage;
// Get the size of the two images
Height1 = GetImageHeight (firstImage);
Height2 = GetImageHeight (SecondImage);
Width1 = GetImageWidth (FirstImage);
Width2 = GetImageWidth (SecondImage);
// Take a mesh matching template from the second image
SecondImageGrid = GetSecondImageGrid (SecondImage);
// Rough match, the grid moves from left to right in the first image, moves from bottom to top, moving a grid spacing, step_width or step_height, when the grid is removed after the grid
Y = heitht1-gridheight; minvalue = maxinteger;
While (Y { X = grid_width / 2; // When the grid is on the left side of the first image, the abscissa of the A point. While (x <(width1-grid_width / 2)))) { FirstImagegrid = GetImgaeGrid (FirstIMGAEGRID, X, Y); Differ = ca Aculated, secondimagegrid, secondimagegrid; / / calculate the pixel value // IF (Differ { BestMatch_x = x; Bestmatch_y = y; Minvalue = DIFFER; } X = x step_width; } y = y-step_height; } / / Accurate match Step_width = step_width / 2; Step_height = step_height / 2; While (Step_HEight> 0 & Step_Width> 0) // Ends when horizontal and vertical steps are reduced { IF (step_height == 0) // When only vertical step length is reduced to zero, it is set to 1 Step_height = 1; IF (Step_width == 0) // When only horizontal step is reduced to zero, it is set to 1 Step_width = 1; TEMP_X = BestMatch_x; Temp_y = bestmatch_y; For (i = -1; i <1; i ) For (j = -1; j <1; j ) { IF ((i = 0 & j! = 0) | (i! = 0 & j = 0)))) { Firstimagegrid = GetImgaeGrid (FirstIMGAEGRID, Temp_x i * step_width, temp_y j * step_height); Differ = Caculatediff (FirstIMGAEGRID, SecondImageGrid); IF (Differ { BestMatch_x = x; Bestmatch_y = y; Minvalue = DIFFER; } } } Step_height = step_height / 2; Step_width = step_width / 2; } } Third, overlapping image stitching technology based on multi-text-blocking window model Programs require attention to some technical issues during the Visual C implementation. 1, read and display of bitmap files The bitmap file is the simplest image file, and several data for each point of the screen image. There are 1, 4, 8-bit, 24-bit standards. The 24-bit bitmap does not contain a color table, and each pixel is represented by 3 bytes, which represents the blue, green, red grayscale value in the RGB space. Each bitmap file consists of two parts, a part is the file header and bitmap information head, and the other is the number of images of the image. So you want to display a bitmap file first to declare a CFILE class instance read files into memory, then obtain the relevant information of the image and the start address of the image according to the file header and bitmap information header. Call the setDibitStodevice () function to display the image on the screen. 2, the acquisition of any pixel point color value in bitmap file To achieve the processing of images, the pixel values that access any pixel points are required. There are two points when accessing the bitmap file, one is that the storage of the image bit array is done from bottom to top. That is to say, the most basic data of the image has the first position of the bit array. Another feature is that the space occupied by each line of the image is an integer multiple of the double word, and it is insufficient to use zero filled. The actual storage size of each row of pixels can be calculated by the following formula. Widthbytes = ((biwidth * bibitcount) 31) & ~ 31) >> 3 (1) Assuming the starting pointer of the bit array is the pointer of the pixel value of the LPStartBits screen coordinates (X, Y) available in the formula. LPBITS = LPSTARTBITS (Width-y-1) x * Bibitcount); (2) Where widthbytes is the value calculated by (1), Height is the height of the image. 3, implementation of data exchange between different documents Data exchange between different document classes We can do with the application class or main window class. In the document class or window class, the pointer to the application class and the main window class can be obtained by AFXGetApp () or AFXGETMAINWnd (). In the application class and main window classes, the document class can be accessed by obtaining a document class. . This allows us to exchange data by applying a member variable of the application class or the main window class. 4, smoothing connection After finding the best match point, the subsequent work will be an image to synthesize two images. For overlapping portions, if we simply take the first image or the data of the second image, we will cause the blurred and obvious boundary of the image, which cannot be tolerated. Even if the average of two images is taken, the effect cannot be satisfactory. In order to make the splicing area smooth, we use the quality of the image, that is, the overlapping part is slowly transitioned from the first image from the first image, and it is natural that we can think of a gradual change. The factor is 0 Four, overlapping image splicing program frame for multi-text windows model 1. Program composition In addition to application classes, main window classes, including CFRistImagedoc, CSecondImagedoc, CTHIRDIMAGEDOC class to save the first image of the first image. There is also a document class and framework that is connected thereto. 2. Procedure flow chart The main operation of the program is completed in the application class. First, open the first image, the image of the image is completed by the cfirstimagedoc class and the window class and framework associated with it, and the bit number group pointer and image size Pass to the application class member variable. Then open the second image to complete the display, etc., and pass the bit argument pointer and the image size to the application class member variable. Complete the image matching alignment of the image in the application class, and finally realize the synthesis of the image. The synthesized image is passed to the CTHIRDIMAGEDOC class to display that after the user is basically satisfied with the splicing result, you can select a smoothed connection to connect the two images smoothly. The user can save the last result into a BMP file. Five, summary