图像分割与边缘检测
来源:互联网 发布:卡尔曼滤波c语言 编辑:程序博客网 时间:2024/05/20 21:44
OTSU
#include <stdio.h>#include <string>#include "opencv2/highgui/highgui.hpp"#include "opencv2/opencv.hpp"using namespace std;using namespace cv;// OTSU函数实现int OTSU(cv::Mat srcImage){ int nCols = srcImage.cols; int nRows = srcImage.rows; int threshold = 0; // 初始化统计参数 int nSumPix[256]; float nProDis[256]; for (int i = 0; i < 256; i++) { nSumPix[i] = 0; nProDis[i] = 0; } // 统计灰度级中每个像素在整幅图像中的个数 for (int i = 0; i < nCols; i++) { for (int j = 0; j < nRows; j++) { nSumPix[(int)srcImage.at<uchar>(i, j)]++; } } // 计算每个灰度级占图像中的概率分布 for (int i = 0; i < 256; i++) { nProDis[i] = (float)nSumPix[i] / (nCols * nRows); } // 遍历灰度级[0,255],计算出最大类间方差下的阈值 float w0, w1, u0_temp, u1_temp, u0, u1, delta_temp; double delta_max = 0.0; for (int i = 0; i < 256; i++) { // 初始化相关参数 w0 = w1 = u0_temp = u1_temp = u0 = u1 = delta_temp = 0; for (int j = 0; j < 256; j++) { //背景部分 if (j <= i) { // 当前i为分割阈值,第一类总的概率 w0 += nProDis[j]; u0_temp += j * nProDis[j]; } //前景部分 else { // 当前i为分割阈值,第一类总的概率 w1 += nProDis[j]; u1_temp += j * nProDis[j]; } } // 分别计算各类的平均灰度 u0 = u0_temp / w0; u1 = u1_temp / w1; delta_temp = (float)(w0 *w1* pow((u0 - u1), 2)); // 依次找到最大类间方差下的阈值 if (delta_temp > delta_max) { delta_max = delta_temp; threshold = i; } } return threshold;}int main(){ // 图像读取及判断 cv::Mat srcImage = cv::imread("images/hand1.jpg"); if (!srcImage.data) return 1; // 灰度转换 cv::Mat srcGray; cv::cvtColor(srcImage, srcGray, CV_RGB2GRAY); cv::imshow("srcGray", srcGray); // 调用OTSU二值化算法得到阈值 int ostuThreshold = OTSU(srcGray); std::cout << ostuThreshold << std::endl; // 定义输出结果图像 cv::Mat otsuResultImage = cv::Mat::zeros(srcGray.rows, srcGray.cols, CV_8UC1); // 利用得到的阈值实现二值化操作 for (int i = 0; i < srcGray.rows; i++) { for (int j = 0; j < srcGray.cols; j++) { // 满足大于阈值ostuThreshold置255 if (srcGray.at<uchar>(i, j) > ostuThreshold) otsuResultImage.at<uchar>(i, j) = 255; else otsuResultImage.at<uchar>(i, j) = 0; } } cv::imshow("otsuResultImage", otsuResultImage); cv::waitKey(0); return 0;}
霍夫变换
#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"#include <iostream>using namespace cv;using namespace std;int main( ){ cv::Mat srcImage = cv::imread("images/Pic3_3.png", 0); if (!srcImage.data) return -1; cv::Mat edgeMat, houghMat; // Canny边缘检测 二值图像 Canny(srcImage, edgeMat, 50, 200, 3); cvtColor(edgeMat, houghMat, CV_GRAY2BGR); #if 0 // 标准的霍夫变换 vector<Vec2f> lines; HoughLines(edgeMat, lines, 1, CV_PI/180, 100, 0, 0 ); for( size_t i = 0; i < lines.size(); i++ ) { // 根据直线参数表达式绘制相应检测结果 float rho = lines[i][0], theta = lines[i][1]; Point pt1, pt2; double a = cos(theta), b = sin(theta); double x0 = a*rho, y0 = b*rho; pt1.x = cvRound(x0 + 1000*(-b)); pt1.y = cvRound(y0 + 1000*(a)); pt2.x = cvRound(x0 - 1000*(-b)); pt2.y = cvRound(y0 - 1000*(a)); line( houghMat, pt1, pt2, Scalar(0,0,255), 3, CV_AA); } #else // 统计概率的霍夫变换 vector<Vec4i> lines; HoughLinesP(edgeMat, lines, 1, CV_PI/180, 50, 50, 10 ); for( size_t i = 0; i < lines.size(); i++ ) { Vec4i l = lines[i]; // 绘制线检测结果 line( houghMat, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,255,255), 3, CV_AA); } #endif cv::imshow("srcImage", srcImage); cv::imshow("houghMat", houghMat); cv::waitKey(); return 0;}
阅读全文
0 0
- 边缘检测与图像分割
- 图像分割与边缘检测
- 图像分割 和 边缘检测
- 图像分割之边缘检测
- 回复:请问图像分割与边缘检测有何不同?
- 灰度图像--图像分割 Canny边缘检测
- 图像分割技术之图像边缘检测
- 数字图像处理之边缘检测,图像分割
- U-net使用, 图像分割(边缘检测)
- U-net使用, 图像分割(边缘检测)
- U-net使用, 图像分割(边缘检测)
- 图像分割·基于边缘检测
- U-net使用, 图像分割(边缘检测)
- 图像分割-10.2.4边缘模型、边缘检测、
- 图像处理与边缘检测
- 灰度图像--图像分割 边缘检测算子 综述
- 图像分割算法:概述及常用边缘检测算法
- 图像分割算法简介及常用边缘检测算法
- Intellij-SpringBoot热部署
- tensorflow学习笔记一
- hadoop-3.0.0-beta1运维手册(004):安装分布式hdfs3.0.0-配置JDK、设置主机名
- 11.30开通博客
- PAT
- 图像分割与边缘检测
- 机器学习第三课——特征提取
- 王牌特工:黄金圈 经典例句
- 【OpenCV】VS2017配置OpenCV2.4.13.4(其余高版本同理)
- C语言贪心算法之非洲小孩
- java多线程学习(二)---线程通信
- Opencv for python 对于图形文件和图形的基本操作
- 九宫格布局
- JSP引擎的内置对象Request对象来获取客户提交的信息