久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在不遺漏任何東西的情況下檢測所有矩形框

How to detect all rectangular boxes python opencv without missing anything(如何在不遺漏任何東西的情況下檢測所有矩形框python opencv)
本文介紹了如何在不遺漏任何東西的情況下檢測所有矩形框python opencv的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試從關系數據庫中檢測所有矩形.但是我的腳本沒有檢測到一些盒子.請幫我這樣做.謝謝.

I'm trying to detect all the rectangles from the relational database. But some of the boxes are not being detected by my script. Please help me to do that. Thank you.

圖片:

我的代碼:

#!/usr/bin/python
import cv2
import numpy as np

im = cv2.imread("table.png")

image = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(image,0,255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

edge = cv2.Canny(thresh,30,200)
cont = cv2.findContours(edge,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]

for j,i in enumerate(cont):
   x,y,w,h = cv2.boundingRect(i)

   if (w*h>900):
     cv2.drawContours(image,[i],0,(0,0,255),3)

cv2.imshow("Image",image)

cv2.waitKey(0)  

輸出:

推薦答案

這是一個使用閾值+形態學運算的簡單方法.

Here's an simple approach using thresholding + morphological operations.

  1. 獲取二值圖像.加載圖像,轉換為灰度,然后自適應閾值

  1. Obtain binary image. Load image, convert to grayscale, then adaptive threshold

填充矩形輪廓.查找輪廓并填充輪廓以創建填充矩形塊.

Fill rectangular contours. Find contours and fill the contours to create filled rectangular blocks.

執行 morph open.我們創建一個矩形結構元素并 morph open 以移除線條

Perform morph open. We create a rectangular structuring element and morph open to remove the lines

繪制矩形.查找輪廓并繪制邊界矩形.

Draw rectangle. Find contours and draw bounding rectangles.

<小時>

這是每個步驟的可視化:


Here's each step visualized:

使用此截屏圖像(包含更多邊框,因為提供的圖像的矩形太靠近邊框).您可以為輸入圖像添加邊框,而不是截圖以獲得更多邊框區域.看看為圖片添加邊框

Using this screenshotted image (contains more border since the provided image has the rectangles too close to the border). You could add a border to the input image instead of screenshotting for more border area. Take a look at add border to image

二值圖像

填充矩形輪廓

變形打開

結果

代碼

import cv2

# Load iamge, grayscale, adaptive threshold
image = cv2.imread('1.png')
result = image.copy()
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,51,9)

# Fill rectangular contours
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(thresh, [c], -1, (255,255,255), -1)

# Morph open
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=4)

# Draw rectangles
cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 3)

cv2.imshow('thresh', thresh)
cv2.imshow('opening', opening)
cv2.imshow('image', image)
cv2.waitKey()

注意:根據圖像,您可能需要修改內核大小.例如,可能需要將內核從 (5, 5) 增加到 (11, 11).此外,您可以在執行 cv2.morphologyEx() 時增加或減少迭代次數.增加或減少內核大小時需要權衡取舍,因為您可能會刪除更多或更少的行.同樣,這一切都取決于輸入圖像.

Note: Depending on the image, you may have to modify the kernel size. For instance, it may be necessary to increase the kernel from (5, 5) to say (11, 11). In addition, you could increase or decrease the number of iterations when performing cv2.morphologyEx(). There is a trade-off when increasing or decreasing the kernel size as you may remove more or less of the lines. Again, it all varies depending on the input image.

這篇關于如何在不遺漏任何東西的情況下檢測所有矩形框python opencv的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 欧洲免费毛片 | 中午字幕在线观看 | 欧美国产日韩精品 | 伊人导航 | 激情小说综合网 | 国产一区 | 日本黄色片免费在线观看 | 91视频观看 | 国产色婷婷精品综合在线播放 | 91视频.com| 91色综合 | 色香蕉在线| 超碰在线97国产 | 黄色大片网站 | 亚洲女优在线播放 | 99在线免费视频 | 日韩视频在线观看中文字幕 | 91麻豆精品国产91久久久资源速度 | 精品国产精品三级精品av网址 | 中文字幕爱爱视频 | 亚洲一区二区三区免费在线观看 | 欧美专区在线 | 特黄视频| 国内精品久久影院 | 中文字幕视频在线观看 | 亚洲一区中文字幕在线观看 | 欧美专区日韩专区 | 麻豆国产一区二区三区四区 | 精品视频在线观看 | 国产精品国产a级 | 欧美一区二区三区四区视频 | 日韩中文字幕在线观看 | 99tv成人影院 | 午夜一级黄色片 | 亚洲欧美日韩中文字幕一区二区三区 | 日韩在线免费视频 | www.日日操 | 久久久久久久国产精品 | 国产免费一区二区 | 亚洲国产第一页 | 国产精品福利在线 |