Hi,

I am converting some C++ code to C# now and need some help on reading it!

The C++ code is:

uchar p2 = im.at<uchar>(i-1, j);
uchar p3 = im.at<uchar>(i-1, j+1);
uchar p4 = im.at<uchar>(i, j+1);
uchar p5 = im.at<uchar>(i+1, j+1);
uchar p6 = im.at<uchar>(i+1, j);
uchar p7 = im.at<uchar>(i+1, j-1);
uchar p8 = im.at<uchar>(i, j-1); 
uchar p9 = im.at<uchar>(i-1, j-1);

int C  = (!p2 & (p3 | p4)) + (!p4 & (p5 | p6)) +
         (!p6 & (p7 | p8)) + (!p8 & (p9 | p2));
int N1 = (p9 | p2) + (p3 | p4) + (p5 | p6) + (p7 | p8);
int N2 = (p2 | p3) + (p4 | p5) + (p6 | p7) + (p8 | p9);
int N  = N1 < N2 ? N1 : N2;
int m  = iter == 0 ? ((p6 | p7 | !p9) & p8) : ((p2 | p3 | !p5) & p4);

And the C# code I have written so far is:

int i2 = ( i - 1 ) * widthStep + ( j );
int i3 = ( i - 1 ) * widthStep + ( j + 1 );
int i4 = ( i ) * widthStep + ( j + 1);
int i5 = ( i + 1 ) * widthStep + ( j + 1 );
int i6 = ( i + 1) * widthStep + ( j );
int i7 = ( i + 1) * widthStep + ( j - 1);
int i8 = ( i ) * widthStep + ( j - 1 );
int i9 = ( i - 1) * widthStep + ( j - 1 );

byte p2 = imagePtr[ i2 ];
byte p3 = imagePtr[ i3 ];
byte p4 = imagePtr[ i4 ];
byte p5 = imagePtr[ i5 ];
byte p6 = imagePtr[ i6 ];
byte p7 = imagePtr[ i7 ];
byte p8 = imagePtr[ i8 ];
byte p9 = imagePtr[ i9 ];

int C = ??
int N1 = ??
int N2 = ??
int N = //I can do this
int m = ??

Can someone please tell me how I build C, N1, N2 and m?

Thanks. I have converted the code :)
I tried both of Zhang-Suen algorithm and Guo-Hall algorithm but I keep having discontinued lines.. :S

private IplImage Thinning( IplImage image )
{
    IplImage output = image.Clone();
    IplImage prev = new IplImage( image.Size, image.Depth, 1 );
    IplImage diff = new IplImage( image.Size, image.Depth, 1 );
    do
    {
        ThinningZhangSuenIteration( ref output, 0 );
        ThinningZhangSuenIteration( ref output, 1 );
        Cv.AbsDiff( output, prev, diff );
        output.Copy( prev );

    } while( diff.CountNonZero() > 0 );

    return output;
}

private void ThinningZhangSuenIteration( ref IplImage image, int iter )
{

    IplImage marker = new IplImage( image.Size, image.Depth, 1 );
    marker.Set( CvColor.Black );

    try
    {

        unsafe
        {
            byte* imagePtr = ( byte* )image.ImageData.ToPointer();
            byte* markerPtr = ( byte* )marker.ImageData.ToPointer();

            int widthStep = image.WidthStep;
            // Loop through to make 16 bit image to 8 bits
            int i, j;
            int i1, i2, i3, i4, i5, i6, i7, i8, i9;
            int p2, p3, p4, p5, p6, p7, p8, p9;
            for( i = 1; i < image.Height - 1; i++ )
            {
                for( j = 1; j < image.Width - 1; j++ )
                {
                    i1 = ( i ) * widthStep + ( j );
                    i2 = ( i - 1 ) * widthStep + ( j );
                    i3 = ( i - 1 ) * widthStep + ( j + 1 );
                    i4 = ( i ) * widthStep + ( j + 1 );
                    i5 = ( i + 1 ) * widthStep + ( j + 1 );
                    i6 = ( i + 1 ) * widthStep + ( j );
                    i7 = ( i + 1 ) * widthStep + ( j - 1 );
                    i8 = ( i ) * widthStep + ( j - 1 );
                    i9 = ( i - 1 ) * widthStep + ( j - 1 );

                    p2 = W(imagePtr[ i2 ]);
                    p3 = W(imagePtr[ i3 ]);
                    p4 = W(imagePtr[ i4 ]);
                    p5 = W(imagePtr[ i5 ]);
                    p6 = W(imagePtr[ i6 ]);
                    p7 = W(imagePtr[ i7 ]);
                    p8 = W(imagePtr[ i8 ]);
                    p9 = W(imagePtr[ i9 ]);

                    int a = AND( B( p2 == 0 ), B( p3 == 1 ) ) + AND( B( p3 == 0 ), B( p4 == 1 ) ) +
                            AND( B( p4 == 0 ), B( p5 == 1 ) ) + AND( B( p5 == 0 ), B( p6 == 1 ) ) +
                            AND( B( p6 == 0 ), B( p7 == 1 ) ) + AND( B( p7 == 0 ), B( p8 == 1 ) ) +
                            AND( B( p8 == 0 ), B( p9 == 1 ) ) + AND( B( p9 == 0 ), B( p2 == 1 ) );

                    int b = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9;
                    int m1 = iter == 0 ? ( p2 * p4 * p6 ) : ( p2 * p4 * p8 );
                    int m2 = iter == 0 ? ( p4 * p6 * p8 ) : ( p2 * p6 * p8 );

                    if (a == 1 && b >= 2 && b <= 6 && m1 == 0 && m2 == 0) 
                        markerPtr[ i1 ] = 255;

                }
            }
        }
        image.Set( CvColor.White, marker );
    }
    catch( Exception e )
    {

    }
}

private int W( int num )
{
    return num > 0 ? 1 : 0;
}
private int AND( int a, int b )
{
    return ( a > 0 && b > 0 ) ? 1 : 0;
}
private int B( bool b )
{
    return b ? 1 : 0;
}

Can you recognize any problems on my code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.