Debris image seamless pitting technology VC ++ implementation

xiaoxiao2021-03-06  94

Fragment image Seamless Prazing Technology VC Realization Information Industry Electronics Twenty-second Institute Qingdao Duochang Lang Rui 2002-4-5 12:57:40

I. Introduction

In terms of mapping, Wenbo and other industries often encounter: observations are relatively large, in order to ensure that all of them can be taken, only partial photography, afterwards, then remove these partial photographic coincidence parts, spell A complete image is synthesized. In the past, there was a manual split, the error was large, and it is often unable to achieve seamlessly, even if there is a small number of professional equipment, the cost is generally higher. In fact, simply entering the photos through the scanner to the computer, through program processing, fully realize seamless fitness of multiple images, meet practical needs, and often encountered in the literature industry, no Rule objects such as the ancient word painting, etc., can also be seamlessly settled. This article brief introduction to the principles and processes of the program.

Second, the program design principle

First of all, we started from actual, we will save all the information through local photography, and to ensure that the sum of the information contained in these local photos can include the overall information inevitable to make each two adjacent pictures A portion of the overlapping part can be guaranteed that any information is not missed during the process of dividing the overall object into several local photos and then spelling the overall image, that is, the division, the entire process of patching is non-destructive. In this case, we only need to guarantee that the overlapping parts of the two adjacent images can be completely coincident, then we can definitely be seamlessly picked in this state. Therefore, the problem is converted to enable the overlapping portion of the adjacent picture to be completely coincident, and it is judge whether the two identical image segments fully overlap can be intuitive judgments, such as we can use "different or" mask, When the pixels of the two pictures in the same position are 0, it is black, so that two images can be moved, and as long as the overlapping part is all black, it indicates that the overlapping portion of the two images has been accurate, while at this time The seamless fitness of the image is also realized. Thereafter, only the "or" raster mask will display the merged image, and then the reply can be stored by a copy screen. In the full process of achieving the splicing, the image is mainly involved in the drag and drop of the image, the reading of the image file, and the storage of the raster mask, the copy screen, and the memory bitmap are saved. The specific application of these technologies will be described next.

Third, the specific implementation of the program

Before you are patching, you must first read images from the scanner from the file to the memory and display it. Since the raster operation mask taken when the grating operation taken is "different or", to keep the image of the original appearance, the initial background of the entire client area can be set to black in the response function of the message WM_ERASEBKGND.

......

