Parallel image refinement algorithm and C code implementation

xiaoxiao2021-03-06  34

Parallel image refinement algorithm and C code implementation

Image refinement generally appears as an image pretreatment technique, the purpose is to extract the skeleton of the source image, that is, the lines of the line width in the original image are refined into only one pixel width, form an 'skeleton', formation. After the skeleton, it is easier to analyze images, such as the characteristics of the image.

Detained into serial refinement and parallel refinement 2, serial refinement is to detect the deletion of the detailed point while detecting the details of the refinement conditions, and the parallel refinement is the deletion of the detection refinement point. Only tags, and remove points to be refinered once after detecting the complete unit.

Refining basic thinking is 'layer deprive', that is, starting from the edge of the line to deprive, until the line remains in the lower pixels. Before the refinement algorithm, the image should be used to 2 value, ie the image The color is straight to the color of 'black' and 'white' 2.

Refining algorithm: 8 defenses in microscopic detection points (due to parallel refinement, some templates are extended to 12 defenses), as follows xxxxoxxxx where o is the detection point X for its adjacent point, 1 means black Point, use 0 to represent the point of any color, the point to deprive (delete) to satisfy one of the eight templates. Template A (to right) 0x1x01110x1x Template B (to right) 00 xx0111x11x template C (to right) X11X011100XX template D111x1x000 template E1X01101x0 template FX00110X1X template GX1X110X00 template H (downward) 000 x1x111x1x

The points that meet the above 8 templates are the points to be deprived, because the points that meet these 8 templates can be confirmed as the point on the edge of the line.

In the case of these 8 templates, there is still the following two special edge points, and the special edge point 1000010111 special edge point 2001011001 causes this 2 special points for the expansion template A and expanded The post-template H. The expansion is to prevent even-numbered lines from being completely eliminated (parallel refined). Solution is to make a serial detail after parallel, select reduced Template A and Template H Template A (After a reduction) 0x10110x1 template H (After reduction) 000x1x111 where a narrowed template A solves the special case 1, the reduced template h solves the special case 2, pay attention to this is serial refinement The critical code is as follows, based on this principle.

// ------------------------------- BCB6 code #include #pragma HDRSTOP # include #include "unit1.h"

#include "file1.h" #include #include #include

#pragma pack (1) using namespace std;

/ * Program: image thinning Author: sboom (Lingch) Date: January 18, 2005 * /// BMP file header

struct BITMAPFILEHEADER_ {short type; int bfSize; short re1, re2; int Offbits;}; // BMP header struct BITMAPINFO_ {long size; long width, height; short planes, bitCount; long comp, sizeImg; long xpels, ypels; Long buy, important;

// ------ Correct the data of the BMP color table to the data of the BCB TColor. TColor * SwitchColor (unsigned char r, unsigned char g, unsigned char b) {tcolor * re = new tcolor; * re = (r | g << 8 | b << 16); * RE = * RE & 0x00FFFFFFFFFFff; Return RE;}

