读取影像
#include //头文件
using namespace cv; //包含cv命名空间
int main()
{
// 【1】读入一张图片
Mat img = imread("D://ly_yun//images//GF1a.tif");
// 【2】在窗口中显示载入的图片
imshow("【载入的图片】", img);
// 【3】等待6000 ms后窗口自动关闭
waitKey(6000);
}
影像腐蚀
//----------------------------------------------------------------------------------------------
#include
#include
//-----------------------------------【命名空间声明部分】---------------------------------------
// 描述:包含程序所使用的命名空间
//-----------------------------------------------------------------------------------------------
using namespace cv;
//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-----------------------------------------------------------------------------------------------
int main()
{
//载入原图
Mat srcImage = imread("1.jpg");
//显示原图
imshow("【原图】腐蚀操作", srcImage);
//进行腐蚀操作
Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));
Mat dstImage;
erode(srcImage, dstImage, element);
//显示效果图
imshow("【效果图】腐蚀操作", dstImage);
waitKey(0);
return 0;
}
转灰度
#include
#include
using namespace cv;
//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main()
{
//【0】载入原始图
Mat srcImage = imread("1.jpg"); //工程目录下应该有一张名为1.jpg的素材图
imshow("【原始图】Canny边缘检测", srcImage); //显示原始图
Mat dstImage, edge, grayImage; //参数定义
//【1】创建与src同类型和大小的矩阵(dst)
dstImage.create(srcImage.size(), srcImage.type());
//【2】将原图像转换为灰度图像
cvtColor(srcImage, grayImage, CV_BGR2GRAY);
//【3】先用使用 3x3内核来降噪
blur(grayImage, edge, Size(3, 3));
//【4】运行Canny算子
Canny(edge, edge, 3, 9, 3);
//【5】显示效果图
imshow("【效果图】Canny边缘检测", edge);
waitKey(0);
return 0;
}
// ----------------------------------影像二值化-------------------start
threshold(srcImg, result, 1, 255, CV_THRESH_BINARY);
namedWindow("二值化后的图像1");
imshow("二值化后的图像1", result);
void RegionGrowing(Mat srcImg, Mat& dstImg, Point pt, int thre)
{
Point ptGrowing;//生长点像素坐标
int nGrowLabel = 0;//是否被标记
int startPtValue = 0;//生长起始点灰度值
int currPtValue = 0;//当前生长点灰度值
//8邻域
int mDir[8][2] = { { -1, -1 }, { 0, -1 }, { 1, -1 }, { -1, 0 }, { 1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 } };
vector growPtVec;//生长点堆栈
growPtVec.push_back(pt);//将初始生长点压入堆栈
Mat markImg = Mat::zeros(srcImg.size(), CV_8UC1);//标记点
unsigned char *pData = (unsigned char *)(markImg.data + pt.y*markImg.step);
pData[pt.x] = 255;//标记初始生长点
startPtValue = ((unsigned char*)(srcImg.data + pt.y*srcImg.step))[pt.x];
while (!growPtVec.empty())
{
Point currPt = growPtVec.back();//返回当前vector最末一个元素
growPtVec.pop_back();//弹出最后压入的数据
for (int i = 0; i<8; i++)
{
ptGrowing.x = currPt.x + mDir[i][0];
ptGrowing.y = currPt.y + mDir[i][1];
//判断是否是边缘点
if (ptGrowing.x<0 || ptGrowing.y<0 || (ptGrowing.x>srcImg.cols - 1) || (ptGrowing.y>srcImg.rows))
continue;//继续执行下一次循环
//判断是否已被标记
nGrowLabel = ((unsigned char*)(markImg.data + ptGrowing.y*markImg.step))[ptGrowing.x];
if (nGrowLabel == 0)//没有被标记
{
currPtValue = ((unsigned char*)(srcImg.data + ptGrowing.y*srcImg.step))[ptGrowing.x];
if (abs(currPtValue - startPtValue) <= thre)
{
((unsigned char*)(markImg.data + ptGrowing.y*markImg.step))[ptGrowing.x] = 255;
growPtVec.push_back(ptGrowing);
}
}
}
}
markImg.copyTo(dstImg);
}
int main()
{
Mat srcImg = imread("D:\\ly_yun\\images\\LBP\\LPB.tif", 0);
Mat result;
if (srcImg.empty())
printf("image read error");
// ----------------------------------影像二值化-------------------start
threshold(srcImg, result, 1, 255, CV_THRESH_BINARY);
namedWindow("二值化后的图像1");
imshow("二值化后的图像1", result);
// ----------------------------------影像二值化-------------------end
//----------------------------------影像闭运算-------------------start
namedWindow("[原图]");
imshow("[原图]", srcImg);
//获取自定义核
Mat element = getStructuringElement(MORPH_RECT, Size(10,10));
Mat dstImage1;
Mat dstImage2;
Mat dstImage3;
Mat dstImage;
//先进行腐蚀操作
erode(result, dstImage1, element);
namedWindow("腐蚀操作[效果图]");
imshow("腐蚀操作[效果图]", dstImage1); //由运行结果看,背景噪声在该阶段就被消除了
//对腐蚀后的图像,进行开运算操作
morphologyEx(result, dstImage2, MORPH_OPEN, element);
namedWindow("开运算操作[效果图]");
imshow("开运算操作[效果图]", dstImage2);
//对dstImage2图像,进行膨胀处理
dilate(result, dstImage3, element);
namedWindow("膨胀操作[效果图]");
imshow("膨胀操作[效果图]", dstImage3);
// 对dstImage3图像进行闭运算操作,
morphologyEx(result, dstImage, MORPH_CLOSE, element);
namedWindow("闭运算操作[效果图]");
imshow("闭运算操作[效果图]", dstImage);
imwrite("D:\\ly_yun\\images\\LBP\\LPBmorphologyEx.tif", dstImage);
Mat srcImg0 = imread("D:\\ly_yun\\images\\LBP\\LPBmorphologyEx.tif", 0);
//对dstImage2图像,进行膨胀处理
dilate(srcImg0, dstImage3, element);
namedWindow("膨胀操作[效果图]");
imshow("膨胀操作[效果图]", dstImage3);
imwrite("D:\\ly_yun\\images\\LBP\\LPBdilate.tif", dstImage3);
//先进行腐蚀操作 膨胀和腐蚀相反????????????????????????????????????????????????????
erode(srcImg0, dstImage1, element);
namedWindow("腐蚀操作[效果图]");
imshow("腐蚀操作[效果图]", dstImage1); //由运行结果看,背景噪声在该阶段就被消除了
imwrite("D:\\ly_yun\\images\\LBP\\LPBerode.tif", dstImage1);
//----------------------------------影像闭运算-------------------end
Mat junzhidst;
blur(result, junzhidst, Size(15, 15), Point(-1, -1));//均值滤波操作
namedWindow("均值滤波[效果图]");
imshow("均值滤波[效果图]", dstImage);
Mat gblur;
//高斯滤波操作
GaussianBlur(result, gblur, Size(15, 15), 11, 11);//高斯滤波
imshow("高斯滤波", gblur);
//----------------------------------区域生长法-------------------start
Mat srcImg1 = result.clone();
Mat outImg1, outImg2;
RegionGrowing(srcImg1, outImg1, Point(241, 258), 10);
RegionGrowing(srcImg1, outImg2, Point(302, 118), 80);
add(outImg1, outImg2, outImg1);
imshow("p1p2", outImg1);
Mat resultImg;
result.copyTo(resultImg, ~outImg1);
imshow("outImg", resultImg);
//----------------------------------区域生长法-------------------end
waitKey(0);
return 0;
}