OpenCV 和 c++ ;比较两个二值图像并获得输出(结果)

发布于 2024-11-27 19:50:25 字数 124 浏览 7 评论 0原文

我想比较两个二进制图像并获得结果输出。

我该怎么做?

我可以使用 cvSobel() 来做到这一点吗?

二进制图像有白色边缘,有没有办法计算白色像素之类的???

谢谢 !

I want to compare two binary images and get an output as a result .

How can i do this ??

Can i use cvSobel() to do that??

Binary image has white edges and is there a way to count white pixels or something ???

Thank you !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒路情人 2024-12-04 19:50:25

尝试简历::比较:
http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html #cv-比较

cv::Mat img1 = ...
cv::Mat img2 = ...
cv::Mat result = ...

int threshold = (double)(img1.rows * img1.cols) * 0.7; 

cv::compare(img1 , img2  , result , cv::CMP_EQ );
int similarPixels  = countNonZero(result);

if ( similarPixels  > threshold ) {
   cout << "similar" << endl;
}

try cv::compare:
http://opencv.willowgarage.com/documentation/cpp/operations_on_arrays.html#cv-compare

cv::Mat img1 = ...
cv::Mat img2 = ...
cv::Mat result = ...

int threshold = (double)(img1.rows * img1.cols) * 0.7; 

cv::compare(img1 , img2  , result , cv::CMP_EQ );
int similarPixels  = countNonZero(result);

if ( similarPixels  > threshold ) {
   cout << "similar" << endl;
}
ι不睡觉的鱼゛ 2024-12-04 19:50:25

这是我根据以下论文编写的函数。 (应该检查我的代码!我将不胜感激!)
值得关注的论文:AJ Baddeley:二进制图像的错误度量

作者还有一个统计包,可以在其中找到代码。它称为“spatstat”www.spatstat.org。要使用 spatstat,请首先从 http://www.r-project.org/。误差度量可作为称为“deltametric”的函数使用。要查看帮助文件,请键入 help(deltametric)。

代码说明:
该函数输入两个文件名,应该是二进制图像文件!返回值是 Baddeley 错误度量编号。
还应该包括 OpenCV 标头和命名空间!

float baddeleyerror (const char * a_file, const char* b_file)
{
Mat A,B,Adist,Bdist,Z;
double c=5;
double p=2;
double nelem;
double minval, maxval;

A=imread(a_file,0);
B=imread(b_file,0);

nelem=A.rows*A.cols;

A=A>1;
B=B>1;
distanceTransform(A,Adist,CV_DIST_L1,3);
distanceTransform(B,Bdist,CV_DIST_L1,3);

min(Adist, c, Adist);
min(Bdist, c, Bdist);

minMaxLoc(Adist, &minval, &maxval, 0, 0);
Adist.convertTo(Adist, CV_8UC1, 255/maxval, 1);

minMaxLoc(Bdist, &minval, &maxval, 0, 0);
Bdist.convertTo(Bdist, CV_8UC1, 255/maxval, 1);

pow(abs(Adist-Bdist),p,Z);

return (pow(sum(Z).val[0]/nelem, 1/p));
}

Here is a function that I have written, based on the following paper. (One should CHECK my CODE! it would be greatly appreciated!)
Paper to look after: A J Baddeley: An error metric for binary images

Also the author has a statistic package, where the code can be found. It is called "spatstat" www.spatstat.org. To use the spatstat first download the R-Stat from http://www.r-project.org/. The error metric is available as a function called 'deltametric'. To see the help file, type help(deltametric).

Description of code:
The input of this function two filename, which should be binary image files! The return value is the Baddeley Error metric number.
One should include the OpenCV headers and namespace also!

float baddeleyerror (const char * a_file, const char* b_file)
{
Mat A,B,Adist,Bdist,Z;
double c=5;
double p=2;
double nelem;
double minval, maxval;

A=imread(a_file,0);
B=imread(b_file,0);

nelem=A.rows*A.cols;

A=A>1;
B=B>1;
distanceTransform(A,Adist,CV_DIST_L1,3);
distanceTransform(B,Bdist,CV_DIST_L1,3);

min(Adist, c, Adist);
min(Bdist, c, Bdist);

minMaxLoc(Adist, &minval, &maxval, 0, 0);
Adist.convertTo(Adist, CV_8UC1, 255/maxval, 1);

minMaxLoc(Bdist, &minval, &maxval, 0, 0);
Bdist.convertTo(Bdist, CV_8UC1, 255/maxval, 1);

pow(abs(Adist-Bdist),p,Z);

return (pow(sum(Z).val[0]/nelem, 1/p));
}
秉烛思 2024-12-04 19:50:25

为什么不使用直接比较呢? (逐像素)
无论如何,Sobel 都会占用 O(pixels),因此像素与像素之间的比较不会改变复杂性。

Why not use a straightforward comparison? (pixel by pixel)
Sobel will take you O(pixels) anyway, so comparing pixel to pixel won't change complexity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文