在 OpenCV C++ 中复制 MATLAB 代码(bwConnComp 和 regionprops)

我打算用 C++ 实现一个 MatLab 代码,但这里有一些问题,这是我的 MatLab 代码的一部分,效果很好,这是我的 MatLab 代码:

              connected_comp=bwconncomp(bw_inter);
              centroid=regionprops(connected_comp,'Centroid');

              if size(centroid,1)>1
                 for k=1:size(centroid,1) 
                      centers=abs(centroid(k).Centroid-win_size);
                      if centers(1)<8 && centers(2)<8
                          centroidXY=centroid(k).Centroid;
                      end
                 end
              else
                  centroidXY=centroid(1).Centroid;
              end

在 openCV 中我的代码是:

            Mat labelImage(bw_inter.size(),CV_32S);
            int connected_comp = connectedComponents(bw_inter,labelImage,8);
            Point2f cen1;
            for (int k = 1; k < connected_comp; k++)
            {
                Mat bb = labelImage == k;
                vector<Point> locations;
                cv::findNonZero(bb,locations);
                int nls = (int)locations.size();
                Scalar smean = mean(locations);
                Point2d cen = { smean.val[0],smean.val[1] };
                
                
                if (nls > 1) {

                    for (int kk = 0; kk < nls; kk++)
                    {
                        Point pt = locations[kk];
                        Scalar smean = mean(locations);
                        Point2d cen = { smean.val[0],smean.val[1] };

                        Point2f cens = { abs((float)cen.x - win_size),abs((float)cen.y - win_size) };

                        int x01 = (int)round(cens.x);
                        int y01 = (int)round(cens.y);
                        if (x01 < 7 && y01 < 7) {
                            cen1 = { ((float)cen.x),((float)cen.y) };
                        }
                    }
                }

                else {
                    
                     cen1 = { abs((float)cen.x ),abs((float)cen.y ) };
                    
                }

它们对我来说应该是一样的,是吗? OpenCV 代码不像 MATLAB 那样完美。有人对此有什么建议吗?

iCMS 回答:在 OpenCV C++ 中复制 MATLAB 代码(bwConnComp 和 regionprops)

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/424104.html

大家都在问