2

スタンドアロンのイメージ クリーナーとして使用していた C++ .exe がありました。しかし、今ではその関数を自分の C# アプリで使用したいので、翻訳を開始しました。しかし、私は本当に完全に C++ とそのロジックについて何も知りません。だから私は助けを求めてここに来ます。

まず、この機能に相当するものを知っている人はいますか? コロナ "getPixels()" (はい、c# には getPixel が組み込まれていることを知っているので、"s" が付きます): コロナ ドキュメントからの関数の説明は次のとおりです: getPixels() コロナ dll は、翻訳しようとしている行で使用されます。 .

ここにすべてがあります:

元の C++ コード:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "corona.h"

#define IMAGE_FORMAT corona::PF_R8G8B8 /* RGB mode - 8 bits each */
#define GetXY(x,y, w)  ((x) + ((w) * (y)))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))

#define SQ(a) ((a) * (a))
#define DISTANCE(a, b, c, d) (SQ(a - c) + SQ(b - d))



int main(int argc, char **argv)
{
corona::Image *img;
unsigned char *pixels;
int threshold = 0;
int distance = 0;
int pixel;
char str[255];
int rows, cols;
int row, col;
unsigned char *bitmap, *p;
unsigned char *outmap;
pixels = (unsigned char*)img->getPixels();
rows = img->getHeight();
cols = img->getWidth();
bitmap = new unsigned char[rows * cols];
p = bitmap;
outmap = new unsigned char[rows * cols];

//convert to single byte grayscale
for (row = 0; row < rows; row++)
for (col = 0; col < cols; col++)
{
    pixel = *pixels++;
    pixel += *pixels++;
    pixel += *pixels++;

    *p++ = pixel / 3;
}

//free corona loading
delete img;

int distance = 8;
int threshold = 7;
//check our threshold
for (row = 0; row < rows; row++)
for (col = 0; col < cols; col++)
{
    if (bitmap[GetXY(col, row, cols)])
    {
        int count = 0;
        int x, y;
        int dhalf = distance / 2 + 1;

        //optimization possible here by checking inside a circle rather than square+dist
        for (x = MAX(col - dhalf, 0); x < MIN(col + dhalf, cols); x++)
        for (y = MAX(row - dhalf, 0); y < MIN(row + dhalf, rows); y++)
        {
            if (SQ(distance) > DISTANCE(col, row, x, y) && bitmap[GetXY(x, y, cols)])
                count++;
        }

        if (count >= threshold)
            outmap[GetXY(col, row, cols)] = 255;
        else
            outmap[GetXY(col, row, cols)] = 0;
    }
    else
        outmap[GetXY(col, row, cols)] = 0;
}
}

私が翻訳できるもので今持っているもの...少なくとも正しく願っています...:

private Bitmap optIm2(Bitmap _img)
        {
            int rows = _img.Height;
            int cols = _img.Width;
            pixels = (unsigned char)img->getPixels();  //here i dont know at all
            bitmap = new unsigned char[rows * cols];  //here i dont know at all
            p = bitmap;
            outmap = new unsigned char[rows * cols];  //here i dont know at all

            //convert to single byte grayscale
            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    pixel = *pixels++;  //here i dont know at all
                    pixel += *pixels++;  //here i dont know at all
                    pixel += *pixels++;  //here i dont know at all

                    *p++ = pixel / 3;  //here i dont know at all
                }
            }
            //free corona loading
            delete img;

            int distance = 9;
            int threshold = 7;

            //check our threshold
            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    if (bitmap[GetXY(col, row, cols)])
                    {
                        int count = 0;
                        int x, y;
                        int dhalf = distance / 2 + 1;

                        //optimization possible here by checking inside a circle rather than square+dist
                        for (x = Math.Max(col - dhalf, 0); x < Math.Min(col + dhalf, cols); x++)
                        {
                            for (y = Math.Max(row - dhalf, 0); y < Math.Min(row + dhalf, rows); y++)
                            {
                                if (SQ(distance) > DISTANCE(col, row, x, y) && bitmap[GetXY(x, y, cols)])
                                    count++;
                            }
                        }
                        if (count >= threshold)
                        {
                            outmap[GetXY(col, row, cols)] = 255;
                        }
                        else
                        {
                            outmap[GetXY(col, row, cols)] = 0;
                        }
                    }
                    else
                    {
                        outmap[GetXY(col, row, cols)] = 0;
                    }
                }
            }
