Opencv学习----图像处理-图像过滤-cv::dilate

6.1.5 cv:: dilate

6.1.5.1 Opencv接口

void cv::dilate(InputArray src,
		OutputArray dst,
		InputArray kernel,
		Point anchor = Point(-1,-1),
		int 	iterations = 1,
		int 	borderType = BORDER_CONSTANT,
		const Scalar& 	borderValue = morphologyDefaultBorderValue() 
	)		
参数
src	输入图像; 通道数可以是任意的,但深度应该是CV_8U,CV_16U,CV_16S,CV_32F或CV_64F之一。
dst	输出与src相同大小和类型的图像。
kernel	结构元素用于扩张; 如果elemenat = Mat(),则使用3 x 3矩形结构元素。可以使用getStructuringElement创建内核
anchor	锚点在元素内的位置; 默认值(-1,-1)表示锚点位于元素中心。
iterations	应用扩张的次数。
borderType	像素外推法
borderValue	边界不变的边界值

通过使用特定的结构元素来扩展图像。

void ES::ImageProcessing::dilateOper(cv::Mat* dst)
{
	Mat src = imread("lena.jpg", IMREAD_COLOR);
	cv::resize(src, src, Size(src.rows / 4 * 3, src.cols / 4 * 3));
	ImageProcessingParams* img_params = static_cast<ImageProcessingParams*>(m_params);
	int type = img_params->m_morphShape;
	int dim = img_params->m_kernelDim;
	Mat kernel = getStructuringElement(type, Size(dim, dim));
	Mat mat;
	cv::dilate(src, mat, kernel);
	Mat mergeMat(src.rows, src.cols + mat.cols, src.type());
	Mat submat = mergeMat.colRange(0, src.cols);
	src.copyTo(submat);
	submat = mergeMat.colRange(src.cols, src.cols + mat.cols);
	mat.copyTo(submat);
	mergeMat.copyTo(*dst);
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用 OpenCV-Python 识别图片中石头剪刀布手势的代码: ```python import cv2 import numpy as np # 定义手势识别函数 def recognize_gesture(hand): # 将手势图像转换为灰度图像 gray = cv2.cvtColor(hand, cv2.COLOR_BGR2GRAY) # 对灰度图像进行高斯模糊 blur = cv2.GaussianBlur(gray, (5, 5), ) # 进行二值化处理 ret, thresh = cv2.threshold(blur, , 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) # 查找轮廓 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 如果没有找到轮廓,则返回 None if len(contours) == : return None # 找到最大的轮廓 max_contour = max(contours, key=cv2.contourArea) # 计算轮廓的凸包 hull = cv2.convexHull(max_contour) # 计算轮廓的缺陷 defects = cv2.convexityDefects(max_contour, cv2.convexHull(max_contour, returnPoints=False)) # 如果没有找到缺陷,则返回 None if defects is None: return None # 计算缺陷的数量 num_defects = for i in range(defects.shape[]): s, e, f, d = defects[i, ] start = tuple(max_contour[s][]) end = tuple(max_contour[e][]) far = tuple(max_contour[f][]) # 计算缺陷的角度 angle = np.degrees(np.arctan2(far[1]-start[1], far[]-start[]) - np.arctan2(end[1]-start[1], end[]-start[])) # 如果角度小于 90 度,并且距离大于 30 像素,则认为是一个缺陷 if angle < 90 and d > 30: num_defects += 1 # 如果缺陷的数量为 ,则认为是石头 if num_defects == : return "rock" # 如果缺陷的数量为 1 或 2,则认为是剪刀 elif num_defects == 1 or num_defects == 2: return "scissors" # 如果缺陷的数量大于 2,则认为是布 else: return "paper" # 加载图像 img = cv2.imread("gesture.jpg") # 调整图像大小 img = cv2.resize(img, (640, 480)) # 获取图像的高度和宽度 height, width = img.shape[:2] # 将图像转换为 HSV 颜色空间 hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # 定义 HSV 颜色范围 lower_skin = np.array([, 20, 70], dtype=np.uint8) upper_skin = np.array([20, 255, 255], dtype=np.uint8) # 对图像进行颜色过滤 mask = cv2.inRange(hsv, lower_skin, upper_skin) # 对图像进行形态学操作 kernel = np.ones((5, 5), np.uint8) mask = cv2.erode(mask, kernel, iterations=1) mask = cv2.dilate(mask, kernel, iterations=1) # 查找轮廓 contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 如果没有找到轮廓,则返回 None if len(contours) == : print("No hand detected") else: # 找到最大的轮廓 max_contour = max(contours, key=cv2.contourArea) # 计算轮廓的凸包 hull = cv2.convexHull(max_contour) # 绘制凸包 cv2.drawContours(img, [hull], -1, (, 255, ), 2) # 获取凸包的矩形 x, y, w, h = cv2.boundingRect(hull) # 绘制矩形 cv2.rectangle(img, (x, y), (x+w, y+h), (, , 255), 2) # 获取手势图像 hand = img[y:y+h, x:x+w] # 如果手势图像的高度或宽度小于 50 像素,则认为手势不明确 if hand.shape[] < 50 or hand.shape[1] < 50: print("Gesture not clear") else: # 调用手势识别函数 gesture = recognize_gesture(hand) # 如果识别结果为 None,则认为手势不明确 if gesture is None: print("Gesture not clear") else: # 输出识别结果 print("Gesture:", gesture) # 显示图像 cv2.imshow("Image", img) cv2.waitKey() cv2.destroyAllWindows() ``` 注意:以上代码仅供参考,实际应用中可能需要根据具体情况进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dylan55_you

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

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

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

打赏作者

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

抵扣说明:

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

余额充值