gpt4 book ai didi

c# - 无法将 uchar ptr 转换为 opencvsharp

转载 作者:行者123 更新时间:2023-11-30 17:49:46 29 4
gpt4 key购买 nike

嗨,我正在使用 Visual Studio 2008 和 C++ 来实现分水岭算法,它运行良好。但我最近正在使用 Visual Studio 2010 中的 opencvsharp 包装类将相同的代码转换为 C#。我完成了大部分代码但我无法将 uchar ptr 转换为 opencvsharp 我什至使用字节数据类型,但它不起作用。

这是 C++ 源代码的链接

code.ros.org/trac/opencv/browser/trunk/opencv/samples/c/watershed.cpp?rev=493

这就是我遇到麻烦的地方

uchar* ptr = color_tab->data.ptr + i*3;  
ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50);
ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50);
ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50);

任何帮助将不胜感激。谢谢。

这是我一直在编写的 C# 代码,我无法完成代码任何帮助

using (IplImage img0 = new IplImage("1180.jpg", LoadMode.AnyDepth | LoadMode.AnyColor)){
using (IplImage img = img0.Clone())
using (IplImage marker_mask = new IplImage(img0.Size, BitDepth.U8, 1))
using (IplImage markers = new IplImage(img.Size, BitDepth.S32, 1))
using (IplImage img_gray = img0.Clone())

using (IplImage wshed = img0.Clone()){
wshed.Zero();
marker_mask.Zero();

using (CvWindow w_image = new CvWindow("image", WindowMode.AutoSize, img))
{
CvPoint prev_pt = new CvPoint(-1, -1);
w_image.OnMouseCallback += delegate(MouseEvent ev, int x, int y, MouseEvent flags)
{
if (ev == MouseEvent.LButtonUp || (flags & MouseEvent.FlagLButton) == 0)
{
prev_pt = new CvPoint(-1, -1);
}
else if (ev == MouseEvent.LButtonDown)
{
prev_pt = new CvPoint(x, y);
}
else if (ev == MouseEvent.MouseMove && (flags & MouseEvent.FlagLButton) != 0)
{
CvPoint pt = new CvPoint(x, y);
if (prev_pt.X < 0)
{
prev_pt = pt;
}

marker_mask.Line(prev_pt,pt,Cv.ScalarAll(255),5,LineType.Link8,0);
img.Line(prev_pt,pt,Cv.ScalarAll(255),5,LineType.Link8,0);
prev_pt = pt;
w_image.ShowImage(img);
}
};

for (; ; )
{
switch (CvWindow.WaitKey(0))
{
case 27:
return;
case 'r':
marker_mask.Zero();
img0.Copy(img);
w_image.ShowImage(img);
break;
case 'w':
case '\r':

CvSeq<CvPoint> contours;
CvMat color_tab=null;
int i,j,comp_count=0;

CvMemStorage storage = new CvMemStorage();
Cv.FindContours(marker_mask,storage, out contours);
markers.Zero();

for(; contours !=null; contours=contours.HNext,comp_count++){

Cv.DrawContours(markers, contours, Cv.ScalarAll(comp_count+1), Cv.ScalarAll(comp_count+1),-1,-1,LineType.Link8,new CvPoint(0,0));

}

if(comp_count ==0){
continue;

color_tab=Cv.CreateMat(1,comp_count,MatrixType.U8C3);

for(i=0;i<comp_count;i++){
/*
uchar* ptr = color_tab->data.ptr + i*3;
ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50);
ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50);
ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50);
*/


}
{
double t =(double)Cv.GetTickCount();
Cv.Watershed(img0,markers);
Cv.Save(markers,"img0.xml");
t=(double)Cv.GetTickCount()-t;
Console.WriteLine("exec time = %gms\n",t/(Cv.GetTickFrequency()*1000));
}
// paint the watershed image
for(i=0;i<markers.Height;i++)
for(j=0;i<markers.Width;j++)
{
int idx =

}
Cv.AddWeighted(wshed,0.5,img_gray,0.5,0,wshed);
Cv.ShowImage("watershed transform",wshed);
Cv.ReleaseMat(color_tab);
}


}

return 0;
}



}
}

谢谢 torak,但似乎它不起作用,我修改了这样的 C# 代码

unsafe{

CvRNG rng = new CvRNG();

byte* ptr = (byte*)color_tab.Data.ptr +i*3;

ptr[0] = (byte)(Cv.RandInt(rng)%180 + 50);
ptr[1] = (byte)(Cv.RandInt(rng)%180 + 50);
ptr[2] = (byte)(Cv.RandInt(rng)%180 + 50);

}

它没有给出任何错误,但我不确定输出,我现在遇到了一个新问题。我无法将 c++ 代码中的宏 CV_IMAGE_ELEM() 转换为 c#。任何人都可以帮忙。谢谢你评论

这就是我在 C++ 代码中遇到麻烦的地方。完整的 C++ 代码显示在问题的开头

// paint the watershed image  
for( i = 0; i < markers->height; i++ )
for( j = 0; j < markers->width; j++ )
{
int idx = CV_IMAGE_ELEM( markers, int, i, j );//markersIPL_DEPTH_32S
uchar* dst = &CV_IMAGE_ELEM( wshed, uchar, i, j*3 );//BGR,j*3
if( idx == -1 ) //-1?
dst[0] = dst[1] = dst[2] = (uchar)255;
else if( idx <= 0 || idx > comp_count ) //
dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here
else //
{
uchar* ptr = color_tab->data.ptr + (idx-1)*3;
dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2];
}
}

最佳答案

快速查看后,您的数据方式似乎是使用 color_tab 的适当 DataArray... 成员。在您的情况下,这将是 color_tab.DataArrayByte[...] 产生类似以下内容:

for (i = 0; i < comp_count * 3; i++)
ptr[i] = (byte)(cvRandInt(...) % 180 + 50);

我还没有研究过随机数生成,因此我目前无法测试它,但我怀疑它可以解决您的问题。手指交叉。

关于c# - 无法将 uchar ptr 转换为 opencvsharp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17691064/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com