opencv 膨胀_OpenCV之图像形态学 - 膨胀与腐蚀2

python代码:

import cv2 as cv
import numpy as np

src = cv.imread("./test.png")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)

# 二值化图像
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow("input", binary)

# 使用3x3结构元素进行膨胀与腐蚀操作
se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3), (-1, -1))
dilate = cv.dilate(binary, se, None, (-1, -1), 1)
erode = cv.erode(binary, se, None, (-1, -1), 1)

# 显示
cv.imshow("dilate", dilate)
cv.imshow("erode", erode)
cv.imwrite("./dilate.png", dilate)
cv.imwrite("./erode.png", erode)
cv.waitKey(0)
cv.destroyAllWindows()

6fa3f56663d58f2bdda786c3c48936c3.png

C++代码:

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <math.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
	Mat dresult, eresult;
	Mat src = imread("./test.png");

	// ��ֵͼ��
	Mat gray, binary;
	cvtColor(src, gray, COLOR_BGR2GRAY);
	threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
	imshow("binary", binary);

	// ����ṹԪ�� 3x3��С����
	Mat se = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
	// ����
	dilate(binary, dresult, se, Point(-1, -1), 1, 0);
	// ��ʴ
	erode(binary, eresult, se, Point(-1, -1), 1, 0);

	// ��ʾ
	imshow("dilate", dresult);
	imshow("erode", eresult);
	waitKey(0);
	return 0;
}

93690b8277c23289d858a0c5895db25c.png

OpenCV学习笔记代码,欢迎follow:

MachineLP/OpenCV-​github.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值