//#include "stdafx.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Vec3b RandomColor(int value); //生成随机颜色函数
int main(int argc, char* argv[])
{
//src = imread( "E://ylab//DY//ETHZShapeClasses-V1.2//Bottles//green.jpg" );
//src = imread( "E://ylab//DY//ETHZShapeClasses-V1.2//Mugs//jazzburger.jpg" );
//src = imread( "E://1.png" );
//Mat image=imread( "E://ylab//DY//ETHZShapeClasses-V1.2//Bottles//green.jpg" );
Mat image = imread("white.png");//载入RGB彩色图像
imshow("Source Image", image);
//灰度化,滤波,Canny边缘检测
Mat imageGray, imageCanny;
cvtColor(image, imageGray, CV_RGB2GRAY);//灰度转换
GaussianBlur(imageGray, imageGray, Size(3, 3), 2); //高斯滤波
imshow("Gray Image", imageGray);
Canny(imageGray, imageCanny, 60, 120);
imshow("Canny Image", imageCanny);
//灰度化,滤波,Canny边缘检测
Mat imageGray, imageCanny;
cvtColor(image, imageGray, CV_RGB2GRAY);//灰度转换
GaussianBlur(imageGray, imageGray, Size(3, 3), 2); //高斯滤波
imshow("Gray Image", imageGray);
Canny(imageGray, imageCanny, 3,9,3);
imshow("Canny Image", imageCanny);
//imwrite("cannny.png", imageCanny);
//查找轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(imageCanny, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE, Point());
Mat imageContours = Mat::zeros(image.size(), CV_8UC1); //轮廓
Mat marks(image.size(), CV_32S); //Opencv分水岭第二个矩阵参数
marks = Scalar::all(0);
int index = 0;
int compCount = 0;
for (; index >= 0; index = hierarchy[index][0], compCount++)
{
//对marks进行标记,对不同区域的轮廓进行编号,相当于设置注水点,有多少轮廓,就有多少注水点
drawContours(marks, contours, index, Scalar::all(compCount + 1), 1, 8, hierarchy);
drawContours(imageContours, contours, index, Scalar(255), 1, 8, hierarchy);
cout << compCount << endl;
}
//我们来看一下传入的矩阵marks里是什么东西
Mat marksShows;
convertScaleAbs(marks, marksShows);
imshow("marksShow", marksShows);
imshow("轮廓", imageContours);
watershed(image, marks);
//我们再来看一下分水岭算法之后的矩阵marks里是什么东西
Mat afterWatershed;
convertScaleAbs(marks, afterWatershed);
imshow("After Watershed", afterWatershed);
//对每一个区域进行颜色填充
Mat PerspectiveImage = Mat::zeros(image.size(), CV_8UC3);
for (int i = 0; i<marks.rows; i++)
{
for (int j = 0; j<marks.cols; j++)
{
int index = marks.at<int>(i, j);
if (marks.at<int>(i, j) == -1)
{
PerspectiveImage.at<Vec3b>(i, j) = Vec3b(255, 255, 255);
}
else
{
PerspectiveImage.at<Vec3b>(i, j) = RandomColor(index);
}
}
}
imshow("After ColorFill", PerspectiveImage);
//分割并填充颜色的结果跟原始图像融合
Mat wshed;
addWeighted(image, 0.2, PerspectiveImage, 0.4, 0, wshed);
imshow("AddWeighted Image", wshed);
waitKey();
}
Vec3b RandomColor(int value) //生成随机颜色函数
{
value = value % 255; //生成0~255的随机数
RNG rng;
int aa = rng.uniform(0, value);
int bb = rng.uniform(0, value);
int cc = rng.uniform(0, value);
return Vec3b(aa, bb, cc);
}
fenshuiling.rar_分水岭分割_图像分割
版权申诉
87 浏览量
2022-09-21
00:41:32
上传
评论
收藏 1KB RAR 举报
JonSco
- 粉丝: 72
- 资源: 1万+
最新资源
- 水稻病害基于Yolov8算法优化目标检测识别与AI辅助决策python源码+模型+使用说明.zip
- 海尔618算价表_七海5.20_16.00xlsx(1)(2).xlsx
- WebCrawler.scr
- 【计算机专业毕业设计】大学生就业信息管理系统设计源码.zip
- YOLO 数据集:8种路面缺陷病害检测【包含划分好的数据集、类别class文件、数据可视化脚本】
- JAVA实现Modbus RTU或Modbus TCPIP案例.zip
- 基于YOLOv8的FPS TPS AI自动锁定源码+使用步骤说明.zip
- JAVA实现Modbus RTU或Modbus TCPIP案例.zip
- 基于yolov8+streamlit的火灾检测部署源码+模型.zip
- 测试aaaaaaabbbbb
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