【问题标题】:Translating a C++ (dealing with corona) function into C#将 C++(处理电晕)函数翻译成 C#
【发布时间】:2015-04-08 22:08:50
【问题描述】:

我有一个 C++ .exe,用作独立的图像清洁器。 但我现在想在我自己的 c# 应用程序中使用它的功能,所以我开始翻译它。但我真的对 C++ 及其逻辑一无所知。 所以我来这里寻求帮助。

首先,有没有人知道这个功能的任何等价物? Corona“getPixels()”(是的,有一个“s”,因为我知道 c# 有一个内置的 getPixel): 这是来自 corona doc 的功能解释:getPixels() Corona 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)); }

谁能帮我理解和转换这个吗?

【问题讨论】:

    标签: c# c++ imaging recode


    【解决方案1】:

    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. 如何将像素数据写回位图

    希望这会有所帮助!

    【讨论】:

    • @ananthonline 首先非常感谢您抽出宝贵时间。至少没有错误,但不是返回一个干净的图像,而是返回一个黄色矩形。我正在查看您的代码以检查已完成的操作以及为什么它可能会给我这个。无论如何都要为您的时间和真正有用的帮助 +1
    • @ananthonline 你能帮我转换一下这最后一件事吗?它可能会解决我的问题...unsigned char *pixels = new unsigned char[width * height * 3]; unsigned char *p = pixels, *b = outmap; int col, row; for (row = 0; row &lt; rows; row++) for (col = 0; col &lt; cols; col++) { *p++ = *b; *p++ = *b; *p++ = *b++; } corona::Image *img = corona::CreateImage(width, height, IMAGE_FORMAT, pixels); corona::SaveImage(filename, corona::FF_AUTODETECT, img);
    • 更新了我的答案,希望这是缺失的部分!请记住,这些只是为您提供所需工具的指针(双关语)。现在你应该拥有所有需要的东西(我无法阻止自己!)。
    • 好吧,它不像原来的 C++ exe 那样工作。我想对我来说唯一的解决方案是完全了解原始 C++ 代码在 C# 中做同样的事情。现在,你的代码给了我一种带有一些噪音的黑白剥离图片(可能是它应该从图片中清除的一些“灰尘”。还不知道。)我会尝试理解所有那些星号的东西正在*失去 **我 *所以 **非常感谢 xD 无论如何
    • 您对“条带化”的描述听起来像是步幅问题。尝试对源也使用 rowPtr、dstPtr 类似方法。
    猜你喜欢
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 2011-12-09
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多