C++ 基于二值图像的图像分割
我有这两个图像,图像1是图像2的二进制文件C++ 基于二值图像的图像分割,c++,opencv,image-processing,image-segmentation,edge-detection,C++,Opencv,Image Processing,Image Segmentation,Edge Detection,我有这两个图像,图像1是图像2的二进制文件 image 1 image 2 我想从图像2中分割出图像1形状的对象 image 1 image 2 我看过几个样品,但没有一个是有用的。考虑过将图像2叠加到图像1的白色区域上,使用copyTo根据条件运行,但我相信有更好的方法。如果有人能告诉我解决这个问题的方法或C++代码,我会很感激的。p>
image 1 image 2
我想从图像2中分割出图像1形状的对象
image 1 image 2
我看过几个样品,但没有一个是有用的。考虑过将图像2叠加到图像1的白色区域上,使用copyTo根据条件运行,但我相信有更好的方法。如果有人能告诉我解决这个问题的方法或C++代码,我会很感激的。p> 您需要使用大津阈值来分离前景和背景
/**@file
Get the Otusu threshold for image segmentation.
@param[in] gray - the grayscale image
@param width - image width
@param height - image height
@returns Threshold at which to split pixels into foreground and
background.
@image html maggie.jpg Margaret Thatcher (1925-2013) greyscale photograph
@image html maggieotsu.gif Mrs Thatcher Otsu thresholded
*/
int getOtsuthreshold(unsigned char *grey, int width, int height)
{
int hist[256] = {0};
int wB = 0;
int wF;
float mB, mF;
float sum = 0;
float sumB = 0;
float varBetween;
float varMax = 0.0f;
int answer = 0;
int i;
int k;
for(i=0;i<width*height;i++)
hist[grey[i]]++;
/* sum of all (for means) */
for (k=0 ; k<256 ; k++)
sum += k * hist[k];
for(k=0;k<256;k++)
{
wB += hist[k];
if (wB == 0)
continue;
wF = width*height - wB;
if (wF == 0)
break;
sumB += (float) (k * hist[k]);
mB = sumB / wB; /* Mean Background */
mF = (sum - sumB) / wF; /* Mean Foreground */
/* Calculate Between Class Variance */
varBetween = (float)wB * (float)wF * (mB - mF) * (mB - mF);
/* Check if new maximum found */
if (varBetween > varMax)
{
varMax = varBetween;
answer = k;
}
}
return answer;
}
/**@文件
得到用于图像分割的Otusu阈值。
@参数[in]灰度-灰度图像
@参数宽度-图像宽度
@参数高度-图像高度
@返回将像素分割为前景和背景的阈值
背景。
@图像html maggie.jpg玛格丽特·撒切尔(1925-2013)灰度照片
@图像html maggieotsu.gif撒切尔夫人-大津阈值
*/
int getOtsuthreshold(无符号字符*灰色,int宽度,int高度)
{
int hist[256]={0};
int wB=0;
int wF;
浮动mB,mF;
浮点数和=0;
浮点数b=0;
浮动变量;
浮动varMax=0.0f;
int-answer=0;
int i;
int k;
对于(i=0;i是的,你是对的
如上所述,您可以在C++中执行图像掩蔽,而不是使用复制:
cv::bitwise_and(image1, image2, output);
其中,image1
是遮罩,image2
是原始图像
另一种对我有效的方法是:
image2.copyTo(image1, image1);
其中,image1
是掩码,image2
是原始图像。结果如下所示:
代码在哪里?你尝试了什么?你可以尝试图像掩蔽…@Christophe我想把图像2叠加到图像1的白色区域上,使用copyTo根据条件进行操作。但我相信有更好的方法。你确定你在尝试回答这个问题吗?即使如此,你也可以使用cv::threshold(…,THRESH_OTSU)
…不需要外部库设置到带掩码的
就更清晰了。在我看来。@Miki Cool。这两个函数之间是否存在性能问题?或者这两个函数占用相同的计算时间?可能相同……我认为“我根据此掩码设置此值”比“我正在和ing这两幅图像”@Miki我实际上尝试了相同的方法,并面临着相同的错误(直到现在)。更改掩蔽通道无法解决错误。我认为我偏离了方向,这就是为什么我要求使用此方法。但感谢您的解释,我下次提问时将展示一些证据!:)@塞缪尔,我看到你已经解决了你的问题。干得好。继续学习!!!