الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Matlab to java translation

مغلق
بدأه نحلة نشيطة في 2 ديسمبر 2005 · 11 رد · 1,245 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

Assalam Alikoum all,

I have this code in matlab - which I don't know anything about it - and need to transfer it to java, so could anybody please help on that ?

Many thanks in advance

 function s = Skeleton(a, varargin)

% function s = Skeleton(a);
% function s = Skeleton(a, iterations);
%
% Skeletonizing a binary image using Zhang and Suen's method from
%
%    "A fast parallel algorithm for thinning digital patterns"
%    Comm ACM, Vol. 7, No. 23, pp. 326 -- 329, 1985.
%
% as described in Gonzales & Wintz.
%
% 'a' should contain binary data with 0 considered to be the
% background and 1 the foreground (or object to be thinned).
%
% 'iterations' is the number of iterations of the algorithm to
% perform.  Each iteration can only strip away a one pixel wide
% boundary. If no 'iterations' argument is supplied, the function
% iterates until the result converges.
%
% This is a straightforward implementation and as such is very slow.
% There is much scope for optimisation.
%
% Gary Dickson <moc.liamtoh@noskcidyrag>
%

% Check arguments
%
if nargin == 1
   iterations = -1; % Flag to iterate until convergence
elseif nargin == 2
   iterations = varargin(1); % Specified number of iterations
   iterations = iterations{1}; % Want plain old number, not a cell
else
   disp('Too many arguments supplied.');
   s = a;
   return;
end

% Check input data
%
if (min(min(a)) < 0) | (max(max(a)) > 1)
   disp('input not binary')
   s = a;
   return;
end


[h w] = size(a);
s = a;
it = 1;
prevsum = 0;

while 1

   % Step 1 (thinning top and left sides)
   %
   m = ones([h w]);
   for j = 2 : h-1
      for i = 2 : w-1

         if (s(j, i) == 1)
            condA = sum(sum(s(j-1 : j+1, i-1 : i+1))) - s(j, i);

            condB = Num01Transitions(s, j, i);

            condC = s(j-1, i) * s(j, i+1) * s(j+1, i); % p2 * p4 * p6

            condD = s(j, i+1) * s(j+1, i) * s(j, i-1); % p4 * p6 * p8

            if (condA >= 2) & (condA <= 6) & (condB == 1) & (condC == 0) & (condD == 0)
               m(j, i) = 0;
            end
         end

      end % i
   end % j

   s = s .* m;


   % Step 2 (thinning bottom and right sides)
   %
   m = ones([h w]);
   for j = 2 : h-1
      for i = 2 : w-1

         if (s(j, i) == 1)
            condA = sum(sum(s(j-1 : j+1, i-1 : i+1))) - s(j, i);

            condB = Num01Transitions(s, j, i);

            condC = s(j-1, i) * s(j, i+1) * s(j, i-1); % p2 * p4 * p8

            condD = s(j-1, i) * s(j+1, i) * s(j, i-1); % p2 * p6 * p8

            if (condA >= 2) & (condA <= 6) & (condB == 1) & (condC == 0) & (condD == 0)
               m(j, i) = 0;
            end
         end

      end % i
   end % j

   s = s .* m;


   % Time to stop? As points are always being removed and never added, we can
   % check if the method has converged by checking if the sum of the image
   % values is the same for two successive iterations.
   %
   newsum = sum(sum(s));

   if (newsum == prevsum) & (iterations == -1)
      break;
   end;

   if (it >= iterations) & (iterations ~= -1)
      break;
   end

   it = it + 1;
   prevsum = newsum;

end % it

%------------------------------------------------------------------------------%
%                                                                              %
%------------------------------------------------------------------------------%
% Count the number of 0 -> 1 transitions that occur while traversing the eight
% neighbours of the point of interest.
%
%       p9 p2 p3
%       p8 p1 p4
%       p7 p6 p3
%
function Nt = Num01Transitions(c, j, i)


p = [c(j-1, i) c(j-1, i+1) c(j, i+1) c(j+1, i+1)];
p = [p c(j+1, i) c(j+1, i-1) c(j, i-1) c(j-1, i-1) c(j-1, i)];

