A new algorithm for two-valued image connected domain tagging

Source: Internet
Author: User
Tags map vector

A new algorithm for two-valued image connected domain tagging

Eight-neighborhood tagging algorithm:

1) Judge this point eight in the neighborhood of the leftmost, upper left, top, upper right point of the situation. If there is no point, it represents the beginning of a new region.
2) If this point eight is the leftmost bit in the neighborhood, and the right side is a bit, mark this point as the smallest marker point in both, and modify the large mark to small mark.
3) If this point in the eight neighborhood is a bit on the left and the right is a bit, mark this point as the smallest marker point in both, and modify the large mark to small mark.
4) Otherwise, mark this point as one of four in the order of leftmost, upper left, top, right.


BOOL Cimagecolorprocess::connectedlabeltwopass (lpbyte lpsrc, lpbyte lpdst, int nsrccount, int nW, int nH) {if (Nsrccount!) = +) {AfxMessageBox ("non-RGB image, not processed!"); return false;}            Lpbyte M_lpimgbitsmove;int * M_lpnmark;       tag array pointer int * M_LPNMARKMOVE;       Tag array move pointer int m_nmarknumbers; Number of marked regions M_lpnmark = null;if (M_lpnmark = = NULL) {M_lpnmark = new INT[NW*NH]; ASSERT (M_lpnmark! = NULL); m_lpnmarkmove = M_lpnmark;}::memset ((lpbyte) m_lpnmark, 0, NW*NH * 4); int nmarkvalue = 1;//each identity Value, Nmarkvalue is incremented at the back to represent a different area, starting with the 1 mark.     int nmaxmarkvalue = 0;                 Record the maximum identification value of int i, J; The loop control variable/* Defines the linked list that holds the equivalent pair, whose elements are the Equalmark type, and the list is defined to conserve storage space.  To use CList, you should include <Afxtempl.h>. */clist < Equalmark, Equalmark > lequalmark;//Initialize the image move pointer m_lpimgbitsmove = lpdst;/* for the first scan, the resulting equivalence pair (Equalmark type) Added to the Lequalmark linked list. Use Nmarkvalue to make each new tag, and then add 1 to its value after the tag. Because of the layout relationship, this part of the code is also omitted to write. The author puts forward the following points to note when programming. Note1: The surrounding pixels of the image do not have 8 pixels adjacent to each other. At this point, according to the upper, lower, left and right four different conditions to do different to find the equivalent of the judgment. Note2: The equivalence pair can be sorted first, each time guaranteed Markvalue1<markValue2, so that it is easy to manage equivalence pairs. Note3: In the actual work, the continuous search for the equivalence pair is very easy to repeat, the equivalence pair found in the list and the last equivalent pair saved in the chain table, if not equal to the list of equivalent pairs, which can greatly reduce the list of equivalent pairs of repetition. Note4: After the first scan, nMarkValue-1 is nmaxmarkvalue. *//************************************************************************///The following is a supplementary code to complete the first scan of the image// Initializes an array of images and a pointer to an array of identifiers int nequalnum = 0;    Equalmark Tempequalmark; Used to temporarily store the equivalence relationship found each time M_lpnmarkmove = M_lpnmark;//m_lpimgbitsmove = M_lpimgbits;int Bobjectgray = 0;//marks the first row of the image, the first column of pixels ( Only this one pixel) if (*m_lpimgbitsmove = = Bobjectgray) {*m_lpnmarkmove = nmarkvalue++;} m_lpnmarkmove++;m_lpimgbitsmove++;//marks the first line of the image, at which point the equivalent is not present for (i = 1; I <= NW; i++) {//The case that needs to be marked if (*m_lpimgbitsmove = = Bobjectgray) {///preceded by a new tag if (* (m_lpnmarkmove-1) = = 0) {*m_lpnmarkmove = nmarkvalue++;} Before being marked, follow the previous tag else{*m_lpnmarkmove = * (m_lpnmarkmove-1);}} m_lpnmarkmove++;m_lpimgbitsmove++;} In addition to the first line of markup, an equivalent relationship for (j = 1; J <= NH; j + +) {M_lpimgbitsmove = lpdst + j*nw;m_lpnmarkmove = M_lpnmark + j*nw;//is displayed for each The first point of the line to do processing, the overall is the left column of the image to do the processing//only need to view the upper right two points IF (*m_lpimgbitsmove = = Bobjectgray) {//< on > position marked if (* (M_LPNMARKMOVE-NW)! = 0) {//follow < on > Mark *m_lpnmarkmove = * (M_LPNMARKMOVE-NW); if (* (M_LPNMARKMOVE-NW)! = * (M_LPNMARKMOVE-NW + 1) && * (M_LPNMARKMOVE-NW + 1)! = 0) {/ /< >< upper right > equivalent marker attachequalmark (tempequalmark, * (M_LPNMARKMOVE-NW), * (M_LPNMARKMOVE-NW + 1), Nequalnum, lequ Almark);}}   < on > No tag, there must be no equivalence relationship at this time else{if (* (M_LPNMARKMOVE-NW + 1)! = 0) {*m_lpnmarkmove = * (M_LPNMARKMOVE-NW + 1); Follow < top right > Mark}//< >, < right up > No tag, start new tag else{*m_lpnmarkmove = nmarkvalue++;}} m_lpnmarkmove++;m_lpimgbitsmove++;//marks the middle point of each line, at which time there are < left >, < top left >, < on >, < right > 4 cases for (i = 1; i <= nW-1; i++) {//Need to mark if ((*m_lpimgbitsmove) = = Bobjectgray) {//< left > Tagged if (* (m_lpnmarkmove-1)! = 0) {*m_lpnmarkmove = * (M          _LPNMARKMOVE-1); Follow < left > if (* (m_lpnmarkmove-1)! = * (m_lpnmarkmove-nw-1) && * (m_lpnmarkmove-nw-1)! = 0) {//Tag < Left >, &lt: Upper left > Equivalent Attachequalmark (Tempequalmark, * (m_lpnmarkmove-1), * (m_lpnmarkmove-nw-1), Nequalnum, Lequalmark);} if (* (m_lpnmarkmove-1)! = * (M_LPNMARKMOVE-NW) && * (M_LPNMARKMOVE-NW)! = 0) {//Mark < left >, < on > equivalent ATTAC Hequalmark (Tempequalmark, * (m_lpnmarkmove-1), * (M_LPNMARKMOVE-NW), Nequalnum, Lequalmark);} if (* (m_lpnmarkmove-1)! = * (M_LPNMARKMOVE-NW + 1) && * (M_LPNMARKMOVE-NW + 1)! = 0) {//Tag < left >, < right up >, etc. Price Attachequalmark (Tempequalmark, * (m_lpnmarkmove-1), * (M_LPNMARKMOVE-NW + 1), nequalnum, Lequalmark);}} < left > not tagged else{//< left upper > marked if (* (m_lpnmarkmove-nw-1)! = 0) {*m_lpnmarkmove = * (m_lpnmarkmove-nw-1);   if (* (m_lpnmarkmove-nw-1)! = * (M_LPNMARKMOVE-NW) && * (M_LPNMARKMOVE-NW)! = 0) {//Mark < top left >, < on > equivalent Attachequalmark (Tempequalmark, * (m_lpnmarkmove-nw-1), * (M_LPNMARKMOVE-NW), Nequalnum, Lequalmark);} if (* (m_lpnmarkmove-nw-1)! = * (M_LPNMARKMOVE-NW + 1) && * (M_LPNMARKMOVE-NW + 1)! = 0) {//Tag < top left >, < top right > equivalent Attachequalmark (Tempequalmark, * (m_lpnmarkmove-nw-1), * (M_LPNMARKMOVE-NW + 1), Nequalnum, Lequalmark);}}            < left >, < upper left > unlabeled else{if (* (M_LPNMARKMOVE-NW)! = 0) {*m_lpnmarkmove = * (M_LPNMARKMOVE-NW); Follow < on > Mark if (* (M_LPNMARKMOVE-NW)! = * (M_LPNMARKMOVE-NW + 1) && * (M_LPNMARKMOVE-NW + 1)! = 0) {//Tag &L t; Upper > and < Right upper > equivalent Attachequalmark (Tempequalmark, * (M_LPNMARKMOVE-NW), * (M_LPNMARKMOVE-NW + 1), Nequalnum, Lequalma RK);}}  < left >, < top left >, < > Not tagged, there is no equivalence relationship else{if (* (M_LPNMARKMOVE-NW + 1)! = 0) {*m_lpnmarkmove = * (m_lpnmarkmove      -NW + 1);    Follow < right > Mark}//< left >, < left >, < on >, < right > not marked, start new Tag Value else{*m_lpnmarkmove = nmarkvalue++;}}   < left >, < top left >, < > Not marked end}//< left >, < left up > unmarked end}//< left > not marked end}//else does not need to be tagged       m_lpnmarkmove++;m_lpimgbitsmove++;} The end of the intermediate point processing//The last point of each row is processed, the overall is the leftmost column of the image to do the processing//presence < Left;, < top left >, < on > 3 cases//need to mark if ((*m_lpimgbitsmove) = = Bobjectgray) {//< left > marked if (* (M_LPNMARKMOVE-1)! = 0) {*m_lpnmarkmove = * (m_lpnmarkmove-1); if (* (m_lpnmarkmove-1)! = * (m_lpnmarkmove-nw-1) && * (M_lpnmarkmov E-NW-1)! = 0) {//Mark < left >, < upper left > equivalent Attachequalmark (Tempequalmark, * (m_lpnmarkmove-1), * (M_LPNMARKMOVE-NW- 1), Nequalnum, Lequalmark);} if (* (m_lpnmarkmove-1)! = * (M_LPNMARKMOVE-NW) && * (M_LPNMARKMOVE-NW)! = 0) {//Mark < left >, < on > equivalent ATTAC Hequalmark (Tempequalmark, * (m_lpnmarkmove-1), * (M_LPNMARKMOVE-NW), Nequalnum, Lequalmark);}}    < left > Not tagged else{if (* (m_lpnmarkmove-nw-1)! = 0) {*m_lpnmarkmove = * (m_lpnmarkmove-nw-1); Follow < top left > if (* (m_lpnmarkmove-nw-1)! = * (M_LPNMARKMOVE-NW) && * (M_LPNMARKMOVE-NW)! = 0) {//Tag < left top & GT, < on > equivalent Attachequalmark (Tempequalmark, * (m_lpnmarkmove-nw-1), * (M_LPNMARKMOVE-NW), Nequalnum, Lequalmark);} }//< left >, < left top > else{if not marked(* (M_LPNMARKMOVE-NW)! = 0)    {*m_lpnmarkmove = * (M_LPNMARKMOVE-NW);  Follow < on > Mark}//< left >, < left up >, < on > not marked, start new Tag Value else{*m_lpnmarkmove = nmarkvalue++;}}} The last point of each row is processed, the overall is the leftmost column of the image to do the processing}//"Mark except the first line" end//because after each mark, Nmarkvalue will automatically + +//So the (-1) operation to record the maximum number of marks NMAXM Arkvalue = nmarkvalue-1;/************************************************************************//* defines the outer linked list of a double-decker list, Its element is a pointer to a list of inner layers. The type of the inner linked list is also cptrlist, and its elements are tagged values. */cptrlist exlist; CPtrList * pinnerlist; POSITION posexelem;if (Lequalmark.getcount ()! = 0) {//Pinnerlistadd, each new element added to exlist cptrlist * pinnerlistadd = new CPTR List;  ASSERT (Pinnerlistadd! = NULL);/* Adds the first equivalence pair to the innerlist pointed to by the first element of Exlist. */pinnerlistadd->addtail ((void *) Lequalmark.gethead (). MarkValue1);p Innerlistadd->addtail ((void *) Lequalmark.gethead (). MarkValue2); Exlist.addtail ((void *) pinnerlistadd); Lequalmark.removehead ();/* Define PFINDVALUE1 and PFindValue2, The head pointer of an inner linked list that finds a specific value in all the inner linked lists, that is, an element value of the outer list. */cptrlist * pFindValue1 = NULL; CPtrList * pFindValue2 = null;//collation remaining equivalent to while (!lequalmark.isempty ()) {Posexelem = Exlist.getheadposition (); pFindValue1 = Null;pfindvalue2 = Null;while (posexelem) {pinnerlist = (CPtrList *) Exlist.getat (Posexelem); if ( Pinnerlist->find ((void *) Lequalmark.gethead (). MarkValue1)) {pFindValue1 = pinnerlist;} if (Pinnerlist->find (void *) Lequalmark.gethead (). MarkValue2)) {pFindValue2 = pinnerlist;} Exlist.getnext (Posexelem);} The two values in the equivalence pair are in an already collated equivalence relationship if (pFindValue1 && pFindValue2) {///When two addresses are different, adjust the list if (pFindValue1! = pFindValue2) {PF Indvalue1->addtail (pFindValue2);/* Clear the list element, the CPtrList type obtained through new, must be deleted with delete, or it will cause a memory leak. */position Posdelete = Exlist.find ((void *) pFindValue2);p Findvalue2->removeall ();d elete pFindValue2; Exlist.removeat (Posdelete);}} /* Only find Value1 in the equivalent relationship that has been collated, then add Vaule2 to the linked list where Value1 is located. */else if (pFindValue1) {Pfindvalue1->addtail ((void *) Lequalmark.gethead (). MarkValue2);} else if (pFindValue2) {Pfindvalue2->addtail ((void *) Lequalmark.gethead (). MarkValue1);} /* Equivalence pair values are not found in the collated equivalence relationship, and new elements are added to the exlist. */else{cptrlist * Pinnerlistadd = new Cptrlist;pinnerlistadd->addtail ((void *) Lequalmark.gethead (). MarkValue1);p Innerlistadd->addtail ((void *) Lequalmark.gethead (). MarkValue2); Exlist.addtail ((void *) pinnerlistadd);}  Remove the first element of the equivalent pair at this time Lequalmark.removehead ();} while (!lequalmark.isempty ()) Loop End}//if (Lequalmark.getcount ()!=0) statement end/* Equivalent to the list size of 0, indicating that no equivalent pair was generated after the first scan, the mark has been completed 。 */else{return TRUE;} /* Equivalent relationship finishing, the following establishes the mapping relationship between the first scan's tag value and the second scan's tag value. */int ntotalequalnum = 0;   The number of tokens included in the equivalence relationship int nmarkregion = 0; The number of connected regions in the image Posexelem = Exlist.getheadposition (); while (Posexelem) {pinnerlist = (CPtrList *) Exlist.getat (Posexelem); Ntotalequalnum + = Pinnerlist->getcount (); Exlist.getnext (Posexelem);} Nmarkregion = Nmaxmarkvalue-ntotalequalnum + Exlist.getcount ();/* Defines the mapping vectors between the first and second scans, to use vectors, should include < Vector> and uses the Std namespace. */vector<markmapping> Vmarkmap (Nmaxmarkvalue);//Initialize the mapping vector so that it does its own mapping for (i = 0; i < Nmaxmarkvalue; i++) {Vmarkmap[I].noriginalmark = i + 1;vmarkmap[i].nmappingmark = i + 1;} POSITION Posinnerelem;              The position of the element in the innerlist int nMin; The minimum value in innerlist int nIndex = 0;posexelem = Exlist.getheadposition ();/* While loops implement the following functions: Find the smallest tag value in each equivalence group, The Nmappingmark in the mapping vector is then set to the smallest tag value of its equivalent group. */while (posexelem) {pinnerlist = (CPtrList *) Exlist.getat (posexelem); nMin = (int) pinnerlist->gethead (); Posinnerelem = Pinnerlist->getheadposition ();p innerlist->getnext (Posinnerelem), while (PosInnerElem) {if (int ) Pinnerlist->getat (Posinnerelem) < nMin) {nMin = (int) pinnerlist->getat (Posinnerelem);} Pinnerlist->getnext (Posinnerelem);} /* Adjusts the mapping vector based on the smallest tag value in each set of equivalence relationships. */posinnerelem = Pinnerlist->getheadposition (); while (Posinnerelem) {nIndex = (int) Pinnerlist->getat ( Posinnerelem)-1;vmarkmap[nindex].nmappingmark = Nmin;pinnerlist->getnext (Posinnerelem);} Exlist.getnext (Posexelem);} /* Locate and sort the parts of the map vector Nmappingmark that are not duplicated. Use the Find () and sort () two generic algorithms, which should include <algorithm>. */vector <int> Vsortmark (Nmarkregion); Sort vector NIndex = 0;for (i = 0; i < Nmaxmarkvalue; i++) {if (Find (Vsortmark.begin (), Vsortmark.end (), Vmarkmap[i].nmappi Ngmark) = = Vsortmark.end ()) {vsortmark[nindex++] = Vmarkmap[i].nmappingmark;}} Sort (Vsortmark.begin (), Vsortmark.end ());/* Re-adjusts the mapping vector based on the position of the sorted tag in the Vsortmark vector. */vector<int>::iterator itfind;vector<int>::iterator itbegin;itbegin = VSortMark.begin (); for (i = 0; i < Nmaxmarkvalue; i++) {Itfind = find (Vsortmark.begin (), Vsortmark.end (), vmarkmap[i].nmappingmark); Vmarkmap[i].nmappingmark = (ItFind- Itbegin + 1);} Adjusts the tag array based on the mapping vectors for (j = 0; J < NH; J + +) {M_lpnmarkmove = M_lpnmark + j*nw;for (i = 0; i < NW; i++) {if (*m_lpnmar Kmove! = 0) {*m_lpnmarkmove = Vmarkmap[*m_lpnmarkmove-1].nmappingmark;} m_lpnmarkmove++;}} Delete the element Posexelem = Exlist.getheadposition () in the linked list structure through new, while (Posexelem) {pinnerlist = (CPtrList *) Exlist.getat ( Posexelem);p Innerlist->removeall ();d elete Pinnerlist;exlist.getnext (Posexelem);} Exlist.removeall ();//through a class member variable toNumber of connected regions recorded m_nmarknumbers = Nmarkregion; CString S;s.format ("Number of connected areas is%d\n", nmarkregion); AfxMessageBox (s); return (long) TRUE;

Successfully identified as 6

A new algorithm for two-valued image connected domain tagging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.