Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用轮廓裁剪手机屏幕_Python_Opencv_Contour - Fatal编程技术网

Python 使用轮廓裁剪手机屏幕

Python 使用轮廓裁剪手机屏幕,python,opencv,contour,Python,Opencv,Contour,我想裁剪图片上给出的手机屏幕。我尝试了这个代码,但结果不是我想要的。 结果就是这样 有什么解决方案吗?好的,这是我为自己编写和工作的代码 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) # defi

我想裁剪图片上给出的手机屏幕。我尝试了这个代码,但结果不是我想要的。

结果就是这样


有什么解决方案吗?

好的,这是我为自己编写和工作的代码

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)
导入cv2
将numpy作为np导入
img=cv2.imread('phone.jpg')
#cv2.imshow(“图像”,img)
#cv2.等待键(0)
#将BGR转换为HSV
hsv=cv2.CVT颜色(img,cv2.COLOR\U BGR2HSV)
#定义HSV中蓝色的范围
lower=np.数组([20,20,20])
上限=np.数组([220,220,220])
#设置HSV图像的阈值以仅获取蓝色
遮罩=cv2.inRange(hsv,下部,上部)
#cv2.imshow(“面具”,面具)
#cv2.等待键(0)
内核=np.ones((1,10),np.uint8)
img_displate=cv2.displate(掩码、内核、迭代次数=1)
#cv2.imshow(“扩张”,img\U扩张)
#cv2.等待键(0)
#寻找轮廓
im2,CTR,hier=cv2.findContours(img_dictional.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_近似简单)
#等高线排序
排序的\u ctrs=sorted(ctrs,key=lambda ctr:cv2.boundingRect(ctr)[0])
对于i,枚举中的ctr(排序的ctr):
#获取边界框
x、 y,w,h=cv2.boundingRect(ctr)
#获得投资回报
roi=img[y:y+h,x:x+w]
#显示投资回报率
#cv2.imshow(‘段号:’+str(一),投资回报率)
cv2.矩形(img,(x,y),(x+w,y+h),(0,0,0),1)#放置(0255,0)以查看绿色矩形
#cv2.等待键(0)
如果h>100且350
取决于您调整代码以满足特定需求(例如:屏幕提取的精度或多或少)

结果


好的,这是我为自己编写和工作的代码

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)
导入cv2
将numpy作为np导入
img=cv2.imread('phone.jpg')
#cv2.imshow(“图像”,img)
#cv2.等待键(0)
#将BGR转换为HSV
hsv=cv2.CVT颜色(img,cv2.COLOR\U BGR2HSV)
#定义HSV中蓝色的范围
lower=np.数组([20,20,20])
上限=np.数组([220,220,220])
#设置HSV图像的阈值以仅获取蓝色
遮罩=cv2.inRange(hsv,下部,上部)
#cv2.imshow(“面具”,面具)
#cv2.等待键(0)
内核=np.ones((1,10),np.uint8)
img_displate=cv2.displate(掩码、内核、迭代次数=1)
#cv2.imshow(“扩张”,img\U扩张)
#cv2.等待键(0)
#寻找轮廓
im2,CTR,hier=cv2.findContours(img_dictional.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_近似简单)
#等高线排序
排序的\u ctrs=sorted(ctrs,key=lambda ctr:cv2.boundingRect(ctr)[0])
对于i,枚举中的ctr(排序的ctr):
#获取边界框
x、 y,w,h=cv2.boundingRect(ctr)
#获得投资回报
roi=img[y:y+h,x:x+w]
#显示投资回报率
#cv2.imshow(‘段号:’+str(一),投资回报率)
cv2.矩形(img,(x,y),(x+w,y+h),(0,0,0),1)#放置(0255,0)以查看绿色矩形
#cv2.等待键(0)
如果h>100且350
取决于您调整代码以满足特定需求(例如:屏幕提取的精度或多或少)

结果


这是我解决这个问题的方法

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()
之前:

之后:
这是我解决这个问题的方法

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()
之前:

之后:

好吧,你基本上是说,轮廓算法中应该使用任何非纯黑色的东西(在jpg中,你会对结果感到惊讶:))。你看过你的
thresh
图像了吗?您可能需要更高的值来将屏幕与其他所有内容隔离开来。您还可以获得手机的边框,这样您就可以获得多个轮廓。。。。您只检查轮廓编号0。。。你甚至可以检查这是否是你想要使用的正确轮廓。好吧,你基本上是说任何不是纯黑色的东西(在jpg中,你会对结果感到惊讶:))都应该用在轮廓算法中。你看过你的
thresh
图像了吗?您可能需要更高的值来将屏幕与其他所有内容隔离开来。您还可以获得手机的边框,这样您就可以获得多个轮廓。。。。您只检查轮廓编号0。。。你甚至可以检查这是否是你想要使用的正确轮廓。我尝试了代码,但它不起作用。我找到了另一个解决方案。我尝试了代码,但它不起作用。我找到了另一个解决办法