pp = zeros(1, 8);
for k = 1 : 8
   pp(k) = p(k+1) - p(k);
end

Nt = sum(pp == 1);

كما أريد أن أكون .. بأمر الله أكون

#2

well, it seems nobody will help in such translation, but I badly need the thinning algorithm implementation in java. I found one and I tried it but no result since my problem was in applying mask to image. I know how to read an image and save it in array, say, image[][], but I dont know how to applay a 3x3 mask to the image. it seems that I will use two for loops to pass the mask over the image but I really failed in get any result.

Here is the algorithm:

Image Thinning (skeletonization) 
We used a simple algorithm for thinning our images.  
The algorithm makes two passes: one pass on the actual image 
and another pass over an intermediate image (constructed from the original image during the first pass).  
The result after the two passes decide if a pixel is to be removed to get the skeleton of the object of interest. 

The thinning algorithm works as follows: 
Figure 3(a) shows an input image on which the thinning algorithm is to be applied.  
Each box represents a pixel.  Figure 3(b) shows a 3x3 mask that is used to move 
over the input image during the first pass and then on the intermediate image during 
the second pass.  
The center of the mask is positioned on the pixel of interest during the scanning process 
and the numbered boxes represent the corresponding neighbor pixels.  
The pixel of interest, shown as “o”, is marked for deletion when all the following conditions are satisfied.  These conditions are checked only for a “dark pixel”. 

•	If the number of neighboring 'dark pixels' of  “o” is between 2 and 6. 
•	If the number of transitions from 'bright pixel' to 'dark pixel' in the order shown is one. 
•	If the logical function P1.P3.P5 = 0 and also P3.P5.P7 = 0, as shown by the darkened portions of the masks in the fig. 4(a), (b). 

These conditions are repeated for each pixel in the image as it is scanned.  
The intermediate result is an image, which consists of pixels that are marked 
for deletion and other pixels that are left as in the original input image.  ures
The second pass is performed on this intermediate image and now the 
conditions of deletion on the pixel are as follows: 
•	If the number of neighboring 'dark pixels' of “o” is between 2 and 6. 
•	If the number of transitions from 'bright pixel' to “dark pixel” in the order shown is one. 
•	If the logical function P1.P3.P7 = 0 and also P1.P5.P7 = 0, as shown by the darkened portions of the masks in fig. 4(c),(d). 
As earlier, the pixel is deleted only when all the conditions are satisfied.

here the algorithm site clarifing it with figures.

http://www.ee.vt.edu/~anbumani/tamilocr/

Sorry to write in English since no Arabic support in this pc.

and agian, thanks in advance for any help.

كما أريد أن أكون .. بأمر الله أكون

#3

سوف أحاول مساعدتك في هذا، ولكن أرجو أن تكون صبورة بعض الشيء، فأنا لست متفرغ تماماً.

#4

Ya Rab Ybarek Feek brother. but plz don not be late since me neither hava not that much time.

Thanks again

كما أريد أن أكون .. بأمر الله أكون

#5

آسف على التأخر، اليوم فقط حصلت على بعض الوقت من أجل دراسة الخوارزمية.

أرى أنك تطلبين فقط طريقة إنشاء الـ 3x3 Mask، وهي بسيطة، انظري كيف:

لنفترض أن النقطة o هي النقطة الجاري دراستها من أجل الحذف، وليكن موقع النقطة (أي إحداثياتها) هي x على محور العرض وy على محور الارتفاع، أي كما في الرياضيات إحداثياتها هي (x,y). ولكن عليك ألتذكر أن اتجاه محور الارتفاع في نوافذ الكمبيوتر يكون من الأعلى إلى الأسفل وليس كما في الرياضيات.

05_12_05_10_03_35_1133849015math.GIF

05_12_05_10_04_04_1133849044comp.GIF

ومن هذا المنطلق، نبدأ بحساب النقاط المجاورة للنقطة المدروسة حتى نحصل على إحداثيات الـ 3x3 Mask:

