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

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

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

問(wèn)題描述

我正在嘗試從關(guān)系數(shù)據(jù)庫(kù)中檢測(cè)所有矩形.但是我的腳本沒(méi)有檢測(cè)到一些盒子.請(qǐng)幫我這樣做.謝謝.

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)  

輸出:

推薦答案

這是一個(gè)使用閾值+形態(tài)學(xué)運(yùn)算的簡(jiǎn)單方法.

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

  1. 獲取二值圖像.加載圖像,轉(zhuǎn)換為灰度,然后自適應(yīng)閾值

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

填充矩形輪廓.查找輪廓并填充輪廓以創(chuàng)建填充矩形塊.

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

執(zhí)行 morph open.我們創(chuàng)建一個(gè)矩形結(jié)構(gòu)元素并 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.

<小時(shí)>

這是每個(gè)步驟的可視化:


Here's each step visualized:

使用此截屏圖像(包含更多邊框,因?yàn)樘峁┑膱D像的矩形太靠近邊框).您可以為輸入圖像添加邊框,而不是截圖以獲得更多邊框區(qū)域.看看為圖片添加邊框

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

二值圖像

填充矩形輪廓

變形打開(kāi)

結(jié)果

代碼

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()

注意:根據(jù)圖像,您可能需要修改內(nèi)核大小.例如,可能需要將內(nèi)核從 (5, 5) 增加到 (11, 11).此外,您可以在執(zhí)行 cv2.morphologyEx() 時(shí)增加或減少迭代次數(shù).增加或減少內(nèi)核大小時(shí)需要權(quán)衡取舍,因?yàn)槟赡軙?huì)刪除更多或更少的行.同樣,這一切都取決于輸入圖像.

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.

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

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 人人草人人干 | 在线视频一区二区 | 特黄特黄a级毛片免费专区 av网站免费在线观看 | 国产精品一区二区在线免费观看 | 国产在线中文字幕 | 欧美综合国产精品久久丁香 | 久久精品国产免费高清 | 欧美13videosex性极品 | 在线免费观看黄色 | 久久久中文 | av大片在线观看 | 福利视频一二区 | 国产成人网 | 大香网伊人 | 国产高清精品一区二区三区 | 亚洲精品国产电影 | 日韩成人免费在线视频 | 91精品国产91久久久久久最新 | 国产免费一区 | 日本一区二区不卡 | 日韩在线视频免费观看 | 免费观看一级毛片 | 亚洲协和影视 | 播放一级黄色片 | 欧美日韩在线观看一区 | 欧美一区二区三区免费电影 | 国产精品九九 | 国产精品久久久久久久久久久久 | 国产一区二区三区在线视频 | 国产区在线观看 | 三级视频网站 | 91久久久久久久久久久 | 麻豆精品久久 | 国产1区2区在线观看 | 99久久精品国产一区二区三区 | 人和拘一级毛片c | 一区二区三区高清在线观看 | 国产乱码精品一区二区三区五月婷 | 国产在线播放av | 久草免费在线视频 | 天天碰夜夜操 |