我正在使用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 ++ code.full 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];
}
}
答案 0 :(得分:0)
快速查看后,您的数据似乎正在使用DataArray...
color_tab
成员的color_tab.DataArrayByte[...]
成员。在你的情况下for (i = 0; i < comp_count * 3; i++)
ptr[i] = (byte)(cvRandInt(...) % 180 + 50);
会产生类似的东西:
{{1}}
我没有查看随机数生成,因此我暂时无法测试它,但我怀疑它可以解决您的问题。手指交叉。