若该文为原创文章,未经允许不得转载
原博主博客地址:https://blog.csdn.net/qq21497936
原博主博客导航:https://blog.csdn.net/qq21497936/article/details/102478062
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/104811545
各位读者,知识无穷而人力有穷,要么改需求,要么找专业人士,要么自己研究
红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中...(点击传送门)
上一篇:《OpenCV开发笔记(二十八):带你学习图像识别之阈值化》
下一篇:《OpenCV开发笔记(三十):带你学习图像识别之经典OTSU算法阈值化》
上一篇中讲解了阈值化(固定阈值化),对应的还有自适应阈值化。
自适应阈值化是相对于固定阈值化的叫法,之前通过固定阈值化的篇章《OpenCV开发笔记(二十八):带你学习图像识别之阈值化》可以从Demo上看出可以分割的效果。但是在实际的场景用,目标和背景区域通常是依存的图像块中,于是可以通过图像像素领域块的分布特征 来自适应确定区域的二值化阈值。
自适应阈值化就是根据图像中亮度变化明显的区域(亮或暗),自适应阈值通常会,进而保证图像中各个像素的阈值会随着周围领域块的变化而变化。
也就是说,没有固定阈值了,完全是通过周围色差对比来进行阈值化,这样便于背景与物体本身差距有点大的图片分割,也就能得到更好的分割效果。
void adaptiveThreshold( InputArray src,
OutputArray dst,
double maxValue,
int adaptiveMethod,
int thresholdType,
int blockSize,
double C );
void cvtColor( InputArray src,
OutputArray dst,
int code,
int dstCn = 0 );
void OpenCVManager::testAdaptiveThreshold()
{
QString fileName1 = "E:/qtProject/openCVDemo/openCVDemo/modules/openCVManager/images/4.jpg";
cv::Mat srcMat = cv::imread(fileName1.toStdString());
cv::Mat thresh1Mat = cv::imread("E:/qtProject/openCVDemo/openCVDemo/modules/openCVManager/images/thresh1.png");
cv::Mat thresh2Mat = cv::imread("E:/qtProject/openCVDemo/openCVDemo/modules/openCVManager/images/thresh2.png");
cv::resize(thresh1Mat, thresh1Mat, cv::Size(160, 80));
cv::resize(thresh2Mat, thresh2Mat, cv::Size(160, 80));
int width = 200;
int height = 160;
cv::resize(srcMat, srcMat, cv::Size(width, height));
cv::String windowName = _windowTitle.toStdString();
cvui::init(windowName);
if(!srcMat.data)
{
qDebug() << __FILE__ << __LINE__
<< "Failed to load image:" << fileName1;
return;
}
qDebug() << __FILE__ << __LINE__
<< "Succeed to load image, type =" << srcMat.type()
<< "channels = " << srcMat.channels();
cv::Mat dstMat;
dstMat = cv::Mat::zeros(srcMat.size(), srcMat.type());
cv::Mat windowMat = cv::Mat(cv::Size(dstMat.cols * 4, dstMat.rows * 3),
srcMat.type());
int maxValue = 255;
int blockSize = 1;
int c = 0;
while(true)
{
windowMat = cv::Scalar(0, 0, 0);
// 原图先copy到左边
cv::Mat leftMat = windowMat(cv::Range(0, srcMat.rows),
cv::Range(0, srcMat.cols));
cv::addWeighted(leftMat, 1.0f, srcMat, 1.0f, 0.0f, leftMat);
// 调整阈值化的参数maxValue
cvui::printf(windowMat, width * 2 + 100, 0 + height * 0, "maxValue");
cvui::trackbar(windowMat, width * 2 + 100, 10 + height * 0, 250, &maxValue, 0, 255);
// 调整阈值化的参数blockSize
cvui::printf(windowMat, width * 2 + 100, 45 + height * 0, "blockSize");
cvui::trackbar(windowMat, width * 2 + 100, 55 + height * 0, 250, &blockSize, 1, 25);
// 调整阈值化的参数c
cvui::printf(windowMat, width * 2 + 100, 90 + height * 0, "c");
cvui::trackbar(windowMat, width * 2 + 100, 100 + height * 0, 250, &c, -100, 100);
cv::Mat tempMat;
cvui::printf(windowMat, width * 0 + 0, 0 + height * 1, "ADAPTIVE_THRESH_MEAN_C");
cvui::printf(windowMat, width * 0 + 0, 15 + height * 1, "THRESH_BINARY");
cvui::printf(windowMat, width * 0 + 0, 30 + height * 1, "maxValue = %d", maxValue);
cvui::printf(windowMat, width * 0 + 0, 45 + height * 1, "blockSize = %d", blockSize * 2 + 1);
cvui::printf(windowMat, width * 0 + 0, 60 + height * 1, "c = %d", c);
tempMat = windowMat(cv::Range(srcMat.rows * 1 + 70, srcMat.rows * 2 - 10),
cv::Range(srcMat.cols * 0 + 20, srcMat.cols * 1 - 20));
cv::addWeighted(tempMat, 0.0f, thresh1Mat, 1.0f, 0.0f, tempMat);
cvui::printf(windowMat, width * 1 + 0, 0 + height * 1, "ADAPTIVE_THRESH_MEAN_C");
cvui::printf(windowMat, width * 1 + 0, 15 + height * 1, "THRESH_BINARY_INV");
cvui::printf(windowMat, width * 1 + 0, 30 + height * 1, "maxValue = %d", maxValue);
cvui::printf(windowMat, width * 1 + 0, 45 + height * 1, "blockSize = %d", blockSize * 2 + 1);
cvui::printf(windowMat, width * 1 + 0, 60 + height * 1, "c = %d", c);
tempMat = windowMat(cv::Range(srcMat.rows * 1 + 70, srcMat.rows * 2 - 10),
cv::Range(srcMat.cols * 1 + 20, srcMat.cols * 2 - 20));
cv::addWeighted(tempMat, 0.0f, thresh2Mat, 1.0f, 0.0f, tempMat);
cvui::printf(windowMat, width * 2 + 0, 0 + height * 1, "ADAPTIVE_THRESH_GAUSSIAN_C");
cvui::printf(windowMat, width * 2 + 0, 15 + height * 1, "THRESH_BINARY");
cvui::printf(windowMat, width * 2 + 0, 30 + height * 1, "maxValue = %d", maxValue);
cvui::printf(windowMat, width * 2 + 0, 45 + height * 1, "blockSize = %d", blockSize * 2 + 1);
cvui::printf(windowMat, width * 2 + 0, 60 + height * 1, "c = %d", c);
tempMat = windowMat(cv::Range(srcMat.rows * 1 + 70, srcMat.rows * 2 - 10),
cv::Range(srcMat.cols * 2 + 20, srcMat.cols * 3 - 20));
cv::addWeighted(tempMat, 0.0f, thresh1Mat, 1.0f, 0.0f, tempMat);
cvui::printf(windowMat, width * 3 + 0, 0 + height * 1, "ADAPTIVE_THRESH_GAUSSIAN_C");
cvui::printf(windowMat, width * 3 + 0, 15 + height * 1, "THRESH_BINARY_INV");
cvui::printf(windowMat, width * 3 + 0, 30 + height * 1, "maxValue = %d", maxValue);
cvui::printf(windowMat, width * 3 + 0, 45 + height * 1, "blockSize = %d", blockSize * 2 + 1);
cvui::printf(windowMat, width * 3 + 0, 60 + height * 1, "c = %d", c);
tempMat = windowMat(cv::Range(srcMat.rows * 1 + 70, srcMat.rows * 2 - 10),
cv::Range(srcMat.cols * 3 + 20, srcMat.cols * 4 - 20));
cv::addWeighted(tempMat, 0.0f, thresh2Mat, 1.0f, 0.0f, tempMat);
// 转换成灰度图像
cv::Mat grayMat; // 多通道
cv::Mat grayMat2; // 单通道
#if 1
// CV_XXXX 与 cv::COLOR_BGR2GRAY 实际并没有区别 是高低版本表现形式的问题
cv::cvtColor(srcMat, grayMat2, CV_BGR2GRAY);
cv::cvtColor(grayMat2, grayMat, CV_GRAY2BGR);
#else
cv::cvtColor(srcMat, grayMat2, cv::COLOR_BGR2GRAY);
cv::cvtColor(grayMat2, grayMat, cv::COLOR_GRAY2BGR);
#endif
// 效果图copy
cv::Mat rightMat = windowMat(cv::Range(srcMat.rows * 0, srcMat.rows * 1),
cv::Range(srcMat.cols * 1, srcMat.cols * 2));
cv::addWeighted(rightMat, 0.0f, grayMat, 1.0f, 0.0f, rightMat);
{
// 自适应阈值
cv::adaptiveThreshold(grayMat2,
dstMat,
maxValue,
cv::ADAPTIVE_THRESH_MEAN_C,
cv::THRESH_BINARY,
blockSize * 2 + 1,
c);
cv::cvtColor(dstMat, dstMat, CV_GRAY2BGR);
// 效果图copy
cv::Mat center = windowMat(cv::Range(srcMat.rows * 2, srcMat.rows * 3),
cv::Range(srcMat.cols * 0, srcMat.cols * 1));
cv::addWeighted(center, 0.0f, dstMat, 1.0f, 0.0f, center);
// 自适应阈值
cv::adaptiveThreshold(grayMat2,
dstMat,
maxValue,
cv::ADAPTIVE_THRESH_MEAN_C,
cv::THRESH_BINARY_INV,
blockSize * 2 + 1,
c);
cv::cvtColor(dstMat, dstMat, CV_GRAY2BGR);
// 效果图copy
cv::Mat center2 = windowMat(cv::Range(srcMat.rows * 2, srcMat.rows * 3),
cv::Range(srcMat.cols * 1, srcMat.cols * 2));
cv::addWeighted(center2, 0.0f, dstMat, 1.0f, 0.0f, center2);
// 自适应阈值
cv::adaptiveThreshold(grayMat2,
dstMat,
maxValue,
cv::ADAPTIVE_THRESH_GAUSSIAN_C,
cv::THRESH_BINARY,
blockSize * 2 + 1,
c);
cv::cvtColor(dstMat, dstMat, CV_GRAY2BGR);
// 效果图copy
cv::Mat center3 = windowMat(cv::Range(srcMat.rows * 2, srcMat.rows * 3),
cv::Range(srcMat.cols * 2, srcMat.cols * 3));
cv::addWeighted(center3, 0.0f, dstMat, 1.0f, 0.0f, center3);
// 自适应阈值
cv::adaptiveThreshold(grayMat2,
dstMat,
maxValue,
cv::ADAPTIVE_THRESH_GAUSSIAN_C,
cv::THRESH_BINARY_INV,
blockSize * 2 + 1,
c);
cv::cvtColor(dstMat, dstMat, CV_GRAY2BGR);
// 效果图copy
cv::Mat center4 = windowMat(cv::Range(srcMat.rows * 2, srcMat.rows * 3),
cv::Range(srcMat.cols * 3, srcMat.cols * 4));
cv::addWeighted(center4, 0.0f, dstMat, 1.0f, 0.0f, center4);
}
// 更新
cvui::update();
// 显示
cv::imshow(windowName, windowMat);
// esc键退出
if(cv::waitKey(25) == 27)
{
break;
}
}
}
对应版本号v1.24.0
上一篇:《OpenCV开发笔记(二十八):带你学习图像识别之阈值化》
下一篇:《OpenCV开发笔记(三十):带你学习图像识别之经典OTSU算法阈值化》
原博主博客地址:https://blog.csdn.net/qq21497936
原博主博客导航:https://blog.csdn.net/qq21497936/article/details/102478062
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/104811545