请教opencv识别小黑点
1个回答
展开全部
连通体检测?Opencv有没有相关的函数提供?
我昨天自己按自己的理解处理了一下,虽然最后也得出了黑点的位置,但是真是土得不得了……
代码如下:
// cv2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/highgui/highgui.hpp>
void thredChange(cv::InputArray src,cv::OutputArray dst)
{
cv::threshold(src,dst,180,255,CV_THRESH_BINARY_INV);
}
void filterPoints(cv::Mat image,cv::Mat result)
{
// allocate if necessary
//result.create(image.size(), image.type());
for(int j=0;j<image.rows;++j)
result.row(j).setTo(cv::Scalar(0));
int d=10;
for (int j= d; j<image.rows-d; j++) { // for all rows
// (except first and last)
const uchar* previous=
image.ptr<const uchar>(j-d); // previous row
const uchar* current=
image.ptr<const uchar>(j); // current row
const uchar* next=
image.ptr<const uchar>(j+d); // next row
uchar* output= result.ptr<uchar>(j); // output row
for (int i=d; i<image.cols-d; i++) {
if(current[i]!=255||
current[i-d]!=0||
current[i+d]!=0||
previous[i]!=0||
previous[i-d]!=0||
previous[i+d]!=0||
previous[i+1]!=0||
previous[i+2]!=0||
previous[i+3]!=0||
previous[i+4]!=0||
previous[i-4]!=0||
previous[i-3]!=0||
previous[i-2]!=0||
previous[i-1]!=0||
next[i-d]!=0||
next[i+d]!=0||
next[i-1]!=0||
next[i-2]!=0||
next[i-3]!=0||
next[i-4]!=0||
next[i+1]!=0||
next[i+2]!=0||
next[i+3]!=0||
next[i+4]!=0||
next[i]!=0
)
*output++=0;
else
*output++=255;
}
}
}
void removeLittle(cv::Mat image,cv::Mat result,int num)
{
// allocate if necessary
//result.create(image.size(), image.type());
for(int j=0;j<image.rows;++j)
result.row(j).setTo(cv::Scalar(0));
int d=1;
for (int j= d; j<image.rows-d; j++) { // for all rows
// (except first and last)
const uchar* previous=
image.ptr<const uchar>(j-d); // previous row
const uchar* current=
image.ptr<const uchar>(j); // current row
const uchar* next=
image.ptr<const uchar>(j+d); // next row
uchar* output= result.ptr<uchar>(j); // output row
for (int i=d; i<image.cols-d; i++) {
int a=current[i]+current[i-1]+current[i+1]
+previous[i-1]+previous[i]+previous[i+1]
+next[i-1]+next[i]+next[i+1];
if((current[i]==255)&&
(a>255*num))
*output++=255;
else
*output++=0;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat image=cv::imread("./data/test1/points.jpg",CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat p2;
cv::Mat p3=image.clone();;
cv::Mat p4=image.clone();
cv::Mat p5=image.clone();
thredChange(image,p2);
filterPoints(p2,p3);
removeLittle(p3,p4,5);
removeLittle(p4,p5,3);
cv::namedWindow("gui");
cv::imshow("gui",p5);
cv::waitKey(0);
return 0;
}
我昨天自己按自己的理解处理了一下,虽然最后也得出了黑点的位置,但是真是土得不得了……
代码如下:
// cv2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/highgui/highgui.hpp>
void thredChange(cv::InputArray src,cv::OutputArray dst)
{
cv::threshold(src,dst,180,255,CV_THRESH_BINARY_INV);
}
void filterPoints(cv::Mat image,cv::Mat result)
{
// allocate if necessary
//result.create(image.size(), image.type());
for(int j=0;j<image.rows;++j)
result.row(j).setTo(cv::Scalar(0));
int d=10;
for (int j= d; j<image.rows-d; j++) { // for all rows
// (except first and last)
const uchar* previous=
image.ptr<const uchar>(j-d); // previous row
const uchar* current=
image.ptr<const uchar>(j); // current row
const uchar* next=
image.ptr<const uchar>(j+d); // next row
uchar* output= result.ptr<uchar>(j); // output row
for (int i=d; i<image.cols-d; i++) {
if(current[i]!=255||
current[i-d]!=0||
current[i+d]!=0||
previous[i]!=0||
previous[i-d]!=0||
previous[i+d]!=0||
previous[i+1]!=0||
previous[i+2]!=0||
previous[i+3]!=0||
previous[i+4]!=0||
previous[i-4]!=0||
previous[i-3]!=0||
previous[i-2]!=0||
previous[i-1]!=0||
next[i-d]!=0||
next[i+d]!=0||
next[i-1]!=0||
next[i-2]!=0||
next[i-3]!=0||
next[i-4]!=0||
next[i+1]!=0||
next[i+2]!=0||
next[i+3]!=0||
next[i+4]!=0||
next[i]!=0
)
*output++=0;
else
*output++=255;
}
}
}
void removeLittle(cv::Mat image,cv::Mat result,int num)
{
// allocate if necessary
//result.create(image.size(), image.type());
for(int j=0;j<image.rows;++j)
result.row(j).setTo(cv::Scalar(0));
int d=1;
for (int j= d; j<image.rows-d; j++) { // for all rows
// (except first and last)
const uchar* previous=
image.ptr<const uchar>(j-d); // previous row
const uchar* current=
image.ptr<const uchar>(j); // current row
const uchar* next=
image.ptr<const uchar>(j+d); // next row
uchar* output= result.ptr<uchar>(j); // output row
for (int i=d; i<image.cols-d; i++) {
int a=current[i]+current[i-1]+current[i+1]
+previous[i-1]+previous[i]+previous[i+1]
+next[i-1]+next[i]+next[i+1];
if((current[i]==255)&&
(a>255*num))
*output++=255;
else
*output++=0;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat image=cv::imread("./data/test1/points.jpg",CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat p2;
cv::Mat p3=image.clone();;
cv::Mat p4=image.clone();
cv::Mat p5=image.clone();
thredChange(image,p2);
filterPoints(p2,p3);
removeLittle(p3,p4,5);
removeLittle(p4,p5,3);
cv::namedWindow("gui");
cv::imshow("gui",p5);
cv::waitKey(0);
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询