如何使用OpenCV-Python提取图像中的红框目标区域
图像处理新手求助:提取图像红框区域技术建议
我是图像处理新手,已经完成图像读取、灰度化(graying)、二值化(binarization)、Canny边缘检测、形态学处理(morphological processing)等基础操作,现寻求提取图像中红框区域的技术建议。
相关处理效果图如下:
- 红框目标图
- 原图(original)
- 灰度图(graying)
- 二值图(binarization)
- Canny边缘检测图
- 形态学处理图
已编写的代码如下:
import cv2 import numpy as np in_file = r"D:test_cut_2.jpg" image = cv2.imdecode(np.fromfile(in_file, dtype=np.uint8), -1) # graying gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Canny edge detection edges = cv2.Canny(gray, 100, 200) # binarization _, binary_image = cv2.threshold(gray, 125, 255, cv2.THRESH_BINARY) # morphological processing kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) dilated = cv2.dilate(binary_image, kernel, iterations=2) dst = cv2.erode(binary_image, kernel, iterations=2) cv2.imshow('Original Photo', image) cv2.imshow('gray photo', gray) cv2.imshow('Binary Image', binary_image) cv2.imshow('edges', edges) cv2.imshow('dst', dst) cv2.imshow('dilated', dilated) cv2.waitKey(0) cv2.destroyAllWindows()
内容的提问来源于stack exchange,提问作者陈大光
相关产品推荐
相关产品推荐

