2

我想使用 opencv Canny 边缘检测来裁剪图像以及边缘。但是我已经写下了边缘检测,但仍然不知道如何将图像与边缘一起裁剪。 在此处输入图像描述

import cv2
import  numpy as np
image = cv2.imread("test.png")

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # convert image to gray

blur_image = cv2.GaussianBlur(gray, (5, 5), 0)  

edged = cv2.Canny(blur_image, 180, 900) 

# cv2.imshow("Image", edged)
# cv2.waitKey(0)

# applying close function

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 10))
# opened = cv2.morphologyEx(edged, cv2.MORPH_OPEN, kernel)
close = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)

# cv2.imshow("Closed", close)
# cv2.waitKey(0)
_,contours, hierarchy = cv2.findContours( close.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# work files
# cv2.drawContours(image,contours,-1,(0,0,255),3)
# 
# cv2.imshow("result", image)

# mask = np.zeros(image.shape,dtype=np.uint8)
#mask = np.ones((image.height,image.width),dtype=np.uint8)

#m#ask[:,:]= 0

# croped = np.zeros()
cv2.drawContours(image,contours,-1,(0,0,255),3)

cv2.imshow("result", image)
cv2.waitKey(0)

在此处输入图像描述

我只想裁剪图像以及边缘的红色。

4

0 回答 0