簡體   English   中英

如何使用 Python OpenCV 識別圖像中的完整網格

[英]How to identify the complete grid in the image using Python OpenCV

我目前正在處理一項圖像處理任務,我需要識別網格單元並創建補丁,每個補丁作為單元格的網格。 我可以使用圖像下方的代碼生成以下 output。

在此處輸入圖像描述

import numpy as np
import pandas as pd
import time 
from datetime import datetime
# from PIL import Image 
import torch 
import os, sys
import math
import cv2


# In[11]:


image = cv2.imread('../data/classes2.jpg')
print('rgb img shape : ',image.shape)
mask = np.zeros(image.shape, dtype=np.uint8)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
print('gray img shape : ',gray.shape)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]




# In[15]:


# Detect only grid
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# print(cnts)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    print(area)
    if area > 10000:
        cv2.drawContours(mask, [c], -1, (255,255,255), -1)


# In[16]:


mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
mask = cv2.bitwise_and(mask, thresh)


# In[17]:


# Find horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (55,1))
detect_horizontal = cv2.morphologyEx(mask, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(image, [c], -1, (0,0,255), 2)


# In[18]:


# Find vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,25))
detect_vertical = cv2.morphologyEx(mask, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detect_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(image, [c], -1, (0,0,255), 2)


# In[19]:


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

但是,我無法在輪廓中完全識別網格線。 我什至嘗試銳化圖像,但仍然沒有成功。 我是圖像處理的新手。 原圖是:

在此處輸入圖像描述

編輯

一個補丁=一個網格單元,即。 網格的一個黃色單元格或橙色或白色單元格。 我的目的是通過輪廓識別網格,然后逐個單元格裁剪網格單元格,其中每個方形單元格將充當補丁,然后我需要識別補丁/單元格的顏色。 在這里,每個補丁都將保存為圖像。

我已經修改了代碼,但仍然缺少一些單元格。 有沒有更好的方法來識別單元格並將它們裁剪為補丁並識別它們的顏色,而不是首先識別網格。 目前,我得到以下 output:

在此處輸入圖像描述

對於上述 output,我參考了以下代碼: link to grid Identification in image python 代碼

嘗試使用硬編碼,我得到了重疊的補丁。 以下代碼是我嘗試過的:

image = cv2.imread(image_path + 'classes2.jpg')
#claculate patch pixels
white_x_pixels = 5
white_y_pixels = 5
grid_rows, grid_cols = 50,51
patch_row_pixels = round((image.shape[0])/50,2)
patch_col_pixels = round((image.shape[1])/51,2)
print('patch_row_pixels : ',patch_row_pixels)
print('patch_col_pixels : ',patch_col_pixels)
w = ceil(patch_col_pixels) 
h = floor(patch_row_pixels) 
print('w:{},h:{}'.format(w,h))
image_number = 0
coord_dict = {}
for r in range(grid_rows):#(49,50):#(grid_rows): 
#     y = white_y_pixels + r*(h)
    if r in [0,1,2]:
        y = white_y_pixels + r*(h) 
    else : 
        y = r*(h) - 1
    yh = int(y+h) - white_y_pixels
    y = int(y)
    for c in range(grid_cols):#(49,51):#(grid_cols):#(1):#(grid_cols):
        coord_dict[image_number] = {}
        if c in [0,1,2]:
            x = white_x_pixels + c*(w)# - white_x_pixels)#patch_col_pixels
        else: 
            x = c*(w) - (white_x_pixels - 1)
        xw = int(x+w) - white_x_pixels
        x = int(x)
        ROI = image[y:yh,x:xw]
        coord_dict[image_number]['x'] = x
        coord_dict[image_number]['x+w'] = x+w
        coord_dict[image_number]['y'] = y
        coord_dict[image_number]['y+h'] = y+h
        coord_dict[image_number]['w'] = w
        coord_dict[image_number]['h'] = h
        print(x,xw,y,yh)
        cv2.imwrite('ROI_'+str(image_number) + '.png',ROI)
#         cv2.imshow('ROI_'+str(image_number),ROI)
#         cv2.waitKey(0)
#         cv2.destroyAllWindows()
        image_number += 1 

對不起,之后我沒有跟進。 我嘗試對您提供的圖像進行硬編碼,但它略微向右傾斜並且沒有正確裁剪(而且我懶得數它們)。 但為了展示如何做到這一點,我裁剪了圖像的一小部分。

在此處輸入圖像描述

您可以使用原始圖像嘗試此代碼。 但是,您需要正確裁剪它並糾正該偏斜。

import cv2

img = cv2.imread('patches.jpg')
no_w = 7 # replace with no. of patches in width
no_h = 5 # replace with no. of patches in height
h, w = img.shape[:2]
pixels_w = round(w/no_w)
pixels_h = round(h/no_h)

for i in range(0, h, pixels_h):
    cv2.line(img, (0, i), (w, i), (0, 255, 0), 1)
for i in range(0, w, pixels_w):
    cv2.line(img, (i, 0), (i, h), (0, 255, 0), 1)

cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

結果

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM