python-opencv实现检测物体轮廓矩形并扣出

通过蓝色的阈值,去除背景,找出轮廓,并将图片扣出。

其余颜色阈值:

python-opencv实现检测物体轮廓矩形并扣出_第1张图片

import cv2
import numpy as np
img = cv2.imread("ce.jpg")
# 检测蓝色的阈值
lower_blue=np.array([78,43,46])
upper_blue=np.array([110,255,255])

# change to hsv model
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# get mask
mask = cv2.inRange(hsv, lower_blue, upper_blue)

# detect blue
image = cv2.bitwise_and(img, img, mask=mask)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gradX = cv2.Sobel(gray, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1)
gradY = cv2.Sobel(gray, ddepth=cv2.CV_32F, dx=0, dy=1, ksize=-1)

# subtract the y-gradient from the x-gradient
gradient = cv2.subtract(gradX, gradY)
gradient = cv2.convertScaleAbs(gradient)
# blur and threshold the image
blurred = cv2.blur(gradient, (9, 9))
(_, thresh) = cv2.threshold(blurred, 90, 255, cv2.THRESH_BINARY)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 25))
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
# perform a series of erosions and dilations
closed = cv2.erode(closed, None, iterations=4)
closed = cv2.dilate(closed, None, iterations=4)
(cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
c = sorted(cnts, key=cv2.contourArea, reverse=True)[0]

# compute the rotated bounding box of the largest contour
rect = cv2.minAreaRect(c)
box = np.int0(cv2.boxPoints(rect))

# draw a bounding box arounded the detected barcode and display the image
cv2.drawContours(image, [box], -1, (0, 255, 0), 3)
#cv2.imshow("Image", image)
cv2.imwrite("contoursImage2.png", image)
#cv2.waitKey(0)
Xs = [i[0] for i in box]
Ys = [i[1] for i in box]
x1 = min(Xs)
x2 = max(Xs)
y1 = min(Ys)
y2 = max(Ys)
hight = y2 - y1
width = x2 - x1
cropImg = img[y1:y1+hight, x1:x1+width]
cv2.imwrite("result.png", cropImg)

 

 

 

参考:https://www.cnblogs.com/6-6-8-8/p/9049849.html

你可能感兴趣的:(Python,opencv,opencv,python,轮廓检测)