Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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_Image Processing_Opencv3.0 - Fatal编程技术网

C++ 从图像中删除具有像素值总和的连接组件<;门槛

C++ 从图像中删除具有像素值总和的连接组件<;门槛,c++,opencv,image-processing,opencv3.0,C++,Opencv,Image Processing,Opencv3.0,简单地说,这是一个可用输入和我想要的相应输出的示例: In: [ 0 0 0 0 0 0 1 0 1 0 0 6 6 0 0 0 0 1 1 0 0 8 9 0 0 0 0 0 1 1 8 0 0 0 9 9 0 0 0 0 0 0 0 0 8 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 9 7 0 0 0 0 0 1 0 0 6 8 0 0 0 0 3 2 0 0 0 0 0 0

简单地说,这是一个可用输入和我想要的相应输出的示例:

In: [ 0 0 0 0 0 0 1 0 1 0
      0 6 6 0 0 0 0 1 1 0
      0 8 9 0 0 0 0 0 1 1
      8 0 0 0 9 9 0 0 0 0
      0 0 0 0 8 8 0 0 0 0
      0 0 0 0 0 0 0 0 0 0
      9 9 7 0 0 0 0 0 1 0
      0 6 8 0 0 0 0 3 2 0
      0 0 0 0 0 0 0 2 1 0
      0 0 0 0 0 0 0 0 0 0 ]
使用cv2.connectedComponents()进行二值化并获得带标签的图像后:

labels: [ 0 0 0 0 0 0 1 0 1 0
          0 2 2 0 0 0 0 1 1 0
          0 2 2 0 0 0 0 0 1 1
          2 0 0 0 3 3 0 0 0 0
          0 0 0 0 3 3 0 0 0 0
          0 0 0 0 0 0 0 0 0 0
          4 4 4 0 0 0 0 0 5 0
          0 4 4 0 0 0 0 5 5 0
          0 0 0 0 0 0 0 5 5 0
          0 0 0 0 0 0 0 0 0 0 ]
从这里,我想要以下输出:

Out: [0 0 0 0 0 0 0 0 0 0
      0 6 6 0 0 0 0 0 0 0
      0 8 9 0 0 0 0 0 0 0
      8 0 0 0 9 9 0 0 0 0
      0 0 0 0 8 8 0 0 0 0
      0 0 0 0 0 0 0 0 0 0
      9 9 7 0 0 0 0 0 0 0
      0 6 8 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 ]
存在许多连接的组件(在本例中:5个组件具有8-connectivity)。像素总和小于10(阈值)的组件(在中的中被移除

如何在C++中实现这一点(可能使用OpenCV)?< /P>

我已经在Python上使用OpenCV完成了,但是不能在C++上实现。p> 这是Python代码的一部分,如果有用的话(labelscv2.connectedComponents()的输出)

输入是一个简单的2D矩阵。这是一个连接组件的示例:


您已经在Python中实现了这一点,因此您知道如何解决这个问题。我想能够在C++中实现它是知道你想要使用的库的问题。 Python实现的效率非常低:迭代标签,并针对每个标签访问每个图像像素(
idx=np.where(labels.copy()==cnt)
)。如果你有超过几个的标签,这会变得非常昂贵

在下面的代码中,我在图像上循环一次以累积每个标签的图像强度总和,然后循环一次以计算出的总和绘制图像(对于每个带有标签的像素,查找该标签的总和),然后再循环一次以对该图像设置阈值。然后,我使用这个阈值图像作为遮罩,将输入图像中不希望保留的像素设置为0

我在这里用。虽然我相信如果你真的想使用OpenCV,你可以用某种方式复制它

#include "diplib.h"
#include "dipviewer.h"
#include "diplib/file_io.h"
#include "diplib/regions.h"
#include "diplib/measurement.h"

int main() {
   // Create a test image with similar properties to your example
   dip::Image input = -dip::ImageReadICS( "/Users/cris/newdip/examples/cermet.ics" );
   input.At( input < 120 ) = 0;
   // Display
   dip::viewer::ShowSimple( input, "input image" );
   // Threshold and label
   dip::Image label = dip::Label( input > 0 );
   // Obtain sum of pixels per label
   dip::MeasurementTool measurementTool;
   auto msr = measurementTool.Measure( label, input, { "Mass" } );
   // Paint each label with the measured value
   dip::Image feature = dip::ObjectToMeasurement( label, msr[ "Mass" ] );
   // Create output as a copy of the input, with low feature values set to 0
   dip::Image output = input.Copy();
   output.At( feature < 100000 ) = 0;
   // Display
   dip::viewer::ShowSimple( output, "output image" );
   dip::viewer::Spin();
}
#包括“diplib.h”
#包括“dipviewer.h”
#包括“diplib/file_io.h”
#包括“diplib/regions.h”
#包括“diplib/measurement.h”
int main(){
//创建具有与示例类似属性的测试映像
dip::Image input=-dip::ImageReadICS(“/Users/cris/newdip/examples/cermet.ics”);
输入。At(输入<120)=0;
//展示
dip::viewer::ShowSimple(输入,“输入图像”);
//阈值和标签
dip::图像标签=dip::标签(输入>0);
//获取每个标签的像素总和
dip::MeasurementTool MeasurementTool;
auto msr=measurementTool.Measure(标签,输入,{“质量”});
//用测量值绘制每个标签
dip::Image feature=dip::ObjectToMeasurement(标签,msr[“质量]);
//将输出创建为输入的副本,将低要素值设置为0
dip::Image output=input.Copy();
输出.At(特性<100000)=0;
//展示
dip::viewer::ShowSimple(输出,“输出图像”);
dip::viewer::Spin();
}

如果您想坚持使用opencv,可以使用
cv::calcHist
来计算每个标签的出现次数,以及对应于小于10的bin值的filrerout值。
然后,应将生成的labeld图像进行二值化,并按元素乘以源以获得所需的结果。

定义“blob”。。。我已经编辑了这个问题。我希望这足够清楚,不是真的。连通性与零有关,对吗?非零点的对角邻接是否构成连接?或者连接必须是并排的?零是背景。我指的是所有非零元素之间的连通性。从图中可以看出,对角线算作连通性。原文中没有这样的例子。谢谢你的详细回答!你是对的,我也知道要使用的函数,但我就是不知道如何编写它(我是C++新手)。这正是我想要的,只是我更喜欢使用OpenCV。这只是使用OpenCV的大型代码的一小部分。我会用你的答案,也许对我有帮助:)谢谢!我试图用C或C++编码它,并被卡在那里(我可以很容易地用python做它很简单)
#include "diplib.h"
#include "dipviewer.h"
#include "diplib/file_io.h"
#include "diplib/regions.h"
#include "diplib/measurement.h"

int main() {
   // Create a test image with similar properties to your example
   dip::Image input = -dip::ImageReadICS( "/Users/cris/newdip/examples/cermet.ics" );
   input.At( input < 120 ) = 0;
   // Display
   dip::viewer::ShowSimple( input, "input image" );
   // Threshold and label
   dip::Image label = dip::Label( input > 0 );
   // Obtain sum of pixels per label
   dip::MeasurementTool measurementTool;
   auto msr = measurementTool.Measure( label, input, { "Mass" } );
   // Paint each label with the measured value
   dip::Image feature = dip::ObjectToMeasurement( label, msr[ "Mass" ] );
   // Create output as a copy of the input, with low feature values set to 0
   dip::Image output = input.Copy();
   output.At( feature < 100000 ) = 0;
   // Display
   dip::viewer::ShowSimple( output, "output image" );
   dip::viewer::Spin();
}