(原創) 如何产生Yokoi Connectivity Number? (.NET) (C++/CLI) (C/C++) (Image Processing)

本范例先将leng.jpg轉成binary image,然後从512*512 downsampling成64*64,downsampling的规则为以8*8为unit,取topmost-left为downsampled data,最后产生Yokoi Connectivity Number。

  1/* 
  2(C) OOMusou 2006 http://oomusou.cnblogs.com
  3
  4Filename    : memset1.cpp
  5Compiler    : Visual C++ 8.0
  6Description : Demo how to produce Yokoi connectivity number
  7Release     : 12/06/2006
  8*/

  9
 10#include "stdafx.h"
 11#include <fstream>
 12#include <iostream>
 13
 14using namespace System::Drawing;
 15using namespace System::Drawing::Imaging;
 16
 17// enum for q,r,s 
 18// only support in C++/CLI
 19enum class hType {
 20  q,
 21  r,
 22  s
 23}
;
 24
 25// Binarize image
 26void binarize(Bitmap^ , Bitmap^);
 27// Downsampling image by factor
 28void downSampleing(Bitmap^ , Bitmap^intint);
 29// h func for Yokoi
 30hType h(Color, Color, Color, Color);
 31// h func for Yokoi
 32int f(hType, hType, hType, hType);
 33// Process Yokoi connectivity number
 34void yokoi(Bitmap^ , const char*);
 35
 36int main() {
 37  // Read lena.jpg
 38  Bitmap^ oriImg = gcnew Bitmap("lena.jpg");
 39  // Declare binary image for lena.jpg
 40  Bitmap^ binImg = gcnew Bitmap(oriImg->Width, oriImg->Height);
 41  // Binarize lena.jpg
 42  binarize(oriImg, binImg);
 43
 44  // Declare down-sampling image for binarized image
 45  Bitmap^ dsImg = gcnew Bitmap(6464);
 46  // Downsampling image by factor 8*8
 47  downSampleing(binImg, dsImg, 88);
 48  // Process Yokoi connectivity number
 49  yokoi(dsImg, "YokoiMatrix.txt");
 50
 51  return 0;
 52}

 53
 54// Binarize image
 55void binarize(Bitmap^ oriImg, Bitmap^ binImg) {
 56  for (int y = 0; y != oriImg->Height; ++y) {
 57    for (int x = 0; x != oriImg->Width; ++x) {
 58      int gray = (oriImg->GetPixel(x, y).R +
 59                  oriImg->GetPixel(x, y).G +
 60                  oriImg->GetPixel(x, y).B) / 3;
 61
 62      // If intensity >= 128, set the pixel to black.
 63      // If intensity < 128, set the pixel to white.
 64      if (gray >= 128{
 65        binImg->SetPixel(x, y, Color::White);
 66      }

 67      else {
 68        binImg->SetPixel(x, y, Color::Black);
 69      }

 70    }

 71  }

 72}

 73
 74// Downsampling image by factor
 75void downSampleing(Bitmap^ oriImg, Bitmap^ dsImg, int unitX, int unitY) {
 76  for (int x = 0, i = 0; x != oriImg->Width; x += unitX, ++i) {
 77    for (int y = 0, j = 0; y != oriImg->Height; y += unitY, ++j) {
 78      dsImg->SetPixel(i, j,oriImg->GetPixel(x, y));
 79    }

 80  }

 81}

 82
 83// h func for Yokoi
 84// Computer and Robot Vision P.274, Robert M. Haralick
 85hType h(Color b, Color c, Color d, Color e) {
 86  if (b == c && (d != b || e != b)) {
 87    //return q;
 88    return hType::q;
 89  }

 90  else if (b == c && (d == b && e == b)) {
 91    //return r;
 92    return hType::r;
 93  }

 94  else {
 95    //return s;
 96    return hType::s;
 97  }

 98}

 99
100// f func for Yokoi
101// Computer and Robot Vision P.274, Robert M. Haralick
102int f(hType a1, hType a2, hType a3, hType a4) {
103  if ((a1 == a2) && (a2 == a3) && (a3 == a4) && (a4 == hType::r)) {
104    return 5;
105  }

106  else {
107    // Count the number of q
108    int n = 0;
109
110    if (a1 == hType::q) ++n;
111    if (a2 == hType::q) ++n;
112    if (a3 == hType::q) ++n;
113    if (a4 == hType::q) ++n;
114
115    return n;
116  }

117}

118
119// Process Yokoi connectivity number
120void yokoi(Bitmap^ oriImg, const char* fileName) {
121  std::ofstream output(fileName);
122
123  for (int y = 0; y != oriImg->Height; ++y) {
124    for (int x = 0; x != oriImg->Width; ++x) {
125      Color x0 = oriImg->GetPixel(x, y);
126
127      // Only process 1 in binary image (White)
128      if (x0 == Color::FromArgb(255,255,255)) {
129        Color x1 = Color::Black;
130        if (x+1 < oriImg->Width) // Check for the boundary
131          x1 = oriImg->GetPixel(x+1, y);
132        }

133
134        Color x2 = Color::Black;
135        if (y-1 >= 0{
136          x2 = oriImg->GetPixel(x, y-1);
137        }

138
139        Color x3 = Color::Black;
140        if (x-1 >= 0{
141          x3 = oriImg->GetPixel(x-1, y);
142        }

143      
144        Color x4 = Color::Black;
145        if (y+1 < oriImg->Height) {
146          x4 = oriImg->GetPixel(x, y+1);
147        }

148
149        Color x5 = Color::Black;
150        if (x+1 < oriImg->Width && y+1 < oriImg->Height) {
151          x5 = oriImg->GetPixel(x+1, y+1);
152        }

153
154        Color x6 = Color::Black;
155        if (x+1 < oriImg->Width && y-1 >= 0{
156          x6 = oriImg->GetPixel(x+1, y-1);
157        }

158
159        Color x7 = Color::Black;
160        if (x-1 >= 0 && y-1 >= 0{
161          x7 = oriImg->GetPixel(x-1, y-1);
162        }

163
164        Color x8 = Color::Black;
165        if (x-1 >= 0 && y+1 < oriImg->Height) {
166          x8 = oriImg->GetPixel(x-1, y+1);
167        }

168
169        // Computer and Robot Vision P.274, Robert M. Haralick
170        int n = f(h(x0, x1, x6, x2),
171                  h(x0, x2, x7, x3),
172                  h(x0, x3, x8, x4),
173                  h(x0, x4, x5, x1));
174
175        // Write to file
176        output << n << " ";
177
178      }

179      else // 0 in binary image (Black)
180        output << " " << " ";
181      }

182    }

183    // New line
184    output << std::endl;
185  }

186}


原图


Downsampling


Yokoi Connectivity Number

 11 1 1 1 1 1 1 1                 1 2 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 2 2 1           1 1 1 1 1 1 1 1 1 1 1 1     0               0 
 21 5 5 5 5 5 5 1                   1 5 5 5 5 5 5 5 5 5 5 1   1 5 5 1     1 1       1 1 5 5 5 5 5 5 5 5 5 1 1                     
 31 5 5 5 5 5 5 1                 1 3 1 1 2 1 1 5 5 1 1 1 3 2 2 1 1 1   1 2 1         1 5 5 5 5 5 5 5 5 5 5 1             2 1     
 41 5 5 5 5 5 5 1                   2     2   1 1 1 1     2     1 1   0   2 1         1 5 5 5 5 5 5 5 5 5 5 1 1           1       
 51 5 5 5 5 5 5 1                 1 3   1 3 1   1 1       1     1 2 1   2 2           1 5 5 5 5 5 5 5 5 5 5 5 1         0         
 61 5 5 5 5 5 5 1                   1         1     1           2 1     2             1 5 5 5 5 5 5 5 5 5 5 5 1 1                 
 71 5 1 1 1 5 5 1                     1 1   1 3     2 1 1 1 1 1 2     1 3 1           1 5 5 5 5 5 5 5 5 5 5 5 5 1 1               
 81 5 1   1 5 5 1                             1   1 1 5 5 5 5 5 1 1 1                 1 5 5 5 5 5 5 5 5 5 5 5 5 5 1               
 91 1 1   1 5 5 1                                 1 5 5 5 5 5 5 5 5 1 1               1 5 5 5 5 5 5 5 5 5 5 5 5 1 1               
101 1     1 5 5 1                       0       1 2 5 1 1 1 5 5 5 5 5 1 1 1           1 5 5 1 1 1 5 5 5 5 5 5 1 1                 
112 1     1 5 5 1                                 1 1 1   1 5 5 5 5 5 5 5 1 1         1 5 5 1   1 1 5 5 5 5 1 1                   
121       1 5 5 1                                   1 1 1 1 5 5 5 5 5 5 5 5 1 1       1 5 5 1     1 1 5 5 5 1                   1 
13        1 5 5 1                   0           1 1 1 5 5 5 5 5 5 5 5 5 5 5 5 1 1     1 5 5 1       1 5 5 1 1                 1 2 
14        1 5 5 1                             1 1 5 1 1 1 5 5 5 5 5 5 5 5 5 5 5 1 1   1 5 5 1       1 1 1 1                 1 1 1 
15        1 5 5 1                 1         2 2 1 1 1   1 5 5 5 5 5 5 5 5 5 5 5 5 1   1 5 5 1         1 1                 1 1 5 1 
16        1 5 5 1                 2         1   2 1   1 1 5 5 5 5 5 5 5 5 5 5 5 5 1 2 2 1 5 1     1 1 1 1 1             1 1 5 5 1 
17        1 5 5 1                 2           1 2   1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1   1 5 1   1 1 5 5 5 1             1 5 5 5 1 
18        1 5 5 1                 2               1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1 1 1 5 1 1 1 5 5 5 1 1           1 1 5 5 5 1 
19        1 5 5 1               1 2       1 1   1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1             1 5 5 5 5 1 
20        1 5 5 1               1 1           1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1           1 1 5 5 5 5 1 
21        1 5 5 1               1 1 1       2 2 1 5 1 1 1 5 1 1 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1 1 1         1 1 5 5 5 5 5 1 
22        1 5 5 1               1 5 1 1     1   1 2 1   2 1 1     2 1 1 1 1 1 5 5 5 5 5 5 5 5 5 1 1 1             1 5 5 5 5 5 5 1 
23        1 5 5 1               1 5 5 1       0   1   2 2     1 2 2         1 5 5 5 5 5 5 5 1 1 1     0         1 1 5 5 5 5 5 5 1 
24        1 5 5 1               1 1 5 1     0   0   2 2   0     1 1       1 1 5 5 5 5 5 1 1 2       0           1 5 5 5 5 5 5 5 1 
25        1 5 5 1                 1 5 1   0       1 2                   1 1 5 5 5 5 1 1 1   2 1 2 1             1 5 5 5 5 5 5 5 1 
26        1 5 5 1                 1 2 2 1   0   0                       1 5 5 5 5 5 1       1 1 1             1 2 5 5 5 5 5 5 5 1 
27        1 5 5 1                   2         0                       1 1 5 5 5 5 5 1 1     2 1                 1 5 5 5 5 5 5 5 1 
28        1 5 5 1                   2       0             0         1 1 5 5 5 5 5 5 5 1     1               1 1 1 5 5 5 5 5 5 5 1 
29        1 5 5 1             0     2                             1 1 5 5 5 5 5 5 5 5 1                     1 5 5 5 5 5 5 5 5 5 1 
30        1 5 5 1                   1     0                     1 1 5 5 5 5 5 5 5 5 5 1 1                 1 1 5 5 5 5 5 5 5 5 5 1 
31        1 5 5 1                                             1 1 5 5 1 1 1 1 5 5 5 5 1 1     1           1 1 5 5 5 5 5 5 5 5 5 1 
32        1 5 5 1               1 1   0                   1 1 1 1 1 1 1     1 5 5 5 1 1       2             1 5 5 5 5 5 5 5 5 5 1 
33        1 5 5 1             1 1 2 1                     1 5 1 1           1 5 1 1 1         2         1 1 1 5 5 5 5 5 5 5 5 5 1 
34        1 5 5 1             2 1 1             0       1 1 1 1       1     1 5 1     1       2         1 5 5 5 5 5 5 5 5 5 5 5 1 
35        1 5 5 1             1                         1 1 1   1   1 2 1 1 1 5 1     1       2         1 5 5 5 5 5 5 5 5 5 5 5 1 
36        1 5 5 1         1                 0           2 1   1 2 1 1 5 5 5 5 5 1 1 1         2         1 5 5 5 5 5 5 5 5 5 5 5 1 
37        1 5 5 1         2 1                       1 3 2     1 5 5 5 5 5 5 5 5 5 5 1 1       2       1 1 5 5 5 5 5 5 5 5 5 5 5 1 
38        1 5 5 1                                     1     1 1 5 5 5 5 5 1 1 1 5 5 5 1       1       1 5 5 5 5 5 5 5 5 5 5 5 5 1 
39        1 5 5 1       1                           1       1 1 5 5 5 5 1 1   1 1 5 5 1         1     1 5 5 5 5 5 5 5 5 5 5 5 5 1 
40        1 5 5 1       1                           1         1 5 5 5 5 1       1 5 1 1         2   1 1 5 5 5 5 5 5 5 5 5 5 5 5 1 
41        1 5 5 1             0   0             1 1           1 5 5 5 5 1 1     1 5 1           2   1 5 5 5 5 5 5 5 5 5 5 5 5 5 1 
42        1 5 5 1                   1                         1 5 5 5 5 5 1 1   1 5 1           2   1 5 5 5 5 5 5 5 5 5 5 5 5 5 1 
43        1 5 5 1                   1       1   0           1 2 5 5 5 5 5 5 1 1 2 1 1           2   1 5 5 5 5 5 5 5 5 5 5 5 5 5 1 
44        1 5 5 1                         2 2                 1 1 5 1 1 1 1 1 5 1               2 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 1 
45        1 5 5 1     0                 1 2                     1 1 1       1 5 2 1             1 5 5 5 5 5 5 5 1 1 1 5 5 5 5 5 1 
46        1 5 5 1               0   0       0                     1 1 1   1 1 5 1               1 5 5 5 5 5 5 5 1   1 5 5 5 5 5 1 
47        1 5 5 1                         0                       1 1 1 1 1 5 1 1               1 5 5 5 5 5 5 5 1   1 5 5 5 5 5 1 
48        1 5 5 1                 0                                 1 1 5 5 5 1                 1 5 5 5 5 5 5 5 1   1 5 5 5 5 1 1 
49        1 5 5 1                     1 1                             1 5 5 5 1                 2 1 1 1 1 1 1 1 1   1 5 5 5 1 1   
50        1 1 5 2 1                 0     1                     0   1 1 5 5 5 1 1 1             2           1 1   1 1 5 5 1 1     
510         1 5 1   0                     2                         1 5 5 5 5 5 5 1 1 1         2 1 1 1           1 5 5 1 1       
52  1       1 5 1 1                     2 3 1                       1 5 5 5 5 5 5 5 5 1 1       1 5 5 1 1 1       1 5 1 1         
53  2 2     1 5 1 1                     1                           1 5 5 5 5 5 5 5 5 5 1 1     1 5 5 5 5 1 1   1 1 5 1           
54    2     1 5 1                         0   1                 1 1 1 5 5 5 5 5 5 5 5 5 5 1     1 5 5 5 1 1 2   1 5 1 1           
55    2     1 5 2 1         0         0     1 3                 1 5 5 5 5 5 5 5 5 5 5 5 5 1 1   1 5 5 5 1   2 2 1 5 1             
56    2     1 5 1                       1 1   1           0     1 5 5 5 5 5 5 5 5 5 5 5 5 5 1   1 5 5 5 2 1   1 5 1 1             
57    2     1 5 1 1                                             1 5 5 5 5 5 5 5 5 5 5 5 5 5 1   1 1 1 5 1     1 5 1               
58    2 1   1 5 1 1                           0                 1 5 5 5 5 5 5 5 5 5 5 5 5 5 1 1     1 1 2 2 1 1 5 1               
59    1 1   1 5 1                     0                       1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1           1 1 5 1 1               
60    1 1   1 5 1                                           1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1             1 5 1                 
61    1 1   1 5 1                         0                 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1             2 1 1                 
62    1 1   1 5 1                                             1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1 1           1                     
63    1 1   1 5 1                                             1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1                                 
64    1 1   1 1 1                 0                     0   1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1                                 


Reference

Computer and Robot Vision P.274, Robert M. Haralick

posted on 2006-12-06 00:20  真 OO无双  阅读(2424)  评论(0编辑  收藏  举报

导航