الأمر الرئيسي الذي يجب أن تضعيه في تفكيرك هنا، أن النقطة التي تقع فوق النقطة o تنقط y فيها بمقدار 1 والتي تحت تزيد بمقدار 1 والتي في نفس المستوى لا تزيد ولا تنقص 0 فـ y متساوية لـ o و p، وبالنسبة لـ x النقطة التي تقع إلى اليمين تزيد بمقدار 1 والتي تقع إلى اليسار تنقص بمقدار 1 والتي على نفس المستوى لا تزيد ولا تنقص 0 فـ x لـ o نفسها لـ p.

وعلى هذا الأساس انظري في الصورة وتابعي الحسابات:

05_12_05_10_05_52_1133849152figure3.GIF

p1: تقع في فوق النقطة o تماماً:

p1: p1x = ox, p1y = oy - 1

p2: تقع فوق وإلى اليمين للنقطة o:

p2: p2x = ox + 1, p2y = oy - 1

وهكذا ...

p3: p3x = ox + 1, p3y = oy

p4: p4x = ox + 1, p4y = oy + 1

p5: p5x = ox, p5y = oy + 1

p6: p6x = ox + 1, p6y = oy + 1

p7: p7x = ox - 1, p7y = oy

p8: p8x = ox - 1, p8y = oy - 1

وحتى أتأكد من أنك فهمت طريقة الحساب أخطأت متعمداً في كتابة أحدهم، إذكريه لي واذكري لي الحساب الأصح له.

أتمنى أن أكون قد أوضحت لك المقصود.

#6

Ok. Suppose we have Binary Image stored in int [] [] image , then

P1 = image [x][y-1]  
P2 = image[x+1][y-1]  
P3 = image[x+1][y]  
P4 =  image[x+1][y+1]  
P5 = image[x][y+1] 
P6 = image[x-1][y+1]
P7 = image[x-1][y] 
P8 = image[x-1][y-1]

The error was in number p6 which must be ox -1 not ox+ 1;

In the Algorithm we have three conditions to apply : pixel to be delete

1/If the number of neighboring 'dark pixels' of “o” is between 2 and 6.

2/If the number of transitions from 'bright pixel' to 'dark pixel' in the order shown is one.

3/ If the logical function P1.P3.P5 = 0 and also P3.P5.P7 = 0.

Condition (1):

For ( j=0,j< image Height; j++)
{ 
   Counter Of Ones = 0;
  
   For (i=0, i< image Width; i++)
   {
    if ( image[j] == 1) 
    {
       for (x= i-1, x<i+1; x++) {
       for (y=j-1,y<j+1; y++) {
            if ( image  [x][y] == 1) {
                Counter Of Ones = Counter Of Ones + 1;} } }

    }
    }
}

if (Counter Of Ones >=2) && (Counter Of Ones <= 6)  image [j] = 0;

Condition (3):

