在 OpenCV 中从 ROI 裁剪矩形扩展
Cropping rectangular extensions from ROI in OpenCV
我有这张图片:
我想从整个 ROI 中提取下面突出显示的矩形扩展。
这是一个简单的方法,使用 morphological operations + cv2.contourArea()
的轮廓过滤
Otsu 的阈值->
形态闭合+反转->
结果
代码
import cv2
# Load image, convert to grayscale, Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Create kernel and morph close
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
close = 255 - cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=5)
# Find contours and filter using contour area
cnts = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
area = cv2.contourArea(c)
if area > 100 and area < 25000:
cv2.drawContours(image, [c], -1, (36,255,12), 4)
cv2.imshow('thresh', thresh)
cv2.imshow('close', close)
cv2.imshow('image', image)
cv2.waitKey()
我有这张图片:
我想从整个 ROI 中提取下面突出显示的矩形扩展。
这是一个简单的方法,使用 morphological operations + cv2.contourArea()
Otsu 的阈值->
形态闭合+反转->
结果
代码
import cv2
# Load image, convert to grayscale, Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Create kernel and morph close
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
close = 255 - cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=5)
# Find contours and filter using contour area
cnts = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
area = cv2.contourArea(c)
if area > 100 and area < 25000:
cv2.drawContours(image, [c], -1, (36,255,12), 4)
cv2.imshow('thresh', thresh)
cv2.imshow('close', close)
cv2.imshow('image', image)
cv2.waitKey()