Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C#发送到C+中的OpenCV后获取扭曲图像+;? 我在我的C++类中创建了一个C DLL,它使用了 opencv < /Cube >进行图像处理,并希望在我的C语言应用中使用这个DLL。目前,我是这样实施的: #ifdef CDLL2_EXPORTS #define CDLL2_API __declspec(dllexport) #else #define CDLL2_API __declspec(dllimport) #endif #include "../classification.h" extern "C" { CDLL2_API void Classify_image(unsigned char* img_pointer, unsigned int height, unsigned int width, char* out_result, int* length_of_out_result, int top_n_results = 2); //... }_C#_C++_Opencv_Dllimport - Fatal编程技术网

从C#发送到C+中的OpenCV后获取扭曲图像+;? 我在我的C++类中创建了一个C DLL,它使用了 opencv < /Cube >进行图像处理,并希望在我的C语言应用中使用这个DLL。目前,我是这样实施的: #ifdef CDLL2_EXPORTS #define CDLL2_API __declspec(dllexport) #else #define CDLL2_API __declspec(dllimport) #endif #include "../classification.h" extern "C" { CDLL2_API void Classify_image(unsigned char* img_pointer, unsigned int height, unsigned int width, char* out_result, int* length_of_out_result, int top_n_results = 2); //... }

从C#发送到C+中的OpenCV后获取扭曲图像+;? 我在我的C++类中创建了一个C DLL,它使用了 opencv < /Cube >进行图像处理,并希望在我的C语言应用中使用这个DLL。目前,我是这样实施的: #ifdef CDLL2_EXPORTS #define CDLL2_API __declspec(dllexport) #else #define CDLL2_API __declspec(dllimport) #endif #include "../classification.h" extern "C" { CDLL2_API void Classify_image(unsigned char* img_pointer, unsigned int height, unsigned int width, char* out_result, int* length_of_out_result, int top_n_results = 2); //... },c#,c++,opencv,dllimport,C#,C++,Opencv,Dllimport,C#相关代码: DLL导入部分: //Dll import [DllImport(@"CDll2.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] static extern void Classify_Image(IntPtr img, uint height, uint width, byte[] out_result, out int out_result_length, int top

C#相关代码:

DLL导入部分:

//Dll import 
[DllImport(@"CDll2.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
static extern void Classify_Image(IntPtr img, uint height, uint width, byte[] out_result, out int out_result_length, int top_n_results = 2);
将映像发送到DLL的实际函数:

//...
//main code 
private string Classify(int top_n)
{
    byte[] res = new byte[200];
    int len;
    Bitmap img = new Bitmap(txtImagePath.Text);
    BitmapData bmpData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), 
                                      ImageLockMode.ReadWrite,  
                                      PixelFormat.Format24bppRgb);
    Classify_Image(bmpData.Scan0, (uint)bmpData.Height, (uint)bmpData.Width, res, out len, top_n);
    img.UnlockBits(bmpData); //Remember to unlock!!!
    //...
}

和DLL中的C++代码:< /P>

CDLL2_API void Classify_Image(unsigned char* img_pointer, unsigned int height, unsigned int width,
                              char* out_result, int* length_of_out_result, int top_n_results)
    {
        auto classifier = reinterpret_cast<Classifier*>(GetHandle());

        cv::Mat img = cv::Mat(height, width, CV_8UC3, (void*)img_pointer, Mat::AUTO_STEP);

        std::vector<Prediction> result = classifier->Classify(img, top_n_results);

        //...
        *length_of_out_result = ss.str().length();
    }
CDLL2\u API void Classify\u图像(无符号字符*img\u指针、无符号整数高度、无符号整数宽度、,
char*out\u结果,int*length\u of\u out\u结果,int top\u n\u结果)
{
自动分类器=重新解释强制转换(GetHandle());
cv::Mat img=cv::Mat(高度、宽度、cv_8UC3,(空*)img_指针、Mat::自动步进);
std::vector result=分类器->分类(img,顶部结果);
//...
*结果的长度=ss.str().length();
}
这对某些图像非常有效,但对其他图像不起作用,例如,当我尝试在
Classify\u image
中显示图像时,从C应用程序发送的数据创建图像后,我会遇到如下图像:

有问题的例子:

好例子:


您最初的问题是关于所谓的图像缓冲区的步幅或音高。 基本上,出于性能原因,像素行值可以与内存对齐,这里我们看到,在您的情况下,它会导致像素行不对齐,因为行大小不等于像素行宽度

一般情况是:

resolution width * bit-depth (in bytes) * num of channels + padding
在您的情况下,类状态:

跨距是一行像素(扫描线)的宽度, 四舍五入到四字节边界

因此,如果我们查看有问题的图像,它的分辨率为1414像素宽,这是一个8位RGB位图,因此如果我们进行数学计算:

1414 * 1 * 3 (we have RGB so 3 channels) = 4242 bytes
现在除以4字节:

4242 / 4 = 1060.5
因此,我们只剩下
0.5*4字节=2字节的填充

所以跨距实际上是4244字节

所以这需要通过,这样步幅是正确的


看看你在做什么,我会将文件作为内存传递给你的openCV dll,它应该能够调用
imdecode
,它会嗅探文件类型,此外,您还可以传递标志,该标志将加载图像并动态转换灰度。

看起来步幅/音高不正确,例如,出于性能原因,步骤arg将对齐内存,使每一行符合某些字节对齐方式。这就是为什么下一个像素没有对齐,您需要查看步长/步幅大小,这可能需要传递,以便在访问后续行时使用内存中的正确偏移,我不能建议任何东西,因为我纯粹在C++中用OpenCV编码,但是这是你的问题。这里的图像错误在这里找到了一些相关的东西:我不在C代码中编码,但是看起来你应该检查这个,它可能和你所找到的图像宽度不一样。它可能是
图像宽度*每个通道的字节数*通道数+填充
。文档说明数字将是4字节对齐的。有问题的图像的宽度为1414,图像为24位,带有3*8位颜色通道,如果我们计算字节数:
1414*3=4242字节
如果我们再除以4
4242/4=1060.5
,你可以看到剩下的是
0.5
,这意味着步幅将设置为
4244
,因为
0.5*4字节=2字节
,请检查这是否是您需要从位图确定位深度和颜色通道数的情况,通常它们会告诉您包装,例如yuv、rgb、,例如,在本例中,您需要获得24位的位数,然后除以颜色通道的数量,这将是3,以获得每个通道8位。我正在做饭,所以不能在任何时候张贴有意义的东西,我不确定递增地更新这个问题来提出一个新的问题是一件好事,所以,我会关闭这个问题,并打开一个新的问题,引用这个问题。除此之外,calcs
GetResizeHeight
的代码在哪里?另外,设置分辨率的文档说明,你应该通过每英寸点数的分辨率,而不是像素值,这可能会让你感到困惑。此外,您应该只使用openCV调整大小,并传递一些缩放的分辨率大小,如半高/宽度,非常感谢。顺便提一下,这是一个新问题,我试着做得很彻底: