opencv识别物体上的黑色污点
来源:互联网 发布:华南师范大学 网络缴费 编辑:程序博客网 时间:2024/04/19 07:19
#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;
}
#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;
}
阅读全文
0 0
- opencv识别物体上的黑色污点
- opencv--颜色物体识别跟踪
- python+opencv识别动态物体
- opencv图像污点修复
- 利用OpenCV的inpaint函数实现图像的污点修复
- 物体的形状识别
- PhotoShop如何去除图片上的污点、水印
- 4招教你“洗白”简历上的致命“污点”!
- 基于OpenCV的目标物体颜色及轮廓的识别方法
- 物体识别系统的流程
- OpenCV之轮廓检测(检测银行卡上的黑色磁条)
- Ogre中物体的材质显示黑色问题
- opencv 使较暗的物体变亮
- 基于opencv的物体定位
- tensorflow在android上的实现,可以实时识别摄像头里的物体
- opencv中利用图像相减识别物体运动
- opencv中利用图像相减识别物体运动
- RGBD物体识别(3)--ubuntu安装opencv3和opencv-contrib
- 代码设置颜色
- [笔记]MySQL查询语法
- IDEA 常用快捷键
- springboot 打包jar
- django之url配置
- opencv识别物体上的黑色污点
- jQuery:unbind方法的使用详解
- 1-1软件介绍及html介绍
- HashSet vs. TreeSet vs. LinkedHashSet
- 文章标题
- redis同步登陆实现session共享
- [YZOJ]P3290-暴力-性质题
- ES6的一些学习
- Oracle中的instr和substr函数用法