我想裁剪图片上给出的手机屏幕。我试过这段代码但结果不是我想要的。 的 Phone Screen
import cv2
import numpy as np
img = cv2.imread('phone_test.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_,thresh = cv2.threshold(gray,1,255,cv2.THRESH_BINARY)
_, contours, _= cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
x,y,w,h = cv2.boundingRect(contours[0])
crop = img[y:y+h,x:x+w]
cv2.imwrite('phone_test_crop.jpg',crop)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image',img) #show the image
cv2.waitKey(0)
结果就是这样 的 Crop Result
任何解决方案?
答案 0 :(得分:1)
好的,这是我为我制作和制作的代码。
import cv2
import numpy as np
img = cv2.imread('phone.jpg')
# cv2.imshow('image', img)
# cv2.waitKey(0)
# Convert BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower = np.array([20, 20, 20])
upper = np.array([220, 220, 220])
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower, upper)
# cv2.imshow('mask', mask)
# cv2.waitKey(0)
kernel = np.ones((1, 10), np.uint8)
img_dilation = cv2.dilate(mask, kernel, iterations=1)
# cv2.imshow('dilated', img_dilation)
# cv2.waitKey(0)
# find contours
im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
for i, ctr in enumerate(sorted_ctrs):
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)
# Getting ROI
roi = img[y:y + h, x:x + w]
# show ROI
# cv2.imshow('segment no:'+str(i), roi)
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 1) # put (0,255,0) to see green rectangles
# cv2.waitKey(0)
if h > 100 and 350 < w < 450:
# cv2.imshow('marked areas', img)
# cv2.waitKey(0)
# cv2.imshow('roi', roi)
# cv2.waitKey(0)
cv2.imwrite('extracted_screen.png', roi)
由您来调整代码以满足特定需求(例如:在屏幕提取上或多或少的精度)。
<强>结果强>
答案 1 :(得分:1)
这是我解决问题的方法。
import cv2
import numpy as np
# read and scale down image
img = cv2.pyrDown(cv2.imread('sample_images/phone_test.jpg',
cv2.IMREAD_UNCHANGED))
# threshold image
ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
150, 255, cv2.THRESH_BINARY)
# find contours and get the external one
image, contours, hier = cv2.findContours(threshed_img,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
# get the bounding rect
x, y, w, h = cv2.boundingRect(c)
crop = img[y:y+h+22,x:x+w+5] #give a bit of space, because the boundingRect is not perfect
cv2.imwrite('sample_images/phone_test_crop.jpg', crop)
cv2.destroyAllWindows()
在: Before crop
在: After crop