opencv识别物体上的黑色污点
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv

·
#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;
}




OpenCV: 开源计算机视觉库
最近提交(Master分支:14 天前 )
3c3a26b6
imgcodecs: bmp: relax decoding size limit to over 1GiB #27811
Close https://github.com/opencv/opencv/issues/27789
Close https://github.com/opencv/opencv/issues/23233
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
3 小时前
744d5ecd
libtiff upgrade to version 4.7.1 #27806
close https://github.com/opencv/opencv/issues/27784
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
5 小时前
更多推荐
所有评论(0)