图像二值化和灰度形态学与操作

本文介绍了OpenCV中灰度腐蚀与膨胀的基本概念,通过示例代码展示了如何使用腐蚀和膨胀操作进行图像处理,并详细讲解了阈值操作和结构元素的选择。同时涵盖了阈值化函数如cv::threshold()的使用方法及其不同模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

灰度腐蚀与膨胀

示例代码


	createTrackbar("Element Size :", OUTPUT_WIN, &element_size, max_size, CallBack_Demo);//滑动条,参数1滑动条名字,参数2传入的窗口名字,参数4滑动条初始值,参数5滑动条最大值
	CallBack_Demo(0, 0);

void CallBack_Demo(int, void*) {
	int s = element_size * 2 + 1;
	Mat structureElement = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1));
	// dilate(src, dst, structureElement, Point(-1, -1), 1);灰度膨胀
	erode(src, dst, structureElement);//灰度腐蚀
	imshow(OUTPUT_WIN, dst);
	return;
}

形态学操作

Mat kernel = getStructuringElement(MORPH_RECT, Size(11, 11), Point(-1, -1));//定义结构元素
morphologyEx(src, dst, CV_MOP_BLACKHAT, kernel);//黑帽操作

阈值操作

threshold binary

                 大于阈值线的赋值为255,小于的赋值为0               

threshold binary Inverted

                   大于阈值线的赋值为0,小于的赋值为255              

truncate

                    大于阈值线的直接赋值为阈值线大小                     

threshold to zero

                   小于阈值线的赋值为0,大于的保留原来值              

threshold to zero inverted

                大于阈值线的赋值为0,小于的保留原来的值               

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace std;
using namespace cv;
Mat src, gray_src, dst;
int threshold_value = 127;
int threshold_max = 255;
int type_value = 2;
int type_max = 4;
void Threshold_Demo(int, void*);
int main(int argc, char** argv) {	
	src = imread("22.jpg");
	if (!src.data) {
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", src);
	namedWindow("output_title", CV_WINDOW_AUTOSIZE);
	//参数1:滑块名 参数2:窗口  
	//参数3:滑块初始值  参数4:滑块最大值 
	//最后一个参数是函数,意思就是滑块变动,就调用该函数一次
	createTrackbar("Threshold Value:", "output_title", &threshold_value, threshold_max, Threshold_Demo);
	createTrackbar("Type Value:", "output_title", &type_value, type_max, Threshold_Demo);//切换阈值的模式,总共5种
	Threshold_Demo(0, 0);

	waitKey(0);
	return 0;
}

void Threshold_Demo(int, void*) {
	cvtColor(src, gray_src, CV_BGR2GRAY); //灰度化
	threshold(gray_src, dst, threshold_value, 255, type_value);
	imshow("output_title", dst);
}

参考文献

【OpenCV3】阈值化操作——cv::threshold()与cv::adaptiveThreshold()详解

 

出现这个错误的原因是在导入seaborn包时,无法从typing模块中导入名为'Protocol'的对象。 解决这个问题的方法有以下几种: 1. 检查你的Python版本是否符合seaborn包的要求,如果不符合,尝试更新Python版本。 2. 检查你的环境中是否安装了typing_extensions包,如果没有安装,可以使用以下命令安装:pip install typing_extensions。 3. 如果你使用的是Python 3.8版本以下的版本,你可以尝试使用typing_extensions包来代替typing模块来解决该问题。 4. 检查你的代码是否正确导入了seaborn包,并且没有其他导入错误。 5. 如果以上方法都无法解决问题,可以尝试在你的代码中使用其他的可替代包或者更新seaborn包的版本来解决该问题。 总结: 出现ImportError: cannot import name 'Protocol' from 'typing'错误的原因可能是由于Python版本不兼容、缺少typing_extensions包或者导入错误等原因造成的。可以根据具体情况尝试上述方法来解决该问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [ImportError: cannot import name ‘Literal‘ from ‘typing‘ (D:\Anaconda\envs\tensorflow\lib\typing....](https://blog.csdn.net/yuhaix/article/details/124528628)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪猪派对

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值