Void xxx () {file * f = fopen ("f: //1.bmp", "rb"); if (f == null) / * Judgment file is open success * / {showMessage ("File Open Error" Return;} fseek (f, 0, 0); // Move to the beginning

// ---------- Read BMP file head BitmapFileHeader_ * bmph = new bitmapfileHeader_ (); if (FREAD ((Char *) BMPH, SIZEOF (BitmapFileHeader_), 1, f) == null) {showMessage ("File Read Error"); RETURN;}

// ----------- Read BMP information head bitmapinfo_ * bmpi = new bitmapinfo_ (); if (FREAD ((char *) BMPI, SIZEOF (BitmapInfo_), 1, f) == null) { ShowMessage ("File Read Error2"); return; fseek (f, bmph-> offbits, 0); // ---------- display some information form1-> edit1-> text = INTOSTR (BMPH -> bfsize); form1-> edit2-> text = inttostr (bmpi-> width); form1-> edit3-> text = INTOSTR (bmpi-> height); form1-> edit4-> text = INTOSTR (bmpi-> Comp); FORM1-> Edit5-> text = INTOSTR (BMPI-> Used);

INT I, J, K, L, WC, POS; long n = bmph-> bfsize- bmph-> offbits; // Pixel total Unsigned char * image = new unsigned char [n]; // bit map matrix

Fread (image, n, 1, f); // read bitmap matrix

INT SKIP = 0; // BMP file 4 byte align IF (bmpi-> width% 4 == 0) Skip = 0; else skip = 4-bmpi-> width% 4; unsigned char color = 0; tcolor * TC ;

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # 2 value for (i = 0; I

While (Flag == 1) // FLAG = 0 {结 结 {{{

FLAG = 0; for (i = 2; i height-2; i ) {for (j = 2; j width-2; j ) {// template Aix (Image [(i -1) * (BMPI-> Width Skip) J-1] == 0xFF && Image [(i-1) * (BMPI-> Width SKIP) J 1] == 0 && Image [(i ) * (BMPI-> Width SKIP) J-1] == 0xFF && Image [(i) * (BMPI-> Width Skip) J-0] == 0 && Image [(i) * (BMPI -> Width Skip) J 1] == 0 && Image [(i) * (BMPI-> Width Skip) J 2] == 0 && Image [(i 1) * (BMPI-> Width SKIP) J-1] == 0xFF && Image [(i 1) * (BMPI-> Width Skip) J 1] == 0) {om [(i) * (bmpi-> width SKIP) J] = 0xFF; Flag = 1; Continue;}

// Template B IF (Image [(i-1) * (BMPI-> Width Skip) J-0] == 0 && Image [(i-1) * (BMPI-> Width Skip) J 1] == 0 && Image [(i) * (BMPI-> Width Skip) J-1] == 0xFF && Image [(i) * (BMPI-> Width Skip) J-0] == 0 && Image [(i) * (BMPI-> Width Skip) J 1] == 0 && Image [(i) * (BMPI-> Width Skip) J 2] == 0 && Image [ (i 1) * (BMPI-> Width SKIP) J-1] == 0xFF && Image [(i 1) * (BMPI-> Width Skip) J-0] == 0xFF) {om [(i) * (BMPI-> Width Skip) J] = 0xff; Flag = 1; Continue;} // Template C IF (Image [(i-1) * (BMPI-> Width Skip) J -1] == 0xff && image [(i-1) * (BMPI-> Width Skip) J-0] == 0xFF && Image [(i) * (BMPI-> Width Skip) J-1 ] == 0xff && Image [(i) * (BMPI-> Wi DTH SKIP) J-0] == 0 && Image [(i) * (BMPI-> Width Skip) J 1] == 0 && Image [(i) * (BMPI-> Width Skip) J 2] == 0 && Image [(i 1) * (BMPI-> Width Skip) J-0] == 0 && Image [(i 1) * (BMPI-> Width Skip) J 1] ==

0) {om [(i) * (BMPI-> Width SKIP) J] = 0xff; Flag = 1; Continue;} // Template D if (Image [(i-1) * (BMPI-> Width SKIP) J-1] == 0xff && Image [(i-1) * (BMPI-> Width Skip) J-0] == 0xFF && Image [(i-1) * (BMPI-> Width SKIP) J 1] == 0xff && Image [(i) * (BMPI-> Width Skip) J-0] == 0 && Image [(i 1) * (BMPI-> Width Skip) J-1] == 0 && Image [(i 1) * (BMPI-> Width Skip) J-0] == 0 && Image [(i 1) * (BMPI-> Width Skip) J 1] == 0) {om [(i) * (BMPI-> Width Skip) J] = 0xFF; FLAG = 1; CONTINUE;}

// Template E IF (Image [(i-1) * (BMPI-> Width Skip) J-1] == 0 && Image [(i-1) * (BMPI-> Width Skip) J 1] == 0xFF && Image [(i) * (BMPI-> Width Skip) J-1] == 0 && Image [(i) * (BMPI-> Width Skip) J-0] == 0 && Image [(i) * (BMPI-> Width Skip) J 1] == 0xFF && Image [(i 1) * (BMPI-> Width Skip) J-1] == 0 && Image [(i 1) * (BMPI-> Width SKIP) J 1] == 0xFF) {om [(i) * (BMPI-> Width Skip) J] = 0xFF; Flag = 1; Continue;} // Template F IF (Image [(i-1) * (BMPI-> Width Skip) J-0] == 0 && Image [(i) * (BMPI-> Width Skip) J -1] == 0 && Image [(i) * (BMPI-> Width Skip) J-0] == 0 && Image [(i) * (BMPI-> Width Skip) J 1] = = 0xFF && Image [(i 1) * (BMPI-> Width Skip) J-0] == 0xFF && image [(i 1) * (BMPI-> Width Skip) J 1] == 0xFF) {om [(i) * (BMPI-> Width Skip) J] = 0xFF; Flag = 1 Continue;}

// Template G IF (Image [(i-1) * (BMPI-> Width Skip) J-0] == 0xFF && Image [(i-1) * (BMPI-> Width Skip) J 1] == 0xFF && Image [(i) * (BMPI-> Width Skip) J-1] == 0 && Image [(i) * (BMPI-> Width Skip) J-0] == 0 && Image [(i) * (BMPI-> Width SKIP) J 1] == 0xFF && Image [(i 1) * (BMPI-> Width Skip) J-0] == 0) {Om [(i) * (BMPI-> Width Skip) J] = 0xFF; FLAG = 1; Continue;}

// Template H if (image [(i-2) * (BMPI-> Width Skip) J-0] == 0 && Image [(i-1) * (BMPI-> Width Skip) J- 1] == 0 && Image [(i-1) * (BMPI-> Width SKIP) J-0] == 0 && Image [(i-1) * (BMPI-> Width Skip) J 1] == 0 && Image [(i) * (BMPI-> Width Skip) J-0] == 0 && Image [(i 1) * (BMPI-> Width Skip) J-1] == 0xff && image [(i 1) * (BMPI-> Width Skip) J-0] == 0xff && Image [(i 1) * (BMPI-> Width Skip) J 1] == 0xFF) {om [(i) * (BMPI-> Width Skip) j] = 0xff; Flag = 1; Continue;}}}} for (i = 0; i

// ------------ Second serial refinement for (i = 2; i height-2; i ) {for (j = 2; j width-2; j ) {// Regenerated template A IF (Image [(i-1) * (BMPI-> Width Skip) J-1] == 0xFF && Image [(i-1) * (BMPI-> Width SKIP) J 1] == 0 && Image [(i) * (BMPI-> Width Skip) J-1] == 0xFF && Image [(i) * (BMPI-> Width Skip) J-0] == 0 && Image [(i) * (BMPI-> Width Skip) J 1] == 0 && Image [(i 1) * (BMPI-> Width SKIP) J-1] == 0xFF && Image [(i 1) * (BMPI-> Width Skip) J 1] == 0) {Image [(i) * (BMPI-> Width Skip ) j] = 0xff; Flag = 1; Continue;} // Regenerated template H if (Image [(i-1) * (BMPI-> Width Skip) J-1] == 0 && Image [ (i-1) * (BMPI-> Width Skip) J-0] == 0 && Image [(i-1) * (BMPI-> Width Skip) J 1] == 0 && Image [(i) * (BMPI-> Width Skip) J-0] == 0 && Image [(i 1) * (BMPI-> Width SKIP) J-1] == 0xFF && Image [(i 1) * (BMPI-> Width Skip) J-0] == 0xFF && Image [(i 1) * (BMPI-> Width SKIP)

J 1] == 0xFF) {image [(i) * (bmpi-> width Skip) j] = 0xff; Flag = 1; Continue;}}} // ---------- ----- Display graphical for (i = 0; i height; i ) {for (j = 0; j width; j ) {// ----- Raw Graphic color = Image [(i) * (BMPI-> Width SKIP) J]; TC = SwitchColor (Color, Color, Color); Form1-> Canvas-> Pixels [10 J] [400-I] = * TC; }} // ---------- Close the file fclose (f);

/ / -------------------------------------------------------------------------------------------- ------------------------ Original map and refinement renderings

Refining is a common image pretreatment technology. There are currently many image refinement algorithms. After refining, it can analyze images after refinement, and now there are still many images, and the feature analysis algorithm is based on refined images.

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

New Post(0)