For ( j=0,j< image Height; j++)
{ 
   
      Cond3 = False;
  
   For (i=0, i< image Width; i++)
   {

P1 = image [j-1];  P2 = image[i+1][j-1];
P3 = image[i+1][j];  P4 =  image[i+1][j+1];
P5 = image[j+1];  P6 = image[i-1][j+1];
P7 = image[i-1][j];   P8 = image[i-1][j-1];

If (p1*p3*p5 == 0) && (p3*p5*p7 == 0) { Cond3 = true;}

I don’t know how to apply condition 2 ?

Is what I did is right ?

Thank you again for your time brother.

تم تعديل هذه المشاركة بواسطة نحلة نشيطة في 7 ديسمبر 2005 في 16:03

كما أريد أن أكون .. بأمر الله أكون

#7

إجابتك صحيحة وبارك الله فيك، وبالتوفيق ...

أما بالنسبة للخوارزميات التي اقترحتها، فهناك عدد من الملاحظات أتمنى أن تنتبهي لها جيداً:

عندما تسخدمي حلقتين متداخلتين من الشكل:

for (j = 0; j < image.height; i++) {
    for (i = 0; i < image.width; i++) {
        
    }
}

فعليك أن تعلمي أن التعقيد الزمني لهذه الخوارزمية هو من الربتة n^2 أي أن هذه الخورازمية سوف تتكرر height * width فإذا كان ارتفاع الصورة 200px وعرضها 400px فإن حلقة for سوف تتكرر 200*400=80000 مرة، وهذا أمر مهم جداً للمحافظة على أداء جيد للبرنامج. لذلك يجب عليك أن تقللي عدد مرات استدعاء حلقات for قدر الإمكان وطالما ذلك ممكن.

واضح أنك الآن في صدد تصميم برنامج للتعرف على المحارف، وأنت الآن لازلت في مرحلة الـ Image Processing، ولايزال أمامك مرحلة الشبكات الاصطناعي أو مرحلة الذكاء الاصطناعي بحسب ما تنوين العمل، ومن المفروض أن تكون مرحلة Image Processing تستهلك أقل ما يمكن من موارد الحاسب لأن القادم أعظم.

المهم، الخطوة الأولى التي يجب أن تفعليه من أجل ذلك، هي أنك سوف تمسحين المصفوفة التي تحوي الصورة مرة واحدة فقط من أجل فحص الشروط الثالثة التي تثبت إمكانية حذف العنصر الجاري فحصه أو لا. ومن أجل ذلك، تخيلي أن كل شرط من الشروط الثالثة هو عبارة عن method تأخذ وسيطين هما x وy يحددان إحداثيات النقطة الجاري دراستها، وتعيد قيمة من النوع boolean تفيد تحقق الشرط أو عدمه.

وفي هذه الحالة سوف نضيف توابع استدعاء لهذه الطرق بعد التحقق من أن العنصر الجاري فحصه هو عنصر عتم إلى حلقتي for المتداخلتين السابقتين:

if ( darkpixel ) {
    if ( !condition1(i, j) ) { continue; }
    if ( !condition2(i, j) ) { continue; }
    if ( !condition3(i, j) ) { continue; }

}

طبعاً تستطيعين أيضاً استخدام if واحدة بدلاً من ثلاثة و && بين الشروط إن أحببت. المهم عندما تكون النقطة الجاري دراستها نقطة سوداء يتم المتابعة للتحقق من الشروط، وفي حال عدم تحقق أحد الشروط التالية سوف تنفذ continue للخروج من الدورة الحالية للحلقة لمتابعة دورة جديدة. أما في حال تحققت الشروط الثلاثة فيتم إضافة علامة لهذه النقطة بأنها نقطة قابلة للحذف.

ومن أجل إضافة علامة على أن هذه النقطة قابلة للحذف، يمكن أن نفترض بأن النقط البيضاء مثلت في المصفوفة بأصفار، والنقط السوداء مثلت بواحدات، فتكون العلامة مثلاً هي إضافة واحد ، فتصبح النقط القابلة للحذف ممثلة بـ 2. وطبعاً يضاف عندها أمر تعديل قيمة هذا البيكسل إلى المصفوة بعد الشروط الثلاث السابقة.

image_array [ i ] [ j ] ++

وسوف أتابع في المرة القادمة إن شاء الله مع بقية الملاحظات.

#8

Barak Allah Feek Wa Jazak Kol Alkhair Brother, and thanks million for your great help, I'm following you, and try to code the algorithm, here what I did so far:

	private void processImage() {
  biInputImage = readImage(fileImage);
  arInputTemplate = copyBufferedImageToArray(biInputImage);
                 
                 int nImageWidth;                //4. Width of the input image
          int nImageHeight;               //5. Height of the input image
                 
                //4. Read Width and Height of the buffered image.
  nImageWidth = biInputImage.getWidth();
  nImageHeight = biInputImage.getHeight();
                int[][] arOutputImage = new int[nImageWidth][nImageHeight];
                biOutputImage =	new BufferedImage(nImageWidth,
        nImageHeight,
        BufferedImage.TYPE_BYTE_BINARY);

               
               

// First Pass
for (int i=0; i< nImageWidth; i++)
              
{ 
   int CounterOfOnes = 0; boolean Cond3 = false;
     for ( int j=0; j< nImageHeight; j++)
  
   {
    
    if ( arInputTemplate[j] == 1) 
    {  
        // Cndition (3)
    
    int P1 = arInputTemplate[j-1]; int P2 = arInputTemplate[i+1][j-1];
    int P3 = arInputTemplate[i+1][j];  int P4 =  arInputTemplate[i+1][j+1];
    int P5 = arInputTemplate[j+1];  int P6 = arInputTemplate[i-1][j+1];
    int P7 = arInputTemplate[i-1][j];  int P8 = arInputTemplate[i-1][j-1];
      if ((P1*P3*P5 == 0) & (P3*P5*P7 == 0)) { Cond3 = true;}   
   
      // Condition (1)
       for (int x= i-1; x<i+1; x++)   
       {
         for (int y=j-1;y<j+1; y++) 
          {
              if ( arInputTemplate[x][y] == 1) 
               {
                   CounterOfOnes = CounterOfOnes + 1;
               } 
          } 
        }  
     }
    
     if  ((CounterOfOnes >=2) & (CounterOfOnes <= 6)  & (Cond3 == true))
         {arInputTemplate[j] = 0;}  
  
     }
 }

 // Second Pass
for (int i=0; i< nImageWidth; i++)
              
{ 
   int CounterOfOnes = 0; boolean Cond3 = false;
     for ( int j=0; j< nImageHeight; j++)
  
   {
    
    if ( arInputTemplate[j] == 1) 
    {  
        // Cndition (3)
    
    int P1 = arInputTemplate[j-1]; int P2 = arInputTemplate[i+1][j-1];
    int P3 = arInputTemplate[i+1][j];  int P4 =  arInputTemplate[i+1][j+1];
    int P5 = arInputTemplate[j+1];  int P6 = arInputTemplate[i-1][j+1];
    int P7 = arInputTemplate[i-1][j];  int P8 = arInputTemplate[i-1][j-1];
      if ((P1*P3*P7 == 0) & (P1*P5*P7 == 0)) { Cond3 = true;}   
   
      // Condition (1)
       for (int x= i-1; x<i+1; x++)   
       {
         for (int y=j-1;y<j+1; y++) 
          {
              if ( arInputTemplate[x][y] == 1) 
               {
                   CounterOfOnes = CounterOfOnes + 1;
               } 
          } 
        }  
     }
    
     if  ((CounterOfOnes >=2) & (CounterOfOnes <= 6)  & (Cond3 == true))
         {arInputTemplate[j] = 0;}  
  
     }
 }

   	 
             //Display outputs
  this.writeOutputArrayToOutputBuffer(arInputTemplate, biOutputImage.getRaster());
  this.showImage(biOutputImage);
  this.saveOutput("thinning.png", biOutputImage);

  
	}




}

And the result is in the attached image. I didnt code condition two because I don't know how?

I read your last reply just now. I want to read it carfully to understand it.

Thaks again brother

post-17900-1133990437_thumb.jpg

كما أريد أن أكون .. بأمر الله أكون

#9

Assalam Alikoum ,

I discovered that this algorithm titled "hilditch algorithm" and i found an implementation for it in the internet that give a good result, but it was written in C language, I tried to transfer it to Java but I failed, any one could do that please ?

Here is the code in C:

http://cis.k.hosei.ac.jp/~wakahara/Hilditch.c

Thanks in advance for any help?

كما أريد أن أكون .. بأمر الله أكون

#10

السلام عليكم،

آسف مرة أخرى أختي بسبب تأخري في الرد وذلك لقلة الوقت.

أختي الكريمة، إليك بعض النصائح قبل أن أكمل مناقشة الموضوع: أولاً وبما أنك لا تستطيعين الكتابة إلا بالإنكليزية فاستخدمي الوسم left والذي إغلاقه بنفس طريقة إغلاق أي وسم تتعاملي معه مثل code لجعل محاذاة كتابتك إلى اليسار وتظهر للقارئ بشكل نظامي وأسهل في القراءة، والثانية هي أن الأفضل لك إن كنت تدرسين أن تكتبي الخوارزمية والشيفرة بنفسك ولا تستعيني بما كتبه غيرك وما تجديه على الإنترنت، فأنا أحاول مساعدتك في الوصول إلى الخوارزمية وليس كتابتها لك.

أما الآن فدعينا نتابع في الخوارزمية التي كتبت.

في مشاركتي السابقة أشرت لك بأننا استدعينا الشروط الثالثة من داخل حلقتي for المتداخلتين، وأأكد لك أمر ((استدعينا))، فحتى تكون الخوارزمية التي تكتبيها مطابقة لقواعد غرضية التوجه يجب عليك أن تكتبي Method خاصة بكل شرط واستدعاءه عند الحاجة. وذلك يسهل عليك المراجعة وتصحيح الآخطاء، ومن ثم يوفر عليك طول الشيفرة. فإذا راجعت الشيفرة التي كتبت تلاحظين أنك أعدت كتابة خوارزمية اختبار الشرط الأول والثالث مرتين من أجل كل مرحلة، وهذا مرفوض تماماً.

بالنسبة لخوارزمية الشرط الأول فعندك فهناك خطأين، الاول في السطرين الأول والثاني ضمن شرط تكرار حلقتي for. فقد استخدمت عنصر المقارنة أصغر تماماً في حين وجب استخدام أصغر أو يساوي حتى تصل x إلى القيمة i+1 وy إلى القيمة j+1 . الخطأ الثاني هو أن حلقتي for هاتين سوف تمران بالنقطة x=i وy=j وهي نفس النقطة الجاري دراستها وبالتالي سوف يضاف 1 إلى CounterOfOnes زيادة، ولحل هذه المشكلة يجب طرح 1 منه بعد الخروج من الحلقتين أو وضع شرط إضافي دالة if يشترط أن لا تكون قيمة x==i و y==j .

أما خوارزمية الشرط الثالث فلا أجدها إلا صحيحة إن شاء الله. وأما الشرط الثاني فلم أفهمه، ساعديني على فهمه حتى أساعدك في تطبيق خوارزميته.

وبالتوفيق إن شاء الله...

#11

Assalam Alikoum,

First of all, I appreciate your advice, and the time that you used to support me here, Barak Allhah Feek.

Well, I suppose you want me to put each condition in a method, isn’t it?. I don’t know how in the current time, I know it is not acceptable but I’m trying first to let the algorithm works correctly, then I will try to shortcut it.

About the second condition I think it is about the number of transition from 0 to 1which is clarified in the algorithm by figure I try to use this – as my understanding of the condition –

 // Condition (2)
    if ((P1*P3*P5 == 1) || (P3*P5*P7 == 1) || (P1*P3*P7 == 1) || (P1*P5*P7 == 0)) { Cond2 = true;}

And also I took your advice for "<=" and for not include x & y. So the code now as follows:

	private void processImage() {
  biInputImage = readImage(fileImage);
  arInputTemplate = copyBufferedImageToArray(biInputImage);
                 
                 int nImageWidth;                //4. Width of the input image
          int nImageHeight;               //5. Height of the input image
                 
                //4. Read Width and Height of the buffered image.
  nImageWidth = biInputImage.getWidth();
  nImageHeight = biInputImage.getHeight();
                int[][] arOutputImage = new int[nImageWidth][nImageHeight];
                biOutputImage =	new BufferedImage(nImageWidth,
        nImageHeight,
        BufferedImage.TYPE_BYTE_BINARY);

               
               

// First Pass
for (int i=0; i< nImageWidth; i++)
{ 
     for ( int j=0; j< nImageHeight; j++)
     {
            int CounterOfOnes = 0; boolean Cond3 = false; boolean Cond2 = false;
            if ( arInputTemplate[j] == 1) 
    {  
        // Cndition (3)
     int P1 = arInputTemplate[j-1]*0; int P2 = arInputTemplate[i+1][j-1]*0;
    int P3 = arInputTemplate[i+1][j]*0;  int P4 =  arInputTemplate[i+1][j+1]*0;
    int P5 = arInputTemplate[j+1]*0;  int P6 = arInputTemplate[i-1][j+1]*0;
    int P7 = arInputTemplate[i-1][j]*0;  int P8 = arInputTemplate[i-1][j-1]*0;
      if ((P1*P3*P5 == 0) & (P3*P5*P7 == 0)) { Cond3 = true;}   
   
    // Condition (2)
    if ((P1*P3*P5 == 1) || (P3*P5*P7 == 1) || (P1*P3*P7 == 1) || (P1*P5*P7 == 0)) { Cond2 = true;}
    
       // Condition (1)
       for (int x= i-1; x<=i+1; x++)   
       {
         for (int y=j-1;y<=j+1; y++) 
          {
              if(x!=i && y!=j ) 
               {
                 if ( arInputTemplate[x][y] == 1) 
                 {
                   CounterOfOnes = CounterOfOnes + 1;
                 } 
               }
          } 
        }
         if  ((CounterOfOnes >=2) & (CounterOfOnes <= 6)  & (Cond3 == true) & (Cond2 == true))
         {arInputTemplate[j] = 0;}  

     }
    

     }
 }
  
   

 // Second Pass
for (int i=0; i< nImageWidth; i++)
{ 
     for ( int j=0; j< nImageHeight; j++)
     {
    
         int CounterOfOnes = 0; boolean Cond3 = false; boolean Cond2 = false;
         if ( arInputTemplate[j] == 1) 
            {  
                // Cndition (3)
                int P1 = arInputTemplate[j-1]*1;  int P2 = arInputTemplate[i+1][j-1]*0;
                int P3 = arInputTemplate[i+1][j]*1;  int P4 =  arInputTemplate[i+1][j+1]*0;
                int P5 = arInputTemplate[j+1]*1;  int P6 = arInputTemplate[i-1][j+1]*0;
                int P7 = arInputTemplate[i-1][j]*1;  int P8 = arInputTemplate[i-1][j-1]*0;
                 if ((P1*P3*P7 == 0) & (P1*P5*P7 == 0)) { Cond3 = true;}   

                // Condition (2)
                 if ((P1*P3*P5 == 1) || (P3*P5*P7 == 1) || (P1*P3*P7 == 1) || (P1*P5*P7 == 0)) { Cond2 = true;}

                // Condition (1)
                for (int x= i-1; x<=i+1; x++)   
                {
                    for (int y=j-1;y<=j+1; y++) 
                    {
                         if(x!=i && y!=j ) 
                         {
                            if ( arInputTemplate[x][y] == 1) 
                            {
                                CounterOfOnes = CounterOfOnes + 1;
                            } 
                         }
                    } 
                }
                
            if  ((CounterOfOnes >=2) & (CounterOfOnes <= 6)  & (Cond3 == true) & (Cond2 == true))
                {arInputTemplate[j] = 0;}  

            }  
     }
 }

With somehow reasonable output:

post-17900-1134139358_thumb.png

تم تعديل هذه المشاركة بواسطة نحلة نشيطة في 9 ديسمبر 2005 في 17:43

كما أريد أن أكون .. بأمر الله أكون

#12

أختي نحلة نشيطة، مع الأسف إلى الآن لم أفهم شيء من الشرط الثاني.

بصراحة، شروط هذه الخوارزمية غريبة بعض الشيء، فلماذا يجب تنفيذ الشروط كلها أول مرة ليقوم بوضع علامة فقط على البيكسل الذي يمكن مسحه، ثم يعود ويختبر الشروط الثلاثة مع تعديل بسيط في الشرط الثالث حتى يمسح هذا البيكسل. هذا الأمر أجده غريباً بعض الشيء. كما أن شرح الخوارزمية ضعيف جداً وغير واضح.

أتمنى أن تجدي خوارزمية مشروحة بشكل أفضل! أو أن تكوني استطعت تفسير هذه الخوارزمية وفهمها على الأقل حتى نساعدك في تطبيقها.

هذا الموضوع مغلق.

مواضيع مشابهة