当前位置:网站首页>[opencv450] image subtraction, binarization, threshold segmentation
[opencv450] image subtraction, binarization, threshold segmentation
2022-06-23 05:32:00 【Ten year dream Lab】
original image :


Gray image subtraction :

cv::threshold(dif2, threshold1, value, 255, THRESH_BINARY):

cv::threshold(dif2, threshold2, value, 255, THRESH_BINARY_INV):
cv::threshold(dif2, threshold3, value, 255, THRESH_TRUNC):

cv::threshold(dif2, threshold4, value, 255, THRESH_TOZERO):

cv::threshold(dif2, threshold5, value, 255, THRESH_TOZERO_INV):

cv::threshold(dif2, threshold6, value, 255, THRESH_MASK):

cv::threshold(dif2, threshold7, value, 255, THRESH_OTSU):

cv::threshold(dif2, threshold8, value, 255, THRESH_TRIANGLE):

value=75

value = 119

Source code :
#include <opencv2/highgui.hpp>
#include <opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
// Slider name //
const string trackbarname = "expand";
// Window name //
const string winname = "result";
// Maximum //
const int maxNum = 255;
// Default //
int value = 88;
Mat frame1, frame2, gray1, gray2, dif, dif2;
int g_nStructElementSize = 3; // Structural elements ( Kernel matrix ) The size of the //
// Get custom core //
Mat element = getStructuringElement(MORPH_RECT,
Size(2 * g_nStructElementSize + 1, 2 * g_nStructElementSize + 1),
Point(g_nStructElementSize, g_nStructElementSize));
void onExpand(int value, void* p) {
// printf("value: %d\n", value);
//dilate(img, res, element, Point(-1, -1), value);
Mat dif = *(Mat*)p;
dif.copyTo(dif2);
resize(dif2, dif2, Size(478, 400));
imshow("dif", dif2);
//threshold(dif2, dif2, value, 255, cv::THRESH_BINARY);
threshold(dif2, dif2, value, 255, cv::THRESH_BINARY);
cv::Mat threshold1, threshold2, threshold3, threshold4, threshold5, threshold6, threshold7, threshold8;
cv::threshold(dif2, threshold1, value, 255, THRESH_BINARY);
cv::threshold(dif2, threshold2, value, 255, THRESH_BINARY_INV);
cv::threshold(dif2, threshold3, value, 255, THRESH_TRUNC);
cv::threshold(dif2, threshold4, value, 255, THRESH_TOZERO);
cv::threshold(dif2, threshold5, value, 255, THRESH_TOZERO_INV);
cv::threshold(dif2, threshold6, value, 255, THRESH_MASK);
cv::threshold(dif2, threshold7, value, 255, THRESH_OTSU);
cv::threshold(dif2, threshold8, value, 255, THRESH_TRIANGLE);
//cv::imshow("THRESH_BINARY", threshold1);
//cv::imshow("THRESH_BINARY_INV", threshold2);
//cv::imshow("THRESH_TRUNC", threshold3);
//cv::imshow("THRESH_TOZERO", threshold4);
//cv::imshow("THRESH_TOZERO_INV", threshold5);
//cv::imshow("THRESH_MASK", threshold6);
//cv::imshow("THRESH_OTSU", threshold7);
//cv::imshow("THRESH_TRIANGLE", threshold8);
// inflation //
dilate(dif2, dif2, element);
dilate(threshold1, threshold1, element);
dilate(threshold2, threshold2, element);
dilate(threshold3, threshold3, element);
dilate(threshold4, threshold4, element);
dilate(threshold5, threshold5, element);
dilate(threshold6, threshold6, element);
dilate(threshold7, threshold7, element);
dilate(threshold7, threshold7, element);
dilate(threshold8, threshold8, element);
// corrosion //
erode(dif2, dif2, element);
erode(threshold1, threshold1, element);
erode(threshold2, threshold2, element);
erode(threshold3, threshold3, element);
erode(threshold4, threshold4, element);
erode(threshold5, threshold5, element);
erode(threshold6, threshold6, element);
erode(threshold7, threshold7, element);
erode(threshold7, threshold7, element);
erode(threshold8, threshold8, element);
// corrosion //
resize(frame1, frame1, Size(478, 400));
resize(frame2, frame2, Size(478, 400));
// resize(dif2, dif2, Size(478, 400));
imshow("frame1", frame1);
imshow("frame2", frame2);
imshow("result", dif2);
cv::imshow("THRESH_BINARY", threshold1);
cv::imshow("THRESH_BINARY_INV", threshold2);
cv::imshow("THRESH_TRUNC", threshold3);
cv::imshow("THRESH_TOZERO", threshold4);
cv::imshow("THRESH_TOZERO_INV", threshold5);
cv::imshow("THRESH_MASK", threshold6);
cv::imshow("THRESH_OTSU", threshold7);
cv::imshow("THRESH_TRIANGLE", threshold8);
// imshow(winname, res);
}
int main(int argc, char* argv[])
{
frame1 = imread("10.bmp");
frame2 = imread("11.bmp");
cvtColor(frame1, gray1, COLOR_BGR2GRAY);
cvtColor(frame2, gray2, COLOR_BGR2GRAY);
Mat F1frame1, F1frame2;
F1frame1.create(frame1.size(), CV_32FC1);
F1frame2.create(frame2.size(), CV_32FC1);
dif.create(frame1.size(), CV_32FC1);
gray1.convertTo(F1frame1, CV_32FC1);
gray2.convertTo(F1frame2, CV_32FC1);
absdiff(F1frame1, F1frame2, dif);
dif.convertTo(dif, CV_8UC1);
namedWindow(winname);
createTrackbar(trackbarname, winname, &value, maxNum, onExpand, (void*)&dif);
onExpand(value, &dif);
if (waitKey(0) != -1)
{
destroyAllWindows();
}
inflation //
//dilate(dif, dif, element);
corrosion //
//erode(dif, dif, element);
//resize(frame1, frame1, Size(478, 400));
//resize(frame2, frame2, Size(478, 400));
//resize(dif, dif, Size(478, 400));
//imshow("frame1", frame1);
//imshow("frame2", frame2);
//imshow("dif", dif);
//if (waitKey(0) != -1)
//{
// destroyAllWindows();
//}
// waitKey();
return 0;
}
Reference resources :
OpenCV Basics ——threshold Use of functions _ I'm not the headmaster's blog -CSDN Blog _cv::threshold
opencv Slider bar Trackbar Use ( It's comprehensive )_ Redfish blog -CSDN Blog
边栏推荐
- [microservices | Nacos] Nacos realizes data isolation of multi environment and multi tenant
- MCS: continuous random variable - student's t distribution
- Facing new challenges and becoming a better self -- an advanced technology er
- Win11不能录制音频怎么办?Win11无法录入声音的解决方法
- Laravel8 implementation of picture verification code
- MCS:连续随机变量——Student’s t分布
- Today's sleep quality record 80 points
- JDBC入门学习(四)之Druid连接池的使用
- LeetCode-1757. 可回收且低脂的产品_SQL
- Web application security testing guide
猜你喜欢

JDBC introductory learning (II) encapsulation tool class

STC 32比特8051單片機開發實例教程 一 開發環境搭建

(IntelliJ) plug in background image plus

Complete one-time GC process of JVM principle

Introduction to unityshader -- rendering optimization technology in unity (IV)

Event log keyword: eventlogtags logtags

英文字母pc是什么意思,互联网的pc指的是什么

抽奖 ddd 代码

How to conduct exploratory data analysis

MCS: continuous random variable chi square distribution
随机推荐
PHP move_ uploaded_ File failed to upload mobile pictures
Introduction to JDBC (IV) - use of Druid connection pool
第九章 APP项目测试(1)
H5 adaptive full screen
Today's sleep quality record 80 points
Shutdown shutdown command
Qt QWidget嵌套相对位置获取 (qt 画线 嵌套)
弱者易怒如虎,强者平静如水,真正厉害的人早已戒掉了情绪
电脑开机显示器黑屏是什么原因,电脑显示器黑屏怎么办
STM32cube CMSIS_ V2 FreeRTOS queue usage
View of MySQL introductory learning (III)
Laravel8 implementation of picture verification code
Error related to synchronizing domestic AOSP code
Complete one-time GC process of JVM principle
STC 32位8051单片机开发实例教程 一 开发环境搭建
关于信息泄露和防御
数据库连接异常:create connection error, url: jdbc:mysql://ip/数据库名, errorCode 0, state 08S01问题处理
JDBC入门学习(三)之事务回滚功能的实现
WebRTC[47] - WebRTC 保存 YUV 数据的常用方式
Introduction to unityshader -- rendering optimization technology in unity (IV)