0

嗨,我正在使用 Visual Studio 2008 和 C++ 来实现它运行良好的分水岭算法。但我最近正在使用 Visual Studio 2010 中的 opencvsharp 包装类将相同的代码转换为 C#。我完成了大部分代码,但我不能't convertCV_IMAGE_ELEM() 成 opencvsharp 谁能帮忙

这是我完整的 C++ 源代码

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

这就是我遇到麻烦的地方

// 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];  
                }  
            }

他们是否有其他方法可以在不使用 CV_IMAGE_ELEM 的情况下完成这项工作?错误是“当前上下文中不存在该名称”

这是我一直在处理的 c# 代码,我使用 opencvsharp 完成了这个,我无法完成代码任何帮助

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);
                                             */



                                            unsafe{

                                                 CvRNG rng = new CvRNG(); 

                                            byte* ptr = (byte*)color_tab.DataArrayByte.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);

                                            }

                                            /*

                                            CvRNG rng = new CvRNG(); 

                                           for (i = 0; i < comp_count * 3; i++)

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

                                            //for (i = 0; i < comp_count * 3; i++)
                                           // ptr[i] = (byte)(cvRandInt(...) % 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_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];  
                }  
            }  


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


                                    }

                                    return 0;

我正在努力寻找答案,任何帮助将不胜感激。谢谢你。

4

0 回答 0