return iDontKnowWhatYet;
        }


        private int GetXY(int x,int y, int w) { return ((x) + ((w) * (y))); }
        private int SQ(int a) { return ((a) * (a)); }
        private int DISTANCE(int a, int b, int c, int d) { return (SQ(a - c) + SQ(b - d)); }

これを理解して変換するのを手伝ってくれる人はいますか?

4

1 に答える 1

3

C# コードは次のようになります。

private unsafe Bitmap optIm2(Bitmap img)
{
    int rows = img.Height;
    int cols = img.Width;

    var dstImg = new Bitmap(cols, rows, img.PixelFormat);
    var srcImageData = img.LockBits(new Rectangle(0, 0, cols, rows), System.Drawing.Imaging.ImageLockMode.ReadOnly, img.PixelFormat);
    var dstImageData = dstImg.LockBits(new Rectangle(0, 0, cols, rows), System.Drawing.Imaging.ImageLockMode.ReadOnly, dstImg.PixelFormat);
    try
    {
        var bitmap = new byte[rows * cols];
        var outmap = new byte[rows * cols];

        fixed (byte* ptr = &bitmap[0])
        {
            byte* pixels = (byte*)srcImageData.Scan0;
            byte* p = ptr;

            //convert to single byte grayscale
            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    var pixel = *pixels++;
                    pixel += *pixels++; 
                    pixel += *pixels++;

                    *p++ = (byte)(pixel / 3);  //here i dont know at all
                }
            }
        }

        int distance = 9;
        int threshold = 7;

        //check our threshold
        for (int row = 0; row < rows; row++)
        {
            for (int col = 0; col < cols; col++)
            {
                if (bitmap[GetXY(col, row, cols)] != 0)
                {
                    int count = 0;
                    int x, y;
                    int dhalf = distance / 2 + 1;

                    //optimization possible here by checking inside a circle rather than square+dist
                    for (x = Math.Max(col - dhalf, 0); x < Math.Min(col + dhalf, cols); x++)
                    {
                        for (y = Math.Max(row - dhalf, 0); y < Math.Min(row + dhalf, rows); y++)
                            if ((SQ(distance) > DISTANCE(col, row, x, y)) && (bitmap[GetXY(x, y, cols)] != 0))
                                count++;
                    }
                    if (count >= threshold)
                        outmap[GetXY(col, row, cols)] = 255;
                    else
                        outmap[GetXY(col, row, cols)] = 0;
                }
                else
                    outmap[GetXY(col, row, cols)] = 0;
            }
        }

        // Copy data from outmap to pixels of bitmap. Since outmap is grayscale data, we replicate it for all channels
        byte* dstPtr = (byte*)dstImageData.Scan0;
        for (int row = 0; row < rows; row++)
        {
            byte* rowPtr = dstPtr;
            for (int col = 0; col < cols; col++)
            {
                *rowPtr++ = outmap[GetXY(col, row, cols)];
                *rowPtr++ = outmap[GetXY(col, row, cols)];
                *rowPtr++ = outmap[GetXY(col, row, cols)];
            }
            dstPtr += dstImageData.Stride;
        }
    }
    finally
    {
        img.UnlockBits(srcImageData);
        img.Dispose();

        dstImg.UnlockBits(dstImageData);
    }

    return dstImg;
}

private int GetXY(int x, int y, int w) { return ((x) + ((w) * (y))); }
private int SQ(int a) { return ((a) * (a)); }
private int DISTANCE(int a, int b, int c, int d) { return (SQ(a - c) + SQ(b - d)); }

実際のロジックで正しいアルゴリズムを確認していませんが、上記のコードには、自分で行うために必要なすべてのビットが含まれています。主な注意点は次のとおりです。

  1. IntPtr からポインターを取得する方法 (修正済み)
  2. ビットマップからピクセル データを取得する方法
  3. ピクセル データをビットマップに書き戻す方法

お役に立てれば!

于 2015-04-08T22:43:44.527 に答える