2

I want to use opencv Canny edge detection to crop the image along with the edge. But I have wrote down the edge detection, but still don't know how to crop the image along with the edge. 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

I just want to crop the image along with the red color of the edge.

nooper
  • 691
  • 1
  • 9
  • 25
  • Possible duplicate of [How to crop the internal area of a contour?](https://stackoverflow.com/questions/28759253/how-to-crop-the-internal-area-of-a-contour) – GPPK Nov 02 '17 at 14:51
  • You don't want to *crop*, you actually want to *mask*. Hopefully that terminology should give you some good search results :) – alkasm Nov 02 '17 at 16:07
  • if you can remove the small noise contours, you can use all points from all contours to compute a boundingRect and use that for cropping. – Micka Nov 02 '17 at 19:41

0 Answers0