[学习opencv](opencv)去除图像中白的边界干扰

C++- opencv

[学习opencv](opencv)去除图像中白的边界干扰_第1张图片

[学习opencv](opencv)去除图像中白的边界干扰_第2张图片

//去除图像中白的边界干扰 way-2速度快

Mat lookUpTable(1, 256, CV_8U);
uchar *p = lookUpTable.data;
for(int i=0; i<256; i++)
{
if(i>=240)
{
p[i]=0;
}
else
{
p[i]=i;
}
}
//通过LUT函数实现图像取反
LUT(image,lookUpTable,image);

python-opencv//

ListaVred =np.array([0 for i in range(0,256)],dtype='uint8')

for i in range(0,256):
    if i>240:
            ListaVred[i]=0
        else:
            ListaVred[i]=i
#cv2.LUT(image,ListaVred,image2)

image2=cv2.LUT(image,ListaVred)


print ListaVred.dtype
print image.dtype

ListaVred =np.array([0 for i in range(0,256)],dtype='uint8')

2018-5-8

// 边缘区域去除1.cpp : 定义控制台应用程序的入口点。
// VS2010 opencv2.4.9

//#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include 
using namespace cv;
using namespace std;

void floodFillborder(const cv::Mat& binsrcImg,cv::Mat& bindstImg);
int main()
{
Mat src=imread("1.jpg",1);; 
	imshow("src",src);

	//灰度化
	Mat grayImage;
	cvtColor(src,grayImage,CV_BGR2GRAY);

	//二值化原图像
	Mat thresImage=Mat::zeros(grayImage.rows,grayImage.cols,CV_8UC1);
	threshold(grayImage,thresImage,250,255,THRESH_BINARY);
	//imshow("thresImage",thresImage);


	////方法1:轮廓提取与阈值填充
	//vector> contours;  
	//vectorcontours_sum;
	//// find  
	//findContours(thresImage,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE); 
	//for(int n =0; n edgePts;
	//const int minLength=std::min(nr,nc)/4;
	//for(int i=0;i<4;++i)
	//{
	//	std::vector line;
	//	Mat_::const_iterator iter = edge[i].begin();       //当前像素
	//	Mat_::const_iterator nextIter = edge[i].begin()+1; //下一个像素
	//	while(nextIter!=edge[i].end())
	//	{
	//		if(*iter==255)
	//		{
	//			if(*nextIter==255)
	//			{
	//				Point pt = iter.pos();
	//				if(i==1)
	//					pt.y = nr-1;
	//				if(i==3)
	//					pt.x = nc-1;
	//				
	//				//line.push_back(pt);
	//				edgePts.push_back(pt);
	//			}
	//			//if(*nextIter!=255)
	//			//{
	//			//	if(line.size()>minLength)
	//			//		edgePts.push_back(line.at(line.size()/2));
	//			//	line.clear();
	//			//}
	//		}
	//		++iter;
	//		++nextIter;
	//	}
	//}
	//
	//for(int n =0; n edgePts;
	const int minLength=std::min(nr,nc)/4;
	for(int i=0;i<4;++i)
	{
		std::vector line;
		Mat_::const_iterator iter = edge[i].begin();       //当前像素
		Mat_::const_iterator nextIter = edge[i].begin()+1; //下一个像素
		while(nextIter!=edge[i].end())
		{
			if(*iter==255)
			{
				if(*nextIter==255)
				{
					Point pt = iter.pos();
					if(i==1)
						pt.y = nr-1;
					if(i==3)
						pt.x = nc-1;

					edgePts.push_back(pt);
				}
			}
			++iter;
			++nextIter;
		}
	}

	for(int n =0; n


你可能感兴趣的:(opencv)