Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.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 如何使用open cv从图像中获取最外层的标志(最外层的框)?_Python_Opencv - Fatal编程技术网

Python 如何使用open cv从图像中获取最外层的标志(最外层的框)?

Python 如何使用open cv从图像中获取最外层的标志(最外层的框)?,python,opencv,Python,Opencv,所以我试图从这张照片中提取盒子。根据,RETR\u EXTERNAL应返回“仅极端外部标志”。他们的话不是我说的。根据他们的说法,他们只返回每个家庭中的长辈。因此,我假设使用RETR\u EXTERNAL可以提取参考图像中的表格和按钮。但是当我使用RETR\u EXTERNAL时,它只生成参考图像(我首先链接的图像)作为输出。就好像整个图像周围有一个看不见的盒子。 谢谢你的帮助 以下是您需要的代码: import cv2 import numpy as np import argparse i

所以我试图从这张照片中提取盒子。根据,
RETR\u EXTERNAL
应返回“仅极端外部标志”。他们的话不是我说的。根据他们的说法,他们只返回每个家庭中的长辈。因此,我假设使用
RETR\u EXTERNAL
可以提取参考图像中的表格和按钮。但是当我使用
RETR\u EXTERNAL
时,它只生成参考图像(我首先链接的图像)作为输出。就好像整个图像周围有一个看不见的盒子。
谢谢你的帮助


以下是您需要的代码:

import cv2
import numpy as np
import argparse
import imutils
import nn 
from PIL import Image, ImageFont, ImageDraw, ImageEnhance
def sort_contours(cnts, method="left-to-right"):
    # initialize the reverse flag and sort index
    reverse = False
    i = 0

    # handle if we need to sort in reverse
    if method == "right-to-left" or method == "bottom-to-top":
        reverse = True

    # handle if we are sorting against the y-coordinate rather than
    # the x-coordinate of the bounding box
    if method == "top-to-bottom" or method == "bottom-to-top":
        i = 1

    # construct the list of bounding boxes and sort them from top to
    # bottom
    boundingBoxes = [cv2.boundingRect(c) for c in cnts]
    (cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
        key=lambda b:b[1][i], reverse=reverse))

    # return the list of sorted contours and bounding boxes
    return (cnts, boundingBoxes)
def box_extraction(img_for_box_extraction_path, cropped_dir_path):
    # Read the image
    img = cv2.imread('41.jpg', 0)
    (thresh, img_bin) = cv2.threshold(img, 128, 255,cv2.THRESH_BINARY|cv2.THRESH_OTSU)
    #print len(img_bin)

    img_bin =255-img_bin 
    cv2.imwrite("Image_bin.jpg",img_bin)
    # Defining a kernel length
    kernel_length = np.array(img).shape[1]/80
    #print kernel_length
    verticle_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_length))

    # A horizontal kernel of (kernel_length X 1), which will help to detect all the horizontal line from the image.
    hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1))

    # A kernel of (3 X 3) ones.
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))

    # Morphological operation to detect vertical lines from an image
    img_temp1 = cv2.erode(img_bin, verticle_kernel, iterations=7)
    verticle_lines_img = cv2.dilate(img_temp1, verticle_kernel, iterations=7)
    cv2.imwrite("verticle_lines.jpg",verticle_lines_img)
    # Morphological operation to detect horizontal lines from an image
    img_temp2 = cv2.erode(img_bin, hori_kernel, iterations=7)
    horizontal_lines_img = cv2.dilate(img_temp2, hori_kernel, iterations=7)
    cv2.imwrite("horizontal_lines.jpg",horizontal_lines_img)

    # Weighting parameters, this will decide the quantity of an image to be added to make a new image.
    alpha = 0.6
    beta = 1.0 - alpha


    # This function helps to add two image with specific weight parameter to get a third image as summation of two image.
    img_final_bin = cv2.addWeighted(verticle_lines_img, alpha, horizontal_lines_img, beta, 0.0)
    img_final_bin = cv2.erode(~img_final_bin, kernel, iterations=2)
    (thresh, img_final_bin) = cv2.threshold(img_final_bin, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)



    # Find contours for image, which will detect all the boxes
    im2, contours, hierarchy = cv2.findContours(img_final_bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    # Sort all the contours by top to bottom.
    #(contours, boundingBoxes) = sort_contours(contours, method="top-to-bottom")
从:

在OpenCV中,查找轮廓就像从黑色背景中查找白色对象。所以请记住,要找到的对象应该是白色的,背景应该是黑色的

findContours首先搜索白色对象。因此,最外面的轮廓是白色背景。通过使用
img=cv2.bitwise\u not(img)
或阈值化时使用
cv2.THRESH\u BINARY\u INV
反转图像,可以轻松解决此问题:

结果:

示例代码:

    import numpy as np 
    import cv2
    #load the image:  
    img = cv2.imread("box.jpg") 
    # create grayscale
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    # threshold image to remove noise and create an inverted mask
    ret,mask = cv2.threshold(gray,230,255,cv2.THRESH_BINARY_INV)
    #Find contours (external only):  
    im, contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  
    #draw contours on original image
    cv2.drawContours(img, contours, -1, (0,0,255), thickness=2)
    # show image
    cv2.imshow("Image", img)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
注意:您也可以将图像加载为灰度并跳过创建,但我在这里使用了它,以便可以绘制更明显的红色框。

一个适用于我的J.D.更新代码
表演,plz,img_final_bin。
import numpy as np 
import cv2

img = cv2.imread("image_1.jpg") 

## create grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

## threshold image to remove noise and create an inverted mask with with OTSU
mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

## Find contours (external only):  
## Since the cv2.findContours has been updated to return only 2 parameters
contours, hierarchy= cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 

## draw contours on original image
cv2.drawContours(img, contours, -1, (255,0,0), thickness=2)

## show image
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()