如何使用python-opencv裁剪图像和边缘

时间:2017-11-02 14:50:58

标签: python image opencv canny-operator

我想使用opencv Canny边缘检测来裁剪图像和边缘。但我已经写下了边缘检测,但仍然不知道如何 裁剪图像和边缘。 enter image description here

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)

enter image description here

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

0 个答案:

没有答案