PDC-> Patblt (0, 0, Rect.width (), Rect.Height (), Blackness;

Return True;

Reading bitmap files can be implemented with the loadImage function, m_spath1 specifies the path to the file, the LR_LoadFromFile property specifies the reading bitmap from the file, the return value is the handle of the bitmap:

......

Hbitmap hbitmap;

Hbitmap = (hbitmap) loadimage (AfxGetInstanceHandle (),

M_spath1,

Image_bitmap, 0,0,

Lr_loadfromfile | lr_createdibsection;

Then we can create a memory device environment HMEMDC1 compatible with the current device environment and place the bitmap just read to the memory in the device environment:

HMEMDC1 = :: CreateCompatibleDC (NULL);

SelectObject (HMEMDC1, HBitmap);

:: deleteObject (hbitmap); // Release the used bitmap handle

Invalidate ();

As for the display of bitmaps, due to frequent drag and other processing, placing it is more reasonable in the onDraw function, and only reverse the display is refreshed with the invalidate () function when updating the display. The display bitmap section in OnDraw () is best to complete with a Bitblt function, which is responsible for placing the bitmap in HMEMDC1 in the PDC page to complete the replacement of the memory page, and its processing speed is still relatively fast: ......

:: Bitblt (PDC-> M_HDC, M_NX1, M_NY1, M_NWIDTH1, M_NHEIGHT1, HMEMDC1, 0, 0, M_DWROP);

......

The M_DWROP variable in the function is set to the raster operation code, which is initially set to the Srcinvert, which is set to the SrcPaint grating or operation when the merged effect needs to be displayed.

We can implement bitmaps in the client zone to drag and drop by programming the mouse message response function. Follow the WINDOWS system habits, first determine that the mouse is pressed by the PtinRect () function in the left button of the mouse. Whether it is falling on a bitmap, if you can drag the picture with the mouse before the left button, obviously write code within the response function of the WM_MOUSEMOVE message:

......

IF (m_bcanmove1 == true) // Before the movement, the left mouse button is on the image.

{

INT DX = m_noldx1-m_nx1; // calculate the distance of the mouse distance image origin

INT DY = m_noldy1-m_ny1;

m_nx1 = point.x-dx; // Calculate the coordinates of the new image origin (customer area coordinate)

m_ny1 = point.y-dy;

INVALIDATE (); // update view

}

m_noldx1 = point.x; // Save the last mouse position

m_noldy1 = point.y;

......

As soon as this, the program can run a plurality of debris images, dragging an image with the mouse moves on the edge of another image, because the "different or" raster mask, two pictures overlapping places The color will change, but only when it is completely coincident, it indicates that the spelling at this time is seamless, and the mask is changed to "or" to display the appearance after the appearance. But at this time, it is only retained in memory, and further processing can be reserved after the merged image storage.

First, the position, size of the rectangular frame where the merged image is located, and can be done with the following similar code (this example can have a 4-piece image to fit):

......

INT TEMP1, TEMP2, X0, Y0, X1, Y1;

Temp1 = m_nx1

IF (m_spath3! = ") // If there are three pictures to participate in the fit

{

IF (m_spath4! = "") // If there are 4 pictures to participate in the stand

Temp2 = m_nx3

Else

TEMP2 = m_nx3;

X0 = TEMP1

}

Else

X0 = TEMP1;

......

Temp1 = m_nx1 m_nwidth1> m_nx2 m_nwidth2? m_nx1 m_nwidth1: m_nx2 m_nwidth2;

IF (m_spath3! = "")

{

IF (m_spath4! = "")

TEMP2 = m_nx3 m_nwidth3> m_nx4 m_nwidth4? m_nx3 m_nwidth3: m_nx4 m_nwidth4;

Else

Temp2 = m_nx3 m_nwidth3;

X1 = TEMP1> TEMP2? TEMP1: TEMP2;}

Else

X1 = TEMP1;

Y0 and Y1 can be calculated with similar code. The rectangle composed of X0, Y0, X1, Y1 must be converted to a screen coordinate before the screen screenshot is performed, and can be implemented with a clienttoscreen () function. Below is the main implementation code of the function that copies the screen specified area in a bitmap to the memory:

HBitmap CIMAGEVIEW :: CopyScreenTOBITMAP (LPRECT LPRECT)

{

......

/ / Make sure the selected area is not empty rectangle

IF (IsRectempty (LPRECT))

Return NULL;

/ / Create a device description table for the screen

HSCRDC = CREATEDC ("Display", NULL, NULL, NULL;

/ / Create a compatible memory device description for the screen device description table

HMEMDC = CREATECOMPATIBLEDC (HSCRDC);

......

// Create a bitmap compatible with the screen device description table

Hbitmap = createcompatiblebitmap (hscrdc, lprect-> width (), lpRect-> height ());

/ / Put the new bitmap in the memory device descriptor table

Holdbitmap = (hbitmap) SelectObject (HMEMDC, HBitmap);

/ / Copy the screen device description table to the memory device descriptor table

Bitblt (HMEMDC, 0, 0, LPRECT-> Width (), LPRECT-> HEIGHT,

HSCRDC, LPRECT-> LEFT LPRECT-> TOP, SRCCOPY;

/ / Get the handle of the screen bitmap

Hbitmap = (hbitmap) SelectObject (HMEMDC, Holdbitmap);

// Clear

Deletedc (HSCRDC);

Deletedc (HMEMDC);

......

// Return the bitmap section

Return hbitmap;

}

When the collocated area is copied to the memory, and after the handle of the memory bit map, it can be transferred to the other graphics processing software after the handle of the memory bit map, and it can be directly used in the format of the bitmap. Save as a document, for easy gauges, this example uses the latter. Its implementation process is mainly to fill the information head and pixel array of the BMP file according to the memory bit acquired, and the key code of the implementation is introduced below.

First get the device description table handle, and use the function getDeviceCaps () to get the number of bytes per pixel at the current display resolution, and calculate the size of the palette accordingly:

......

HDC = CREATEDC ("Display", NULL, NULL, NULL;

IBITS = GetDeviceCaps (HDC, Bitspixel) * getDeviceCaps (HDC, Plaso);

Deletedc (HDC);

IF (iBITS <= 1)

Wbitcount = 1;

Else IF (iBITS <= 4)

Wbitcount = 4;

ELSE IF (iBITS <= 8)

Wbitcount = 8;

Else IF (iBITS <= 24)

Wbitcount = 24; // Calculate the palette size

......

Then you can set the bitmap information head structure, where Bi is an instance object of the BitMapInfoHeader structure:

......

IF (wbitcount <= 8)

DWPALETESIZE = (1 <

GetObject (HbitMap, Sizeof (Bitmap), (LPSTR) & Bitmap;

Bi.bisize = sizeof (BitmapInfoHeader); bi.biwidth = bitmap.bmwidth;

bi.biheight = bitmap.bmheight;

Bi.Biplanes = 1;

Bi.biBitcount = WbitCount;

Bi.Bicompression = BI_RGB;

Bi.bisizeImage = 0;

bi.bixpelspermeter = 0;

Bi.biypelspermeter = 0;

Bi.bi.biRused = 0;

Bi.Biclrimportant = 0;

Use the globalalloc () function to allocate memory according to the calculated result, and return the assigned memory handle HDIB,

And use getStockObject () to set the palette under the default state:

......

DWBMBITSSIZE = ((Bitmap.Bmwidth * Wbit 31) / 32) * 4 * bitmap.bmheight;

HDIB = GlobalAlloc (GHND, DWBMBITSSI DWPALETTESIZE SIZEOF (BitmapInfoHeader);

LPBI = (LPbitMapInfoHeader) Globalock (HDIB);

* LPBI = Bi; // Processing palette

HPAL = getStockObject (Default_palette);

IF (HPAL)

{

HDC = :: getDC (null);

Holdpal = SELECTPALETTE (HDC, HPALETTE) HPAL, FALSE);

RealizePalette (HDC);

}

/ / Get new pixel values ​​under this palette

GetDibits (HDC, Hbitmap, 0, (UINT) Bitmap.Bmheight,

(LPSTR) LPBI SIZEOF (BitMapInfoHeader) DWPALETESIZE,

(BitMapInfo *) lpbi, dib_rgb_colors;

// Restore palette

IF (Holdpal)

{

SelectPalette (HPALETTE) HOLDPAL, TRUE;

RealizePalette (HDC);

:: ReleaseDC (NULL, HDC);

}

......

The final job is to create a bitmap file, you need to save the set bitmap file header and pixel point array information in the file, where bmfhdr is an instance object of the BitmapFileHeader bitmap file header, which needs to be in the BMP bitmap. Format is set:

......

FH = CREATEFILE (LPFileName,

Generic_Write, 0, NULL,

Create_ALWAYS,

FILE_ATTRIBUTE_NORMAL | file_flag_sequential_scan,

NULL);

/ / Set the bitmap file header

BMFHDR.BFTYPE = 0x4d42; // "BM"

Dwdibsize = sizeof (bitmapfileHeader) sizeof (BitmapInfoHeader)

DWPALETESIZE DWBMBITSSIZE;

BMFHDR.BFSIZE = DWDIBSIZE;

BMFHDR.BFRESERVED1 = 0;

BMFHDR.BFRESERVED2 = 0;

BMFHDR.BFOFFBITS = (DWORD) SIZEOF (BitmapFileHeader)

(DWORD) SIZEOF (BitMapInfoHeader) dwpalettesize; // Write the bitmap file header

Writefile (FH, (LPSTR) & BMFHDR, SIZEOF (BitmapfileHeader), & dwwritten, null;

/ / Write the rest of the file

Writefile (FH, (LPSTR) LPBI, DWDIBSIZE, & DWWRITEN, NULL

......

Fourth, the example of the program

Below, you can detect the procedure of the procedure by one example - a plaque of ancient Chinese painting. Among them, Fig. 1 to three is the three ancient Chinese paintings before spelling, and Fig. 4 is a seamless picture obtained by the post-depositor after the process. After testing, the spell effect is quite good, and there is no seam at all in the joint of the fragment image:

Image of image of a plywood

Patching image

Summary: This program tells a practical method for processing pictures seamlessly by an instance, with large application potential in surveying, survey, and Wenbo and other industries. Under the premise of understanding the design ideas and programming ideas, combined with specific actual needs, similar software that is more suitable for this unit's actual situation can be designed by modifying the specific code of this article. In addition, the interception described herein can also provide a certain reference in the preparation of the screen technology. This program is compiled by Microsoft Visual C 6.0 under Windows 98. <> <> <>

转载请注明原文地址:https://www.9cbs.com/read-121804.html

New Post(0)